From 3a684c71bea2ada0a3e7a48b5883b65641fa7c50 Mon Sep 17 00:00:00 2001 From: Jamal Soueidan Date: Fri, 1 Mar 2024 13:42:45 +0100 Subject: [PATCH 1/2] feat(customer-booking): add customer booking function with controllers for getting bookings by group and booking range feat(customer-order): remove redundant controllers for getting orders by group and order range feat(customer-booking): add aggregation pipeline for customer booking data feat(booking): add functionality to retrieve orders by group feat(booking): add functionality to retrieve orders within a specified range refactor(get-by-group.ts): remove unused imports and variables feat(get.spec.ts): add creation of shipping entity for testing purposes refactor(get.ts): update CustomerOrderServiceGetAggregate type definition feat(get.ts): add CustomLineItems type for more specific line item structure docs(get.ts): add comment explaining purpose of CustomerOrderServiceGet method chore(range.ts): remove unused CustomerOrderServiceRange function and types --- src/functions/customer-booking.function.ts | 18 ++ src/functions/customer-order.function.ts | 16 - .../controllers/booking/get-by-group.ts | 25 ++ .../customer/controllers/booking/range.ts | 26 ++ .../controllers/order/get-by-group.ts | 25 -- .../customer/controllers/order/range.ts | 26 -- .../customer/services/booking/aggregation.ts | 210 +++++++++++++ .../{order => booking}/get-by-group.spec.ts | 6 +- .../customer/services/booking/get-by-group.ts | 52 ++++ .../services/{order => booking}/range.spec.ts | 6 +- .../customer/services/booking/range.ts | 110 +++++++ .../customer/services/order/get-by-group.ts | 286 ------------------ .../customer/services/order/get.spec.ts | 14 +- src/functions/customer/services/order/get.ts | 132 +++----- .../customer/services/order/paginate.ts | 4 +- .../customer/services/order/range.ts | 280 ----------------- 16 files changed, 506 insertions(+), 730 deletions(-) create mode 100644 src/functions/customer-booking.function.ts create mode 100644 src/functions/customer/controllers/booking/get-by-group.ts create mode 100644 src/functions/customer/controllers/booking/range.ts delete mode 100644 src/functions/customer/controllers/order/get-by-group.ts delete mode 100644 src/functions/customer/controllers/order/range.ts create mode 100644 src/functions/customer/services/booking/aggregation.ts rename src/functions/customer/services/{order => booking}/get-by-group.spec.ts (89%) create mode 100644 src/functions/customer/services/booking/get-by-group.ts rename src/functions/customer/services/{order => booking}/range.spec.ts (94%) create mode 100644 src/functions/customer/services/booking/range.ts delete mode 100644 src/functions/customer/services/order/get-by-group.ts delete mode 100644 src/functions/customer/services/order/range.ts diff --git a/src/functions/customer-booking.function.ts b/src/functions/customer-booking.function.ts new file mode 100644 index 00000000..2bf5d83f --- /dev/null +++ b/src/functions/customer-booking.function.ts @@ -0,0 +1,18 @@ +import { app } from "@azure/functions"; +import "module-alias/register"; +import { CustomerBookingControllerGetByGroup } from "./customer/controllers/booking/get-by-group"; +import { CustomerBookingControllerRange } from "./customer/controllers/booking/range"; + +app.http("customerOrderGetByGroupId", { + methods: ["GET"], + authLevel: "anonymous", + route: "customer/{customerId?}/bookings/{orderId?}/group/{groupId?}", + handler: CustomerBookingControllerGetByGroup, +}); + +app.http("customerOrderRange", { + methods: ["GET"], + authLevel: "anonymous", + route: "customer/{customerId?}/bookings/range", + handler: CustomerBookingControllerRange, +}); diff --git a/src/functions/customer-order.function.ts b/src/functions/customer-order.function.ts index b92cac22..da57e7f9 100644 --- a/src/functions/customer-order.function.ts +++ b/src/functions/customer-order.function.ts @@ -3,15 +3,6 @@ import "module-alias/register"; import { app } from "@azure/functions"; import { CustomerOrderControllerGet } from "./customer/controllers/order/get"; -import { CustomerOrderControllerGetByGroup } from "./customer/controllers/order/get-by-group"; -import { CustomerOrderControllerRange } from "./customer/controllers/order/range"; - -app.http("customerOrderGetByGroupId", { - methods: ["GET"], - authLevel: "anonymous", - route: "customer/{customerId?}/orders/{orderId?}/group/{groupId?}", - handler: CustomerOrderControllerGetByGroup, -}); app.http("customerOrderGet", { methods: ["GET"], @@ -19,10 +10,3 @@ app.http("customerOrderGet", { route: "customer/{customerId?}/orders/{orderId?}", handler: CustomerOrderControllerGet, }); - -app.http("customerOrderRange", { - methods: ["GET"], - authLevel: "anonymous", - route: "customer/{customerId?}/orders-range", - handler: CustomerOrderControllerRange, -}); diff --git a/src/functions/customer/controllers/booking/get-by-group.ts b/src/functions/customer/controllers/booking/get-by-group.ts new file mode 100644 index 00000000..e73ea8c7 --- /dev/null +++ b/src/functions/customer/controllers/booking/get-by-group.ts @@ -0,0 +1,25 @@ +import { z } from "zod"; +import { _ } from "~/library/handler"; +import { NumberOrStringType } from "~/library/zod"; +import { CustomerBookingServiceGetByGroup } from "../../services/booking/get-by-group"; + +export type CustomerBookingControllerGetByGroupRequest = { + query: z.infer; +}; + +export const CustomerBookingControllerGetByGroupSchema = z.object({ + customerId: NumberOrStringType, + orderId: NumberOrStringType, + groupId: z.string(), +}); + +export type CustomerBookingControllerGetByGroupResponse = Awaited< + ReturnType +>; + +export const CustomerBookingControllerGetByGroup = _( + async ({ query }: CustomerBookingControllerGetByGroupRequest) => { + const validateData = CustomerBookingControllerGetByGroupSchema.parse(query); + return CustomerBookingServiceGetByGroup(validateData); + } +); diff --git a/src/functions/customer/controllers/booking/range.ts b/src/functions/customer/controllers/booking/range.ts new file mode 100644 index 00000000..d6fde7b0 --- /dev/null +++ b/src/functions/customer/controllers/booking/range.ts @@ -0,0 +1,26 @@ +import { _ } from "~/library/handler"; + +import { z } from "zod"; +import { NumberOrStringType } from "~/library/zod"; +import { CustomerBookingServiceRange } from "../../services/booking/range"; + +export type CustomerBookingControllerRangeRequest = { + query: z.infer; +}; + +export const CustomerBookingControllerRangeSchema = z.object({ + customerId: NumberOrStringType, + start: z.string(), + end: z.string(), +}); + +export type CustomerBookingControllerRangeResponse = Awaited< + ReturnType +>; + +export const CustomerBookingControllerRange = _( + async ({ query }: CustomerBookingControllerRangeRequest) => { + const validateData = CustomerBookingControllerRangeSchema.parse(query); + return CustomerBookingServiceRange(validateData); + } +); diff --git a/src/functions/customer/controllers/order/get-by-group.ts b/src/functions/customer/controllers/order/get-by-group.ts deleted file mode 100644 index cbdce04e..00000000 --- a/src/functions/customer/controllers/order/get-by-group.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { z } from "zod"; -import { _ } from "~/library/handler"; -import { NumberOrStringType } from "~/library/zod"; -import { CustomerOrderServiceGetByGroup } from "../../services/order/get-by-group"; - -export type CustomerOrderControllerGetByGroupRequest = { - query: z.infer; -}; - -export const CustomerOrderControllerGetByGroupSchema = z.object({ - customerId: NumberOrStringType, - orderId: NumberOrStringType, - groupId: z.string(), -}); - -export type CustomerOrderControllerGetByGroupResponse = Awaited< - ReturnType ->; - -export const CustomerOrderControllerGetByGroup = _( - async ({ query }: CustomerOrderControllerGetByGroupRequest) => { - const validateData = CustomerOrderControllerGetByGroupSchema.parse(query); - return CustomerOrderServiceGetByGroup(validateData); - } -); diff --git a/src/functions/customer/controllers/order/range.ts b/src/functions/customer/controllers/order/range.ts deleted file mode 100644 index e38c27ce..00000000 --- a/src/functions/customer/controllers/order/range.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { _ } from "~/library/handler"; - -import { z } from "zod"; -import { NumberOrStringType } from "~/library/zod"; -import { CustomerOrderServiceRange } from "../../services/order/range"; - -export type CustomerOrderControllerRangeRequest = { - query: z.infer; -}; - -export const CustomerOrderControllerRangeSchema = z.object({ - customerId: NumberOrStringType, - start: z.string(), - end: z.string(), -}); - -export type CustomerOrderControllerRangeResponse = Awaited< - ReturnType ->; - -export const CustomerOrderControllerRange = _( - async ({ query }: CustomerOrderControllerRangeRequest) => { - const validateData = CustomerOrderControllerRangeSchema.parse(query); - return CustomerOrderServiceRange(validateData); - } -); diff --git a/src/functions/customer/services/booking/aggregation.ts b/src/functions/customer/services/booking/aggregation.ts new file mode 100644 index 00000000..94179e70 --- /dev/null +++ b/src/functions/customer/services/booking/aggregation.ts @@ -0,0 +1,210 @@ +import { PipelineStage } from "mongoose"; + +export const bookingAggregation: PipelineStage[] = [ + { + $addFields: { + refunds: { + $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"], + }, + }, + }, + }, + }, + }, + fulfillments: { + $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"], + }, + }, + }, + }, + }, + }, + }, + }, + { + $sort: { + "line_items.properties.groupId": 1, + "line_items.properties.from": 1, + }, + }, + { + $group: { + _id: "$line_items.properties.groupId", + line_items: { $push: "$line_items" }, + customer: { $first: "$customer" }, + orderNumber: { $first: "$order_number" }, + fulfillmentStatus: { $first: "$fulfillment_status" }, + financialStatus: { $first: "$financial_status" }, + createdAt: { $first: "$created_at" }, + updatedAt: { $first: "$updated_at" }, + cancelReason: { $first: "$cancel_reason" }, + cancelledAt: { $first: "$cancelled_at" }, + note: { $first: "$note" }, + noteAttributes: { $first: "$note_attributes" }, + fulfillmentsArray: { $push: "$fulfillments" }, + refundsArray: { $push: "$refunds" }, + }, + }, + { + $addFields: { + locationId: { $first: "$line_items.properties.locationId" }, + shippingId: { $first: "$line_items.properties.shippingId" }, + groupId: { $first: "$line_items.properties.groupId" }, + end: { $last: "$line_items.properties.to" }, + start: { $first: "$line_items.properties.from" }, + }, + }, + { + $lookup: { + from: "Location", + let: { locationId: "$locationId" }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: ["$_id", { $toObjectId: "$$locationId" }], + }, + ], + }, + }, + }, + { + $project: { + name: 1, + fullAddress: 1, + originType: 1, + locationType: 1, + }, + }, + ], + as: "location", + }, + }, + { + $unwind: { + path: "$location", + preserveNullAndEmptyArrays: true, // Set to false if you always expect a match + }, + }, + { + $lookup: { + from: "Shipping", + let: { shippingId: "$shippingId" }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: ["$_id", { $toObjectId: "$$shippingId" }], + }, + ], + }, + }, + }, + { + $project: { + origin: 1, + destination: 1, + duration: 1, + distance: 1, + cost: 1, + }, + }, + ], + as: "shipping", + }, + }, + { + $unwind: { + path: "$shipping", + preserveNullAndEmptyArrays: true, + }, + }, + { + $addFields: { + start: { + $cond: { + if: { $gt: ["$shipping.duration.value", 0] }, + then: { + $dateSubtract: { + startDate: "$start", + unit: "minute", + amount: { $toInt: "$shipping.duration.value" }, + }, + }, + else: "$start", + }, + }, + end: { + $cond: { + if: { $gt: ["$shipping.duration.value", 0] }, + then: { + $dateAdd: { + startDate: "$end", + unit: "minute", + amount: { $toInt: "$shipping.duration.value" }, + }, + }, + else: "$end", + }, + }, + }, + }, + { + $project: { + id: "$_id", + start: 1, + end: 1, + shipping: 1, + user: 1, + groupId: 1, + location: 1, + line_items: 1, + customer: 1, + orderNumber: 1, + fulfillmentStatus: 1, + financialStatus: 1, + createdAt: 1, + updatedAt: 1, + cancelReason: 1, + cancelledAt: 1, + note: 1, + noteAttributes: 1, + fulfillments: { + $reduce: { + input: "$fulfillmentsArray", + initialValue: [], + in: { $concatArrays: ["$$value", "$$this"] }, + }, + }, + refunds: { + $reduce: { + input: "$refundsArray", + initialValue: [], + in: { $concatArrays: ["$$value", "$$this"] }, + }, + }, + }, + }, +]; diff --git a/src/functions/customer/services/order/get-by-group.spec.ts b/src/functions/customer/services/booking/get-by-group.spec.ts similarity index 89% rename from src/functions/customer/services/order/get-by-group.spec.ts rename to src/functions/customer/services/booking/get-by-group.spec.ts index f813cb0d..53bc566a 100644 --- a/src/functions/customer/services/order/get-by-group.spec.ts +++ b/src/functions/customer/services/booking/get-by-group.spec.ts @@ -3,7 +3,7 @@ import { Order } from "~/functions/order/order.types"; import { orderWithfulfillmentAndRefunds } from "~/functions/webhook/data-order-with-fullfilment-and-refunds"; import { createUser } from "~/library/jest/helpers"; import { createLocation } from "~/library/jest/helpers/location"; -import { CustomerOrderServiceGetByGroup } from "./get-by-group"; +import { CustomerBookingServiceGetByGroup } from "./get-by-group"; require("~/library/jest/mongoose/mongodb.jest"); describe("CustomerOrderServiceGetByGroup", () => { @@ -21,7 +21,7 @@ describe("CustomerOrderServiceGetByGroup", () => { const orderId = response.id; const ownerCustomerId = user.customerId; - let order = await CustomerOrderServiceGetByGroup({ + let order = await CustomerBookingServiceGetByGroup({ customerId: ownerCustomerId, orderId, groupId, @@ -31,7 +31,7 @@ describe("CustomerOrderServiceGetByGroup", () => { expect(order.fulfillments.length).toBe(1); expect(order.refunds.length).toBe(1); - order = await CustomerOrderServiceGetByGroup({ + order = await CustomerBookingServiceGetByGroup({ customerId: ownerCustomerId, orderId, groupId: "123", diff --git a/src/functions/customer/services/booking/get-by-group.ts b/src/functions/customer/services/booking/get-by-group.ts new file mode 100644 index 00000000..4e29cf43 --- /dev/null +++ b/src/functions/customer/services/booking/get-by-group.ts @@ -0,0 +1,52 @@ +import { OrderModel } from "~/functions/order/order.models"; +import { NotFoundError } from "~/library/handler"; +import { bookingAggregation } from "./aggregation"; +import { CustomerBookingServiceRangeAggregate } from "./range"; + +export type CustomerBookingServiceGetByGroupProps = { + customerId: number; + orderId: number; + groupId: string; +}; + +export const CustomerBookingServiceGetByGroup = async ({ + customerId, + orderId, + groupId, +}: CustomerBookingServiceGetByGroupProps) => { + const orders = + await OrderModel.aggregate([ + { + $match: { + $and: [ + { + "line_items.properties.customerId": customerId, + }, + { + "line_items.properties.groupId": groupId, + }, + { + id: orderId, + }, + ], + }, + }, + { $unwind: "$line_items" }, + { + $match: { "line_items.properties.groupId": groupId }, + }, + ...bookingAggregation, + ]); + + if (orders.length === 0) { + throw new NotFoundError([ + { + code: "custom", + message: "ORDER_NOT_FOUND", + path: ["lineItemId"], + }, + ]); + } + + return orders[0]; +}; diff --git a/src/functions/customer/services/order/range.spec.ts b/src/functions/customer/services/booking/range.spec.ts similarity index 94% rename from src/functions/customer/services/order/range.spec.ts rename to src/functions/customer/services/booking/range.spec.ts index 62a17245..ed6b2d52 100644 --- a/src/functions/customer/services/order/range.spec.ts +++ b/src/functions/customer/services/booking/range.spec.ts @@ -6,7 +6,7 @@ import { orderWithShipping } from "~/functions/webhook/data-order-with-shipping" import { createUser } from "~/library/jest/helpers"; import { createLocation } from "~/library/jest/helpers/location"; import { createShipping } from "~/library/jest/helpers/shipping"; -import { CustomerOrderServiceRange } from "./range"; +import { CustomerBookingServiceRange } from "./range"; require("~/library/jest/mongoose/mongodb.jest"); describe("CustomerOrderServiceRange", () => { @@ -18,7 +18,7 @@ describe("CustomerOrderServiceRange", () => { const customerId = response.line_items[0].properties?.customerId || 0; - const orders = await CustomerOrderServiceRange({ + const orders = await CustomerBookingServiceRange({ customerId: customerId, start: "2023-11-26T00:00:00+03:00", end: "2024-01-07T00:00:00+03:00", @@ -44,7 +44,7 @@ describe("CustomerOrderServiceRange", () => { await OrderModel.create(dumbData); - const orders = await CustomerOrderServiceRange({ + const orders = await CustomerBookingServiceRange({ customerId: customerId, start: "2023-11-26T00:00:00+03:00", end: "2024-01-07T00:00:00+03:00", diff --git a/src/functions/customer/services/booking/range.ts b/src/functions/customer/services/booking/range.ts new file mode 100644 index 00000000..ecfe0c00 --- /dev/null +++ b/src/functions/customer/services/booking/range.ts @@ -0,0 +1,110 @@ +import { Location } from "~/functions/location"; +import { OrderModel } from "~/functions/order/order.models"; +import { + Order, + OrderFulfillment, + OrderRefund, + OrderRefundLineItem, +} from "~/functions/order/order.types"; +import { Shipping } from "~/functions/shipping/shipping.types"; +import { bookingAggregation } from "./aggregation"; + +export type CustomerBookingServiceRangeProps = { + customerId: number; + start: string; + end: string; +}; + +export type CustomerBookingServiceRangeAggregate = Omit< + Order, + "refunds" | "fulfillments" +> & { + start: Date; + end: Date; + groupId: string; + shippingId: string; + title: string; + shipping?: Shipping; + location: Pick< + Location, + "name" | "fullAddress" | "locationType" | "originType" + >; + fulfillments: Array>; + refunds: Array< + Omit & { + refund_line_items: Array>; + } + >; +}; + +export const CustomerBookingServiceRange = async ({ + customerId, + start: startDate, + end: endDate, +}: CustomerBookingServiceRangeProps) => { + const start = new Date(startDate); + const end = new Date(endDate); + + return OrderModel.aggregate([ + { + $match: { + $and: [ + { + $or: [ + { + "line_items.properties.customerId": customerId, + }, + ], + }, + { + $or: [ + { + "line_items.properties.from": { + $gte: start, + $lte: end, + }, + }, + { + "line_items.properties.to": { + $gte: start, + $lte: end, + }, + }, + ], + }, + ], + }, + }, + { $unwind: "$line_items" }, + { + $match: { + $and: [ + { + $or: [ + { + "line_items.properties.customerId": customerId, + }, + ], + }, + { + $or: [ + { + "line_items.properties.from": { + $gte: start, + $lte: end, + }, + }, + { + "line_items.properties.to": { + $gte: start, + $lte: end, + }, + }, + ], + }, + ], + }, + }, + ...bookingAggregation, + ]); +}; diff --git a/src/functions/customer/services/order/get-by-group.ts b/src/functions/customer/services/order/get-by-group.ts deleted file mode 100644 index 1f33fd74..00000000 --- a/src/functions/customer/services/order/get-by-group.ts +++ /dev/null @@ -1,286 +0,0 @@ -import { OrderModel } from "~/functions/order/order.models"; -import { NotFoundError } from "~/library/handler"; -import { CustomerOrderServiceGetAggregate } from "./get"; - -export type CustomerOrderServiceGetByGroupProps = { - customerId: number; - orderId: number; - groupId: string; -}; - -export const CustomerOrderServiceGetByGroup = async ({ - customerId, - orderId, - groupId, -}: CustomerOrderServiceGetByGroupProps) => { - const orders = await OrderModel.aggregate([ - { - $match: { - $and: [ - { - "line_items.properties.customerId": customerId, - }, - { - "line_items.properties.groupId": groupId, - }, - { - id: orderId, - }, - ], - }, - }, - { $unwind: "$line_items" }, - { - $match: { "line_items.properties.groupId": groupId }, - }, - { - $addFields: { - refunds: { - $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"], - }, - }, - }, - }, - }, - }, - fulfillments: { - $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"], - }, - }, - }, - }, - }, - }, - }, - }, - { - $sort: { - "line_items.properties.from": 1, - }, - }, - { - $group: { - _id: "$line_items.properties.groupId", - line_items: { $push: "$line_items" }, - customer: { $first: "$customer" }, - orderNumber: { $first: "$order_number" }, - fulfillmentStatus: { $first: "$fulfillment_status" }, - financialStatus: { $first: "$financial_status" }, - createdAt: { $first: "$created_at" }, - updatedAt: { $first: "$updated_at" }, - cancelReason: { $first: "$cancel_reason" }, - cancelledAt: { $first: "$cancelled_at" }, - note: { $first: "$note" }, - noteAttributes: { $first: "$note_attributes" }, - fulfillmentsArray: { $push: "$fulfillments" }, - refundsArray: { $push: "$refunds" }, - }, - }, - { - $addFields: { - customerId: { $first: "$line_items.properties.customerId" }, - locationId: { $first: "$line_items.properties.locationId" }, - shippingId: { $first: "$line_items.properties.shippingId" }, - groupId: { $first: "$line_items.properties.groupId" }, - end: { $last: "$line_items.properties.to" }, - start: { $first: "$line_items.properties.from" }, - }, - }, - { - $lookup: { - from: "User", - let: { customerId: "$customerId" }, - pipeline: [ - { - $match: { - $expr: { - $eq: ["$customerId", "$$customerId"], - }, - }, - }, - { - $project: { - customerId: 1, - username: 1, - createdAt: 1, - fullname: 1, - shortDescription: 1, - "images.profile": "$images.profile", - }, - }, - ], - as: "user", - }, - }, - { - $unwind: { - path: "$user", - preserveNullAndEmptyArrays: true, // Set to false if you always expect a match - }, - }, - { - $lookup: { - from: "Location", - let: { locationId: "$locationId" }, - pipeline: [ - { - $match: { - $expr: { - $and: [ - { - $eq: ["$_id", { $toObjectId: "$$locationId" }], - }, - ], - }, - }, - }, - { - $project: { - name: 1, - fullAddress: 1, - originType: 1, - locationType: 1, - }, - }, - ], - as: "location", - }, - }, - { - $unwind: { - path: "$location", - preserveNullAndEmptyArrays: true, // Set to false if you always expect a match - }, - }, - { - $lookup: { - from: "Shipping", - let: { shippingId: "$shippingId" }, - pipeline: [ - { - $match: { - $expr: { - $and: [ - { - $eq: ["$_id", { $toObjectId: "$$shippingId" }], - }, - ], - }, - }, - }, - { - $project: { - origin: 1, - destination: 1, - duration: 1, - distance: 1, - cost: 1, - }, - }, - ], - as: "shipping", - }, - }, - { - $unwind: { - path: "$shipping", - preserveNullAndEmptyArrays: true, - }, - }, - { - $addFields: { - start: { - $cond: { - if: { $gt: ["$shipping.duration.value", 0] }, - then: { - $dateSubtract: { - startDate: "$start", - unit: "minute", - amount: { $toInt: "$shipping.duration.value" }, - }, - }, - else: "$start", - }, - }, - end: { - $cond: { - if: { $gt: ["$shipping.duration.value", 0] }, - then: { - $dateAdd: { - startDate: "$end", - unit: "minute", - amount: { $toInt: "$shipping.duration.value" }, - }, - }, - else: "$end", - }, - }, - }, - }, - { - $project: { - id: "$_id", - start: 1, - end: 1, - shipping: 1, - groupId: 1, - locationId: 1, - line_items: 1, - customer: 1, - orderNumber: 1, - fulfillmentStatus: 1, - financialStatus: 1, - createdAt: 1, - updatedAt: 1, - cancelReason: 1, - cancelledAt: 1, - note: 1, - noteAttributes: 1, - fulfillments: { - $reduce: { - input: "$fulfillmentsArray", - initialValue: [], - in: { $concatArrays: ["$$value", "$$this"] }, - }, - }, - refunds: { - $reduce: { - input: "$refundsArray", - initialValue: [], - in: { $concatArrays: ["$$value", "$$this"] }, - }, - }, - }, - }, - ]); - - if (orders.length === 0) { - throw new NotFoundError([ - { - code: "custom", - message: "ORDER_NOT_FOUND", - path: ["lineItemId"], - }, - ]); - } - - return orders[0]; -}; diff --git a/src/functions/customer/services/order/get.spec.ts b/src/functions/customer/services/order/get.spec.ts index 1902e4b2..5cb63ca1 100644 --- a/src/functions/customer/services/order/get.spec.ts +++ b/src/functions/customer/services/order/get.spec.ts @@ -3,6 +3,7 @@ import { Order } from "~/functions/order/order.types"; import { orderWithfulfillmentAndRefunds } from "~/functions/webhook/data-order-with-fullfilment-and-refunds"; import { createUser } from "~/library/jest/helpers"; import { createLocation } from "~/library/jest/helpers/location"; +import { createShipping } from "~/library/jest/helpers/shipping"; import { CustomerOrderServiceGet } from "./get"; require("~/library/jest/mongoose/mongodb.jest"); @@ -12,17 +13,18 @@ describe("CustomerOrderServiceGet", () => { const groupId = "2332"; const user = await createUser({ customerId }); const location = await createLocation({ customerId }); + const shipping = await createShipping({ location: location.id }); const dumbData = Order.parse(orderWithfulfillmentAndRefunds); - dumbData.line_items[0].properties!.customerId = user.customerId; - dumbData.line_items[0].properties!.locationId = location._id.toString(); - dumbData.line_items[0].properties!.groupId = groupId; + dumbData.line_items.map((lineItem) => { + lineItem.properties!.customerId = user.customerId; + lineItem.properties!.locationId = location._id.toString(); + lineItem.properties!.groupId = groupId; + }); const response = await OrderModel.create(dumbData); - const orderId = response.id; - const ownerCustomerId = user.customerId; let order = await CustomerOrderServiceGet({ - customerId: ownerCustomerId, + customerId: dumbData.customer.id, orderId, }); diff --git a/src/functions/customer/services/order/get.ts b/src/functions/customer/services/order/get.ts index 539eff91..af499bae 100644 --- a/src/functions/customer/services/order/get.ts +++ b/src/functions/customer/services/order/get.ts @@ -1,28 +1,38 @@ import { Location } from "~/functions/location"; import { OrderModel } from "~/functions/order/order.models"; +import { OrderLineItem } from "~/functions/order/order.types"; import { Shipping } from "~/functions/shipping/shipping.types"; import { User } from "~/functions/user"; import { NotFoundError } from "~/library/handler"; -import { CustomerOrderServiceRangeAggregate } from "./range"; +import { CustomerBookingServiceRangeAggregate } from "../booking/range"; -export type CustomerOrderServiceGetAggregate = - CustomerOrderServiceRangeAggregate & { - user: Pick< - User, - "customerId" | "username" | "fullname" | "images" | "shortDescription" - >; - location: Pick< - Location, - "name" | "fullAddress" | "locationType" | "originType" - >; - shipping?: Pick; - }; +export type CustomLineItems = OrderLineItem & { + user: Pick< + User, + "customerId" | "username" | "fullname" | "images" | "shortDescription" + >; + location: Pick< + Location, + "name" | "fullAddress" | "locationType" | "originType" + >; + shipping?: Shipping; +}; + +export type CustomerOrderServiceGetAggregate = Omit< + CustomerBookingServiceRangeAggregate, + "line_items" | "shipping" | "location" +> & { + line_items: CustomLineItems[]; +}; export type CustomerOrderServiceGetProps = { customerId: number; orderId: number; }; +/* + The purpose of this method is to render the whole order with all the treatments, not just one groupId. +*/ export const CustomerOrderServiceGet = async ({ customerId, orderId, @@ -32,7 +42,7 @@ export const CustomerOrderServiceGet = async ({ $match: { $and: [ { - "line_items.properties.customerId": customerId, + "customer.id": customerId, }, { id: orderId, @@ -81,41 +91,14 @@ export const CustomerOrderServiceGet = async ({ }, { $sort: { + "line_items.properties.groupId": 1, "line_items.properties.from": 1, }, }, - { - $group: { - _id: "$order_number", - line_items: { $push: "$line_items" }, - customer: { $first: "$customer" }, - orderNumber: { $first: "$order_number" }, - fulfillmentStatus: { $first: "$fulfillment_status" }, - financialStatus: { $first: "$financial_status" }, - createdAt: { $first: "$created_at" }, - updatedAt: { $first: "$updated_at" }, - cancelReason: { $first: "$cancel_reason" }, - cancelledAt: { $first: "$cancelled_at" }, - note: { $first: "$note" }, - noteAttributes: { $first: "$note_attributes" }, - fulfillmentsArray: { $push: "$fulfillments" }, - refundsArray: { $push: "$refunds" }, - }, - }, - { - $addFields: { - customerId: { $first: "$line_items.properties.customerId" }, - locationId: { $first: "$line_items.properties.locationId" }, - shippingId: { $first: "$line_items.properties.shippingId" }, - groupId: { $first: "$line_items.properties.groupId" }, - end: { $last: "$line_items.properties.to" }, - start: { $first: "$line_items.properties.from" }, - }, - }, { $lookup: { from: "User", - let: { customerId: "$customerId" }, + let: { customerId: "$line_items.properties.customerId" }, pipeline: [ { $match: { @@ -135,19 +118,19 @@ export const CustomerOrderServiceGet = async ({ }, }, ], - as: "user", + as: "line_items.user", }, }, { $unwind: { - path: "$user", + path: "$line_items.user", preserveNullAndEmptyArrays: true, // Set to false if you always expect a match }, }, { $lookup: { from: "Location", - let: { locationId: "$locationId" }, + let: { locationId: "$line_items.properties.locationId" }, pipeline: [ { $match: { @@ -169,19 +152,19 @@ export const CustomerOrderServiceGet = async ({ }, }, ], - as: "location", + as: "line_items.location", }, }, { $unwind: { - path: "$location", + path: "$line_items.location", preserveNullAndEmptyArrays: true, // Set to false if you always expect a match }, }, { $lookup: { from: "Shipping", - let: { shippingId: "$shippingId" }, + let: { shippingId: "$line_items.properties.shippingId" }, pipeline: [ { $match: { @@ -204,53 +187,36 @@ export const CustomerOrderServiceGet = async ({ }, }, ], - as: "shipping", + as: "line_items.shipping", }, }, { $unwind: { - path: "$shipping", + path: "$line_items.shipping", preserveNullAndEmptyArrays: true, }, }, { - $addFields: { - start: { - $cond: { - if: { $gt: ["$shipping.duration.value", 0] }, - then: { - $dateSubtract: { - startDate: "$start", - unit: "minute", - amount: { $toInt: "$shipping.duration.value" }, - }, - }, - else: "$start", - }, - }, - end: { - $cond: { - if: { $gt: ["$shipping.duration.value", 0] }, - then: { - $dateAdd: { - startDate: "$end", - unit: "minute", - amount: { $toInt: "$shipping.duration.value" }, - }, - }, - else: "$end", - }, - }, + $group: { + _id: "$order_number", + line_items: { $push: "$line_items" }, + customer: { $first: "$customer" }, + orderNumber: { $first: "$order_number" }, + fulfillmentStatus: { $first: "$fulfillment_status" }, + financialStatus: { $first: "$financial_status" }, + createdAt: { $first: "$created_at" }, + updatedAt: { $first: "$updated_at" }, + cancelReason: { $first: "$cancel_reason" }, + cancelledAt: { $first: "$cancelled_at" }, + note: { $first: "$note" }, + noteAttributes: { $first: "$note_attributes" }, + fulfillmentsArray: { $push: "$fulfillments" }, + refundsArray: { $push: "$refunds" }, }, }, { $project: { id: "$_id", - start: 1, - end: 1, - shipping: 1, - groupId: 1, - locationId: 1, line_items: 1, customer: 1, orderNumber: 1, diff --git a/src/functions/customer/services/order/paginate.ts b/src/functions/customer/services/order/paginate.ts index f6403f4f..00434c20 100644 --- a/src/functions/customer/services/order/paginate.ts +++ b/src/functions/customer/services/order/paginate.ts @@ -1,6 +1,6 @@ import { PipelineStage } from "mongoose"; import { OrderModel } from "~/functions/order/order.models"; -import { CustomerOrderServiceRangeAggregate } from "./range"; +import { CustomerBookingServiceRangeAggregate } from "../booking/range"; export type CustomerOrderServiceListProps = { customerId: number; @@ -110,7 +110,7 @@ export const CustomerOrderServicePaginate = async ({ ); const results = - await OrderModel.aggregate(pipeline); + await OrderModel.aggregate(pipeline); return { results, diff --git a/src/functions/customer/services/order/range.ts b/src/functions/customer/services/order/range.ts deleted file mode 100644 index 588528b7..00000000 --- a/src/functions/customer/services/order/range.ts +++ /dev/null @@ -1,280 +0,0 @@ -import { OrderModel } from "~/functions/order/order.models"; -import { - Order, - OrderFulfillment, - OrderLineItem, - OrderRefund, - OrderRefundLineItem, -} from "~/functions/order/order.types"; -import { Shipping } from "~/functions/shipping/shipping.types"; - -export type CustomerOrderServiceRangeProps = { - customerId: number; - start: string; - end: string; -}; - -export type CustomerOrderServiceRangeAggregate = Omit< - Order, - "line_items" | "refunds" | "fulfillments" -> & { - start: Date; - end: Date; - groupId: string; - shippingId: string; - title: string; - line_items: OrderLineItem[]; - shipping?: Shipping; - fulfillments: Array>; - refunds: Array< - Omit & { - refund_line_items: Array>; - } - >; -}; - -export const CustomerOrderServiceRange = async ({ - customerId, - start: startDate, - end: endDate, -}: CustomerOrderServiceRangeProps) => { - const start = new Date(startDate); - const end = new Date(endDate); - - return OrderModel.aggregate([ - { - $match: { - $and: [ - { - $or: [ - { - "line_items.properties.customerId": customerId, - }, - { - "customer.id": customerId, - }, - ], - }, - { - $or: [ - { - "line_items.properties.from": { - $gte: start, - $lte: end, - }, - }, - { - "line_items.properties.to": { - $gte: start, - $lte: end, - }, - }, - ], - }, - ], - }, - }, - { $unwind: "$line_items" }, - { - $match: { - $and: [ - { - $or: [ - { - "line_items.properties.customerId": customerId, - }, - { - "customer.id": customerId, - }, - ], - }, - { - $or: [ - { - "line_items.properties.from": { - $gte: start, - $lte: end, - }, - }, - { - "line_items.properties.to": { - $gte: start, - $lte: end, - }, - }, - ], - }, - ], - }, - }, - { - $addFields: { - refunds: { - $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"], - }, - }, - }, - }, - }, - }, - fulfillments: { - $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"], - }, - }, - }, - }, - }, - }, - }, - }, - { - $sort: { - "line_items.properties.groupId": 1, - "line_items.properties.from": 1, - }, - }, - { - $group: { - _id: "$line_items.properties.groupId", - line_items: { $push: "$line_items" }, - customer: { $first: "$customer" }, - orderNumber: { $first: "$order_number" }, - fulfillmentStatus: { $first: "$fulfillment_status" }, - financialStatus: { $first: "$financial_status" }, - createdAt: { $first: "$created_at" }, - updatedAt: { $first: "$updated_at" }, - cancelReason: { $first: "$cancel_reason" }, - cancelledAt: { $first: "$cancelled_at" }, - note: { $first: "$note" }, - noteAttributes: { $first: "$note_attributes" }, - fulfillmentsArray: { $push: "$fulfillments" }, - refundsArray: { $push: "$refunds" }, - }, - }, - { - $addFields: { - shippingId: { $first: "$line_items.properties.shippingId" }, - groupId: { $first: "$line_items.properties.groupId" }, - end: { $last: "$line_items.properties.to" }, - start: { $first: "$line_items.properties.from" }, - }, - }, - { - $lookup: { - from: "Shipping", - let: { shippingId: "$shippingId" }, - pipeline: [ - { - $match: { - $expr: { - $and: [ - { - $eq: ["$_id", { $toObjectId: "$$shippingId" }], - }, - ], - }, - }, - }, - { - $project: { - origin: 1, - destination: 1, - duration: 1, - distance: 1, - cost: 1, - }, - }, - ], - as: "shipping", - }, - }, - { - $unwind: { - path: "$shipping", - preserveNullAndEmptyArrays: true, - }, - }, - { - $addFields: { - start: { - $cond: { - if: { $gt: ["$shipping.duration.value", 0] }, - then: { - $dateSubtract: { - startDate: "$start", - unit: "minute", - amount: { $toInt: "$shipping.duration.value" }, - }, - }, - else: "$start", - }, - }, - end: { - $cond: { - if: { $gt: ["$shipping.duration.value", 0] }, - then: { - $dateAdd: { - startDate: "$end", - unit: "minute", - amount: { $toInt: "$shipping.duration.value" }, - }, - }, - else: "$end", - }, - }, - }, - }, - { - $project: { - id: "$_id", - start: 1, - end: 1, - shipping: 1, - groupId: 1, - line_items: 1, - customer: 1, - orderNumber: 1, - fulfillmentStatus: 1, - financialStatus: 1, - createdAt: 1, - updatedAt: 1, - cancelReason: 1, - cancelledAt: 1, - note: 1, - noteAttributes: 1, - fulfillments: { - $reduce: { - input: "$fulfillmentsArray", - initialValue: [], - in: { $concatArrays: ["$$value", "$$this"] }, - }, - }, - refunds: { - $reduce: { - input: "$refundsArray", - initialValue: [], - in: { $concatArrays: ["$$value", "$$this"] }, - }, - }, - }, - }, - ]); -}; From 729fd93573404ef4f4c5270b638e0db4e0283c92 Mon Sep 17 00:00:00 2001 From: Jamal Soueidan Date: Fri, 1 Mar 2024 13:43:21 +0100 Subject: [PATCH 2/2] feat(openapi): add base order schema and related sub-schemas feat(openapi): add client schema for orders feat(openapi): add customer schema for orders feat(openapi): add fulfillment schema for orders feat(openapi): add line item schema for orders feat(openapi): add new YAML files for order-related types and properties refactor(order.yaml): refactor order schema to use base-order schema and add line_items property fix(response.yaml): update payload reference to use correct order schema --- openapi/openapi.yaml | 72 ++++++----- .../_types => _types/order}/address.yaml | 0 .../customer/_types/order/base-order.yaml | 88 +++++++++++++ .../_types => _types/order}/client.yaml | 0 .../_types => _types/order}/customer.yaml | 0 .../_types => _types/order}/fulfillment.yaml | 0 .../customer/_types/order/line-item.yaml | 75 +++++++++++ .../{order/_types => _types/order}/money.yaml | 0 .../_types => _types/order}/properties.yaml | 0 .../order}/refund-line-item.yaml | 0 .../_types => _types/order}/refund.yaml | 0 .../order}/shipping_lines.yaml | 0 .../order}/simple-line-item.yaml | 0 .../{order/_types => _types/order}/user.yaml | 0 .../customer/booking/_types/booking.yaml | 30 +++++ .../get-by-group/index.yaml | 4 +- .../get-by-group/response.yaml | 2 +- .../{order => booking}/range/index.yaml | 4 +- .../{order => booking}/range/response.yaml | 2 +- .../customer/order/_types/line-item.yaml | 88 ++----------- .../order/_types/order-with-lookup.yaml | 11 -- .../paths/customer/order/_types/order.yaml | 117 ++---------------- .../paths/customer/order/get/response.yaml | 2 +- 23 files changed, 265 insertions(+), 230 deletions(-) rename openapi/paths/customer/{order/_types => _types/order}/address.yaml (100%) create mode 100644 openapi/paths/customer/_types/order/base-order.yaml rename openapi/paths/customer/{order/_types => _types/order}/client.yaml (100%) rename openapi/paths/customer/{order/_types => _types/order}/customer.yaml (100%) rename openapi/paths/customer/{order/_types => _types/order}/fulfillment.yaml (100%) create mode 100644 openapi/paths/customer/_types/order/line-item.yaml rename openapi/paths/customer/{order/_types => _types/order}/money.yaml (100%) rename openapi/paths/customer/{order/_types => _types/order}/properties.yaml (100%) rename openapi/paths/customer/{order/_types => _types/order}/refund-line-item.yaml (100%) rename openapi/paths/customer/{order/_types => _types/order}/refund.yaml (100%) rename openapi/paths/customer/{order/_types => _types/order}/shipping_lines.yaml (100%) rename openapi/paths/customer/{order/_types => _types/order}/simple-line-item.yaml (100%) rename openapi/paths/customer/{order/_types => _types/order}/user.yaml (100%) create mode 100644 openapi/paths/customer/booking/_types/booking.yaml rename openapi/paths/customer/{order => booking}/get-by-group/index.yaml (94%) rename openapi/paths/customer/{order => booking}/get-by-group/response.yaml (73%) rename openapi/paths/customer/{order => booking}/range/index.yaml (94%) rename openapi/paths/customer/{order => booking}/range/response.yaml (80%) delete mode 100644 openapi/paths/customer/order/_types/order-with-lookup.yaml diff --git a/openapi/openapi.yaml b/openapi/openapi.yaml index 25f1b25b..58862314 100644 --- a/openapi/openapi.yaml +++ b/openapi/openapi.yaml @@ -95,37 +95,45 @@ components: CustomerScheduleSlotsUpdateBody: $ref: paths/customer/schedule/slots/update/body.yaml + # Base Order + CustomerBaseOrder: + $ref: paths/customer/order/_types/order.yaml + CustomerBaseOrderMoney: + $ref: paths/customer/_types/order/money.yaml + CustomerBaseOrderAddress: + $ref: paths/customer/_types/order/address.yaml + CustomerBaseOrderClient: + $ref: paths/customer/_types/order/client.yaml + CustomerBaseOrderLineItemProperties: + $ref: paths/customer/_types/order/properties.yaml + CustomerBaseOrderSimpleLineItem: + $ref: paths/customer/_types/order/simple-line-item.yaml + CustomerBaseOrderCustomer: + $ref: paths/customer/_types/order/customer.yaml + CustomeBaserOrderLineItem: + $ref: paths/customer/_types/order/line-item.yaml + CustomerBaseOrderRefund: + $ref: paths/customer/_types/order/refund.yaml + CustomerBaseOrderShippingLines: + $ref: paths/customer/_types/order//shipping_lines.yaml + CustomerBaseOrderFulfillment: + $ref: paths/customer/_types/order/fulfillment.yaml + # Order - CustomerOrder: + CustomerOrderResponse: $ref: paths/customer/order/_types/order.yaml - CustomerOrderMoney: - $ref: paths/customer/order/_types/money.yaml - CustomerOrderAddress: - $ref: paths/customer/order/_types/address.yaml - CustomerOrderClient: - $ref: paths/customer/order/_types/client.yaml - CustomerOrderLineItemProperties: - $ref: paths/customer/order/_types/properties.yaml - CustomerOrderSimpleLineItem: - $ref: paths/customer/order/_types/simple-line-item.yaml - CustomerOrderCustomer: - $ref: paths/customer/order/_types/customer.yaml - CustomerOrderLineItem: + CustomerOrderLineItemResponse: $ref: paths/customer/order/_types/line-item.yaml - CustomerOrderRefund: - $ref: paths/customer/order/_types/refund.yaml - CustomerOrderShippingLines: - $ref: paths/customer/order/_types/shipping_lines.yaml - CustomerOrderFulfillment: - $ref: paths/customer/order/_types/fulfillment.yaml - CustomerOrderWithLookup: - $ref: paths/customer/order/_types/order-with-lookup.yaml CustomerOrderGetResponse: $ref: paths/customer/order/get/response.yaml - CustomerOrderGetByGroupIdResponse: - $ref: paths/customer/order/get-by-group/response.yaml - CustomerOrderRangeResponse: - $ref: paths/customer/order/range/response.yaml + + # Booking + CustomerBooking: + $ref: paths/customer/booking/_types/booking.yaml + CustomerBookingGetByGroupIdResponse: + $ref: paths/customer/booking/get-by-group/response.yaml + CustomerBookingRangeResponse: + $ref: paths/customer/booking/range/response.yaml # Customer CustomerProductList: @@ -326,12 +334,16 @@ paths: $ref: "./paths/customer/product/list-ids/index.yaml" /customer/{customerId}/product/{productId}: $ref: "paths/customer/product/product.yaml" - /customer/{customerId}/orders/{orderId}/group/{groupId}: - $ref: "paths/customer/order/get-by-group/index.yaml" + + # Orders + /customer/{customerId}/bookings/{orderId}/group/{groupId}: + $ref: "paths/customer/booking/get-by-group/index.yaml" + /customer/{customerId}/bookings/range: + $ref: "paths/customer/booking/range/index.yaml" + + # Booking /customer/{customerId}/orders/{orderId}: $ref: "paths/customer/order/get/index.yaml" - /customer/{customerId}/orders-range: - $ref: "paths/customer/order/range/index.yaml" # schedule /customer/{customerId}/schedule: diff --git a/openapi/paths/customer/order/_types/address.yaml b/openapi/paths/customer/_types/order/address.yaml similarity index 100% rename from openapi/paths/customer/order/_types/address.yaml rename to openapi/paths/customer/_types/order/address.yaml diff --git a/openapi/paths/customer/_types/order/base-order.yaml b/openapi/paths/customer/_types/order/base-order.yaml new file mode 100644 index 00000000..74fced8d --- /dev/null +++ b/openapi/paths/customer/_types/order/base-order.yaml @@ -0,0 +1,88 @@ +type: object +properties: + id: + type: number + order_number: + type: number + admin_graphql_api_id: + type: string + buyer_accepts_marketing: + type: boolean + cancel_reason: + type: string + nullable: true + cancelled_at: + type: string + client_details: + $ref: client.yaml + closed_at: + type: string + nullable: true + confirmed: + type: boolean + contact_email: + type: string + nullable: true + created_at: + type: string + currency: + type: string + current_subtotal_price: + type: string + current_subtotal_price_set: + $ref: money.yaml + current_total_additional_fees_set: + $ref: money.yaml + nullable: true + current_total_discounts: + type: string + current_total_discounts_set: + $ref: money.yaml + current_total_duties_set: + $ref: money.yaml + nullable: true + current_total_price: + type: string + current_total_price_set: + $ref: money.yaml + current_total_tax: + type: string + current_total_tax_set: + $ref: money.yaml + customer: + $ref: customer.yaml + fulfillments: + type: array + items: + $ref: fulfillment.yaml + refunds: + type: array + items: + $ref: refund.yaml + shipping_address: + $ref: address.yaml + nullable: true + shipping_lines: + type: array + items: + $ref: shipping_lines.yaml +required: + - id + - admin_graphql_api_id + - buyer_accepts_marketing + - confirmed + - created_at + - currency + - customer + - current_subtotal_price + - current_subtotal_price_set + - current_total_discounts + - current_total_discounts_set + - current_total_price + - current_total_price_set + - current_total_tax + - current_total_tax_set + - fulfillments + - refunds + - shipping_lines + - order_number diff --git a/openapi/paths/customer/order/_types/client.yaml b/openapi/paths/customer/_types/order/client.yaml similarity index 100% rename from openapi/paths/customer/order/_types/client.yaml rename to openapi/paths/customer/_types/order/client.yaml diff --git a/openapi/paths/customer/order/_types/customer.yaml b/openapi/paths/customer/_types/order/customer.yaml similarity index 100% rename from openapi/paths/customer/order/_types/customer.yaml rename to openapi/paths/customer/_types/order/customer.yaml diff --git a/openapi/paths/customer/order/_types/fulfillment.yaml b/openapi/paths/customer/_types/order/fulfillment.yaml similarity index 100% rename from openapi/paths/customer/order/_types/fulfillment.yaml rename to openapi/paths/customer/_types/order/fulfillment.yaml diff --git a/openapi/paths/customer/_types/order/line-item.yaml b/openapi/paths/customer/_types/order/line-item.yaml new file mode 100644 index 00000000..9cea2254 --- /dev/null +++ b/openapi/paths/customer/_types/order/line-item.yaml @@ -0,0 +1,75 @@ +type: object +properties: + id: + type: number + admin_graphql_api_id: + type: string + fulfillable_quantity: + type: number + fulfillment_service: + type: string + fulfillment_status: + type: string + nullable: true + gift_card: + type: boolean + grams: + type: number + name: + type: string + price: + type: string + price_set: + $ref: money.yaml + product_exists: + type: boolean + product_id: + type: number + properties: + $ref: properties.yaml + quantity: + type: number + requires_shipping: + type: boolean + sku: + type: string + nullable: true + taxable: + type: boolean + title: + type: string + total_discount: + type: string + total_discount_set: + $ref: money.yaml + variant_id: + type: number + variant_inventory_management: + type: string + nullable: true + variant_title: + type: string + nullable: true + vendor: + type: string + nullable: true +required: + - id + - admin_graphql_api_id + - fulfillable_quantity + - fulfillment_service + - gift_card + - grams + - name + - price + - properties + - price_set + - product_exists + - product_id + - variant_id + - quantity + - requires_shipping + - taxable + - title + - total_discount + - total_discount_set diff --git a/openapi/paths/customer/order/_types/money.yaml b/openapi/paths/customer/_types/order/money.yaml similarity index 100% rename from openapi/paths/customer/order/_types/money.yaml rename to openapi/paths/customer/_types/order/money.yaml diff --git a/openapi/paths/customer/order/_types/properties.yaml b/openapi/paths/customer/_types/order/properties.yaml similarity index 100% rename from openapi/paths/customer/order/_types/properties.yaml rename to openapi/paths/customer/_types/order/properties.yaml diff --git a/openapi/paths/customer/order/_types/refund-line-item.yaml b/openapi/paths/customer/_types/order/refund-line-item.yaml similarity index 100% rename from openapi/paths/customer/order/_types/refund-line-item.yaml rename to openapi/paths/customer/_types/order/refund-line-item.yaml diff --git a/openapi/paths/customer/order/_types/refund.yaml b/openapi/paths/customer/_types/order/refund.yaml similarity index 100% rename from openapi/paths/customer/order/_types/refund.yaml rename to openapi/paths/customer/_types/order/refund.yaml diff --git a/openapi/paths/customer/order/_types/shipping_lines.yaml b/openapi/paths/customer/_types/order/shipping_lines.yaml similarity index 100% rename from openapi/paths/customer/order/_types/shipping_lines.yaml rename to openapi/paths/customer/_types/order/shipping_lines.yaml diff --git a/openapi/paths/customer/order/_types/simple-line-item.yaml b/openapi/paths/customer/_types/order/simple-line-item.yaml similarity index 100% rename from openapi/paths/customer/order/_types/simple-line-item.yaml rename to openapi/paths/customer/_types/order/simple-line-item.yaml diff --git a/openapi/paths/customer/order/_types/user.yaml b/openapi/paths/customer/_types/order/user.yaml similarity index 100% rename from openapi/paths/customer/order/_types/user.yaml rename to openapi/paths/customer/_types/order/user.yaml diff --git a/openapi/paths/customer/booking/_types/booking.yaml b/openapi/paths/customer/booking/_types/booking.yaml new file mode 100644 index 00000000..72925f4b --- /dev/null +++ b/openapi/paths/customer/booking/_types/booking.yaml @@ -0,0 +1,30 @@ +allOf: + - $ref: "../../_types/order/base-order.yaml" + - type: object + properties: + start: + type: string + end: + type: string + title: + type: string + groupId: + type: string + shipping: + $ref: ../../../shipping/_types/shipping.yaml + user: + $ref: ../../_types/order/user.yaml + location: + $ref: ../../location/_types/base-location.yaml + line_items: + type: array + items: + $ref: ../../_types/order/line-item.yaml + required: + - user + - location + - groupId + - start + - end + - title + - line_items diff --git a/openapi/paths/customer/order/get-by-group/index.yaml b/openapi/paths/customer/booking/get-by-group/index.yaml similarity index 94% rename from openapi/paths/customer/order/get-by-group/index.yaml rename to openapi/paths/customer/booking/get-by-group/index.yaml index b3143eb8..80c28dbf 100644 --- a/openapi/paths/customer/order/get-by-group/index.yaml +++ b/openapi/paths/customer/booking/get-by-group/index.yaml @@ -19,8 +19,8 @@ get: schema: type: string tags: - - CustomerOrder - operationId: customerOrderGetByGroup + - CustomerBooking + operationId: customerBookingGetByGroup summary: GET Get order with lineItems array for specific groupId description: This endpoint gets order with lineItems array of objects specific for groupId responses: diff --git a/openapi/paths/customer/order/get-by-group/response.yaml b/openapi/paths/customer/booking/get-by-group/response.yaml similarity index 73% rename from openapi/paths/customer/order/get-by-group/response.yaml rename to openapi/paths/customer/booking/get-by-group/response.yaml index ab7234e8..0db28ccb 100644 --- a/openapi/paths/customer/order/get-by-group/response.yaml +++ b/openapi/paths/customer/booking/get-by-group/response.yaml @@ -4,7 +4,7 @@ properties: type: boolean example: true payload: - $ref: ../_types/order-with-lookup.yaml + $ref: ../_types/booking.yaml required: - success - payload diff --git a/openapi/paths/customer/order/range/index.yaml b/openapi/paths/customer/booking/range/index.yaml similarity index 94% rename from openapi/paths/customer/order/range/index.yaml rename to openapi/paths/customer/booking/range/index.yaml index fbbc2eee..d6ae4076 100644 --- a/openapi/paths/customer/order/range/index.yaml +++ b/openapi/paths/customer/booking/range/index.yaml @@ -19,8 +19,8 @@ get: schema: type: string tags: - - CustomerOrder - operationId: customerOrderRange + - CustomerBooking + operationId: customerBookingRange summary: GET Get all bookings for customer from orders description: This endpoint get all bookings from orders responses: diff --git a/openapi/paths/customer/order/range/response.yaml b/openapi/paths/customer/booking/range/response.yaml similarity index 80% rename from openapi/paths/customer/order/range/response.yaml rename to openapi/paths/customer/booking/range/response.yaml index 7f5280a3..4d8174a9 100644 --- a/openapi/paths/customer/order/range/response.yaml +++ b/openapi/paths/customer/booking/range/response.yaml @@ -6,7 +6,7 @@ properties: payload: type: array items: - $ref: "../_types/order.yaml" + $ref: ../_types/booking.yaml required: - success - payload diff --git a/openapi/paths/customer/order/_types/line-item.yaml b/openapi/paths/customer/order/_types/line-item.yaml index 9cea2254..c9386b78 100644 --- a/openapi/paths/customer/order/_types/line-item.yaml +++ b/openapi/paths/customer/order/_types/line-item.yaml @@ -1,75 +1,13 @@ -type: object -properties: - id: - type: number - admin_graphql_api_id: - type: string - fulfillable_quantity: - type: number - fulfillment_service: - type: string - fulfillment_status: - type: string - nullable: true - gift_card: - type: boolean - grams: - type: number - name: - type: string - price: - type: string - price_set: - $ref: money.yaml - product_exists: - type: boolean - product_id: - type: number - properties: - $ref: properties.yaml - quantity: - type: number - requires_shipping: - type: boolean - sku: - type: string - nullable: true - taxable: - type: boolean - title: - type: string - total_discount: - type: string - total_discount_set: - $ref: money.yaml - variant_id: - type: number - variant_inventory_management: - type: string - nullable: true - variant_title: - type: string - nullable: true - vendor: - type: string - nullable: true -required: - - id - - admin_graphql_api_id - - fulfillable_quantity - - fulfillment_service - - gift_card - - grams - - name - - price - - properties - - price_set - - product_exists - - product_id - - variant_id - - quantity - - requires_shipping - - taxable - - title - - total_discount - - total_discount_set +allOf: + - $ref: "../../_types/order/line-item.yaml" + - type: object + properties: + user: + $ref: ../../_types/order/user.yaml + location: + $ref: ../../location/_types/base-location.yaml + shipping: + $ref: ../../../shipping/_types/shipping.yaml + required: + - user + - location diff --git a/openapi/paths/customer/order/_types/order-with-lookup.yaml b/openapi/paths/customer/order/_types/order-with-lookup.yaml deleted file mode 100644 index 8c2c4ef2..00000000 --- a/openapi/paths/customer/order/_types/order-with-lookup.yaml +++ /dev/null @@ -1,11 +0,0 @@ -allOf: - - $ref: "./order.yaml" - - type: object - properties: - user: - $ref: ../_types/user.yaml - location: - $ref: ../../location/_types/base-location.yaml - required: - - user - - location diff --git a/openapi/paths/customer/order/_types/order.yaml b/openapi/paths/customer/order/_types/order.yaml index b89af6a5..9b902909 100644 --- a/openapi/paths/customer/order/_types/order.yaml +++ b/openapi/paths/customer/order/_types/order.yaml @@ -1,107 +1,10 @@ -type: object -properties: - id: - type: number - order_number: - type: number - admin_graphql_api_id: - type: string - buyer_accepts_marketing: - type: boolean - cancel_reason: - type: string - nullable: true - cancelled_at: - type: string - client_details: - $ref: client.yaml - closed_at: - type: string - nullable: true - confirmed: - type: boolean - contact_email: - type: string - nullable: true - created_at: - type: string - currency: - type: string - current_subtotal_price: - type: string - current_subtotal_price_set: - $ref: money.yaml - current_total_additional_fees_set: - $ref: money.yaml - nullable: true - current_total_discounts: - type: string - current_total_discounts_set: - $ref: money.yaml - current_total_duties_set: - $ref: money.yaml - nullable: true - current_total_price: - type: string - current_total_price_set: - $ref: money.yaml - current_total_tax: - type: string - current_total_tax_set: - $ref: money.yaml - customer: - $ref: customer.yaml - fulfillments: - type: array - items: - $ref: fulfillment.yaml - refunds: - type: array - items: - $ref: refund.yaml - shipping_address: - $ref: address.yaml - nullable: true - shipping_lines: - type: array - items: - $ref: shipping_lines.yaml - start: - type: string - end: - type: string - title: - type: string - groupId: - type: string - shipping: - $ref: ../../../shipping/_types/shipping.yaml - line_items: - type: array - items: - $ref: ../_types/line-item.yaml -required: - - id - - admin_graphql_api_id - - buyer_accepts_marketing - - confirmed - - created_at - - currency - - customer - - current_subtotal_price - - current_subtotal_price_set - - current_total_discounts - - current_total_discounts_set - - current_total_price - - current_total_price_set - - current_total_tax - - current_total_tax_set - - fulfillments - - refunds - - shipping_lines - - groupId - - start - - end - - title - - order_number - - line_items +allOf: + - $ref: "../../_types/order/base-order.yaml" + - type: object + properties: + line_items: + type: array + items: + $ref: ./line-item.yaml + required: + - line_items diff --git a/openapi/paths/customer/order/get/response.yaml b/openapi/paths/customer/order/get/response.yaml index ab7234e8..2b08a948 100644 --- a/openapi/paths/customer/order/get/response.yaml +++ b/openapi/paths/customer/order/get/response.yaml @@ -4,7 +4,7 @@ properties: type: boolean example: true payload: - $ref: ../_types/order-with-lookup.yaml + $ref: ../_types/order.yaml required: - success - payload