newTransaction.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. let newTransaction = {
  2. display: function(Transaction){
  3. let recipeList = document.getElementById("newTransactionRecipes");
  4. let template = document.getElementById("createTransaction").content.children[0];
  5. while(recipeList.children.length > 0){
  6. recipeList.removeChild(recipeList.firstChild);
  7. }
  8. for(let i = 0; i < merchant.recipes.length; i++){
  9. let recipeDiv = template.cloneNode(true);
  10. recipeDiv.recipe = merchant.recipes[i];
  11. recipeList.appendChild(recipeDiv);
  12. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  13. }
  14. document.getElementById("submitNewTransaction").onclick = ()=>{this.submit(Transaction)};
  15. },
  16. submit: function(Transaction){
  17. let recipeDivs = document.getElementById("newTransactionRecipes");
  18. let date = document.getElementById("newTransactionDate").valueAsDate;
  19. if(date > new Date()){
  20. banner.createError("CANNOT HAVE A DATE IN THE FUTURE");
  21. return;
  22. }
  23. let data = {
  24. date: date,
  25. recipes: [],
  26. ingredientUpdates: {}
  27. };
  28. for(let i = 0; i < recipeDivs.children.length; i++){
  29. let quantity = recipeDivs.children[i].children[1].value;
  30. const recipe = recipeDivs.children[i].recipe;
  31. if(quantity !== "" && quantity > 0){
  32. data.recipes.push({
  33. recipe: recipe.id,
  34. quantity: quantity
  35. });
  36. for(let j = 0; j < recipe.ingredients.length; j++){
  37. const ingredient = recipe.ingredients[j];
  38. if(data.ingredientUpdates[ingredient.ingredient.id]){
  39. data.ingredientUpdates[ingredient.ingredient.id] += ingredient.quantity * quantity;
  40. }else{
  41. data.ingredientUpdates[ingredient.ingredient.id] = ingredient.quantity * quantity;
  42. }
  43. }
  44. }else if(quantity < 0){
  45. banner.createError("CANNOT HAVE NEGATIVE VALUES");
  46. return;
  47. }
  48. }
  49. if(data.recipes.length > 0){
  50. let loader = document.getElementById("loaderContainer");
  51. loader.style.display = "flex";
  52. fetch("/transaction/create", {
  53. method: "post",
  54. headers: {
  55. "Content-Type": "application/json;charset=utf-8"
  56. },
  57. body: JSON.stringify(data)
  58. })
  59. .then(response => response.json())
  60. .then((response)=>{
  61. if(typeof(response) === "string"){
  62. banner.createError(response);
  63. }else{
  64. let transaction = new Transaction(
  65. response._id,
  66. response.date,
  67. response.recipes,
  68. merchant
  69. );
  70. merchant.editTransactions(transaction, data.ingredientUpdates);
  71. banner.createNotification("NEW TRANSACTION CREATED, INGREDIENTS UPDATED ACCORDINGLY");
  72. }
  73. })
  74. .catch((err)=>{
  75. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  76. })
  77. .finally(()=>{
  78. loader.style.display = "none";
  79. });
  80. }
  81. }
  82. }
  83. module.exports = newTransaction;