Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(order): remove unnecessary filtering of line items in Order schema #94

Merged
merged 6 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ components:
$ref: paths/customer/order/_types/client.yaml
CustomerOrderDiscount:
$ref: paths/customer/order/_types/discount.yaml
CustomerOrderLineItemProperties:
$ref: paths/customer/order/_types/properties.yaml
CustomerOrderSimpleLineItem:
$ref: paths/customer/order/_types/simple-line-item.yaml
CustomerOrderCustomer:
Expand Down Expand Up @@ -249,6 +251,8 @@ components:
$ref: paths/shipping/create/response.yaml
ShippingCalculateResponse:
$ref: paths/shipping/calculate/response.yaml
ShippingGetResponse:
$ref: paths/shipping/get/response.yaml

# Orchestrators
UploadBody:
Expand Down Expand Up @@ -369,6 +373,8 @@ paths:
$ref: "./paths/shipping/create/index.yaml"
/shipping/calculate:
$ref: "./paths/shipping/calculate/index.yaml"
/shipping/{shippingId}:
$ref: "./paths/shipping/get/index.yaml"

# Orchestrators
/orchestrators/upload:
Expand Down
4 changes: 1 addition & 3 deletions openapi/paths/customer/order/_types/line-item.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ properties:
type: number
nullable: true
properties:
type: array
items:
$ref: property.yaml
$ref: properties.yaml
quantity:
type: number
requires_shipping:
Expand Down
18 changes: 18 additions & 0 deletions openapi/paths/customer/order/_types/properties.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type: object
properties:
customer_id:
type: number
from:
type: string
to:
type: string
locationId:
type: string
shippingId:
type: string

required:
- from
- to
- customer_id
- locationId
15 changes: 0 additions & 15 deletions openapi/paths/customer/order/_types/property.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion openapi/paths/shipping/calculate/index.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
post:
tags:
- ShippingCalculate
- Shipping
operationId: shippingCalculate
summary: POST get shipping calculate
requestBody:
Expand Down
2 changes: 1 addition & 1 deletion openapi/paths/shipping/create/index.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
post:
tags:
- ShippingCreate
- Shipping
operationId: shippingCreate
summary: POST create shipping
requestBody:
Expand Down
31 changes: 31 additions & 0 deletions openapi/paths/shipping/get/index.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
get:
parameters:
- name: shippingId
in: path
required: true
schema:
type: string

tags:
- Shipping
operationId: shippingGet
summary: GET Get shipping
description: This endpoint gets shipping object
responses:
"200":
description: Response with payload
content:
application/json:
schema:
$ref: "./response.yaml"

"400":
$ref: "../../../responses/bad.yaml"
"401":
$ref: "../../../responses/unauthorized.yaml"
"403":
$ref: "../../../responses/forbidden.yaml"
"404":
$ref: "../../../responses/not-found.yaml"

security: []
10 changes: 10 additions & 0 deletions openapi/paths/shipping/get/response.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type: object
properties:
success:
type: boolean
example: true
payload:
$ref: ../_types/shipping.yaml
required:
- success
- payload
6 changes: 2 additions & 4 deletions src/functions/customer/services/order/get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ describe("CustomerOrderServiceGet", () => {

const lineItemId = response.line_items[0].id;

const customerId = response.line_items[0].properties?.find(
(p) => p.name === "_customerId"
)?.value as number | undefined;
const customerId = response.line_items[0].properties?.customerId || 0;

const order = await CustomerOrderServiceGet({
customerId: customerId || 0,
customerId: customerId,
lineItemId,
});

Expand Down
8 changes: 2 additions & 6 deletions src/functions/customer/services/order/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export const CustomerOrderServiceGet = async ({
$match: {
$and: [
{
"line_items.properties": {
$elemMatch: { name: "_customerId", value: customerId },
},
"line_items.properties.customerId": customerId,
},
{
line_items: {
Expand All @@ -33,9 +31,7 @@ export const CustomerOrderServiceGet = async ({
$match: {
$and: [
{
"line_items.properties": {
$elemMatch: { name: "_customerId", value: customerId },
},
"line_items.properties.customerId": customerId,
},
{
"line_items.id": lineItemId,
Expand Down
8 changes: 3 additions & 5 deletions src/functions/customer/services/order/list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import { CustomerOrderServiceList } from "./list";
require("~/library/jest/mongoose/mongodb.jest");

describe("CustomerOrderServiceList", () => {
it("should return orders for customer on specific year/month", async () => {
it("should return orders for customer on range of start/end", async () => {
const dumbData = Order.parse(orderWithfulfillmentAndRefunds);
const response = await OrderModel.create(dumbData);

const customerId = response.line_items[0].properties?.find(
(p) => p.name === "_customerId"
)?.value as number | undefined;
const customerId = response.line_items[0].properties?.customerId || 0;

const orders = await CustomerOrderServiceList({
customerId: customerId || 0,
customerId: customerId,
start: "2023-11-26T00:00:00+03:00",
end: "2024-01-07T00:00:00+03:00",
});
Expand Down
58 changes: 10 additions & 48 deletions src/functions/customer/services/order/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,12 @@ export const CustomerOrderServiceList = async ({
$match: {
$and: [
{
"line_items.properties": {
$elemMatch: { name: "_customerId", value: customerId },
},
"line_items.properties.customerId": customerId,
},
{
"line_items.properties": {
$elemMatch: {
name: "_from",
value: {
$gte: startDate,
$lte: endDate,
},
},
"line_items.properties.from": {
$gte: startDate,
$lte: endDate,
},
},
],
Expand All @@ -65,52 +58,21 @@ export const CustomerOrderServiceList = async ({
$match: {
$and: [
{
"line_items.properties": {
$elemMatch: { name: "_customerId", value: customerId },
},
"line_items.properties.customerId": customerId,
},
{
"line_items.properties": {
$elemMatch: {
name: "_from",
value: {
$gte: startDate,
$lte: endDate,
},
},
"line_items.properties.from": {
$gte: startDate,
$lte: endDate,
},
},
],
},
},
{
$addFields: {
start: {
$reduce: {
input: "$line_items.properties",
initialValue: null,
in: {
$cond: {
if: { $eq: ["$$this.name", "_from"] },
then: "$$this.value",
else: "$$value",
},
},
},
},
end: {
$reduce: {
input: "$line_items.properties",
initialValue: null,
in: {
$cond: {
if: { $eq: ["$$this.name", "_to"] },
then: "$$this.value",
else: "$$value",
},
},
},
},
start: "$line_items.properties.from",
end: "$line_items.properties.to",
title: "$line_items.title",
refunds: {
$filter: {
Expand Down
9 changes: 1 addition & 8 deletions src/functions/customer/services/order/paginate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import { OrderModel } from "~/functions/order/order.models";
import { Order } from "~/functions/order/order.types";
import { getOrderObject } from "~/library/jest/helpers/order";
import { CustomerOrderServicePaginate } from "./paginate";
require("~/library/jest/mongoose/mongodb.jest");

describe("CustomerOrderServicePaginate", () => {
it("should return orders for customer pagination", async () => {
const customerId = 1;
const dumbData = Order.parse(
getOrderObject({ customerId, lineItemsTotal: 50 })
);
const dumbData = getOrderObject({ customerId, lineItemsTotal: 50 });

await OrderModel.create(dumbData);

const orders = await CustomerOrderServicePaginate({
customerId: customerId || 0,
});

console.log(orders);

orders.results.forEach((p) => console.log(p.start));
});
});
29 changes: 3 additions & 26 deletions src/functions/customer/services/order/paginate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,23 @@ export const CustomerOrderServicePaginate = async ({
const pipeline: PipelineStage[] = [
{
$match: {
"line_items.properties": {
$elemMatch: { name: "_customerId", value: customerId },
},
"line_items.properties.customerId": customerId,
},
},
{ $unwind: "$line_items" },
{
$match: {
"line_items.properties": {
$elemMatch: { name: "_customerId", value: customerId },
},
"line_items.properties.customerId": customerId,
},
},
{
$addFields: {
start: {
$reduce: {
input: "$line_items.properties",
initialValue: null,
in: {
$cond: {
if: { $eq: ["$$this.name", "_from"] },
then: "$$this.value",
else: "$$value",
},
},
},
},
},
},
{
$sort: { "line_items._from": 1 },
$sort: { "line_items.properties.from": 1 },
},
];

const todayStart = new Date();
todayStart.setUTCHours(0, 0, 0, 0);

console.log({ $gte: nextCursor || todayStart });

pipeline.push({
$match: {
start: { $gte: nextCursor || todayStart },
Expand Down
36 changes: 11 additions & 25 deletions src/functions/order/order.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ const LineItemSchema = new Schema(
price_set: PriceSetSchema,
product_exists: Boolean,
product_id: Number,
properties: [propertySchema],
properties: {
from: Date,
to: Date,
customerId: { type: Number, index: true },
locationId: String,
shippingId: String,
},
quantity: Number,
requires_shipping: Boolean,
sku: String,
Expand All @@ -142,31 +148,11 @@ const LineItemSchema = new Schema(
}
);

const properties =
LineItemSchema.path<Schema.Types.DocumentArray>("properties");

properties.discriminator(
"CustomerIdProperty",
new Schema(
{
value: { type: Number, index: true },
},
{ _id: false }
)
);

properties.discriminator(
"DateProperty",
new Schema({
value: { type: Date, index: true },
})
);
LineItemSchema.index({ id: 1, "properties.customerId": 1 }, { unique: true });

properties.discriminator(
"OtherProperty",
new Schema({
value: String,
})
LineItemSchema.index(
{ "properties.from": 1, "properties.customerId": 1 },
{ unique: false }
);

const FulfillmentSchema = new Schema(
Expand Down
Loading
Loading