Skip to content

Commit

Permalink
Merge pull request #162 from jamalsoueidan/correct-shopify-version
Browse files Browse the repository at this point in the history
 Refactor product location updates and remove metafieldId dependency
  • Loading branch information
jamalsoueidan authored Jun 7, 2024
2 parents 2fa626c + f3025ea commit 6cffd24
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 0 additions & 3 deletions openapi/paths/customer/schedule/_types/product-locations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const updatePrice = async ({
productId,
});

const isDestination = locations.some(
const requiresShipping = locations.some(
(l) => l.locationType === LocationTypes.DESTINATION
);

Expand All @@ -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,
},
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand All @@ -42,7 +53,9 @@ export const updateScheduleLocationsField = async ({
fields: [
{
key: "locations",
value: JSON.stringify(locations),
value: JSON.stringify(
locations.map((location) => location.metafieldId)
),
},
],
},
Expand Down
1 change: 0 additions & 1 deletion src/functions/schedule/schedule.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const NoticePeriodZodSchema = z.object({
export type ScheduleProductNoticePeriod = z.infer<typeof NoticePeriodZodSchema>;

const LocationZodSchema = z.object({
metafieldId: z.string(),
location: StringOrObjectId,
locationType: z.nativeEnum(LocationTypes),
});
Expand Down
1 change: 0 additions & 1 deletion src/functions/schedule/schemas/product.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ export const ProductSchema = new mongoose.Schema<ScheduleProduct>(
locations: {
type: [
{
metafieldId: String,
location: {
type: mongoose.Schema.Types.ObjectId,
ref: "Location",
Expand Down
2 changes: 1 addition & 1 deletion src/library/shopify/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export const shopifyRest = () => {
return createAdminRestApiClient({
storeDomain: process.env["ShopifyStoreDomain"] || "",
accessToken: process.env[randomKey] || "",
apiVersion: "2024-01",
apiVersion: "2024-04",
});
};

0 comments on commit 6cffd24

Please sign in to comment.