prioritize group in sequelize - node.js

I have a table named Products that is associated with another table named BaseProducts.
for getting the list of Products, I need to group the Products with BaseProductId.
but the problem is that I need to return only those products that has more quantity and the most cheapest. so I used Order. but it does not work.
const e = await Product.findAll({
order: [
["quantity", "DESC"],
["price", "ASC"],
["name", "ASC"]
],
group: "baseProductId",
limit: 100,
offset: 0,
});
here it's like it returns random items when using group. but I want to only include those Items that has first, more Quantity, second the most cheapest, and third sort alphabetically.
here is the sql generated by sequelize:
SELECT `id`, `name`, `nameEn`, `sku`, `barcode`, `quantity`, `price`, `finalPrice`, `sort`, `status`, `kianSync`, `createdAt`, `updatedAt`, `deletedAt`, `baseProductId` FROM `Products` AS `Product`
WHERE (`Product`.`deletedAt` IS NULL)
GROUP BY `baseProductId`
ORDER BY `Product`.`quantity` DESC, `Product`.`price` ASC, `Product`.`name` ASC
LIMIT 0, 100;
this is with group by:
[
{
"id": "3d2d181a-23f9-47b6-abee-ab61d1a86d02",
"name": "rrrrrrrr",
"nameEn": "nameEn",
"sku": "sku",
"barcode": "barcode",
"quantity": 1,
"price": 1000,
"finalPrice": 1000,
"sort": 1,
"status": "status",
"kianSync": 1,
"createdAt": "2023-01-02T06:53:26.000Z",
"updatedAt": "2023-01-02T06:53:26.000Z",
"deletedAt": null,
"baseProductId": null
}
]
this is without group by:
[
{
"id": "83939936-2c40-4921-9046-e40a1313bf77",
"name": "rrrrrrrr",
"nameEn": "nameEn",
"sku": "sku",
"barcode": "barcode",
"quantity": 2,
"price": 1000,
"finalPrice": 1000,
"sort": 2,
"status": "status",
"kianSync": 1,
"createdAt": "2023-01-02T06:53:26.000Z",
"updatedAt": "2023-01-02T06:53:26.000Z",
"deletedAt": null,
"baseProductId": null
},
{
"id": "8184b96c-1175-4de2-98c4-9faf231dabdc",
"name": "rrrrrrrr",
"nameEn": "nameEn",
"sku": "sku",
"barcode": "barcode",
"quantity": 2,
"price": 2000,
"finalPrice": 2000,
"sort": 3,
"status": "status",
"kianSync": 1,
"createdAt": "2023-01-02T06:53:26.000Z",
"updatedAt": "2023-01-02T06:53:26.000Z",
"deletedAt": null,
"baseProductId": null
},
{
"id": "3d2d181a-23f9-47b6-abee-ab61d1a86d02",
"name": "rrrrrrrr",
"nameEn": "nameEn",
"sku": "sku",
"barcode": "barcode",
"quantity": 1,
"price": 1000,
"finalPrice": 1000,
"sort": 1,
"status": "status",
"kianSync": 1,
"createdAt": "2023-01-02T06:53:26.000Z",
"updatedAt": "2023-01-02T06:53:26.000Z",
"deletedAt": null,
"baseProductId": null
}
]

Related

Grouping sequelize data in separate objects

i am new with nodejs express with posgresql , i made a route like this :
get: async (req, res, next) => {
var cat = await sequelize.query(
`
SELECT
o."id" as "cart_id",
o."user_id",
o."createdBy",
o."createdAt",
o."updatedAt",
i."quantity",
pi."name",
pi."price"
FROM "Orders" AS o
Left join public."CartItem" i on i."cart_id"=o."id"
Left join public."Item" pi on pi."id"=i."product_id"
WHERE o."user_id"=${req.body.user_id}
`
,
{ type: QueryTypes.SELECT,group:`i."cart_id"` })
.catch(e => res.send(e))
if (cat) {
// console.log(cat.Orders)
cat.map(e=> {
console.log(e)
})
res.send(cat)
}
},
if i hit this route with postman i get this response :
[
{
"cart_id": 9,
"user_id": 1,
"createdBy": null,
"createdAt": "2022-11-11T09:51:47.968Z",
"updatedAt": "2022-11-11T09:51:47.968Z",
"quantity": 4,
"name": "test1",
"price": 12000
},
{
"cart_id": 9,
"user_id": 1,
"createdBy": null,
"createdAt": "2022-11-11T09:51:47.968Z",
"updatedAt": "2022-11-11T09:51:47.968Z",
"quantity": 4,
"name": "test2",
"price": 12000
}
]
My question is, is it possible to assemble the response in the following way:
[
"cart_9":[
{
"user_id": 1,
"createdBy": null,
"createdAt": "2022-11-11T09:51:47.968Z",
"updatedAt": "2022-11-11T09:51:47.968Z",
"quantity": 4,
"name": "test1",
"price": 12000
},
{
"user_id": 1,
"createdBy": null,
"createdAt": "2022-11-11T09:51:47.968Z",
"updatedAt": "2022-11-11T09:51:47.968Z",
"quantity": 4,
"name": "test2",
"price": 12000
}
]
]
note that the response assembled by the "cart_id" .
any suggestion to assemble the response and get the required response ?

How to add orders array in product

Whenever a customer checks out its saves an order that has products ordered by a customer and the product id, quantity and customers details like delivery address and customer phone number.
I want the same details to be added to all products in that order also so that the seller can also see that his product has been ordered
This i can achieve using each product's id in the orders since every product has its ID . But i dont know how to map the order's products id's and find the products in the products schema then add to them an array called orders.
This is how my orders schema looks like after an order has been placed
{
"allOrders": [
{
"_id": "6249ee9baa59aceb7d88f91d",
"placedby": "624471321dff6794a802431c",
"fullname": "Collins Jimu",
"deliveryaddress": "cnr fourth and herbet chitepo",
"phonenumber": "0773468496",
"whatsappphonenumber": "0773468496",
"ordersList": [
{
"name": "Hp 200 G4Core I5",
"qty": 1,
"price": 560,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648734312/cwynmzauqqnylrwsgejy.jpg",
"id": "6245b0c09390975e16263136"
},
{
"name": "bed",
"qty": 1,
"price": 160,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648732217/dtrb8saj0ellgyrcjpyy.jpg",
"id": "6245a88cf22df558d8d69162"
},
{
"name": "Sunglass",
"qty": 1,
"price": 6,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648738341/rtpiptqc1le6kywmuisf.jpg",
"id": "6245c03bf22df558d8d6a739"
},
{
"name": "sofas",
"qty": 1,
"price": 890,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648732837/xae655b5e9wovodfkvqn.jpg",
"id": "6245aacdf22df558d8d6939f"
}
],
"paymentmethod": "cashondeliver",
"orderstatus": "placed",
"createdAt": "2022-04-03T18:59:39.681Z",
"updatedAt": "2022-04-03T18:59:39.681Z",
"__v": 0
},
{
"_id": "6249fdc602cefac4faa36dde",
"placedby": "624471321dff6794a802431c",
"fullname": "My MUM",
"deliveryaddress": "123 HARARE STREET",
"phonenumber": "0773550804",
"whatsappphonenumber": "0773550804",
"ordersList": [
{
"name": "out door benches",
"qty": 1,
"price": 25,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648736078/in8rumwcm6rrebrmgb1q.jpg",
"id": "6245b792be41db13f9220867"
},
{
"name": "hammer",
"qty": 1,
"price": 10,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648735570/joceetdkx4penomtab8s.jpg",
"id": "6245b5849390975e16263647"
},
{
"name": "Watches",
"qty": 1,
"price": 4,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648735962/r4jmwlj7ltgc0njdtfdc.jpg",
"id": "6245b71abe41db13f92207ac"
},
{
"name": "iPhone X",
"qty": 1,
"price": 350,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648729600/ekcb86lmtkr0uniirb2i.jpg",
"id": "62459fc3be41db13f921f27a"
},
{
"name": "REFRIDGERATOR",
"qty": 1,
"price": 370,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648731962/fjrlvfnotdbonqnt9pjn.jpg",
"id": "6245a750be41db13f921f796"
}
],
"paymentmethod": "cashondeliver",
"orderstatus": "placed",
"createdAt": "2022-04-03T20:04:22.349Z",
"updatedAt": "2022-04-03T20:04:22.349Z",
"__v": 0
},
{
"_id": "624a12ea02cefac4faa431fd",
"placedby": "624471321dff6794a802431c",
"fullname": "Munyaradzi Makosa",
"deliveryaddress": "cnr fourth and herbet",
"phonenumber": "0777727363663",
"whatsappphonenumber": "0777727363663",
"ordersList": [
{
"name": "Wheels",
"qty": 1,
"price": 35,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648741496/xs9zpht0rhxpijrzyqrz.jpg",
"id": "6245cc859390975e162657ce"
},
{
"name": "MacBook Pro 2021",
"qty": 1,
"price": 1250,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648731661/h0dcvvhwntdy06mzgpah.webp",
"id": "6245a6649390975e16262737"
},
{
"name": "electric kettle",
"qty": 1,
"price": 22,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648734446/tyrdlrayn3opm7gagwmi.jpg",
"id": "6245b1239390975e162631e3"
}
],
"paymentmethod": "cashondeliver",
"orderstatus": "placed",
"createdAt": "2022-04-03T21:34:34.721Z",
"updatedAt": "2022-04-03T21:34:34.721Z",
"__v": 0
},
{
"_id": "624aa7cd02cefac4faa4750c",
"placedby": "624aa7a102cefac4faa474f3",
"fullname": "Ryan Katayi",
"deliveryaddress": "4164 Gwabalanda",
"phonenumber": "0718518438",
"whatsappphonenumber": "0718518438",
"ordersList": [
{
"name": "Budi 2017",
"qty": 1,
"price": 250,
"imgUrl": "/assets/images/products/Automotive/2.Audi2017.png",
"id": "6270512109915791"
}
],
"paymentmethod": "cashondeliver",
"orderstatus": "placed",
"createdAt": "2022-04-04T08:09:49.618Z",
"updatedAt": "2022-04-04T08:09:49.618Z",
"__v": 0
},
{
"_id": "624c0e770d2cf94693988609",
"placedby": "624aa7a102cefac4faa474f3",
"fullname": "Ryan Katayi",
"deliveryaddress": "4164 Gwabalanda",
"phonenumber": "0718518438",
"whatsappphonenumber": "0778687452",
"ordersList": [
{
"name": "Nike Airforce",
"qty": 2,
"price": 40,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1649082553/hoodaley8bzrwa8xi2tr.jpg",
"id": "624b01250d2cf94693982652"
},
{
"name": "iPhone X",
"qty": 1,
"price": 350,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648729600/ekcb86lmtkr0uniirb2i.jpg",
"id": "62459fc3be41db13f921f27a"
}
],
"paymentmethod": "cashondeliver",
"orderstatus": "placed",
"createdAt": "2022-04-05T09:40:07.719Z",
"updatedAt": "2022-04-05T09:40:07.719Z",
"__v": 0
}
]
}
i have managed to get the ids of all the orders. But my challenge is i am stuck with only the products ids not sure how to use them and find each products and then patch them with an orders details array
const sellerProductsOrdered = asyncHandler (async (req, res) => {
const Orders = await Order.find();
const ordersList = Orders.map(order => {
return order
})
const ordersListProductsIds = ordersList.map(product => {
return product.id
})
console.log(ordersListProductsIds);
})

How to add an orders array under product schema

I have an orders schema which saves all orders when ever a customers checksout. It stores product ID.
This is how my orders schema looks like after saving an order.
{
"allOrders": [
{
"_id": "6249ee9baa59aceb7d88f91d",
"placedby": "624471321dff6794a802431c",
"fullname": "Collins Jimu",
"deliveryaddress": "cnr fourth and herbet chitepo",
"phonenumber": "0773468496",
"whatsappphonenumber": "0773468496",
"ordersList": [
{
"name": "Hp 200 G4Core I5",
"qty": 1,
"price": 560,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648734312/cwynmzauqqnylrwsgejy.jpg",
"id": "6245b0c09390975e16263136"
},
{
"name": "bed",
"qty": 1,
"price": 160,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648732217/dtrb8saj0ellgyrcjpyy.jpg",
"id": "6245a88cf22df558d8d69162"
},
{
"name": "Sunglass",
"qty": 1,
"price": 6,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648738341/rtpiptqc1le6kywmuisf.jpg",
"id": "6245c03bf22df558d8d6a739"
},
{
"name": "sofas",
"qty": 1,
"price": 890,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648732837/xae655b5e9wovodfkvqn.jpg",
"id": "6245aacdf22df558d8d6939f"
}
],
"paymentmethod": "cashondeliver",
"orderstatus": "placed",
"createdAt": "2022-04-03T18:59:39.681Z",
"updatedAt": "2022-04-03T18:59:39.681Z",
"__v": 0
},
]
my challenge now is after an order has been placed i want products ordered to display the orders under products schema since these products under the Products schema are showing.
The moment an order with different products is placed each product ordered must receiver the order details of the person who orderd it and quantity.

How to project an item in an array to the top level and also fetch only the matched objects in the array

Currently this is an example of a dataset in my salesOrder collection
{
"totalCost": 4,
"salesOrderRef": "s4",
"customerRef": "c10",
"date": "2021-06-03T00:00:00.000Z",
"etd": "2021-06-24T00:00:00.000Z",
"delivered": true,
"paid": false,
"inventory": [{
"sku": "i3",
"quantity": 1,
"priceEa": 2,
"discount": "0"
}, {
"sku": "i2",
"quantity": 2,
"priceEa": 2,
"discount": 2
}]
}
How do I get it to match
{
"salesOrderRef": "s4",
"customerRef": "c10",
"sku": "i3",
"quantity": 1,
"priceEa": 2,
"discount": "0"
}
if I search sku: i3? If there are any duplicate sku in the inventory array of a single salesOrder document, I would also like it to duplicate another result out, like
{
"salesOrderRef": "s4",
"customerRef": "c10",
"sku": "i3",
"quantity": 1,
"priceEa": 2,
"discount": "0"
},
{
"salesOrderRef": "s4",
"customerRef": "c10",
"sku": "i3",
"quantity": 3,
"priceEa": 4,
"discount": "0"
}
How could I achieve this? I tried using aggregation and filters but I'm not sure how
Try this aggregation query, from what I understood this is should give you the output document you have mentioned.
db.collection.aggregate([
{
$unwind: "$inventory"
}, {
$project: {
"_id" : 0,
"salesOrderRef": 1,
"customerRef": 1,
"sku": "$inventory.sku",
"quantity": "$inventory.quantity",
"priceEa": "$inventory.priceEa",
"discount": "$inventory.discount"
}
}, ])

Sequelize - Search using the associated table but exclude results from the associated table

Is it possible to search using the associated table but excluding the data in the associated table?
E.g.
db.Bill.findAll({
include: [{
model: self.db.Group,
where: {
id: groupId
},
include: [{
model: self.db.Account,
where: {username: username}
}]
}]
});
Currently, I get these results...
[
{
"id": 2,
"title": "Title",
"category": "OTHERS",
"currency": "php",
"recurring": false,
"date_from": null,
"date_to": null,
"createdAt": "2015-08-12T10:54:08.000Z",
"updatedAt": "2015-08-12T10:54:08.000Z",
"group_id": 2,
"Group": {
"id": 2,
"name": "Group",
"createdAt": "2015-08-12T10:54:07.000Z",
"updatedAt": "2015-08-12T10:54:07.000Z",
"Account": {
"username": "username"
}
}
}
]
I want to find all the bills under a group but I only want the results to be
[
{
"id": 2,
"title": "Title",
"category": "OTHERS",
"currency": "php",
"recurring": false,
"date_from": null,
"date_to": null,
"createdAt": "2015-08-12T10:54:08.000Z",
"updatedAt": "2015-08-12T10:54:08.000Z",
"group_id": 2
}
];

Resources