ingredientData.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. const Ingredient = require("../models/ingredient");
  2. const InventoryAdjustment = require("../models/inventoryAdjustment.js");
  3. const helper = require("./helper.js");
  4. const xlsx = require("xlsx");
  5. const fs = require("fs");
  6. module.exports = {
  7. /*
  8. POST - create a single ingredient and then add to the merchant
  9. req.body = {
  10. ingredient: {
  11. name: name of ingredient,
  12. category: category of ingredient,
  13. unitType: category for the unit (mass, volume, length)
  14. },
  15. quantity: quantity of ingredient for current merchant,
  16. defaultUnit: default unit of measurement to display
  17. }
  18. Returns:
  19. Same as above, with the _id
  20. */
  21. createIngredient: function(req, res){
  22. let newIngredient = {...req.body};
  23. if(req.body.defaultUnit === "bottle"){
  24. newIngredient.ingredient.unitSize = helper.convertQuantityToBaseUnit(newIngredient.ingredient.unitSize, newIngredient.ingredient.unitType);
  25. }
  26. newIngredient = new Ingredient(newIngredient.ingredient);
  27. newIngredient.save()
  28. .then((ingredient)=>{
  29. newIngredient = {
  30. ingredient: ingredient,
  31. defaultUnit: req.body.defaultUnit
  32. }
  33. newIngredient.quantity = helper.convertQuantityToBaseUnit(req.body.quantity, req.body.defaultUnit);
  34. res.locals.owner.inventory.push(newIngredient);
  35. return res.locals.owner.save();
  36. })
  37. .then((response)=>{
  38. return res.json(newIngredient);
  39. })
  40. .catch((err)=>{
  41. if(typeof(err) === "string"){
  42. return res.json(err);
  43. }
  44. if(err.name === "ValidationError"){
  45. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  46. }
  47. return res.json("ERROR: UNABLE TO CREATE THE INGREDIENT");
  48. });
  49. },
  50. /*
  51. POST - Updates data for a single ingredient
  52. req.body = {
  53. id: id of the ingredient,
  54. name: new name of the ingredient,
  55. quantity: new quantity of the unit (in grams),
  56. category: new category of the unit,
  57. unit: new default unit of the ingredient,
  58. }
  59. */
  60. updateIngredient: function(req, res){
  61. Ingredient.findOne({_id: req.body.id})
  62. .then((ingredient)=>{
  63. ingredient.name = req.body.name,
  64. ingredient.category = req.body.category
  65. return ingredient.save();
  66. })
  67. .then((ingredient)=>{
  68. let updatedIngredient = {};
  69. for(let i = 0; i < res.locals.owner.inventory.length; i++){
  70. if(res.locals.owner.inventory[i].ingredient.toString() === req.body.id){
  71. res.locals.owner.inventory[i].defaultUnit = req.body.unit;
  72. if(res.locals.owner.inventory[i].quantity !== req.body.quantity){
  73. new InventoryAdjustment({
  74. date: new Date(),
  75. merchant: req.session.owner,
  76. ingredient: req.body.id,
  77. quantity: req.body.quantity - res.locals.owner.inventory[i].quantity
  78. }).save().catch(()=>{});
  79. res.locals.owner.inventory[i].quantity = req.body.quantity;
  80. }
  81. updatedIngredient = {
  82. ingredient: ingredient,
  83. quantity: req.body.quantity,
  84. defaultUnit: req.body.unit
  85. }
  86. break;
  87. }
  88. }
  89. res.locals.owner.save().catch((err)=>{throw err});
  90. return res.json(updatedIngredient);
  91. })
  92. .catch((err)=>{
  93. if(typeof(err) === "string"){
  94. return res.json(err);
  95. }
  96. if(err.name === "ValidationError"){
  97. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  98. }
  99. return res.json("ERROR: UNABLE TO UPDATE DATA");
  100. });
  101. },
  102. createFromSpreadsheet: function(req, res){
  103. //read file, get the correct sheet, create array from sheet
  104. let workbook = xlsx.readFile(req.file.path);
  105. fs.unlink(req.file.path, ()=>{});
  106. let sheets = Object.keys(workbook.Sheets);
  107. let sheet = {};
  108. for(let i = 0; i < sheets.length; i++){
  109. let str = sheets[i].toLowerCase();
  110. if(str === "ingredient" || str === "ingredients"){
  111. sheet = workbook.Sheets[sheets[i]];
  112. }
  113. }
  114. const array = xlsx.utils.sheet_to_json(sheet, {
  115. header: 1
  116. });
  117. //get property locations
  118. let locations = {};
  119. for(let i = 0; i < array[0].length; i++){
  120. switch(array[0][i].toLowerCase()){
  121. case "name": locations.name = i; break;
  122. case "category": locations.category = i; break;
  123. case "quantity": locations.quantity = i; break;
  124. case "unit": locations.unit = i; break;
  125. case "bottle size": locations.bottleSize = i; break;
  126. case "bottle unit": locations.bottleUnit = i; break;
  127. }
  128. }
  129. //Create ingredients
  130. let ingredients = [];
  131. let merchantData = [];
  132. for(let i = 1; i < array.length; i++){
  133. let ingredient = new Ingredient({
  134. name: array[i][locations.name],
  135. category: array[i][locations.category],
  136. unitType: helper.getUnitType(array[i][locations.unit].toLowerCase())
  137. });
  138. if(array[i][locations.unit] === "bottle"){
  139. ingredient.unitType = array[i][locations.bottleUnit];
  140. ingredient.unitSize = helper.convertQuantityToBaseUnit(array[i][locations.bottleSize], array[i][locations.bottleUnit]);
  141. }
  142. let merchantItem = {
  143. ingredient: ingredient,
  144. quantity: helper.convertQuantityToBaseUnit(array[i][locations.quantity], array[i][locations.unit]),
  145. defaultUnit: array[i][locations.unit]
  146. }
  147. merchantData.push(merchantItem);
  148. ingredients.push(ingredient);
  149. }
  150. for(let i = 0; i < merchantData.length; i++){
  151. res.locals.owner.inventory.push(merchantData[i]);
  152. }
  153. //Update the database
  154. Promise.all([Ingredient.create(ingredients), res.locals.owner.save()])
  155. .then((response)=>{
  156. return res.json(merchantData);
  157. })
  158. .catch((err)=>{
  159. if(typeof(err) === "string"){
  160. return res.json(err);
  161. }
  162. if(err.name === "ValidationError"){
  163. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  164. }
  165. return "ERROR: UNABLE TO CREATE YOUR INGREDIENTS";
  166. });
  167. },
  168. spreadsheetTemplate: function(req, res){
  169. let workbook = xlsx.utils.book_new();
  170. workbook.SheetNames.push("Ingredients");
  171. let workbookData = [];
  172. workbookData.push(["Name", "Category", "Quantity", "Unit", "Bottle Size", "Bottle Unit"]);
  173. workbookData.push(["Example Ingredient 1", "Produce", 100, "lbs"]);
  174. workbookData.push(["Example Ingredient 2", "Fruit", 3.24, "kg"]);
  175. workbookData.push(["Example Ingredient 3", "Beverage", 5, "bottle", 750, "ml"]);
  176. workbook.Sheets.Ingredients = xlsx.utils.aoa_to_sheet(workbookData);
  177. xlsx.writeFile(workbook, "SublineIngredients.xlsx");
  178. return res.download("SublineIngredients.xlsx", (err)=>{
  179. fs.unlink("SublineIngredients.xlsx", ()=>{});
  180. });
  181. },
  182. //DELETE - Removes an ingredient from the merchant's inventory
  183. removeIngredient: function(req, res){
  184. for(let i = 0; i < res.locals.owner.inventory.length; i++){
  185. if(req.params.id === res.locals.owner.inventory[i].ingredient._id.toString()){
  186. res.locals.owner.inventory.splice(i, 1);
  187. break;
  188. }
  189. }
  190. Promise.all([res.locals.owner.save(), Ingredient.deleteOne({_id: req.params.id})])
  191. .then((response)=>{
  192. return res.json({});
  193. })
  194. .catch((err)=>{
  195. if(typeof(err) === "string"){
  196. return res.json(err);
  197. }
  198. if(err.name === "ValidationError"){
  199. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  200. }
  201. return res.json("ERROR: UNABLE TO RETRIEVE DATA");
  202. });
  203. }
  204. }