| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- const axios = require("axios");
- const Merchant = require("../models/merchant");
- const Ingredient = require("../models/ingredient");
- const Recipe = require("../models/recipe");
- // const merchantId = "HCVKASXH94531";
- // const token = "f1c88a69-e3e4-059a-da06-8858d0636e82";
- const merchantId = "YHVPCQMVB1P81";
- const token = "b48068eb-411a-918e-ea64-52007147e42c";
- module.exports = {
- landingPage: function(req, res){
- return res.render("landingPage/landing");
- },
- displayInventory: function(req, res){
- Merchant.findOne({posId: merchantId})
- .populate("inventory.ingredient")
- .populate("recipes")
- .then((merchant)=>{
- if(merchant){
- req.session.user = merchant._id;
- axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${token}`)
- .then((result)=>{
- for(let order of result.data.elements){
- for(let item of order.lineItems.elements){
- let recipe = merchant.recipes.find(r => r.posId === item.item.id);
- if(recipe){
- 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;
- }
- }
- }
- }
- merchant.lastUpdatedTime = Date.now();
- merchant.save()
- .then((updatedMerchant)=>{
- return res.render("inventoryPage/inventory", {merchant: updatedMerchant});
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- })
- .catch((err)=>{
- console.log(err);
- });
- }else{
- return res.redirect("/merchant/new");
- }
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- //Display page to set up new merchant with Clover POS
- //TODO: This is for development, needs updating for production
- merchantSetupClover: function(req, res){
- Ingredient.find()
- .then((ingredients)=>{
- axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/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");
- });
- },
- //Display page to set up merchant with no POS system
- 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");
- });
- },
- 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");
- });
- },
- updateRecipes: function(req, res){
- if(!req.session.user){
- return res.render("error");
- }
- Merchant.findOne({_id: req.session.user})
- .populate("recipes")
- .then((merchant)=>{
- axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
- .then((result)=>{
- let deletedRecipes = merchant.recipes.slice();
- for(let i = 0; i < result.data.elements.length; i++){
- for(let j = 0; j < deletedRecipes.length; j++){
- if(result.data.elements[i].id === deletedRecipes[j].posId){
- result.data.elements.splice(i, 1);
- deletedRecipes.splice(j, 1);
- i--;
- break;
- }
- }
- }
- for(let recipe of deletedRecipes){
- for(let i = 0; i < merchant.recipes.length; i++){
- if(recipe._id === merchant.recipes[i]._id){
- merchant.recipes.splice(i, 1);
- break;
- }
- }
- }
- let newRecipes = []
- for(let recipe of result.data.elements){
- let newRecipe = new Recipe({
- posId: recipe.id,
- merchant: merchant._id,
- name: recipe.name,
- ingredients: []
- });
- merchant.recipes.push(newRecipe);
- newRecipes.push(newRecipe);
- }
- Recipe.create(newRecipes)
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- merchant.save()
- .then((newMerchant)=>{
- newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
- .then((newestMerchant)=>{
- return res.json({merchant: newestMerchant, count: result.data.elements.length});
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- createMerchant: function(req, res){
- let data = JSON.parse(req.body.data);
- axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
- .then((merchant)=>{
- let newMerchant = new Merchant({
- name: merchant.data.name,
- posId: merchant.data.id,
- lastUpdatedTime: Date.now(),
- inventory: [],
- recipes: []
- });
- for(let ingredient of data.ingredients){
- let newIngredient = {
- ingredient: ingredient.id,
- quantity: parseInt(ingredient.quantity)
- }
- newMerchant.inventory.push(newIngredient);
- }
- let newRecipes = []
- for(let recipe of data.recipes){
- let newRecipe = {
- posId: recipe.posId,
- merchant: newMerchant._id,
- name: recipe.name,
- ingredients: []
- };
- for(let ingredient of recipe.ingredients){
- newRecipe.ingredients.push(ingredient);
- }
- newRecipes.push(newRecipe);
- }
- Recipe.create(newRecipes)
- .then((recipes)=>{
- for(let recipe of recipes){
- newMerchant.recipes.push(recipe);
- }
- newMerchant.save()
- .then((merchant)=>{
- return res.redirect("/inventory");
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- })
- .catch((err)=>{
- console.log(err);
- });
- },
- addMerchantIngredient: function(req, res){
- if(!req.session.user){
- return res.render("error");
- }
- Merchant.findOne({_id: req.session.user})
- .then((merchant)=>{
- merchant.inventory.push(req.body);
- merchant.save()
- .then((newMerchant)=>{
- return res.json(newMerchant);
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- removeMerchantIngredient: function(req, res){
- if(!req.session.user){
- return res.render("error");
- }
- Merchant.findOne({_id: req.session.user})
- .then((merchant)=>{
- for(let i = 0; i < merchant.inventory.length; i++){
- if(req.body.ingredientId === merchant.inventory[i]._id.toString()){
- merchant.inventory.splice(i, 1);
- break;
- }
- }
- merchant.save()
- .then(()=>{
- return res.json();
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- updateMerchantIngredient: function(req, res){
- if(!req.session.user){
- return res.render("error");
- }
- Merchant.findOne({_id: req.session.user})
- .then((merchant)=>{
- let updateIngredient = merchant.inventory.find(i => i._id.toString() === req.body.ingredientId);
- updateIngredient.quantity = req.body.quantity;
- merchant.save()
- .then((merchant)=>{
- return res.json();
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- })
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- addRecipeIngredient: function(req, res){
- if(!req.session.user){
- return res.render("error");
- }
- Recipe.findOne({_id: req.body.recipeId})
- .then((recipe)=>{
- recipe.ingredients.push({
- ingredient: req.body.item.ingredient,
- quantity: req.body.item.quantity
- });
-
- recipe.save()
- .then((recipe)=>{
- return res.json();
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- updateRecipeIngredient: function(req, res){
- if(!req.session.user){
- return res.render("error");
- }
- Recipe.findOne({_id: req.body.recipeId})
- .then((recipe)=>{
- for(let ingredient of recipe.ingredients){
- if(ingredient._id.toString() === req.body.ingredient._id){
- ingredient.quantity = req.body.ingredient.quantity;
- recipe.save()
- .then((recipe)=>{
- return res.json();
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- })
- }
- }
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- removeRecipeIngredient: function(req, res){
- if(!req.session.user){
- return res.render("error");
- }
- Recipe.findOne({_id: req.body.recipeId})
- .then((recipe)=>{
- for(let i = 0; i < recipe.ingredients.length; i++){
- if(recipe.ingredients[i]._id.toString() === req.body.ingredientId){
- recipe.ingredients.splice(i, 1);
- }
- }
- recipe.save()
- .then((recipe)=>{
- return res.json();
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- getIngredients: function(req, res){
- Ingredient.find()
- .then((ingredients)=>{
- return res.json(ingredients);
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- createNewIngredients: function(req, res){
- Ingredient.create(req.body)
- .then((ingredients)=>{
- return res.json(ingredients);
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- createIngredient: function(req, res){
- Ingredient.create(req.body.ingredient)
- .then((ingredient)=>{
- Merchant.findOne({_id: req.session.user})
- .then((merchant)=>{
- let item = {
- ingredient: ingredient,
- quantity: req.body.quantity
- }
- merchant.inventory.push(item);
- merchant.save()
- .then((merchant)=>{
- console.log("something");
- return res.json(merchant);
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- getCloverRecipes: function(req, res){
- if(!req.session.user){
- return res.render("error");
- }
- Merchant.findOne({_id: req.session.user})
- .then((merchant)=>{
- axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${token}`)
- .then((recipes)=>{
- return res.json(recipes);
- })
- .catch((err)=>{
- return res.json(err);
- });
- })
- .catch((err)=>{
- console.log(err);
- return res.render("error");
- });
- },
- unregistered: function(req, res){
- return res.redirect("/");
- }
- }
|