Skip to content

Commit

Permalink
fix(order): add index to line_item_id and id fields in LineItemSchema
Browse files Browse the repository at this point in the history
fix(order): add index to id field in FulfillmentSchema
fix(order): add index to line_item_id and id fields in RefundLineItemSchema
fix(order): add index to id field in OrderMongooseSchema
fix(order): add transform function to FulfillmentZod and RefundZod schemas
fix(webhook): change findOneAndUpdate query to use id field instead of _id field
  • Loading branch information
jamalsoueidan committed Dec 9, 2023
1 parent d1fb32f commit 272d052
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 164 deletions.
97 changes: 17 additions & 80 deletions src/functions/customer/services/order/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,100 +46,37 @@ export const CustomerOrderServiceGet = async ({
{
$addFields: {
refunds: {
$map: {
input: {
$filter: {
input: "$refunds",
as: "refund",
cond: {
$anyElementTrue: {
$map: {
input: "$$refund.refund_line_items",
as: "refund_line_item",
in: {
$eq: [
"$$refund_line_item.line_item_id",
"$line_items.id",
],
},
},
},
},
},
},
as: "filtered_refund",
in: {
id: "$$filtered_refund.id",
admin_graphql_api_id: "$$filtered_refund.admin_graphql_api_id",
created_at: "$$filtered_refund.created_at",
note: "$$filtered_refund.note",
order_id: "$$filtered_refund.order_id",
processed_at: "$$filtered_refund.processed_at",
restock: "$$filtered_refund.restock",
total_duties_set: "$$filtered_refund.total_duties_set",
user_id: "$$filtered_refund.user_id",
order_adjustments: "$$filtered_refund.order_adjustments",
transactions: "$$filtered_refund.transactions",
duties: "$$filtered_refund.duties",
refund_line_items: {
$filter: {
input: "$refunds",
as: "refund",
cond: {
$anyElementTrue: {
$map: {
input: "$$filtered_refund.refund_line_items",
input: "$$refund.refund_line_items",
as: "refund_line_item",
in: {
id: "$$refund_line_item.id",
line_item_id: "$$refund_line_item.line_item_id",
location_id: "$$refund_line_item.location_id",
quantity: "$$refund_line_item.quantity",
restock_type: "$$refund_line_item.restock_type",
subtotal: "$$refund_line_item.subtotal",
subtotal_set: "$$refund_line_item.subtotal_set",
total_tax: "$$refund_line_item.total_tax",
total_tax_set: "$$refund_line_item.total_tax_set",
// Excluding the 'line_item' field
$eq: ["$$refund_line_item.line_item_id", "$line_items.id"],
},
},
},
},
},
},
fulfillments: {
$map: {
input: {
$filter: {
input: "$fulfillments",
as: "fulfillment",
cond: {
$anyElementTrue: {
$map: {
input: "$$fulfillment.line_items",
as: "fulfillment_line_item",
in: {
$eq: ["$$fulfillment_line_item.id", "$line_items.id"],
},
},
$filter: {
input: "$fulfillments",
as: "fulfillment",
cond: {
$anyElementTrue: {
$map: {
input: "$$fulfillment.line_items",
as: "fulfillment_line_item",
in: {
$eq: ["$$fulfillment_line_item.id", "$line_items.id"],
},
},
},
},
as: "fulfillment",
in: {
id: "$$fulfillment.id",
admin_graphql_api_id: "$$fulfillment.admin_graphql_api_id",
created_at: "$$fulfillment.created_at",
location_id: "$$fulfillment.location_id",
name: "$$fulfillment.name",
order_id: "$$fulfillment.order_id",
service: "$$fulfillment.service",
shipment_status: "$$fulfillment.shipment_status",
status: "$$fulfillment.status",
tracking_company: "$$fulfillment.tracking_company",
tracking_number: "$$fulfillment.tracking_number",
tracking_numbers: "$$fulfillment.tracking_numbers",
tracking_url: "$$fulfillment.tracking_url",
tracking_urls: "$$fulfillment.tracking_urls",
updated_at: "$$fulfillment.updated_at",
// Excluding the 'line_items' field
},
},
},
},
Expand Down
97 changes: 17 additions & 80 deletions src/functions/customer/services/order/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,100 +103,37 @@ export const CustomerOrderServiceList = async ({
{
$addFields: {
refunds: {
$map: {
input: {
$filter: {
input: "$refunds",
as: "refund",
cond: {
$anyElementTrue: {
$map: {
input: "$$refund.refund_line_items",
as: "refund_line_item",
in: {
$eq: [
"$$refund_line_item.line_item_id",
"$line_items.id",
],
},
},
},
},
},
},
as: "filtered_refund",
in: {
id: "$$filtered_refund.id",
admin_graphql_api_id: "$$filtered_refund.admin_graphql_api_id",
created_at: "$$filtered_refund.created_at",
note: "$$filtered_refund.note",
order_id: "$$filtered_refund.order_id",
processed_at: "$$filtered_refund.processed_at",
restock: "$$filtered_refund.restock",
total_duties_set: "$$filtered_refund.total_duties_set",
user_id: "$$filtered_refund.user_id",
order_adjustments: "$$filtered_refund.order_adjustments",
transactions: "$$filtered_refund.transactions",
duties: "$$filtered_refund.duties",
refund_line_items: {
$filter: {
input: "$refunds",
as: "refund",
cond: {
$anyElementTrue: {
$map: {
input: "$$filtered_refund.refund_line_items",
input: "$$refund.refund_line_items",
as: "refund_line_item",
in: {
id: "$$refund_line_item.id",
line_item_id: "$$refund_line_item.line_item_id",
location_id: "$$refund_line_item.location_id",
quantity: "$$refund_line_item.quantity",
restock_type: "$$refund_line_item.restock_type",
subtotal: "$$refund_line_item.subtotal",
subtotal_set: "$$refund_line_item.subtotal_set",
total_tax: "$$refund_line_item.total_tax",
total_tax_set: "$$refund_line_item.total_tax_set",
// Excluding the 'line_item' field
$eq: ["$$refund_line_item.line_item_id", "$line_items.id"],
},
},
},
},
},
},
fulfillments: {
$map: {
input: {
$filter: {
input: "$fulfillments",
as: "fulfillment",
cond: {
$anyElementTrue: {
$map: {
input: "$$fulfillment.line_items",
as: "fulfillment_line_item",
in: {
$eq: ["$$fulfillment_line_item.id", "$line_items.id"],
},
},
$filter: {
input: "$fulfillments",
as: "fulfillment",
cond: {
$anyElementTrue: {
$map: {
input: "$$fulfillment.line_items",
as: "fulfillment_line_item",
in: {
$eq: ["$$fulfillment_line_item.id", "$line_items.id"],
},
},
},
},
as: "fulfillment",
in: {
id: "$$fulfillment.id",
admin_graphql_api_id: "$$fulfillment.admin_graphql_api_id",
created_at: "$$fulfillment.created_at",
location_id: "$$fulfillment.location_id",
name: "$$fulfillment.name",
order_id: "$$fulfillment.order_id",
service: "$$fulfillment.service",
shipment_status: "$$fulfillment.shipment_status",
status: "$$fulfillment.status",
tracking_company: "$$fulfillment.tracking_company",
tracking_number: "$$fulfillment.tracking_number",
tracking_numbers: "$$fulfillment.tracking_numbers",
tracking_url: "$$fulfillment.tracking_url",
tracking_urls: "$$fulfillment.tracking_urls",
updated_at: "$$fulfillment.updated_at",
// Excluding the 'line_items' field
},
},
},
},
Expand Down
17 changes: 15 additions & 2 deletions src/functions/order/order.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ const LineItemSchema = new Schema(
type: Number,
required: true,
unique: true,
index: true,
},
admin_graphql_api_id: String,
fulfillable_quantity: Number,
Expand Down Expand Up @@ -221,7 +222,15 @@ const FulfillmentSchema = new Schema(
tracking_url: String,
tracking_urls: [String],
updated_at: Date,
line_items: [LineItemSchema],
line_items: [
{
id: {
type: Number,
index: true,
},
name: String,
},
],
},
{
autoIndex: false,
Expand All @@ -247,7 +256,10 @@ const RefundLineItemSchema = new Schema(
subtotal_set: PriceSetSchema,
total_tax: String,
total_tax_set: PriceSetSchema,
line_item: LineItemSchema,
line_item: {
id: Number,
name: String,
},
},
{
autoIndex: false,
Expand Down Expand Up @@ -312,6 +324,7 @@ export const OrdreMongooseSchema = new Schema<IOrderDocument, IOrderModel>({
type: Number,
required: true,
unique: true,
index: true,
},
admin_graphql_api_id: String,
app_id: Number,
Expand Down
16 changes: 16 additions & 0 deletions src/functions/order/order.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ const FulfillmentZod = z.object({
items.filter((item) =>
item.properties.some((prop) => prop.name === "_customerId")
)
)
.transform((items) =>
items.map((item) => ({
id: item.id,
name: item.name,
// If there are other specific fields you want to include, add them here.
}))
),
});

Expand Down Expand Up @@ -238,6 +245,15 @@ const RefundZod = z.object({
items.filter((item) =>
item.line_item.properties?.some((prop) => prop.name === "_customerId")
)
)
.transform((items) =>
items.map((item) => ({
...item,
line_item: {
id: item.line_item.id,
name: item.line_item.name,
},
}))
),
duties: z.array(
z.object({
Expand Down
1 change: 0 additions & 1 deletion src/functions/webhook/order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe("webhookOrderProcess", () => {
expect(res?.refunds.length).toBe(0);

res = await webhookOrderProcess(orderWithfulfillmentAndRefunds, context);
if (!res) throw new Error("res is undefined");

// TODO: Find a way to test that fulfillment actually gets the latest fulfillment of the unique line_item out from updated_at
/*const oldFulfillment = orderWithfulfillmentAndRefunds.fulfillments.find(
Expand Down
2 changes: 1 addition & 1 deletion src/functions/webhook/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function webhookOrderProcess(

context.log("webhook order success");

return await OrderModel.findOneAndUpdate({ _id: order.id }, order, {
return OrderModel.findOneAndUpdate({ id: order.id }, order, {
new: true,
upsert: true,
});
Expand Down

0 comments on commit 272d052

Please sign in to comment.