Skip to content

Commit

Permalink
Add last 5 orders and order count by product to customer insights
Browse files Browse the repository at this point in the history
  • Loading branch information
megastary committed Sep 12, 2024
1 parent c655e7f commit ec4241a
Showing 1 changed file with 102 additions and 6 deletions.
108 changes: 102 additions & 6 deletions routes/api/customerInsights.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,99 @@
import { Router } from 'express'
import { ensureAuthenticatedAPI } from '../../functions/ensureAuthenticatedAPI.js'
import User from '../../models/user.js'
import Order from '../../models/order.js'
const router = Router()
let responseJson

function getLastOrders(userId) {
return Order.aggregate([
{
$match: { buyerId: userId }
},
{
$sort: {
_id: -1
}
},
{
$lookup: {
from: 'deliveries',
localField: 'deliveryId',
foreignField: '_id',
as: 'deliveryInfo'
}
},
{
$unwind: '$deliveryInfo'
},
{
$lookup: {
from: 'products',
localField: 'deliveryInfo.productId',
foreignField: '_id',
as: 'productInfo'
}
},
{
$unwind: '$productInfo'
},
{
$limit: 5
},
{
$project: {
_id: 0,
keypadId: '$productInfo.keypadId',
productName: '$productInfo.displayName',
order_date: 1
}
}
])
}

function getMonthlyProductCount(userId) {
return Order.aggregate([
{
$match: { buyerId: userId }
},
{
$sort: {
_id: -1
}
},
{
$lookup: {
from: 'deliveries',
localField: 'deliveryId',
foreignField: '_id',
as: 'deliveryInfo'
}
},
{
$unwind: '$deliveryInfo'
},
{
$lookup: {
from: 'products',
localField: 'deliveryInfo.productId',
foreignField: '_id',
as: 'productInfo'
}
},
{
$unwind: '$productInfo'
},
{
$group: {
_id: '$productInfo.displayName',
count: {
$sum: 1
}
}
}
])
}

// GET /api/customerInsights - accepts customer's phone number and returns customer insights to be used by voice bot
router.get('/', ensureAuthenticatedAPI, function (req, res) {
// Check if request contains 'customer' parameter
Expand Down Expand Up @@ -46,12 +136,18 @@ router.get('/', ensureAuthenticatedAPI, function (req, res) {
res.json('NOT_FOUND')
} else {
res.status(200)
const orderId = user.card ? user.card : user.keypadId
responseJson = {
orderId: orderId,
display_name: user.displayName
}
res.json(responseJson)
getLastOrders(user._id).then((orders) => {
getMonthlyProductCount(user._id).then((monthlyProductCount) => {
const orderId = user.card ? user.card : user.keypadId
responseJson = {
orderId: orderId,
product_name: user.displayName,
last_five_products_bought: orders,
product_purchase_count_last_month: monthlyProductCount
}
res.json(responseJson)
})
})
}
})
.catch(() => {
Expand Down

0 comments on commit ec4241a

Please sign in to comment.