| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- const axios = require("axios");
- const bcrypt = require("bcryptjs");
- const Merchant = require("../models/merchant");
- const Recipe = require("../models/recipe");
- const InventoryAdjustment = require("../models/inventoryAdjustment");
- const RecipeChange = require("../models/recipeChange");
- module.exports = {
- //POST - Create a new merchant with no POS system
- //Inputs:
- // req.body.name: restaurant name
- // req.body.email: registration email
- // req.body.password: password
- // req.body.confirmPassword: confirmation password
- //Redirects to /dashboard
- createMerchantNone: function(req, res){
- if(req.body.password === req.body.confirmPassword){
- let salt = bcrypt.genSaltSync(10);
- let hash = bcrypt.hashSync(req.body.password, salt);
- let merchant = new Merchant({
- name: req.body.name,
- email: req.body.email.toLowerCase(),
- password: hash,
- pos: "none",
- lastUpdatedTime: Date.now(),
- createdAt: Date.now(),
- accountStatus: "valid",
- inventory: [],
- recipes: []
- });
- merchant.save()
- .then((merchant)=>{
- req.session.user = merchant._id;
- return res.redirect("/dashboard");
- })
- .catch((err)=>{
- req.session.error = "Error: Unable to create account at this time";
- return res.redirect("/");
- });
- }else{
- req.session.error = "Error: Passwords must match";
- return res.redirect("/");
- }
- },
- //POST - Creates a Clover merchant from all entered data
- //Inputs:
- // req.body.data: All data from frontend in form of merchant model
- //Redirect to /dashboard
- createMerchantClover: async function(req, res){
- axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${req.session.merchantId}?access_token=${req.session.accessToken}`)
- .then((response)=>{
- let merchant = new Merchant({
- name: response.data.name,
- pos: "clover",
- posId: req.session.merchantId,
- posAccessToken: req.session.accessToken,
- lastUpdatedTime: Date.now(),
- createdAt: Date.now(),
- inventory: [],
- recipes: []
- });
- axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${req.session.merchantId}/items?access_token=${req.session.accessToken}`)
- .then((response)=>{
- let recipes = [];
- for(let item of response.data.elements){
- let recipe = new Recipe({
- posId: item.id,
- merchant: merchant,
- name: item.name,
- price: item.price,
- ingredients: []
- });
- recipes.push(recipe);
- merchant.recipes.push(recipe);
- }
- Recipe.create(recipes)
- .catch((err)=>{
- req.session.error = "Error: unable to create your recipes from Clover. Try using updating your recipes on the recipe page."
- })
- merchant.save()
- .then((newMerchant)=>{
- req.session.accessToken = undefined;
- req.session.user = newMerchant._id;
- return res.redirect("/dashboard");
- })
- .catch((err)=>{
- req.session.error = "Error: unable to save data from Clover";
- return res.redirect("/");
- });
- })
- .catch((err)=>{
- req.session.error = "Error: unable to retrieve necessary data from Clover";
- return res.redirect("/");
- })
-
- })
- .catch((err)=>{
- req.session.error = "Error: Unable to retrieve data from Clover";
- return res.redirect("/");
- });
- },
- //GET - Checks clover for new or deleted recipes
- //Returns:
- // merchant: Full merchant (recipe ingredients populated)
- // count: Number of new recipes
- updateRecipes: function(req, res){
- if(!req.session.user){
- req.session.error = "Must be logged in to do that";
- return res.redirect("/");
- }
- Merchant.findOne({_id: req.session.user})
- .populate("recipes")
- .then((merchant)=>{
- axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${merchant.posId}/items?access_token=${merchant.posAccessToken}`)
- .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)=>{
- return res.json("Error: unable to create recipes");
- });
- merchant.save()
- .then((newMerchant)=>{
- newMerchant.populate(["recipes.ingredients.ingredient", "inventory.ingredient"]).execPopulate()
- .then((newestMerchant)=>{
- merchant.password = undefined;
- return res.json(newestMerchant);
- })
- .catch((err)=>{
- return res.json("Error: unable to retrieve user data");
- });
- })
- .catch((err)=>{
- return res.json("Error: unable to retrieve user data");
- });
- })
- .catch((err)=>{
- return res.json("Error: unable to retrieve data from Clover");
- });
- })
- .catch((err)=>{
- return res.json("Error: unable to retrieve user data");
- });
- },
- //DELETE - removes a single recipe
- removeRecipe: function(req, res){
- if(!req.session.user){
- req.session.error = "Must be logged in to do that";
- return res.redirect("/");
- }
- Merchant.findOne({_id: req.session.user})
- .then((merchant)=>{
- if(merchant.pos === "clover"){
- return res.json("Error: you must edit your recipes inside Clover");
- }
-
- for(let i = 0; i < merchant.recipes.length; i++){
- if(merchant.recipes[i].toString() === req.params.id){
- merchant.recipes.splice(i, 1);
- break;
- }
- }
- merchant.save()
- .then((updatedMerchant)=>{
- return res.json({});
- })
- .catch((err)=>{
- return res.json("Error: unable to save data")
- })
- })
- .catch((err)=>{
- return res.json("Error: unable to retrieve merchant data");
- });
- },
- //POST - Adds an ingredient to merchant's inventory
- //Inputs:
- // req.body: array of objects (each object is a full ingredient)
- addMerchantIngredient: function(req, res){
- if(!req.session.user){
- req.session.error = "Must be logged in to do that";
- return res.redirect("/");
- }
- Merchant.findOne({_id: req.session.user})
- .then((merchant)=>{
- for(let ingredient of req.body){
- for(let item of merchant.inventory){
- if(item.ingredient.toString() === ingredient.ingredient._id){
- return res.json("Error: Duplicate ingredient detected");
- }
- }
-
- merchant.inventory.push({
- ingredient: ingredient.ingredient._id,
- quantity: ingredient.quantity
- });
- }
- merchant.save()
- .then((newMerchant)=>{
- newMerchant.populate("inventory.ingredient", (err)=>{
- if(err){
- return res.json("Warning: refresh page to view updates");
- }else{
- return res.json({});
- }
- });
- })
- .catch((err)=>{
- return res.json("Error: unable to save new ingredient");
- });
- })
- .catch((err)=>{
- return res.json("Error: unable to retrieve user data");
- });
- },
- //POST - Removes an ingredient from the merchant's inventory
- removeMerchantIngredient: function(req, res){
- if(!req.session.user){
- req.session.error = "Must be logged in to do that";
- return res.redirect("/");
- }
- Merchant.findOne({_id: req.session.user})
- .then((merchant)=>{
- for(let i = 0; i < merchant.inventory.length; i++){
- if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
- merchant.inventory.splice(i, 1);
- break;
- }
- }
- merchant.save()
- .then((merchant)=>{
- return res.json({});
- })
- .catch((err)=>{
- return res.json("Error: unable to save user data");
- });
- })
- .catch((err)=>{
- return res.json("Error: unable to retrieve user data");
- });
- },
- //POST - Update the quantity for a merchant inventory item
- //Inputs:
- // req.body: array of ingredient data
- // id: id of ingredient to update
- // quantity: New value for the ingredient
- updateMerchantIngredient: function(req, res){
- if(!req.session.user){
- req.session.error = "Must be logged in to do that";
- return res.redirect("/");
- }
- let adjustments = [];
- Merchant.findOne({_id: req.session.user})
- .then((merchant)=>{
- for(let i = 0; i < req.body.length; i++){
- for(let j = 0; j < merchant.inventory.length; j++){
- if(merchant.inventory[j].ingredient.toString() === req.body[i].id){
- updateIngredient = merchant.inventory[j];
- break;
- }
- }
- adjustments.push(new InventoryAdjustment({
- date: Date.now(),
- merchant: req.session.user,
- ingredient: req.body[i].id,
- quantity: req.body[i].quantity - updateIngredient.quantity
- }));
- updateIngredient.quantity = req.body[i].quantity;
- }
- merchant.save()
- .then((newMerchant)=>{
- res.json({});
- InventoryAdjustment.create(adjustments).catch(()=>{});
- return;
- })
- .catch((err)=>{
- return res.json("Error: your data could not be saved");
- })
- })
- .catch((err)=>{
- return res.json("Error: your data could not be retrieved");
- });
- },
- //POST - Adds an ingredient to a recipe
- //Inputs:
- // req.body.recipeId: Id of recipe to change
- // req.body.item: Ingredient to add with a quantity
- //Returns:
- // recipe: Updated recipe with populated ingredients
- addRecipeIngredient: function(req, res){
- if(!req.session.user){
- req.session.error = "Must be logged in to do that";
- return res.redirect("/");
- }
- 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)=>{
- recipe.populate("ingredients.ingredient", (err)=>{
- if(err){
- return res.json("Error: could not retrieve ingredients. Please refresh page to see changes");
- }
- res.json(recipe);
- let rc = new RecipeChange({
- recipe: recipe,
- ingredient: req.body.item.ingredient,
- change: req.body.item.quantity
- });
- rc.save().catch((err)=>{});
- return;
- })
- })
- .catch((err)=>{
- return res.json("Error: unable to save recipe data");
- });
- })
- .catch((err)=>{
- return res.json("Error: unable to retrieve recipe data");
- });
- },
- //POST - Change quantity of a recipe's ingredient
- //Inputs:
- // req.body.recipeId: Id of recipe containing the ingredient
- // req.body.ingredient: The ingredient to update (_id and quantity)
- //Returns: Nothing
- updateRecipeIngredient: function(req, res){
- if(!req.session.user){
- req.session.error = "Must be logged in to do that";
- return res.redirect("/");
- }
- Recipe.findOne({_id: req.body.recipeId})
- .then((recipe)=>{
- for(let ingredient of recipe.ingredients){
- if(ingredient._id.toString() === req.body.ingredient._id){
- let change = Number(req.body.ingredient.quantity) - ingredient.quantity;
- ingredient.quantity = req.body.ingredient.quantity;
- recipe.save()
- .then((recipe)=>{
- res.json({});
- let rc = new RecipeChange({
- recipe: recipe,
- ingredient: ingredient.ingredient,
- change: change
- });
- rc.save().catch((err)=>{});
- return;
- })
- .catch((err)=>{
- return res.json("Error: Could not save recipe data");
- });
- }
- }
- })
- .catch((err)=>{
- return res.json("Error: unable to retrieve recipe data");
- });
- },
- //POST - Remove an ingredient from a recipe
- //Inputs:
- // req.body.ingredientId: Id of ingredient to be removed
- // req.body.recipeId: Id of recipe to remove ingredient from
- // req.body.quantity: quantity of recipe ingredient for storing
- //Returns: Nothing
- removeRecipeIngredient: function(req, res){
- if(!req.session.user){
- req.session.error = "Must be logged in to do that";
- return res.redirect("/");
- }
- Recipe.findOne({_id: req.body.recipeId})
- .then((recipe)=>{
- for(let i = 0; i < recipe.ingredients.length; i++){
- if(recipe.ingredients[i].ingredient._id.toString() === req.body.ingredientId){
- recipe.ingredients.splice(i, 1);
- break;
- }
- }
- recipe.save()
- .then((recipe)=>{
- res.json({});
- let rc = new RecipeChange({
- recipe: req.body.recipeId,
- ingredient: req.body.ingredientId,
- change: -req.body.quantity
- });
- rc.save().catch((err)=>{});
- return;
- })
- .catch((err)=>{
- return res.json("Error: unable to save recipe data");
- });
- })
- .catch((err)=>{
- return res.json("Error: unable to retrieve recipe data");
- });
- },
- //POST - Update merchant information
- //Inputs:
- // req.body.name: name update
- // req.body.email: email update
- //Returns: Nothing
- updateMerchant: function(req, res){
- if(!req.session.user){
- req.session.error = "Must be logged in to do that";
- return res.redirect("/");
- }
- Merchant.findOne({_id: req.session.user})
- .then((merchant)=>{
- merchant.name = req.body.name;
- merchant.email = req.body.email;
- merchant.save()
- .then((updatedMerchant)=>{
- return res.json({});
- })
- .catch((err)=>{
- return res.json("Error: unable to save merchant data");
- });
- })
- .catch((err)=>{
- return res.json("Error: unable to retrieve merchant data");
- });
- },
- //POST - Update merchant password
- //Inputs:
- // req.body.oldPass: current merchant password (supposedly)
- // req.body.newPass: replacement password
- //Returns: Nothing
- updatePassword: function(req, res){
- if(!req.session.user){
- req.session.error = "Must be logged in to do that";
- return res.redirect("/");
- }
- Merchant.findOne({_id: req.session.user})
- .then((merchant)=>{
- bcrypt.compare(req.body.oldPass, merchant.password, (err, result)=>{
- if(result){
- let salt = bcrypt.genSaltSync(10);
- let hash = bcrypt.hashSync(req.body.newPass, salt);
- merchant.password = hash;
- merchant.save()
- .then((updatedMerchant)=>{
- return res.json({});
- })
- .catch((err)=>{
- return res.json("Error: Unable to save new password");
- });
- }else{
- return res.json("Error: old password does not match current password");
- }
- });
- })
- .catch((err)=>{
- return res.json("Error: Unable to retrieve merchant data");
- });
- }
- }
|