|
|
@@ -184,98 +184,6 @@ module.exports = {
|
|
|
});
|
|
|
},
|
|
|
|
|
|
- createFromSpreadsheet: function(req, res){
|
|
|
- //read file, get the correct sheet, create array from sheet
|
|
|
- let workbook = xlsx.readFile(req.file.path);
|
|
|
- fs.unlink(req.file.path, ()=>{});
|
|
|
-
|
|
|
- let sheets = Object.keys(workbook.Sheets);
|
|
|
- let sheet = {};
|
|
|
- for(let i = 0; i < sheets.length; i++){
|
|
|
- let str = sheets[i].toLowerCase();
|
|
|
- if(str === "ingredient" || str === "ingredients"){
|
|
|
- sheet = workbook.Sheets[sheets[i]];
|
|
|
- }
|
|
|
- }
|
|
|
- const array = xlsx.utils.sheet_to_json(sheet, {
|
|
|
- header: 1
|
|
|
- });
|
|
|
-
|
|
|
- //get property locations
|
|
|
- let locations = {};
|
|
|
- for(let i = 0; i < array[0].length; i++){
|
|
|
- switch(array[0][i].toLowerCase()){
|
|
|
- case "name": locations.name = i; break;
|
|
|
- case "category": locations.category = i; break;
|
|
|
- case "quantity": locations.quantity = i; break;
|
|
|
- case "unit": locations.unit = i; break;
|
|
|
- case "bottle size": locations.bottleSize = i; break;
|
|
|
- case "bottle unit": locations.bottleUnit = i; break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //Create ingredients
|
|
|
- let ingredients = [];
|
|
|
- let merchantData = [];
|
|
|
- for(let i = 1; i < array.length; i++){
|
|
|
- let ingredient = new Ingredient({
|
|
|
- name: array[i][locations.name],
|
|
|
- category: array[i][locations.category],
|
|
|
- unitType: helper.getUnitType(array[i][locations.unit].toLowerCase())
|
|
|
- });
|
|
|
-
|
|
|
- if(array[i][locations.unit] === "bottle"){
|
|
|
- ingredient.unitType = array[i][locations.bottleUnit];
|
|
|
- ingredient.unitSize = helper.convertQuantityToBaseUnit(array[i][locations.bottleSize], array[i][locations.bottleUnit]);
|
|
|
- }
|
|
|
-
|
|
|
- let merchantItem = {
|
|
|
- ingredient: ingredient,
|
|
|
- quantity: helper.convertQuantityToBaseUnit(array[i][locations.quantity], array[i][locations.unit]),
|
|
|
- defaultUnit: array[i][locations.unit]
|
|
|
- }
|
|
|
-
|
|
|
- merchantData.push(merchantItem);
|
|
|
- ingredients.push(ingredient);
|
|
|
- }
|
|
|
-
|
|
|
- for(let i = 0; i < merchantData.length; i++){
|
|
|
- res.locals.merchant.inventory.push(merchantData[i]);
|
|
|
- }
|
|
|
-
|
|
|
- //Update the database
|
|
|
- Promise.all([Ingredient.create(ingredients), res.locals.merchant.save()])
|
|
|
- .then((response)=>{
|
|
|
- return res.json(merchantData);
|
|
|
- })
|
|
|
- .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 "ERROR: UNABLE TO CREATE YOUR INGREDIENTS";
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- spreadsheetTemplate: function(req, res){
|
|
|
- let workbook = xlsx.utils.book_new();
|
|
|
- workbook.SheetNames.push("Ingredients");
|
|
|
- let workbookData = [];
|
|
|
-
|
|
|
- workbookData.push(["Name", "Category", "Quantity", "Unit", "Bottle Size", "Bottle Unit"]);
|
|
|
- workbookData.push(["Example Ingredient 1", "Produce", 100, "lbs"]);
|
|
|
- workbookData.push(["Example Ingredient 2", "Fruit", 3.24, "kg"]);
|
|
|
- workbookData.push(["Example Ingredient 3", "Beverage", 5, "bottle", 750, "ml"]);
|
|
|
-
|
|
|
- workbook.Sheets.Ingredients = xlsx.utils.aoa_to_sheet(workbookData);
|
|
|
- xlsx.writeFile(workbook, "SublineIngredients.xlsx");
|
|
|
- return res.download("SublineIngredients.xlsx", (err)=>{
|
|
|
- fs.unlink("SublineIngredients.xlsx", ()=>{});
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
//DELETE - Removes an ingredient from the merchant's inventory
|
|
|
removeIngredient: function(req, res){
|
|
|
for(let i = 0; i < res.locals.merchant.inventory.length; i++){
|