diff --git a/src/functions/customer/controllers/order/get-lineitem.ts b/src/functions/customer/controllers/order/get-lineitem.ts index 92ae3d96..9c30a469 100644 --- a/src/functions/customer/controllers/order/get-lineitem.ts +++ b/src/functions/customer/controllers/order/get-lineitem.ts @@ -1,7 +1,10 @@ import { z } from "zod"; +import { ShippingServiceGet } from "~/functions/shipping/services/get"; import { _ } from "~/library/handler"; import { NumberOrStringType } from "~/library/zod"; +import { CustomerLocationServiceGetOne } from "../../services/location"; import { CustomerOrderServiceGetLineItem } from "../../services/order/get-lineitem"; +import { CustomerProductServiceGet } from "../../services/product"; export type CustomerOrderControllerGetLineItemRequest = { query: z.infer; @@ -19,6 +22,34 @@ export type CustomerOrderControllerGetLineItemResponse = Awaited< export const CustomerOrderControllerGetLineItem = _( async ({ query }: CustomerOrderControllerGetLineItemRequest) => { const validateData = CustomerOrderControllerGetLineItemSchema.parse(query); - return CustomerOrderServiceGetLineItem(validateData); + + // Should we use lookup to get product,location,shippingId? + const order = await CustomerOrderServiceGetLineItem(validateData); + + const product = await CustomerProductServiceGet({ + productId: order.line_items.product_id, + customerId: order.line_items.properties?.customerId!, + }); + + const location = await CustomerLocationServiceGetOne({ + customerId: order.line_items.properties?.customerId!, + locationId: order.line_items.properties?.locationId, + }); + + const shippingId = order.line_items.properties?.shippingId; + const shipping = shippingId + ? await ShippingServiceGet({ + shippingId: shippingId, + }) + : null; + + return { + order, + product: { + selectedOptions: product.selectedOptions, + }, + location, + shipping, + }; } ); diff --git a/src/functions/order/order.schema.ts b/src/functions/order/order.schema.ts index 7146e6d6..a7ef37cb 100644 --- a/src/functions/order/order.schema.ts +++ b/src/functions/order/order.schema.ts @@ -97,13 +97,6 @@ const CustomerSchema = new Schema( } ); -const propertySchema = new Schema( - { - name: { type: String, required: true, unqiue: true, index: true }, - }, - { discriminatorKey: "kind", _id: false } -); - const LineItemSchema = new Schema( { id: { diff --git a/src/functions/order/order.types.ts b/src/functions/order/order.types.ts index 6e45a1ee..28335d0d 100644 --- a/src/functions/order/order.types.ts +++ b/src/functions/order/order.types.ts @@ -92,7 +92,7 @@ interface Properties { customerId: number; from: Date; to: Date; - locationId?: string; + locationId: string; shippingId?: string; } @@ -131,7 +131,7 @@ const LineItemZod = z.object({ price: z.string(), price_set: MoneySetZod, product_exists: z.boolean(), - product_id: z.number().nullable(), + product_id: z.number(), properties: propertiesSchema.optional(), quantity: z.number(), requires_shipping: z.boolean(), diff --git a/src/functions/shipping/services/get.ts b/src/functions/shipping/services/get.ts index 3acd3704..090ae62d 100644 --- a/src/functions/shipping/services/get.ts +++ b/src/functions/shipping/services/get.ts @@ -7,9 +7,7 @@ export type ShippingServiceGetProps = { shippingId: StringOrObjectId; }; -export const ShippingServiceGet = async ({ - shippingId, -}: ShippingServiceGetProps) => { +export const ShippingServiceGet = ({ shippingId }: ShippingServiceGetProps) => { return ShippingModel.findOne(new mongoose.Types.ObjectId(shippingId)) .orFail( new NotFoundError([ diff --git a/src/library/jest/helpers/order.ts b/src/library/jest/helpers/order.ts index 852ea068..2e9eb2e3 100644 --- a/src/library/jest/helpers/order.ts +++ b/src/library/jest/helpers/order.ts @@ -29,6 +29,7 @@ export const getOrderObject = ({ properties: { ...generateRandomDateRange(), customerId, + locationId: "1", }, quantity: 1, requires_shipping: false,