Advertisement
Shad3yz

Untitled

Jun 26th, 2020
2,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const makeAnOrder = async (
  2.   userId,
  3.   shopId,
  4.   branchId,
  5.   items,
  6.   notes,
  7.   payment_method,
  8.   address
  9. ) => {
  10.   if (!userId) throw new AppError('user id not specified in the request', 400);
  11.   if (!shopId || !branchId || !address)
  12.     throw new AppError('Missing parameters in the request', 400);
  13.  
  14.   if (!payment_method || !['Visa', 'Cash'].includes(payment_method))
  15.     throw new AppError('Missing payment method or invalid payment method');
  16.  
  17.   if (!items || !Array.isArray(items))
  18.     throw new AppError('Missing items argument or it is not an array');
  19.  
  20.   const itemsIds = items.map((el) => el.id);
  21.  
  22.   let itemsObjects = (
  23.     await Item.find({ _id: { $in: itemsIds }, active: true }).select(
  24.       '_id addons'
  25.     )
  26.   ).map((el) => el.toObject());
  27.  
  28.   itemsObjects.forEach((item) => {
  29.     item.addons.forEach((addon) => {
  30.       addon.options = convertArrayToObject(addon.options, '_id');
  31.     });
  32.     item.addons = convertArrayToObject(item.addons, '_id');
  33.   });
  34.  
  35.   itemsObjects = convertArrayToObject(itemsObjects, '_id');
  36.  
  37.   //console.log(JSON.stringify(itemsObjects, null, 2));
  38.  
  39.   //console.log(JSON.stringify(items, null, 2));
  40.  
  41.   itemsToBeInserted = [];
  42.  
  43.   items.forEach((item) => {
  44.     const itemToBeInserted = {
  45.       item: item,
  46.       addons: [],
  47.     };
  48.  
  49.     if (item.addons && Array.isArray(item.addons)) {
  50.       item.addons.forEach((addon) => {
  51.         const originalAddon = itemsObjects[item.id].addons[addon.id];
  52.         if (!originalAddon)
  53.           throw new AppError('Addon not found for an item', 400);
  54.  
  55.         if (!addon.options || addon.options.length === 0)
  56.           throw new AppError('A requested item does not have any addons', 400);
  57.  
  58.         addon.options.forEach((option) => {
  59.           if (!originalAddon.options[option])
  60.             throw new AppError(
  61.               'A requested option does not belong to its addon',
  62.               400
  63.             );
  64.         });
  65.  
  66.         if (originalAddon.multiple === false) {
  67.           if (addon.options.length > 1)
  68.             throw new AppError(
  69.               'A requested addon can only contain one option but you requested more than one option'
  70.             );
  71.         }
  72.  
  73.         const addonToBeInserted = {
  74.           name: originalAddon.name,
  75.           multiple: originalAddon.mutliple,
  76.           options: [],
  77.         };
  78.  
  79.         addon.options.forEach((option) => {
  80.           const originalOption = originalAddon.options[option];
  81.           addonToBeInserted.options.push({
  82.             name: originalOption.name,
  83.             price: originalOption.price,
  84.           });
  85.         });
  86.  
  87.         itemToBeInserted.addons.push(addonToBeInserted);
  88.       });
  89.     } else {
  90.       throw new AppError('Invalid addons parameter in an item', 400);
  91.     }
  92.  
  93.     itemsToBeInserted.push(itemToBeInserted);
  94.   });
  95.  
  96.   console.log(itemsToBeInserted);
  97. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement