renderer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. const axios = require("axios");
  2. const Merchant = require("../models/merchant");
  3. const Ingredient = require("../models/ingredient");
  4. const Transaction = require("../models/transaction");
  5. const Purchase = require("../models/purchase");
  6. const NonPosTransaction = require("../models/nonPosTransaction");
  7. module.exports = {
  8. //GET - Shows the public landing page
  9. //Returns:
  10. // Error: a single error message (only if there is an error)
  11. //Renders landingPage
  12. landingPage: function(req, res){
  13. let error = {};
  14. let isLoggedIn = req.session.isLoggedIn || false;
  15. if(req.session.error){
  16. error = req.session.error;
  17. req.session.error = undefined;
  18. }else{
  19. error = null;
  20. }
  21. return res.render("landingPage/landing", {error: error, isLoggedIn: isLoggedIn});
  22. },
  23. //GET - Displays the main inventory page for merchants
  24. //Returns:
  25. // merchant: the logged in merchant
  26. //Renders inventoryPage
  27. displayInventory: function(req, res){
  28. if(!req.session.user){
  29. req.session.error = "You must be logged in to view that page";
  30. return res.redirect("/");
  31. }
  32. Merchant.findOne({_id: req.session.user})
  33. .populate("inventory.ingredient")
  34. .populate({
  35. path: "recipes",
  36. model: "Recipe",
  37. populate: {
  38. path: "ingredients.ingredient",
  39. model: "Ingredient"
  40. }
  41. })
  42. .then((merchant)=>{
  43. if(merchant.pos === "clover"){
  44. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${merchant.posAccessToken}`)
  45. .then((result)=>{
  46. let transactions = [];
  47. for(let order of result.data.elements){
  48. let newTransaction = new Transaction({
  49. merchant: merchant._id,
  50. date: new Date(order.createdTime),
  51. device: order.device.id
  52. });
  53. for(let item of order.lineItems.elements){
  54. let recipe = merchant.recipes.find(r => r.posId === item.item.id);
  55. if(recipe){
  56. newTransaction.recipes.push(recipe._id);
  57. for(let ingredient of recipe.ingredients){
  58. let inventoryIngredient = {};
  59. for(let invItem of merchant.inventory){
  60. if(invItem.ingredient._id.toString() === ingredient.ingredient._id.toString()){
  61. inventoryIngredient = invItem;
  62. }
  63. }
  64. inventoryIngredient.quantity = (inventoryIngredient.quantity - ingredient.quantity).toFixed(2);
  65. }
  66. }
  67. }
  68. transactions.push(newTransaction);
  69. }
  70. merchant.lastUpdatedTime = Date.now();
  71. merchant.save()
  72. .then((updatedMerchant)=>{
  73. updatedMerchant.password = undefined;
  74. updatedMerchant.accessToken = undefined;
  75. res.render("inventoryPage/inventory", {merchant: updatedMerchant, error: undefined});
  76. Transaction.create(transactions);
  77. return;
  78. })
  79. .catch((err)=>{
  80. let errorMessage = "Error: unable to save user data";
  81. merchant.password = undefined;
  82. return res.render("inventoryPage/inventory", {merchant: updatedMerchant, error: errorMessage});
  83. });
  84. })
  85. .catch((err)=>{
  86. let errorMessage = "There was an error and we could not retrieve your transactions from Clover";
  87. merchant.password = undefined;
  88. return res.render("inventoryPage/inventory", {merchant: merchant, error: errorMessage});
  89. });
  90. }else if(merchant.pos === "none"){
  91. merchant.password = undefined;
  92. return res.render("inventoryPage/inventory", {merchant: merchant, error: undefined})
  93. }else{
  94. req.session.error = "Error: WEBSITE PANIC";
  95. return res.redirect("/");
  96. }
  97. })
  98. .catch((err)=>{
  99. req.session.error = "Error: could not retrieve user data";
  100. return res.redirect("/");
  101. });
  102. },
  103. //GET - Renders the merchant setup page for a clover client
  104. //Returns:
  105. // ingredients: all ingredients from database
  106. // recipes: recipes from the users clover account
  107. // error: returns error (if any) from session
  108. //Renders merchantSetupPage
  109. merchantSetupClover: function(req, res){
  110. let errorMessage = {};
  111. if(req.session.error){
  112. errorMessage = req.session.error;
  113. req.session.error = undefined;
  114. }else{
  115. errorMessage = null;
  116. }
  117. Ingredient.find()
  118. .then((ingredients)=>{
  119. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.merchantId}/items?access_token=${req.session.accessToken}`)
  120. .then((recipes)=>{
  121. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data, error: errorMessage});
  122. })
  123. .catch((err)=>{
  124. req.session.error = "Error: unable to retrieve data from Clover";
  125. return res.redirect("/");
  126. });
  127. })
  128. .catch((err)=>{
  129. req.session.error = "Error: data for new merchants could not be retrieved";
  130. return res.redirect("/");
  131. });
  132. },
  133. //GET - Renders the merchant setup page for a non-pos client
  134. //Returns:
  135. // ingredients: all ingredients from database
  136. // recipes: null (to signify non-post client)
  137. // error: returns error (if any) from session
  138. //Renders merchantSetupPage
  139. merchantSetupNone: function(req, res){
  140. let errorMessage = {};
  141. if(req.session.error){
  142. errorMessage = req.session.error;
  143. req.session.error = undefined;
  144. }else{
  145. errorMessage = null;
  146. }
  147. Ingredient.find()
  148. .then((ingredients)=>{
  149. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: null, error: errorMessage});
  150. })
  151. .catch((err)=>{
  152. req.session.error = "Error: data for new merchants could not be retrieved";
  153. return res.redirect("/");
  154. });
  155. },
  156. //GET - Renders the recipe display page
  157. //Returns:
  158. // merchant: merchant with recipes and recipe ingredients populated
  159. //Renders recipesPage
  160. displayRecipes: function(req, res){
  161. if(!req.session.user){
  162. req.session.error = "You must be logged in to view that page";
  163. return res.redirect("/");
  164. }
  165. Merchant.findOne({_id: req.session.user})
  166. .populate({
  167. path: "recipes",
  168. model: "Recipe",
  169. populate: {
  170. path: "ingredients.ingredient",
  171. model: "Ingredient"
  172. }
  173. })
  174. .populate("inventory.ingredient")
  175. .then((merchant)=>{
  176. merchant.password = undefined;
  177. return res.render("recipesPage/recipes", {merchant: merchant});
  178. })
  179. .catch((err)=>{
  180. req.session.error = "Error: unable to retrieve user data";
  181. return res.redirect("/");
  182. });
  183. },
  184. //GET - Renders the information page
  185. //Renders information page
  186. displayLegal: function(req, res){
  187. return res.render("informationPage/information");
  188. },
  189. displayData: function(req, res){
  190. if(!req.session.user){
  191. req.session.error = "You must be logged in to view that page";
  192. return res.redirect("/");
  193. }
  194. let merchTransPromise = new Promise((resolve, reject)=>{
  195. Merchant.findOne({_id: req.session.user})
  196. .populate("recipes")
  197. .populate("inventory.ingredient")
  198. .then((merchant)=>{
  199. let date = new Date();
  200. let firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
  201. let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  202. if(merchant.pos === "clover"){
  203. Transaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}})
  204. .then((transactions)=>{
  205. resolve({merchant: merchant, transactions: transactions});
  206. })
  207. .catch((err)=>{});
  208. }else{
  209. NonPosTransaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}})
  210. .then((transactions)=>{
  211. resolve({merchant: merchant, transactions: transactions});
  212. })
  213. .catch((err)=>{});
  214. }
  215. })
  216. .catch((err)=>{});
  217. });
  218. let purchasePromise = new Promise((resolve, reject)=>{
  219. Purchase.find({merchant: req.session.user})
  220. .then((purchases)=>{
  221. resolve(purchases);
  222. })
  223. .catch((err)=>{});
  224. });
  225. Promise.all([merchTransPromise, purchasePromise])
  226. .then((response)=>{
  227. let data = {
  228. merchant: response[0].merchant,
  229. transactions: response[0].transactions,
  230. purchases: response[1]
  231. }
  232. return res.render("dataPage/data", {data: data});
  233. })
  234. .catch((err)=>{
  235. req.session.error = "Error: unable to retrieve user data";
  236. return res.redirect("/");
  237. });
  238. }
  239. }