From 136cad027313508a63187b58ca1371caac7f64fb Mon Sep 17 00:00:00 2001 From: Jamal Soueidan Date: Sat, 9 Dec 2023 22:50:26 +0300 Subject: [PATCH] feat(customer-order): add customer order function and controllers - Add a new file `customer-order.function.ts` to handle customer order related requests - Implement `CustomerOrderControllerGet` function in `customer/controllers/order/get.ts` to handle GET requests for retrieving customer order details - Implement `CustomerOrderControllerList` function in `customer/controllers/order/list.ts` to handle GET requests for listing customer orders - Define request and response types for `CustomerOrderControllerGet` and `CustomerOrderControllerList` - Update `order.schema.ts` to remove unused schemas and properties - Update `order.types.ts` to remove unused types and properties --- src/functions/customer-order.function.ts | 20 ++++++ .../customer/controllers/order/get.ts | 24 +++++++ .../customer/controllers/order/list.ts | 26 ++++++++ src/functions/order/order.schema.ts | 58 ---------------- src/functions/order/order.types.ts | 66 +------------------ 5 files changed, 71 insertions(+), 123 deletions(-) create mode 100644 src/functions/customer-order.function.ts create mode 100644 src/functions/customer/controllers/order/get.ts create mode 100644 src/functions/customer/controllers/order/list.ts diff --git a/src/functions/customer-order.function.ts b/src/functions/customer-order.function.ts new file mode 100644 index 00000000..daff458f --- /dev/null +++ b/src/functions/customer-order.function.ts @@ -0,0 +1,20 @@ +import "module-alias/register"; + +import { app } from "@azure/functions"; + +import { CustomerOrderControllerGet } from "./customer/controllers/order/get"; +import { CustomerOrderControllerList } from "./customer/controllers/order/list"; + +app.http("customerOrderGet", { + methods: ["GET"], + authLevel: "anonymous", + route: "customer/{customerId?}/lineItemId/{lineItemId?}", + handler: CustomerOrderControllerGet, +}); + +app.http("customerOrderList", { + methods: ["GET"], + authLevel: "anonymous", + route: "customer/{customerId?}/orders/{year?}/{month?}", + handler: CustomerOrderControllerList, +}); diff --git a/src/functions/customer/controllers/order/get.ts b/src/functions/customer/controllers/order/get.ts new file mode 100644 index 00000000..1fd8dd00 --- /dev/null +++ b/src/functions/customer/controllers/order/get.ts @@ -0,0 +1,24 @@ +import { z } from "zod"; +import { _ } from "~/library/handler"; +import { NumberOrStringType } from "~/library/zod"; +import { CustomerOrderServiceGet } from "../../services/order/get"; + +export type CustomerOrderControllerGetRequest = { + query: z.infer; +}; + +export const CustomerOrderControllerGetSchema = z.object({ + customerId: NumberOrStringType, + lineItemId: NumberOrStringType, +}); + +export type CustomerOrderControllerGetResponse = Awaited< + ReturnType +>; + +export const CustomerOrderControllerGet = _( + async ({ query }: CustomerOrderControllerGetRequest) => { + const validateData = CustomerOrderControllerGetSchema.parse(query); + return CustomerOrderServiceGet(validateData); + } +); diff --git a/src/functions/customer/controllers/order/list.ts b/src/functions/customer/controllers/order/list.ts new file mode 100644 index 00000000..07bdef7b --- /dev/null +++ b/src/functions/customer/controllers/order/list.ts @@ -0,0 +1,26 @@ +import { _ } from "~/library/handler"; + +import { z } from "zod"; +import { NumberOrStringType } from "~/library/zod"; +import { CustomerOrderServiceList } from "../../services/order/list"; + +export type CustomerOrderControllerListRequest = { + query: z.infer; +}; + +export const CustomerOrderControllerListSchema = z.object({ + customerId: NumberOrStringType, + year: NumberOrStringType, + month: NumberOrStringType, +}); + +export type CustomerOrderControllerListResponse = Awaited< + ReturnType +>; + +export const CustomerOrderControllerList = _( + async ({ query }: CustomerOrderControllerListRequest) => { + const validateData = CustomerOrderControllerListSchema.parse(query); + return CustomerOrderServiceList(validateData); + } +); diff --git a/src/functions/order/order.schema.ts b/src/functions/order/order.schema.ts index 73c13688..ca950755 100644 --- a/src/functions/order/order.schema.ts +++ b/src/functions/order/order.schema.ts @@ -24,32 +24,6 @@ const PriceSetSchema = new Schema( } ); -const TaxLineSchema = new Schema( - { - channel_liable: Boolean, - price: String, - price_set: PriceSetSchema, - rate: Number, - title: String, - }, - { - autoIndex: false, - _id: false, - } -); - -const DiscountAllocationSchema = new Schema( - { - amount: String, - amount_set: PriceSetSchema, - discount_application_index: Number, - }, - { - autoIndex: false, - _id: false, - } -); - const DiscountCodeSchema = new Schema( { code: String, @@ -158,13 +132,6 @@ const LineItemSchema = new Schema( variant_inventory_management: String, variant_title: String, vendor: String, - tax_lines: [TaxLineSchema], - duties: [ - { - /* Schema definition if available */ - }, - ], - discount_allocations: [DiscountAllocationSchema], }, { autoIndex: false, @@ -211,8 +178,6 @@ const FulfillmentSchema = new Schema( location_id: Number, name: String, order_id: Number, - origin_address: {}, - receipt: {}, service: String, shipment_status: String, status: String, @@ -282,10 +247,7 @@ const RefundSchema = new Schema( restock: Boolean, total_duties_set: PriceSetSchema, user_id: Number, - order_adjustments: [{}], - transactions: [{}], refund_line_items: [RefundLineItemSchema], - duties: [{}], }, { autoIndex: false, @@ -310,8 +272,6 @@ const ShippingLineSchema = new Schema( requested_fulfillment_service_id: String, source: String, title: String, - tax_lines: [{}], - discount_allocations: [{}], }, { autoIndex: false, @@ -327,16 +287,10 @@ export const OrdreMongooseSchema = new Schema({ index: true, }, admin_graphql_api_id: String, - app_id: Number, - browser_ip: String, buyer_accepts_marketing: Boolean, cancel_reason: String, cancelled_at: Date, - cart_token: String, - checkout_id: Number, - checkout_token: String, closed_at: Date, - confirmation_number: String, confirmed: Boolean, contact_email: String, created_at: Date, @@ -352,34 +306,23 @@ export const OrdreMongooseSchema = new Schema({ current_total_tax: String, current_total_tax_set: PriceSetSchema, customer_locale: String, - device_id: Number, discount_codes: [DiscountCodeSchema], email: String, estimated_taxes: Boolean, financial_status: String, fulfillment_status: String, - landing_site: String, - landing_site_ref: String, location_id: Number, - merchant_of_record_app_id: Number, name: String, note: String, note_attributes: [{}], number: Number, order_number: Number, order_status_url: String, - original_total_additional_fees_set: PriceSetSchema, - original_total_duties_set: PriceSetSchema, - payment_gateway_names: [String], phone: String, po_number: String, presentment_currency: String, processed_at: Date, - reference: String, - referring_site: String, source_identifier: String, - source_name: String, - source_url: String, subtotal_price: String, subtotal_price_set: PriceSetSchema, tags: String, @@ -387,7 +330,6 @@ export const OrdreMongooseSchema = new Schema({ tax_lines: [{}], taxes_included: Boolean, test: Boolean, - token: String, total_discounts: String, total_discounts_set: PriceSetSchema, total_line_items_price: String, diff --git a/src/functions/order/order.types.ts b/src/functions/order/order.types.ts index 447d0ac2..c32ed179 100644 --- a/src/functions/order/order.types.ts +++ b/src/functions/order/order.types.ts @@ -83,7 +83,7 @@ const CustomerZod = z.object({ const propertySchema = z.object({ name: z.string(), value: z.union([z.string(), z.number(), z.coerce.date()]), // Allow string, number, or date - kind: z.string().optional(), + kind: z.string(), }); const propertiesSchema = z @@ -145,21 +145,6 @@ const LineItemZod = z.object({ variant_inventory_management: z.string().nullable(), variant_title: z.string().nullable(), vendor: z.string().nullable(), - tax_lines: z.array( - z.object({ - /* ... */ - }) - ), - duties: z.array( - z.object({ - /* ... */ - }) - ), - discount_allocations: z.array( - z.object({ - /* ... */ - }) - ), }); export type OrderLineItem = z.infer; @@ -171,12 +156,6 @@ const FulfillmentZod = z.object({ location_id: z.number(), name: z.string(), order_id: z.number(), - origin_address: z.object({ - /* ... */ - }), // Define further if structure is known - receipt: z.object({ - /* ... */ - }), // Define further if structure is known service: z.string(), shipment_status: z.string().nullable(), status: z.string(), @@ -229,16 +208,6 @@ const RefundZod = z.object({ restock: z.boolean(), total_duties_set: MoneySetZod.nullable(), user_id: z.number(), - order_adjustments: z.array( - z.object({ - /* ... */ - }) - ), // Define further if structure is known - transactions: z.array( - z.object({ - /* ... */ - }) - ), // Define further if structure is known refund_line_items: z .array(RefundLineItemZod) .transform((items) => @@ -255,11 +224,6 @@ const RefundZod = z.object({ }, })) ), - duties: z.array( - z.object({ - /* ... */ - }) - ), // Define further if structure is known }); export type OrderRefund = z.infer; @@ -276,32 +240,16 @@ const ShippingLineZod = z.object({ requested_fulfillment_service_id: z.string().nullable(), source: z.string(), title: z.string(), - tax_lines: z.array( - z.object({ - /* ... */ - }) - ), // Define further if structure is known - discount_allocations: z.array( - z.object({ - /* ... */ - }) - ), // Define further if structure is known }); export const Order = z.object({ id: z.number(), admin_graphql_api_id: z.string(), - app_id: z.number().nullable(), - browser_ip: z.string(), buyer_accepts_marketing: z.boolean(), cancel_reason: z.string().nullable(), cancelled_at: z.string().nullable(), - cart_token: z.string(), - checkout_id: z.number().nullable(), - checkout_token: z.string().nullable(), client_details: ClientDetailsZod, closed_at: z.string().nullable(), - confirmation_number: z.string().nullable(), confirmed: z.boolean(), contact_email: z.string().nullable(), created_at: z.string(), @@ -317,34 +265,23 @@ export const Order = z.object({ current_total_tax: z.string(), current_total_tax_set: MoneySetZod, customer_locale: z.string().nullable(), - device_id: z.number().nullable(), discount_codes: z.array(DiscountCodeZod), email: z.string(), estimated_taxes: z.boolean(), financial_status: z.string(), fulfillment_status: z.string().nullable(), - landing_site: z.string(), - landing_site_ref: z.string().nullable(), location_id: z.number().nullable(), - merchant_of_record_app_id: z.number().nullable(), name: z.string(), note: z.string().nullable(), note_attributes: z.array(z.object({ name: z.string(), value: z.string() })), number: z.number(), order_number: z.number(), order_status_url: z.string(), - original_total_additional_fees_set: MoneySetZod.nullable(), - original_total_duties_set: MoneySetZod.nullable(), - payment_gateway_names: z.array(z.string()), phone: z.string().nullable(), po_number: z.string().nullable(), presentment_currency: z.string(), processed_at: z.string(), - reference: z.string().nullable(), - referring_site: z.string().nullable(), source_identifier: z.string().nullable(), - source_name: z.string(), - source_url: z.string().nullable(), subtotal_price: z.string(), subtotal_price_set: MoneySetZod, tags: z.string(), @@ -356,7 +293,6 @@ export const Order = z.object({ ), taxes_included: z.boolean(), test: z.boolean(), - token: z.string(), total_discounts: z.string(), total_discounts_set: MoneySetZod, total_line_items_price: z.string(),