| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- const axios = require("axios");
- const Merchant = require("../models/merchant");
- const Ingredient = require("../models/ingredient");
- const Transaction = require("../models/transaction");
- const token = "b48068eb-411a-918e-ea64-52007147e42c";
- module.exports = {
- //GET - Shows the public landing page
- //Returns:
- // Error: a single error message
- //Renders landingPage
- landingPage: function(req, res){
- let error = {};
- if(req.session.error){
- error = req.session.error;
- req.session.error = undefined;
- }else{
- error = undefined;
- }
- return res.render("landingPage/landing", {error: error});
- },
- //GET - Displays the main inventory page for merchants
- //Returns:
- // merchant: the logged in merchant
- //Renders inventoryPage
- displayInventory: function(req, res){
- if(!req.session.user){
- return res.redirect("/");
- }
- Merchant.findOne({_id: req.session.user})
- .populate("inventory.ingredient")
- .populate("recipes")
- .then((merchant)=>{
- if(merchant.pos === "clover"){
- axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${token}`)
- .then((result)=>{
- let transactions = [];
- for(let order of result.data.elements){
- let newTransaction = new Transaction({
- merchant: merchant._id,
- date: new Date(order.createdTime),
- device: order.device.id
- });
- for(let item of order.lineItems.elements){
- let recipe = merchant.recipes.find(r => r.posId === item.item.id);
- if(recipe){
- newTransaction.recipes.push(recipe._id);
- for(let ingredient of recipe.ingredients){
- let inventoryIngredient = {};
- for(let invItem of merchant.inventory){
- if(invItem.ingredient._id.toString() === ingredient.ingredient.toString()){
- inventoryIngredient = invItem;
- }
- }
- inventoryIngredient.quantity -= ingredient.quantity;
- }
- }
- }
- transactions.push(newTransaction);
- }
- merchant.lastUpdatedTime = Date.now();
- merchant.save()
- .then((updatedMerchant)=>{
- res.render("inventoryPage/inventory", {merchant: updatedMerchant});
- Transaction.create(transactions);
- return;
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- })
- .catch((err)=>{
- console.log(err);
- });
- }else if(merchant.pos === "none"){
- return res.render("inventoryPage/inventory", {merchant: merchant})
- }else{
- return res.redirect("/");
- }
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- //GET - Renders the merchant setup page for a clover client
- //Returns:
- // ingredients: all ingredients from database
- // recipes: recipes from the users clover account
- //Renders merchantSetupPage
- merchantSetupClover: function(req, res){
- req.session.posId = "YHVPCQMVB1P81";
-
- Ingredient.find()
- .then((ingredients)=>{
- axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}/items?access_token=${token}`)
- .then((recipes)=>{
- return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- //GET - Renders the merchant setup page for a non-pos client
- //Returns:
- // ingredients: all ingredients from database
- // recipes: null (to signify non-post client)
- //Renders merchantSetupPage
- merchantSetupNone: function(req, res){
- Ingredient.find()
- .then((ingredients)=>{
- return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: null});
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- //GET - Renders the recipe display page
- //Returns:
- // merchant: merchant with recipes and recipe ingredients populated
- //Renders recipesPage
- displayRecipes: function(req, res){
- if(!req.session.user){
- return res.render("error");
- }
- Merchant.findOne({_id: req.session.user})
- .populate({
- path: "recipes",
- model: "Recipe",
- populate: {
- path: "ingredients.ingredient",
- model: "Ingredient"
- }
- })
- .populate("inventory.ingredient")
- .then((merchant)=>{
- return res.render("recipesPage/recipes", {merchant: merchant});
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- }
- }
|