Skip to content

Commit

Permalink
feat(get-lineitem.ts): add support for retrieving product, location, …
Browse files Browse the repository at this point in the history
…and shipping information in the get-lineitem controller

fix(order.types.ts): make locationId required in the Properties interface

fix(get.ts): remove async keyword from ShippingServiceGet function

fix(order.ts): add locationId property to getOrderObject helper function
  • Loading branch information
jamalsoueidan committed Dec 14, 2023
1 parent de70e13 commit 53dc1c6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
33 changes: 32 additions & 1 deletion src/functions/customer/controllers/order/get-lineitem.ts
Original file line number Diff line number Diff line change
@@ -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<typeof CustomerOrderControllerGetLineItemSchema>;
Expand All @@ -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,
};
}
);
7 changes: 0 additions & 7 deletions src/functions/order/order.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
4 changes: 2 additions & 2 deletions src/functions/order/order.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ interface Properties {
customerId: number;
from: Date;
to: Date;
locationId?: string;
locationId: string;
shippingId?: string;
}

Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 1 addition & 3 deletions src/functions/shipping/services/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
1 change: 1 addition & 0 deletions src/library/jest/helpers/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const getOrderObject = ({
properties: {
...generateRandomDateRange(),
customerId,
locationId: "1",
},
quantity: 1,
requires_shipping: false,
Expand Down

0 comments on commit 53dc1c6

Please sign in to comment.