From e808ec0a8a3aa36d74d38fb57016213e04600a97 Mon Sep 17 00:00:00 2001 From: Jamal Soueidan Date: Tue, 12 Dec 2023 04:38:56 +0300 Subject: [PATCH] refactor(rates.ts): refactor ShippingControllerRates function to use Azure Functions types and log request details feat(rates.ts): return response as HttpResponseInit with jsonBody fix(rates.spec.ts): fix indentation and formatting in shippingBody object feat(rates.spec.ts): add additional item to shippingBody object for testing purposes fix(rates.ts): fix import statement for mongoose connect function fix(rates.ts): change body parameter name to rate in ShippingServiceRates function fix(rates.ts): fix variable name in ShippingServiceRates function fix(rates.ts): fix variable name in ShippingServiceRates function fix(rates.ts): fix variable name in ShippingServiceRates function fix(rates.ts): fix variable name in ShippingServiceRates function fix(rates.ts): fix variable name in ShippingServiceRates function fix(rates.ts): fix variable name in ShippingServiceRates function fix(rates.ts): fix variable name in ShippingServiceRates function fix(rates.ts): fix variable name in ShippingServiceRates function fix(rates.ts): fix variable name in ShippingServiceRates function fix(rates.ts): fix variable name in ShippingServiceRates function fix(rates.ts): fix variable name in ShippingServiceRates function fix(rates.ts): fix variable name in ShippingServiceRates --- src/functions/shipping/controllers/rates.ts | 35 +- src/functions/shipping/services/rates.spec.ts | 382 +++++++++--------- src/functions/shipping/services/rates.ts | 129 +++--- src/functions/webhook-order.function.ts | 1 - 4 files changed, 279 insertions(+), 268 deletions(-) diff --git a/src/functions/shipping/controllers/rates.ts b/src/functions/shipping/controllers/rates.ts index 2a665506..544fee79 100644 --- a/src/functions/shipping/controllers/rates.ts +++ b/src/functions/shipping/controllers/rates.ts @@ -1,17 +1,20 @@ -import { _ } from "~/library/handler"; +import { + HttpRequest, + HttpResponseInit, + InvocationContext, +} from "@azure/functions"; +import { ShippingServiceRates } from "../services/rates"; -import { ShippingBody, ShippingServiceRates } from "../services/rates"; - -export type ShippingControllerRatesRequest = { - body: ShippingBody; -}; - -export type ShippingControllerRatesResponse = Awaited< - ReturnType ->; - -export const ShippingControllerRates = _( - async ({ body }: ShippingControllerRatesRequest) => { - return ShippingServiceRates(body); - } -); +export async function ShippingControllerRates( + request: HttpRequest, + context: InvocationContext +): Promise { + context.log( + `Http function processed request for url "${request.url}" ${request.method}` + ); + const body = await request.json(); + const jsonBody = await ShippingServiceRates(body as unknown as any); + return { + jsonBody, + }; +} diff --git a/src/functions/shipping/services/rates.spec.ts b/src/functions/shipping/services/rates.spec.ts index b8c01598..062de6a1 100644 --- a/src/functions/shipping/services/rates.spec.ts +++ b/src/functions/shipping/services/rates.spec.ts @@ -34,102 +34,104 @@ describe("ShippingServiceRates", () => { }); const shippingBody = { - origin: { - country: "DK", - postal_code: "8000", - province: null, - city: "Aarhus", - name: null, - address1: "Trepkasgade 25, st", - address2: "", - address3: null, - latitude: 56.14482400000001, - longitude: 10.189414, - phone: "+4570603062", - fax: null, - email: null, - address_type: null, - company_name: "BySisters", - }, - destination: { - country: "DK", - postal_code: "8220", - province: null, - city: "Brabrand", - name: "Jam koads", - address1: "Dortesvej 21, 1 th", - address2: null, - address3: null, - latitude: 56.15948239999999, - longitude: 10.131725, - phone: null, - fax: null, - email: null, - address_type: null, - company_name: null, - }, - items: [ - { - name: "Børneklip (fra 6 år) - Artist 0", - sku: "", - quantity: 1, - grams: 0, - price: 0, - vendor: "By Sisters", - requires_shipping: true, - taxable: null, - fulfillment_service: "manual", - properties: { - _from: "2023-07-12T08:00:00.000Z", - _to: "2023-07-12T09:15:00.000Z", - _customerId: "7106990342471", - Dato: "onsdag, 12. juli ", - Tid: "10:00", - Behandler: "hana nielsen", - Varighed: "1 timer", - _shippingId: shipping._id.toString(), - }, - product_id: 8022088646930, - variant_id: 46727191036231, - }, - { - name: "Børneklip (fra 6 år) - Artist 0", - sku: "", - quantity: 1, - grams: 0, - price: 0, - vendor: "By Sisters", - requires_shipping: true, - taxable: null, - fulfillment_service: "manual", - properties: { - _from: "2023-07-12T08:00:00.000Z", - _to: "2023-07-12T09:15:00.000Z", - _customerId: "7106990342471", - Dato: "onsdag, 12. juli ", - Tid: "10:00", - Behandler: "hana nielsen", - Varighed: "1 timer", - }, - product_id: 8022088646930, - variant_id: 46727191036231, + rate: { + origin: { + country: "DK", + postal_code: "8000", + province: null, + city: "Aarhus", + name: null, + address1: "Trepkasgade 25, st", + address2: "", + address3: null, + latitude: 56.14482400000001, + longitude: 10.189414, + phone: "+4570603062", + fax: null, + email: null, + address_type: null, + company_name: "BySisters", }, - { - name: "Børneklip (fra 6 år) - Artist 0", - sku: "", - quantity: 1, - grams: 0, - price: 0, - vendor: "By Sisters", - requires_shipping: true, - taxable: null, - fulfillment_service: "manual", - product_id: 8022088646930, - variant_id: 46727191036231, + destination: { + country: "DK", + postal_code: "8220", + province: null, + city: "Brabrand", + name: "Jam koads", + address1: "Dortesvej 21, 1 th", + address2: null, + address3: null, + latitude: 56.15948239999999, + longitude: 10.131725, + phone: null, + fax: null, + email: null, + address_type: null, + company_name: null, }, - ], - currency: "DKK", - locale: "da-DK", + items: [ + { + name: "Børneklip (fra 6 år) - Artist 0", + sku: "", + quantity: 1, + grams: 0, + price: 0, + vendor: "By Sisters", + requires_shipping: true, + taxable: null, + fulfillment_service: "manual", + properties: { + _from: "2023-07-12T08:00:00.000Z", + _to: "2023-07-12T09:15:00.000Z", + _customerId: "7106990342471", + Dato: "onsdag, 12. juli ", + Tid: "10:00", + Behandler: "hana nielsen", + Varighed: "1 timer", + _shippingId: shipping._id.toString(), + }, + product_id: 8022088646930, + variant_id: 46727191036231, + }, + { + name: "Børneklip (fra 6 år) - Artist 0", + sku: "", + quantity: 1, + grams: 0, + price: 0, + vendor: "By Sisters", + requires_shipping: true, + taxable: null, + fulfillment_service: "manual", + properties: { + _from: "2023-07-12T08:00:00.000Z", + _to: "2023-07-12T09:15:00.000Z", + _customerId: "7106990342471", + Dato: "onsdag, 12. juli ", + Tid: "10:00", + Behandler: "hana nielsen", + Varighed: "1 timer", + }, + product_id: 8022088646930, + variant_id: 46727191036231, + }, + { + name: "Børneklip (fra 6 år) - Artist 0", + sku: "", + quantity: 1, + grams: 0, + price: 0, + vendor: "By Sisters", + requires_shipping: true, + taxable: null, + fulfillment_service: "manual", + product_id: 8022088646930, + variant_id: 46727191036231, + }, + ], + currency: "DKK", + locale: "da-DK", + }, }; const response = await ShippingServiceRates(shippingBody); @@ -174,103 +176,105 @@ describe("ShippingServiceRates", () => { }); const shippingBody = { - origin: { - country: "DK", - postal_code: "8000", - province: null, - city: "Aarhus", - name: null, - address1: "Trepkasgade 25, st", - address2: "", - address3: null, - latitude: 56.14482400000001, - longitude: 10.189414, - phone: "+4570603062", - fax: null, - email: null, - address_type: null, - company_name: "BySisters", - }, - destination: { - country: "DK", - postal_code: "8220", - province: null, - city: "Brabrand", - name: "Jam koads", - address1: "Dortesvej 21, 1 th", - address2: null, - address3: null, - latitude: 56.15948239999999, - longitude: 10.131725, - phone: null, - fax: null, - email: null, - address_type: null, - company_name: null, - }, - items: [ - { - name: "Børneklip (fra 6 år) - Artist 0", - sku: "", - quantity: 1, - grams: 0, - price: 0, - vendor: "By Sisters", - requires_shipping: true, - taxable: null, - fulfillment_service: "manual", - properties: { - _from: "2023-07-12T08:00:00.000Z", - _to: "2023-07-12T09:15:00.000Z", - _customerId: "7106990342471", - Dato: "onsdag, 12. juli ", - Tid: "10:00", - Behandler: "hana nielsen", - Varighed: "1 timer", - _shippingId: shipping._id.toString(), - _freeShipping: "true", - }, - product_id: 8022088646930, - variant_id: 46727191036231, - }, - { - name: "Børneklip (fra 6 år) - Artist 0", - sku: "", - quantity: 1, - grams: 0, - price: 0, - vendor: "By Sisters", - requires_shipping: true, - taxable: null, - fulfillment_service: "manual", - properties: { - _from: "2023-07-12T08:00:00.000Z", - _to: "2023-07-12T09:15:00.000Z", - _customerId: "7106990342471", - Dato: "onsdag, 12. juli ", - Tid: "10:00", - Behandler: "hana nielsen", - Varighed: "1 timer", - }, - product_id: 8022088646930, - variant_id: 46727191036231, + rate: { + origin: { + country: "DK", + postal_code: "8000", + province: null, + city: "Aarhus", + name: null, + address1: "Trepkasgade 25, st", + address2: "", + address3: null, + latitude: 56.14482400000001, + longitude: 10.189414, + phone: "+4570603062", + fax: null, + email: null, + address_type: null, + company_name: "BySisters", }, - { - name: "Børneklip (fra 6 år) - Artist 0", - sku: "", - quantity: 1, - grams: 0, - price: 0, - vendor: "By Sisters", - requires_shipping: true, - taxable: null, - fulfillment_service: "manual", - product_id: 8022088646930, - variant_id: 46727191036231, + destination: { + country: "DK", + postal_code: "8220", + province: null, + city: "Brabrand", + name: "Jam koads", + address1: "Dortesvej 21, 1 th", + address2: null, + address3: null, + latitude: 56.15948239999999, + longitude: 10.131725, + phone: null, + fax: null, + email: null, + address_type: null, + company_name: null, }, - ], - currency: "DKK", - locale: "da-DK", + items: [ + { + name: "Børneklip (fra 6 år) - Artist 0", + sku: "", + quantity: 1, + grams: 0, + price: 0, + vendor: "By Sisters", + requires_shipping: true, + taxable: null, + fulfillment_service: "manual", + properties: { + _from: "2023-07-12T08:00:00.000Z", + _to: "2023-07-12T09:15:00.000Z", + _customerId: "7106990342471", + Dato: "onsdag, 12. juli ", + Tid: "10:00", + Behandler: "hana nielsen", + Varighed: "1 timer", + _shippingId: shipping._id.toString(), + _freeShipping: "true", + }, + product_id: 8022088646930, + variant_id: 46727191036231, + }, + { + name: "Børneklip (fra 6 år) - Artist 0", + sku: "", + quantity: 1, + grams: 0, + price: 0, + vendor: "By Sisters", + requires_shipping: true, + taxable: null, + fulfillment_service: "manual", + properties: { + _from: "2023-07-12T08:00:00.000Z", + _to: "2023-07-12T09:15:00.000Z", + _customerId: "7106990342471", + Dato: "onsdag, 12. juli ", + Tid: "10:00", + Behandler: "hana nielsen", + Varighed: "1 timer", + }, + product_id: 8022088646930, + variant_id: 46727191036231, + }, + { + name: "Børneklip (fra 6 år) - Artist 0", + sku: "", + quantity: 1, + grams: 0, + price: 0, + vendor: "By Sisters", + requires_shipping: true, + taxable: null, + fulfillment_service: "manual", + product_id: 8022088646930, + variant_id: 46727191036231, + }, + ], + currency: "DKK", + locale: "da-DK", + }, }; const response = await ShippingServiceRates(shippingBody); diff --git a/src/functions/shipping/services/rates.ts b/src/functions/shipping/services/rates.ts index 0f274b5c..21e93e87 100644 --- a/src/functions/shipping/services/rates.ts +++ b/src/functions/shipping/services/rates.ts @@ -1,72 +1,77 @@ import mongoose from "mongoose"; +import { connect } from "~/library/mongoose"; import { ShippingModel } from "../shipping.model"; export type ShippingBody = { - origin: { - country: string; - postal_code: string; - province: any; - city: string; - name: any; - address1: string; - address2: string; - address3: any; - latitude: number; - longitude: number; - phone: string; - fax: any; - email: any; - address_type: any; - company_name: string; - }; - destination: { - country: string; - postal_code: string; - province: any; - city: string; - name: string; - address1: string; - address2: any; - address3: any; - latitude: number; - longitude: number; - phone: any; - fax: any; - email: any; - address_type: any; - company_name: any; - }; - items: Array<{ - name: string; - sku: string; - quantity: number; - grams: number; - price: number; - vendor: string; - requires_shipping: boolean; - taxable: any; - fulfillment_service: string; - properties?: { - _from: string; - _to: string; - _customerId: string; - Dato: string; - Tid: string; - Behandler: string; - Varighed: string; - _shippingId?: string; - _freeShipping?: string; + rate: { + origin: { + country: string; + postal_code: string; + province: any; + city: string; + name: any; + address1: string; + address2: string; + address3: any; + latitude: number; + longitude: number; + phone: string; + fax: any; + email: any; + address_type: any; + company_name: string; + }; + destination: { + country: string; + postal_code: string; + province: any; + city: string; + name: string; + address1: string; + address2: any; + address3: any; + latitude: number; + longitude: number; + phone: any; + fax: any; + email: any; + address_type: any; + company_name: any; }; - product_id: number; - variant_id: number; - }>; - currency: string; - locale: string; + items: Array<{ + name: string; + sku: string; + quantity: number; + grams: number; + price: number; + vendor: string; + requires_shipping: boolean; + taxable: any; + fulfillment_service: string; + properties?: { + _from: string; + _to: string; + _customerId: string; + Dato: string; + Tid: string; + Behandler: string; + Varighed: string; + _shippingId?: string; + _freeShipping?: string; + }; + product_id: number; + variant_id: number; + }>; + currency: string; + locale: string; + }; }; export const ShippingServiceRates = async (body: ShippingBody) => { - let shippingIds = body.items + await connect(); + + let shippingIds = body.rate.items .map((item) => item.properties?._shippingId) .filter((id) => typeof id === "string"); // Ensure only numbers @@ -75,7 +80,7 @@ export const ShippingServiceRates = async (body: ShippingBody) => { const onMinDate = new Date(Date.now() + 86400000).toISOString(); // +1 day const onMaxDate = new Date(Date.now() + 2 * 86400000).toISOString(); // +2 days - const hasFreeShipping = body.items.some( + const hasFreeShipping = body.rate.items.some( (item) => item.properties?._freeShipping === "true" ); @@ -83,7 +88,7 @@ export const ShippingServiceRates = async (body: ShippingBody) => { return { rates: [ { - service_name: `Gratis leverings`, + service_name: `Gratis levering`, service_code: "ETON", total_price: 0, currency: "DKK", diff --git a/src/functions/webhook-order.function.ts b/src/functions/webhook-order.function.ts index 9190b5f4..f48beb08 100644 --- a/src/functions/webhook-order.function.ts +++ b/src/functions/webhook-order.function.ts @@ -27,7 +27,6 @@ export async function webhookOrder( context: InvocationContext ): Promise { const body = await request.json(); - console.log(JSON.stringify(body)); context.extraOutputs.set(orderQueueOutput, body); return { body: "Created queue item." }; }