Advertisement
Guest User

Untitled

a guest
Apr 19th, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [
  2.         // Stage 1: Match Document
  3.         {
  4.           $match: {
  5.             store: storeId,
  6.           }
  7.         },
  8.  
  9.         // Stage 2: Lookup OfferUsers
  10.         {
  11.           $lookup: {
  12.             from: "offerusers",
  13.             localField: "_id",
  14.             foreignField: "offer",
  15.             as: "customers"
  16.           }
  17.         },
  18.  
  19.         // Stage 3: Filter the interestedCustomer
  20.         {
  21.           $addFields: {
  22.             interestedCustomers: {
  23.               $filter: {
  24.                 input: "$customers",
  25.                 as: "customer",
  26.                 cond: {
  27.                   $and: [
  28.                     { $gte: ["$$customer.viewDate", startDate] },
  29.                     { $lte: ["$$customer.viewDate", endDate] }
  30.                   ]
  31.                 }
  32.               }
  33.             }
  34.           }
  35.         },
  36.  
  37.         // Stage 4: Add fields - footTraffic, sales
  38.         {
  39.           $addFields: {
  40.             footTrafficStatus: {
  41.               $filter: {
  42.                 input: "$customers",
  43.                 as: "customer",
  44.                 cond: {
  45.                   $and: [
  46.                     { $gte: ["$$customer.footTrafficDate", startDate] },
  47.                     { $lte: ["$$customer.footTrafficDate", endDate] }
  48.                   ]
  49.                 }
  50.               }
  51.             },
  52.             salesStatus: {
  53.               $filter: {
  54.                 input: "$customers",
  55.                 as: "customer",
  56.                 cond: {
  57.                   $and: [
  58.                     { $gte: ["$$customer.claimedAt", startDate] },
  59.                     { $lte: ["$$customer.claimedAt", endDate] }
  60.                   ]
  61.                 }
  62.               }
  63.             }
  64.           }
  65.         },
  66.  
  67.         // Stage 5: Add fields - interestedUsers, footTraffic, salesCount
  68.         {
  69.           $addFields: {
  70.             interestedUsers: {
  71.               $size: "$interestedCustomers"
  72.             },
  73.             footTraffic: {
  74.               $size: "$footTrafficStatus"
  75.             },
  76.             sales: {
  77.               $size: "$salesStatus"
  78.             },
  79.           }
  80.         },
  81.  
  82.         // Stage 6: Add fields - amount conversionRate, revenue
  83.         {
  84.           $addFields: {
  85.             salesStatus: {
  86.               $map: {
  87.                 input: "$salesStatus",
  88.                 as: "status",
  89.                 in: {
  90.                   $mergeObjects: [
  91.                     "$$status",
  92.                     {
  93.                       amount: {
  94.                         $cond: {
  95.                           if: { $eq: ['$__t', 'ParticularProduct'] },
  96.                           then: "$discountedPrice",
  97.                           else: "$minimumBillAmount",
  98.                         }
  99.                       }
  100.                     }
  101.                   ]
  102.                 }
  103.               }
  104.             },
  105.             conversionRate: {
  106.               $cond: {
  107.                 if: {
  108.                   $eq: ["$interestedUsers", 0]
  109.                 },
  110.                 then: 0,
  111.                 else: {
  112.                   $multiply: [
  113.                     {
  114.                       $divide: [
  115.                         "$sales", {
  116.                           $max: [1, "$interestedUsers"]
  117.                         }
  118.                       ]
  119.                     },
  120.                     100
  121.                   ]
  122.                 }
  123.               }
  124.             },
  125.             revenue: {
  126.               $cond: {
  127.                 if: {
  128.                   $eq: ["$sales", 0]
  129.                 },
  130.                 then: 0,
  131.                 else: {
  132.                   $cond: {
  133.                     if: { $eq: ['$__t', 'ParticularProduct'] },
  134.                     then: { $multiply: ["$sales", "$discountedPrice"] },
  135.                     else: { $multiply: ["$sales", "$minimumBillAmount"] }
  136.                   }
  137.                 }
  138.               }
  139.             }
  140.           }
  141.         },
  142.         // Stage 7: Group by _id to merge salesStatus objects into an array
  143.         {
  144.           $group: {
  145.             _id: null,
  146.             interestedUsers: { $sum: "$interestedUsers" },
  147.             footTraffic: { $sum: "$footTraffic" },
  148.             sales: { $sum: "$sales" },
  149.             revenue: { $sum: "$revenue" },
  150.             conversionRate: { $avg: "$conversionRate" },
  151.             interestedCustomers: { $push: "$interestedCustomers" },
  152.             footTrafficStatus: { $push: "$footTrafficStatus" },
  153.             salesStatus: { $push: "$salesStatus" }
  154.           }
  155.         },
  156.  
  157.         // Stage 8: Add objects in interestedCustomers, footTraffic, salesStatus array to the root level
  158.         {
  159.           $addFields: {
  160.             interestedCustomers: {
  161.               $reduce: {
  162.                 input: "$interestedCustomers",
  163.                 initialValue: [],
  164.                 in: { $concatArrays: ["$$value", "$$this"] }
  165.               }
  166.             },
  167.             footTrafficStatus: {
  168.               $reduce: {
  169.                 input: "$footTrafficStatus",
  170.                 initialValue: [],
  171.                 in: { $concatArrays: ["$$value", "$$this"] }
  172.               }
  173.             },
  174.             salesStatus: {
  175.               $reduce: {
  176.                 input: "$salesStatus",
  177.                 initialValue: [],
  178.                 in: { $concatArrays: ["$$value", "$$this"] }
  179.               }
  180.             }
  181.           }
  182.         },
  183.  
  184.         // Stage 9: Add field - visitedStore, madePurchase in the interestedCustomers & madePurchase in the footTrafficStatus
  185.         {
  186.           $addFields: {
  187.             interestedCustomers: {
  188.               $map: {
  189.                 input: "$interestedCustomers",
  190.                 as: "status",
  191.                 in: {
  192.                   $mergeObjects: [
  193.                     "$$status",
  194.                     {
  195.                       visitedStore: {
  196.                         $cond: {
  197.                           if: { $ifNull: ["$$status.footTrafficDate", false] },
  198.                           then: true,
  199.                           else: false
  200.                         }
  201.                       },
  202.                       madePurchase: {
  203.                         $cond: {
  204.                           if: { $ifNull: ["$$status.claimedAt", false] },
  205.                           then: true,
  206.                           else: false
  207.                         }
  208.                       }
  209.                     }
  210.                   ]
  211.                 }
  212.               }
  213.             },
  214.             footTrafficStatus: {
  215.               $map: {
  216.                 input: "$footTrafficStatus",
  217.                 as: "status",
  218.                 in: {
  219.                   $cond: {
  220.                     if: { $ifNull: ["$$status.claimedAt", false] },
  221.                     then: {
  222.                       $mergeObjects: [
  223.                         "$$status",
  224.                         { madePurchase: true }
  225.                       ]
  226.                     },
  227.                     else: {
  228.                       $mergeObjects: [
  229.                         "$$status",
  230.                         { madePurchase: false }
  231.                       ]
  232.                     }
  233.                   }
  234.                 }
  235.               }
  236.             }
  237.           }
  238.         },
  239.  
  240.         // Stage 10: Exclude specific fields from the interestedCustomers, footTrafficStatus and salesStatus array
  241.         {
  242.           $project: {
  243.             validOffersCount: 1,
  244.             interestedUsers: 1,
  245.             footTraffic: 1,
  246.             sales: 1,
  247.             revenue: 1,
  248.             conversionRate: 1,
  249.             interestedCustomers: {
  250.               $map: {
  251.                 input: "$interestedCustomers",
  252.                 as: "customer",
  253.                 in: {
  254.                   user: "$$customer.user",
  255.                   timeOfClicking: "$$customer.viewDate",
  256.                   visitedStore: "$$customer.visitedStore",
  257.                   madePurchase: "$$customer.madePurchase",
  258.                 }
  259.               }
  260.             },
  261.             footTrafficStatus: {
  262.               $map: {
  263.                 input: "$footTrafficStatus",
  264.                 as: "customer",
  265.                 in: {
  266.                   user: "$$customer.user",
  267.                   visitedTime: "$$customer.footTrafficDate",
  268.                   madePurchase: "$$customer.madePurchase"
  269.                 }
  270.               }
  271.             },
  272.             salesStatus: {
  273.               $map: {
  274.                 input: "$salesStatus",
  275.                 as: "customer",
  276.                 in: {
  277.                   user: "$$customer.user",
  278.                   purchasedAt: "$$customer.claimedAt",
  279.                   amount: "$$customer.amount"
  280.                 }
  281.               }
  282.             },
  283.           }
  284.         }
  285.       ]
  286.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement