diff --git a/.vscode/launch.json b/.vscode/launch.json index 8ec5a4dd..e1ccdd6b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -14,7 +14,7 @@ "name": "Debug Jest Tests", "program": "${workspaceFolder}/node_modules/.bin/jest", "args": [ - "${workspaceFolder}/src/functions/customer/orchestrations/schedule/update/update-schedule-metafield.spec.ts" + "${workspaceFolder}/src/functions/customer/orchestrations/product/update/update-schedule-locations-field.spec.ts" ], "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", diff --git a/openapi/paths/customer/schedule/_types/product-locations.yaml b/openapi/paths/customer/schedule/_types/product-locations.yaml index 2ebf86a5..f5e5f7b3 100644 --- a/openapi/paths/customer/schedule/_types/product-locations.yaml +++ b/openapi/paths/customer/schedule/_types/product-locations.yaml @@ -2,14 +2,11 @@ type: array items: type: object properties: - metafieldId: - type: string location: type: string locationType: type: string enum: [home, commercial, destination, virtual] required: - - metafieldId - location - locationType diff --git a/src/functions/customer/orchestrations/product/update/update-price.ts b/src/functions/customer/orchestrations/product/update/update-price.ts index 5367e759..4459e116 100644 --- a/src/functions/customer/orchestrations/product/update/update-price.ts +++ b/src/functions/customer/orchestrations/product/update/update-price.ts @@ -16,7 +16,7 @@ export const updatePrice = async ({ productId, }); - const isDestination = locations.some( + const requiresShipping = locations.some( (l) => l.locationType === LocationTypes.DESTINATION ); @@ -28,13 +28,9 @@ export const updatePrice = async ({ id: `gid://shopify/ProductVariant/${variantId}`, price: price.amount, compareAtPrice: compareAtPrice.amount, - ...(isDestination - ? { - inventoryItem: { - requiresShipping: true, - }, - } - : {}), + inventoryItem: { + requiresShipping, + }, }, ], }, diff --git a/src/functions/customer/orchestrations/product/update/update-product.spec.ts b/src/functions/customer/orchestrations/product/update/update-product.spec.ts index 83ac125c..4cd1832f 100644 --- a/src/functions/customer/orchestrations/product/update/update-product.spec.ts +++ b/src/functions/customer/orchestrations/product/update/update-product.spec.ts @@ -257,7 +257,7 @@ describe("CustomerProductUpdateOrchestration", () => { }, { id: product?.locationsMetafieldId, - value: JSON.stringify(product.locations.map((p) => p.metafieldId)), + value: JSON.stringify([location.metafieldId]), }, { id: product?.user?.metaobjectId, diff --git a/src/functions/customer/orchestrations/product/update/update-product.ts b/src/functions/customer/orchestrations/product/update/update-product.ts index 2be55796..bee275c5 100644 --- a/src/functions/customer/orchestrations/product/update/update-product.ts +++ b/src/functions/customer/orchestrations/product/update/update-product.ts @@ -166,7 +166,7 @@ export const updateProduct = async ({ }, { id: product?.locationsMetafieldId, - value: JSON.stringify(product.locations.map((p) => p.metafieldId)), + value: JSON.stringify(locations.map((p) => p.metafieldId)), }, { id: product?.user?.metaobjectId, diff --git a/src/functions/customer/orchestrations/product/update/update-schedule-locations-field.ts b/src/functions/customer/orchestrations/product/update/update-schedule-locations-field.ts index 9e1cca4e..f33a17e2 100644 --- a/src/functions/customer/orchestrations/product/update/update-schedule-locations-field.ts +++ b/src/functions/customer/orchestrations/product/update/update-schedule-locations-field.ts @@ -1,4 +1,6 @@ +import { LocationModel } from "~/functions/location"; import { ScheduleModel } from "~/functions/schedule"; +import { NotFoundError } from "~/library/handler"; import { shopifyAdmin } from "~/library/shopify"; export const updateScheduleLocationsFieldName = "updateScheduleLocationsField"; @@ -25,14 +27,23 @@ export const updateScheduleLocationsField = async ({ } // save unique locations for this schedule metafield that are found in the current schedule model. - const locations = schedule.products.reduce((locations, product) => { - product.locations.forEach((location) => { - if (location.metafieldId && !locations.includes(location.metafieldId)) { - locations.push(location.metafieldId); - } - }); - return locations; - }, [] as string[]); + const locationIds = schedule.products.flatMap((product) => + product.locations.map((location) => location.location.toString()) + ); + + console.log(locationIds); + + const locations = await LocationModel.find({ + _id: { $in: locationIds }, + }).orFail( + new NotFoundError([ + { + path: ["customerId", "locations"], + message: "LOCATIONS_ERROR", + code: "custom", + }, + ]) + ); const { data } = await shopifyAdmin().request( UPDATE_SCHEDULE_LOCATIONS_FIELD, @@ -42,7 +53,9 @@ export const updateScheduleLocationsField = async ({ fields: [ { key: "locations", - value: JSON.stringify(locations), + value: JSON.stringify( + locations.map((location) => location.metafieldId) + ), }, ], }, diff --git a/src/functions/schedule/schedule.types.ts b/src/functions/schedule/schedule.types.ts index 3b20eea4..e42978db 100644 --- a/src/functions/schedule/schedule.types.ts +++ b/src/functions/schedule/schedule.types.ts @@ -50,7 +50,6 @@ const NoticePeriodZodSchema = z.object({ export type ScheduleProductNoticePeriod = z.infer; const LocationZodSchema = z.object({ - metafieldId: z.string(), location: StringOrObjectId, locationType: z.nativeEnum(LocationTypes), }); diff --git a/src/functions/schedule/schemas/product.schema.ts b/src/functions/schedule/schemas/product.schema.ts index 37a42303..01fd2188 100644 --- a/src/functions/schedule/schemas/product.schema.ts +++ b/src/functions/schedule/schemas/product.schema.ts @@ -127,7 +127,6 @@ export const ProductSchema = new mongoose.Schema( locations: { type: [ { - metafieldId: String, location: { type: mongoose.Schema.Types.ObjectId, ref: "Location", diff --git a/src/library/shopify/rest.ts b/src/library/shopify/rest.ts index 42e33194..2bbabfc2 100644 --- a/src/library/shopify/rest.ts +++ b/src/library/shopify/rest.ts @@ -18,6 +18,6 @@ export const shopifyRest = () => { return createAdminRestApiClient({ storeDomain: process.env["ShopifyStoreDomain"] || "", accessToken: process.env[randomKey] || "", - apiVersion: "2024-01", + apiVersion: "2024-04", }); };