Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. async create(recipe: CreateRecipeDTO, user: User): Promise<ShowRecipeDTO> {
  2. const newRecipe: Recipe = await this.recipeRepository.create(recipe);
  3. newRecipe.author = Promise.resolve(user);
  4. newRecipe.hasSubrecipes = recipe.recipes.length > 0 ? true : false;
  5.  
  6. // Holds the nutrition of both Ingredients and Subrecipes
  7. const ingredientNutrition: Nutrition[] = [];
  8.  
  9. // Calculate Ingredient Nutrition
  10. recipe.products.map(async (item: any) => {
  11. const quantity = item.amount;
  12. const measure = item.measure;
  13. const gramsPerMeasure = item.product.measures.find((unit: any) => unit.measure === measure).gramsPerMeasure;
  14.  
  15. const itemQuantityInGrams = quantity * gramsPerMeasure;
  16. const coefficient = itemQuantityInGrams / 100;
  17.  
  18. let nutrition: Nutrition = await this.nutritionRepository.create();
  19. nutrition = item.product.nutrition;
  20.  
  21. for (const key in nutrition) {
  22. if (nutrition.hasOwnProperty(key)) {
  23. nutrition[key].value = Number((nutrition[key].value * coefficient).toFixed(2));
  24. }
  25. }
  26.  
  27. ingredientNutrition.push(nutrition);
  28. });
  29.  
  30. // Calculate Subrecipe Nutrition
  31. recipe.recipes.map(async (item: any) => {
  32. const quantity = item.amount;
  33.  
  34. let nutrition: Nutrition = await this.nutritionRepository.create();
  35. nutrition = item.recipe.nutrition;
  36.  
  37. for (const key in nutrition) {
  38. if (nutrition.hasOwnProperty(key)) {
  39. nutrition[key].value = Number((nutrition[key].value * quantity).toFixed(2));
  40. }
  41. }
  42.  
  43. ingredientNutrition.push(nutrition);
  44. });
  45.  
  46. // Calculate Total Nutrition for the Recipe
  47. await ingredientNutrition;
  48.  
  49. let totalPROCNT = 0;
  50. let totalFAT = 0;
  51. let totalCHOCDF = 0;
  52. let totalENERC_KCAL = 0;
  53. let totalSUGAR = 0;
  54. let totalFIBTG = 0;
  55. let totalCA = 0;
  56. let totalFE = 0;
  57. let totalP = 0;
  58. let totalK = 0;
  59. let totalNA = 0;
  60. let totalVITA_IU = 0;
  61. let totalTOCPHA = 0;
  62. let totalVITD = 0;
  63. let totalVITC = 0;
  64. let totalVITB12 = 0;
  65. let totalFOLAC = 0;
  66. let totalCHOLE = 0;
  67. let totalFATRN = 0;
  68. let totalFASAT = 0;
  69. let totalFAMS = 0;
  70. let totalFAPU = 0;
  71.  
  72. ingredientNutrition.map((item: Nutrition) => {
  73. for (const key in item) {
  74. if (item.hasOwnProperty(key)) {
  75. if (key === 'PROCNT') {
  76. totalPROCNT += item[key].value;
  77. }
  78. if (key === 'FAT') {
  79. totalFAT += item[key].value;
  80. }
  81. if (key === 'CHOCDF') {
  82. totalCHOCDF += item[key].value;
  83. }
  84. if (key === 'ENERC_KCAL') {
  85. totalENERC_KCAL += item[key].value;
  86. }
  87. if (key === 'SUGAR') {
  88. totalSUGAR += item[key].value;
  89. }
  90. if (key === 'FIBTG') {
  91. totalFIBTG += item[key].value;
  92. }
  93. if (key === 'CA') {
  94. totalCA += item[key].value;
  95. }
  96. if (key === 'FE') {
  97. totalFE += item[key].value;
  98. }
  99. if (key === 'P') {
  100. totalP += item[key].value;
  101. }
  102. if (key === 'K') {
  103. totalK += item[key].value;
  104. }
  105. if (key === 'NA') {
  106. totalNA += item[key].value;
  107. }
  108. if (key === 'VITA_IU') {
  109. totalVITA_IU += item[key].value;
  110. }
  111. if (key === 'TOCPHA') {
  112. totalTOCPHA += item[key].value;
  113. }
  114. if (key === 'VITD') {
  115. totalVITD += item[key].value;
  116. }
  117. if (key === 'VITC') {
  118. totalVITC += item[key].value;
  119. }
  120. if (key === 'VITB12') {
  121. totalVITB12 += item[key].value;
  122. }
  123. if (key === 'FOLAC') {
  124. totalFOLAC += item[key].value;
  125. }
  126. if (key === 'CHOLE') {
  127. totalCHOLE += item[key].value;
  128. }
  129. if (key === 'FATRN') {
  130. totalFATRN += item[key].value;
  131. }
  132. if (key === 'FASAT') {
  133. totalFASAT += item[key].value;
  134. }
  135. if (key === 'FAMS') {
  136. totalFAMS += item[key].value;
  137. }
  138. if (key === 'FAPU') {
  139. totalFAPU += item[key].value;
  140. }
  141. }
  142. }
  143. });
  144.  
  145. const totalRecipeNutrition: Nutrition = await this.nutritionRepository.create();
  146. totalRecipeNutrition.PROCNT = { description: 'Protein', unit: 'g', value: totalPROCNT }
  147. totalRecipeNutrition.FAT = { description: 'Total lipid (fat)', unit: 'g', value: totalFAT };
  148. totalRecipeNutrition.CHOCDF = { description: 'Carbohydrate, by difference', unit: 'g', value: totalCHOCDF };
  149. totalRecipeNutrition.ENERC_KCAL = { description: 'Energy', unit: 'kcal', value: totalENERC_KCAL };
  150. totalRecipeNutrition.SUGAR = { description: 'Sugars, total', unit: 'g', value: totalSUGAR };
  151. totalRecipeNutrition.FIBTG = { description: 'Fiber, total dietary', unit: 'g', value: totalFIBTG };
  152. totalRecipeNutrition.CA = { description: 'Calcium, Ca', unit: 'mg', value: totalCA };
  153. totalRecipeNutrition.FE = { description: 'Iron, Fe', unit: 'mg', value: totalFE };
  154. totalRecipeNutrition.P = { description: 'Phosphorus, P', unit: 'mg', value: totalP };
  155. totalRecipeNutrition.K = { description: 'Potassium, K', unit: 'mg', value: totalK };
  156. totalRecipeNutrition.NA = { description: 'Sodium, Na', unit: 'mg', value: totalNA };
  157. totalRecipeNutrition.VITA_IU = { description: 'Vitamin A, IU', unit: 'IU', value: totalVITA_IU };
  158. totalRecipeNutrition.TOCPHA = { description: 'Vitamin E (alpha-tocopherol)', unit: 'mg', value: totalTOCPHA };
  159. totalRecipeNutrition.VITD = { description: 'Vitamin D', unit: 'IU', value: totalVITD };
  160. totalRecipeNutrition.VITC = { description: 'Vitamin C, total ascorbic acid', unit: 'mg', value: totalVITC };
  161. totalRecipeNutrition.VITB12 = { description: 'Vitamin B-12', unit: 'µg', value: totalVITB12 };
  162. totalRecipeNutrition.FOLAC = { description: 'Folic acid', unit: 'µg', value: totalFOLAC };
  163. totalRecipeNutrition.CHOLE = { description: 'Cholesterol', unit: 'mg', value: totalCHOLE };
  164. totalRecipeNutrition.FATRN = { description: 'Fatty acids, total trans', unit: 'g', value: totalFATRN };
  165. totalRecipeNutrition.FASAT = { description: 'Fatty acids, total saturated', unit: 'g', value: totalFASAT };
  166. totalRecipeNutrition.FAMS = { description: 'Fatty acids, total monounsaturated', unit: 'g', value: totalFAMS };
  167. totalRecipeNutrition.FAPU = { description: 'Fatty acids, total polyunsaturated', unit: 'g', value: totalFAPU };
  168.  
  169. newRecipe.ingredients = await Promise.all(recipe.products.map(async (item: any) => {
  170. const newIngredient: Ingredient = this.ingredientRepository.create();
  171.  
  172. newIngredient.product = item.product;
  173. newIngredient.quantity = item.amount;
  174. newIngredient.unit = item.measure;
  175.  
  176. const savedIngredient = await this.ingredientRepository.save(newIngredient);
  177.  
  178. return savedIngredient;
  179. }));
  180.  
  181. // console.log(recipe.recipes);
  182.  
  183. newRecipe.subrecipes = await Promise.all(recipe.recipes.map(async (item: any) => {
  184. const newSubrecipe: Subrecipe = this.subrecipeRepository.create();
  185.  
  186. newSubrecipe.linkedRecipe = item.recipe;
  187. newSubrecipe.quantity = item.amount;
  188.  
  189. const savedSubrecipe = await this.subrecipeRepository.save(newSubrecipe);
  190.  
  191. return savedSubrecipe;
  192. }));
  193.  
  194. newRecipe.nutrition = totalRecipeNutrition;
  195.  
  196. const savedRecipe = await this.recipeRepository.save(newRecipe);
  197.  
  198. return this.convertToShowRecipeDTO(savedRecipe);
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement