|
|
@@ -118,175 +118,6 @@ module.exports = {
|
|
|
});
|
|
|
},
|
|
|
|
|
|
- //GET - Checks clover for new or deleted recipes
|
|
|
- //Returns:
|
|
|
- // merchant: Full merchant (recipe ingredients populated)
|
|
|
- // count: Number of new recipes
|
|
|
- updateRecipesClover: function(req, res){
|
|
|
- if(!req.session.user){
|
|
|
- req.session.error = "Must be logged in to do that";
|
|
|
- return res.redirect("/");
|
|
|
- }
|
|
|
-
|
|
|
- let merchant = {};
|
|
|
- let newRecipes = [];
|
|
|
- let deletedRecipes = []
|
|
|
- Merchant.findOne({_id: req.session.user})
|
|
|
- .populate("recipes")
|
|
|
- .then((response)=>{
|
|
|
- merchant = response;
|
|
|
- return axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${merchant.posAccessToken}`);
|
|
|
- })
|
|
|
- .then((result)=>{
|
|
|
- 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 i = 0; i < deletedRecipes.length; i++){
|
|
|
- for(let j = 0; j < merchant.recipes.length; j++){
|
|
|
- if(deletedRecipes[i]._id === merchant.recipes[j]._id){
|
|
|
- merchant.recipes.splice(j, 1);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- for(let i = 0; i < result.data.elements.length; i++){
|
|
|
- let newRecipe = new Recipe({
|
|
|
- posId: result.data.elements[i].id,
|
|
|
- merchant: merchant._id,
|
|
|
- name: result.data.elements[i].name,
|
|
|
- ingredients: [],
|
|
|
- price: result.data.elements[i].price
|
|
|
- });
|
|
|
-
|
|
|
- merchant.recipes.push(newRecipe);
|
|
|
- newRecipes.push(newRecipe);
|
|
|
- }
|
|
|
-
|
|
|
- Recipe.create(newRecipes).catch((err)=>{});
|
|
|
-
|
|
|
- return merchant.save();
|
|
|
- })
|
|
|
- .then((newMerchant)=>{
|
|
|
- return res.json({new: newRecipes, removed: deletedRecipes});
|
|
|
- })
|
|
|
- .catch((err)=>{
|
|
|
- if(typeof(err) === "string"){
|
|
|
- return res.json(err);
|
|
|
- }
|
|
|
- if(err.name === "ValidationError"){
|
|
|
- return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
|
|
|
- }
|
|
|
- return res.json("ERROR: UNABLE TO RETRIEVE MERCHANT DATA");
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- updateRecipesSquare: function(req, res){
|
|
|
- if(!req.session.user){
|
|
|
- req.session.error = "Must be logged in to do that";
|
|
|
- return res.redirect("/");
|
|
|
- }
|
|
|
-
|
|
|
- let merchant = {};
|
|
|
- let merchantRecipes = [];
|
|
|
- let newRecipes = [];
|
|
|
-
|
|
|
- Merchant.findOne({_id: req.session.user})
|
|
|
- .populate("recipes")
|
|
|
- .then((fetchedMerchant)=>{
|
|
|
- merchant = fetchedMerchant;
|
|
|
- return axios.post(`${process.env.SQUARE_ADDRESS}/v2/catalog/search`, {
|
|
|
- object_types: ["ITEM"]
|
|
|
- }, {
|
|
|
- headers: {
|
|
|
- Authorization: `Bearer ${merchant.posAccessToken}`
|
|
|
- }
|
|
|
- });
|
|
|
- })
|
|
|
- .then((response)=>{
|
|
|
- merchantRecipes = merchant.recipes.slice();
|
|
|
-
|
|
|
-
|
|
|
- for(let i = 0; i < response.data.objects.length; i++){
|
|
|
- let itemData = response.data.objects[i].item_data;
|
|
|
- for(let j = 0; j < itemData.variations.length; j++){
|
|
|
- let isFound = false;
|
|
|
-
|
|
|
- for(let k = 0; k < merchantRecipes.length; k++){
|
|
|
- if(itemData.variations[j].id === merchantRecipes[k].posId){
|
|
|
- merchantRecipes.splice(k, 1);
|
|
|
- k--;
|
|
|
- isFound = true;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if(!isFound){
|
|
|
- let newRecipe = new Recipe({
|
|
|
- posId: itemData.variations[j].id,
|
|
|
- merchant: merchant._id,
|
|
|
- name: "",
|
|
|
- price: itemData.variations[j].item_variation_data.price_money.amount,
|
|
|
- ingredients: []
|
|
|
- });
|
|
|
-
|
|
|
- if(itemData.variations.length > 1){
|
|
|
- newRecipe.name = `${itemData.name} '${itemData.variations[j].item_variation_data.name}'`;
|
|
|
- }else{
|
|
|
- newRecipe.name = itemData.name;
|
|
|
- }
|
|
|
-
|
|
|
- newRecipes.push(newRecipe);
|
|
|
- merchant.recipes.push(newRecipe);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- let ids = [];
|
|
|
- for(let i = 0; i < merchantRecipes.length; i++){
|
|
|
- ids.push(merchantRecipes[i]._id);
|
|
|
- for(let j = 0; j < merchant.recipes.length; j++){
|
|
|
- if(merchantRecipes[i]._id.toString() === merchant.recipes[j]._id.toString()){
|
|
|
- merchant.recipes.splice(j, 1);
|
|
|
- j--;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if(newRecipes.length > 0){
|
|
|
- Recipe.create(newRecipes);
|
|
|
- }
|
|
|
-
|
|
|
- if(merchantRecipes.length > 0){
|
|
|
- Recipe.deleteMany({_id: {$in: ids}});
|
|
|
- }
|
|
|
-
|
|
|
- return merchant.save();
|
|
|
- })
|
|
|
- .then((merchant)=>{
|
|
|
- return res.json({new: newRecipes, removed: merchantRecipes});
|
|
|
- })
|
|
|
- .catch((err)=>{
|
|
|
- if(typeof(err) === "string"){
|
|
|
- return res.json(err);
|
|
|
- }
|
|
|
- if(err.name === "ValidationError"){
|
|
|
- return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
|
|
|
- }
|
|
|
- return res.json("ERROR: UNABLE TO RETRIEVE RECIPE DATA FROM SQUARE");
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
createFromSpreadsheet: function(req, res){
|
|
|
if(!req.session.user){
|
|
|
req.session.error = "MUST BE LOGGED IN TO DO THAT";
|