-
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.
Merge pull request #160 from jamalsoueidan/add-geo-location-metaobject
Add durable client input and orchestration for customer location upd…
- Loading branch information
Showing
10 changed files
with
363 additions
and
158 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { InvocationContext } from "@azure/functions"; | ||
import * as df from "durable-functions"; | ||
import { OrchestrationContext } from "durable-functions"; | ||
import { activityType } from "~/library/orchestration"; | ||
import { StringOrObjectIdType } from "~/library/zod"; | ||
import { | ||
updateLocationMetafield, | ||
updateLocationMetafieldName, | ||
} from "./update/update-location-metafield"; | ||
|
||
df.app.activity(updateLocationMetafieldName, { | ||
handler: updateLocationMetafield, | ||
}); | ||
|
||
const orchestrator: df.OrchestrationHandler = function* ( | ||
context: OrchestrationContext | ||
) { | ||
const input = context.df.getInput() as Input; | ||
|
||
const metafield: Awaited<ReturnType<typeof updateLocationMetafield>> = | ||
yield context.df.callActivity( | ||
updateLocationMetafieldName, | ||
activityType<typeof updateLocationMetafield>(input) | ||
); | ||
|
||
return { metafield }; | ||
}; | ||
|
||
df.app.orchestration("CustomerLocationUpdateOrchestration", orchestrator); | ||
|
||
type Input = { | ||
locationId: StringOrObjectIdType; | ||
}; | ||
|
||
export const CustomerLocationUpdateOrchestration = async ( | ||
input: Input, | ||
context: InvocationContext | ||
): Promise<string> => { | ||
const client = df.getClient(context); | ||
const instanceId: string = await client.startNew( | ||
"CustomerLocationUpdateOrchestration", | ||
{ | ||
input, | ||
} | ||
); | ||
|
||
context.log(`Started orchestration with ID = '${instanceId}'.`); | ||
|
||
return instanceId; | ||
}; |
176 changes: 176 additions & 0 deletions
176
src/functions/customer/orchestrations/location/update/update-location-metafield.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,176 @@ | ||
import { LocationOriginTypes, LocationTypes } from "~/functions/location"; | ||
import { createLocation } from "~/library/jest/helpers/location"; | ||
import { ensureType } from "~/library/jest/helpers/mock"; | ||
import { shopifyAdmin } from "~/library/shopify"; | ||
import { | ||
UpdateLocationMetaobjectMutation, | ||
UpdateLocationMetaobjectMutationVariables, | ||
} from "~/types/admin.generated"; | ||
import { | ||
UPDATE_LOCATION_METAOBJECT, | ||
updateLocationMetafield, | ||
} from "./update-location-metafield"; | ||
|
||
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("CustomerLocationUpdateOrchestration", () => { | ||
beforeAll(async () => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("updateLocationMetafield", async () => { | ||
const coordinates = { | ||
longitude: 10.12961271, | ||
latitude: 56.15563438, | ||
fullAddress: "Sigridsvej 45, 1. th, 8220 Brabrand", | ||
city: "Aarhus", | ||
country: "Denmark", | ||
}; | ||
|
||
const location = await createLocation({ | ||
name: "Falafel", | ||
fullAddress: "Sigridsvej 45, 1. th, 8220 Brabrand", | ||
originType: LocationOriginTypes.COMMERCIAL, | ||
locationType: LocationTypes.ORIGIN, | ||
customerId: 12, | ||
distanceHourlyRate: 1, | ||
fixedRatePerKm: 10, | ||
distanceForFree: 10, | ||
}); | ||
|
||
mockRequest.mockResolvedValueOnce({ | ||
data: ensureType<UpdateLocationMetaobjectMutation>({ | ||
metaobjectUpdate: { | ||
metaobject: { | ||
fields: [ | ||
{ | ||
value: location.locationType, | ||
key: "location_type", | ||
}, | ||
{ | ||
value: location.name, | ||
key: "name", | ||
}, | ||
{ | ||
value: coordinates.fullAddress, | ||
key: "full_address", | ||
}, | ||
{ | ||
value: coordinates.city, | ||
key: "city", | ||
}, | ||
{ | ||
value: coordinates.country, | ||
key: "country", | ||
}, | ||
{ | ||
value: location.originType, | ||
key: "origin_type", | ||
}, | ||
{ | ||
value: location.distanceForFree.toString(), | ||
key: "distance_for_free", | ||
}, | ||
{ | ||
value: location.distanceHourlyRate.toString(), | ||
key: "distance_hourly_rate", | ||
}, | ||
{ | ||
value: location.fixedRatePerKm.toString(), | ||
key: "fixed_rate_per_km", | ||
}, | ||
{ | ||
value: location.minDriveDistance.toString(), | ||
key: "min_drive_distance", | ||
}, | ||
{ | ||
value: location.maxDriveDistance.toString(), | ||
key: "max_drive_distance", | ||
}, | ||
{ | ||
value: location.startFee.toString(), | ||
key: "start_fee", | ||
}, | ||
{ | ||
key: "geo_location", | ||
value: JSON.stringify(location.geoLocation), | ||
}, | ||
], | ||
}, | ||
}, | ||
}), | ||
}); | ||
|
||
await updateLocationMetafield({ locationId: location._id }); | ||
|
||
expect(mockRequest).toHaveBeenCalledTimes(1); | ||
|
||
expect(mockRequest).toHaveBeenNthCalledWith(1, UPDATE_LOCATION_METAOBJECT, { | ||
variables: ensureType<UpdateLocationMetaobjectMutationVariables>({ | ||
id: location.metafieldId || "", | ||
fields: [ | ||
{ | ||
value: location.locationType, | ||
key: "location_type", | ||
}, | ||
{ | ||
value: location.name, | ||
key: "name", | ||
}, | ||
{ | ||
value: location.fullAddress, | ||
key: "full_address", | ||
}, | ||
{ | ||
value: location.city, | ||
key: "city", | ||
}, | ||
{ | ||
value: location.country, | ||
key: "country", | ||
}, | ||
{ | ||
value: location.originType, | ||
key: "origin_type", | ||
}, | ||
{ | ||
value: location.distanceForFree.toString(), | ||
key: "distance_for_free", | ||
}, | ||
{ | ||
value: location.distanceHourlyRate.toString(), | ||
key: "distance_hourly_rate", | ||
}, | ||
{ | ||
value: location.fixedRatePerKm.toString(), | ||
key: "fixed_rate_per_km", | ||
}, | ||
{ | ||
value: location.minDriveDistance.toString(), | ||
key: "min_drive_distance", | ||
}, | ||
{ | ||
value: location.maxDriveDistance.toString(), | ||
key: "max_drive_distance", | ||
}, | ||
{ | ||
value: location.startFee.toString(), | ||
key: "start_fee", | ||
}, | ||
{ | ||
key: "geo_location", | ||
value: JSON.stringify(location.geoLocation), | ||
}, | ||
], | ||
}), | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.