-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor schedule update logic and add schedule metafield updates
- Implement `updateScheduleLocationsField` to handle schedule location updates - Modify `updateUserMetaobject` to deduplicate locations and include schedule IDs - Introduce `updateScheduleMetafield` activity in schedule update orchestration - Add `UpdateScheduleLocationsFieldMutation` type for GraphQL operations - Remove redundant location aggregation logic from `updateScheduleMetafield`
- Loading branch information
1 parent
6305551
commit fbacd15
Showing
10 changed files
with
258 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/functions/customer/orchestrations/product/update/update-schedule-locations-field.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { ensureType } from "~/library/jest/helpers/mock"; | ||
import { createSchedule } from "~/library/jest/helpers/schedule"; | ||
import { shopifyAdmin } from "~/library/shopify"; | ||
import { | ||
UpdateScheduleLocationsFieldMutation, | ||
UpdateScheduleLocationsFieldMutationVariables, | ||
} from "~/types/admin.generated"; | ||
|
||
import { | ||
createLocation, | ||
getDumbLocationObject, | ||
} from "~/library/jest/helpers/location"; | ||
import { getProductObject } from "~/library/jest/helpers/product"; | ||
import { | ||
UPDATE_SCHEDULE_LOCATIONS_FIELD, | ||
updateScheduleLocationsField, | ||
} from "./update-schedule-locations-field"; | ||
|
||
require("~/library/jest/mongoose/mongodb.jest"); | ||
|
||
jest.mock("~/library/shopify", () => ({ | ||
shopifyAdmin: jest.fn().mockReturnValue({ | ||
request: jest.fn(), | ||
}), | ||
})); | ||
|
||
const mockRequest = shopifyAdmin().request as jest.Mock; | ||
|
||
describe("CustomerProductUpdateOrchestration", () => { | ||
beforeAll(async () => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("updateScheduleLocationsField", async () => { | ||
const location1 = await createLocation({ | ||
customerId: 123, | ||
metafieldId: "asd", | ||
}); | ||
|
||
const location2 = await createLocation({ | ||
customerId: 123, | ||
metafieldId: "2", | ||
}); | ||
|
||
const product1 = getProductObject({ | ||
locations: [ | ||
getDumbLocationObject({ | ||
location: location1._id, | ||
metafieldId: location1.metafieldId, | ||
}), | ||
getDumbLocationObject({ | ||
location: location2._id, | ||
metafieldId: location2.metafieldId, | ||
}), | ||
], | ||
}); | ||
|
||
const product2 = getProductObject({ | ||
locations: [ | ||
getDumbLocationObject({ | ||
location: location1._id, | ||
metafieldId: location1.metafieldId, | ||
}), | ||
], | ||
}); | ||
|
||
const newSchedule = await createSchedule({ | ||
metafieldId: "1", | ||
name: "ANOTHER CUSTOMER", | ||
customerId: 7, | ||
products: [product1, product2], | ||
}); | ||
|
||
mockRequest.mockResolvedValueOnce({ | ||
data: ensureType<UpdateScheduleLocationsFieldMutation>({ | ||
metaobjectUpdate: { | ||
metaobject: { | ||
fields: [ | ||
{ | ||
value: JSON.stringify([ | ||
location1.metafieldId, | ||
location2.metafieldId, | ||
]), | ||
key: "locations", | ||
}, | ||
], | ||
}, | ||
}, | ||
}), | ||
}); | ||
|
||
await updateScheduleLocationsField({ | ||
productId: product1.productId, | ||
customerId: newSchedule.customerId, | ||
}); | ||
|
||
expect(mockRequest).toHaveBeenCalledTimes(1); | ||
|
||
expect(mockRequest).toHaveBeenNthCalledWith( | ||
1, | ||
UPDATE_SCHEDULE_LOCATIONS_FIELD, | ||
{ | ||
variables: ensureType<UpdateScheduleLocationsFieldMutationVariables>({ | ||
id: newSchedule.metafieldId || "", | ||
fields: [ | ||
{ | ||
value: JSON.stringify([ | ||
location1.metafieldId, | ||
location2.metafieldId, | ||
]), | ||
key: "locations", | ||
}, | ||
], | ||
}), | ||
} | ||
); | ||
}); | ||
}); |
72 changes: 72 additions & 0 deletions
72
src/functions/customer/orchestrations/product/update/update-schedule-locations-field.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { ScheduleModel } from "~/functions/schedule"; | ||
import { shopifyAdmin } from "~/library/shopify"; | ||
|
||
export const updateScheduleLocationsFieldName = "updateScheduleLocationsField"; | ||
export const updateScheduleLocationsField = async ({ | ||
productId, | ||
customerId, | ||
}: { | ||
productId: number; | ||
customerId: number; | ||
}) => { | ||
const schedule = await ScheduleModel.findOne({ | ||
customerId, | ||
products: { | ||
$elemMatch: { | ||
productId, | ||
}, | ||
}, | ||
}); | ||
|
||
if (!schedule?.metafieldId) { | ||
throw new Error( | ||
`Failed to update schedule locations field for productId ${productId}` | ||
); | ||
} | ||
|
||
// 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 { data } = await shopifyAdmin().request( | ||
UPDATE_SCHEDULE_LOCATIONS_FIELD, | ||
{ | ||
variables: { | ||
id: schedule.metafieldId, | ||
fields: [ | ||
{ | ||
key: "locations", | ||
value: JSON.stringify(locations), | ||
}, | ||
], | ||
}, | ||
} | ||
); | ||
|
||
if (!data?.metaobjectUpdate?.metaobject) { | ||
throw new Error( | ||
`Failed to update schedule locations field for schedule ${schedule._id}` | ||
); | ||
} | ||
|
||
return data?.metaobjectUpdate.metaobject; | ||
}; | ||
|
||
export const UPDATE_SCHEDULE_LOCATIONS_FIELD = `#graphql | ||
mutation UpdateScheduleLocationsField($id: ID!, $fields: [MetaobjectFieldInput!]!) { | ||
metaobjectUpdate(id: $id, metaobject: {fields: $fields}) { | ||
metaobject { | ||
fields { | ||
value | ||
key | ||
} | ||
} | ||
} | ||
} | ||
` as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.