-
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 #156 from jamalsoueidan/destroy-orchestrations
Refactor product orchestration and update mocks for product add and …
- Loading branch information
Showing
17 changed files
with
214 additions
and
69 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
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
8 changes: 5 additions & 3 deletions
8
src/functions/customer/orchestrations/customer/update/update-article.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
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,57 @@ | ||
import { InvocationContext } from "@azure/functions"; | ||
import * as df from "durable-functions"; | ||
import { OrchestrationContext } from "durable-functions"; | ||
import { activityType } from "~/library/orchestration"; | ||
import { | ||
updateArticle, | ||
updateArticleName, | ||
} from "../customer/update/update-article"; | ||
import { updatePrice, updatePriceName } from "./update/update-price"; | ||
import { updateProduct, updateProductName } from "./update/update-product"; | ||
|
||
const orchestrator: df.OrchestrationHandler = function* ( | ||
context: OrchestrationContext | ||
) { | ||
const input = context.df.getInput() as Input; | ||
|
||
const productUpdated: Awaited<ReturnType<typeof updateProduct>> = | ||
yield context.df.callActivity( | ||
updateProductName, | ||
activityType<typeof updateProduct>(input) | ||
); | ||
|
||
const priceUpdated: Awaited<ReturnType<typeof updatePrice>> = | ||
yield context.df.callActivity( | ||
updatePriceName, | ||
activityType<typeof updatePrice>(input) | ||
); | ||
|
||
const article: Awaited<ReturnType<typeof updateArticle>> = | ||
yield context.df.callActivity( | ||
updateArticleName, | ||
activityType<typeof updateArticle>(input) | ||
); | ||
|
||
return { article, productUpdated, priceUpdated }; | ||
}; | ||
|
||
df.app.orchestration("CustomerProductAddOrchestration", orchestrator); | ||
|
||
type Input = { productId: number; customerId: number }; | ||
|
||
export const CustomerProductAddOrchestration = async ( | ||
input: Input, | ||
context: InvocationContext | ||
): Promise<string> => { | ||
const client = df.getClient(context); | ||
const instanceId: string = await client.startNew( | ||
"CustomerProductAddOrchestration", | ||
{ | ||
input, | ||
} | ||
); | ||
|
||
context.log(`Started orchestration with ID = '${instanceId}'.`); | ||
|
||
return instanceId; | ||
}; |
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,58 @@ | ||
import { InvocationContext } from "@azure/functions"; | ||
import * as df from "durable-functions"; | ||
import { OrchestrationContext } from "durable-functions"; | ||
import { ScheduleProduct } from "~/functions/schedule"; | ||
import { activityType } from "~/library/orchestration"; | ||
import { | ||
destroyProductOption, | ||
destroyProductOptionName, | ||
} from "../product-options/destroy/destroy-option"; | ||
import { destroyProduct, destroyProductName } from "./destroy/destroy-product"; | ||
|
||
df.app.activity(destroyProductName, { handler: destroyProduct }); | ||
|
||
const orchestrator: df.OrchestrationHandler = function* ( | ||
context: OrchestrationContext | ||
) { | ||
const input = context.df.getInput() as Input; | ||
|
||
for (const productOption of input.product.options || []) { | ||
yield context.df.callActivity( | ||
destroyProductOptionName, | ||
activityType<typeof destroyProductOption>({ | ||
productOptionId: productOption.productId, | ||
}) | ||
); | ||
} | ||
|
||
const productDestroyed: Awaited<ReturnType<typeof destroyProduct>> = | ||
yield context.df.callActivity( | ||
destroyProductName, | ||
activityType<typeof destroyProduct>(input.product) | ||
); | ||
|
||
return { productDestroyed }; | ||
}; | ||
|
||
df.app.orchestration("CustomerProductDestroyOrchestration", orchestrator); | ||
|
||
type Input = { | ||
product: ScheduleProduct; | ||
}; | ||
|
||
export const CustomerProductDestroyOrchestration = async ( | ||
input: Input, | ||
context: InvocationContext | ||
): Promise<string> => { | ||
const client = df.getClient(context); | ||
const instanceId: string = await client.startNew( | ||
"CustomerProductDestroyOrchestration", | ||
{ | ||
input, | ||
} | ||
); | ||
|
||
context.log(`Started orchestration with ID = '${instanceId}'.`); | ||
|
||
return instanceId; | ||
}; |
48 changes: 48 additions & 0 deletions
48
src/functions/customer/orchestrations/product/destroy/destroy-product.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,48 @@ | ||
import { shopifyAdmin } from "~/library/shopify"; | ||
import { ProductOptionDestroyMutation } from "~/types/admin.generated"; | ||
import { destroyProduct, PRODUCT_DESTROY } from "./destroy-product"; | ||
|
||
require("~/library/jest/mongoose/mongodb.jest"); | ||
|
||
jest.mock("~/library/shopify", () => ({ | ||
shopifyAdmin: jest.fn().mockReturnValue({ | ||
request: jest.fn(), | ||
}), | ||
})); | ||
|
||
const mockRequest = shopifyAdmin().request as jest.Mock; | ||
|
||
const productDelete: ProductOptionDestroyMutation = { | ||
productDelete: { | ||
deletedProductId: `gid://shopify/Product/123`, | ||
}, | ||
}; | ||
|
||
describe("CustomerProductDestroyOrchestration", () => { | ||
beforeAll(async () => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("destroyProduct", async () => { | ||
mockRequest.mockResolvedValueOnce({ | ||
data: { | ||
productDelete: { | ||
deletedProductId: { | ||
id: "123", | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
await destroyProduct({ | ||
productId: 123, | ||
}); | ||
|
||
expect(mockRequest).toHaveBeenCalledTimes(1); | ||
expect(mockRequest).toHaveBeenNthCalledWith(1, PRODUCT_DESTROY, { | ||
variables: { | ||
productId: `gid://shopify/Product/123`, | ||
}, | ||
}); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
src/functions/customer/orchestrations/product/destroy/destroy-product.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,20 @@ | ||
import { shopifyAdmin } from "~/library/shopify"; | ||
|
||
export const destroyProductName = "destroyProductName"; | ||
export const destroyProduct = async ({ productId }: { productId: number }) => { | ||
const { data } = await shopifyAdmin().request(PRODUCT_DESTROY, { | ||
variables: { | ||
productId: `gid://shopify/Product/${productId}`, | ||
}, | ||
}); | ||
|
||
return data?.productDelete?.deletedProductId; | ||
}; | ||
|
||
export const PRODUCT_DESTROY = `#graphql | ||
mutation productDestroy($productId: ID!) { | ||
productDelete(input: {id: $productId}) { | ||
deletedProductId | ||
} | ||
} | ||
` 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.