-
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 #168 from jamalsoueidan/webhook-product-update
Webhook product update
- Loading branch information
Showing
16 changed files
with
186 additions
and
19 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,53 @@ | ||
import { InvocationContext } from "@azure/functions"; | ||
import * as df from "durable-functions"; | ||
import { OrchestrationContext } from "durable-functions"; | ||
import { | ||
updateArticle, | ||
updateArticleName, | ||
} from "~/functions/customer/orchestrations/customer/update/update-article"; | ||
import { | ||
updateProduct, | ||
updateProductName, | ||
} from "~/functions/customer/orchestrations/product/update/update-product"; | ||
import { activityType } from "~/library/orchestration"; | ||
|
||
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 article: Awaited<ReturnType<typeof updateArticle>> = | ||
yield context.df.callActivity( | ||
updateArticleName, | ||
activityType<typeof updateArticle>(input) | ||
); | ||
|
||
return { productUpdated, article }; | ||
}; | ||
|
||
df.app.orchestration("WebhookUpdateProductOrchestration", orchestrator); | ||
|
||
type Input = { customerId: number; productId: number }; | ||
|
||
export const WebhookUpdateProductOrchestration = async ( | ||
input: Input, | ||
context: InvocationContext | ||
): Promise<string> => { | ||
const client = df.getClient(context); | ||
const instanceId: string = await client.startNew( | ||
"WebhookUpdateProductOrchestration", | ||
{ | ||
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,98 @@ | ||
import "module-alias/register"; | ||
|
||
import { app, HttpRequest, InvocationContext } from "@azure/functions"; | ||
import * as df from "durable-functions"; | ||
import { connect } from "~/library/mongoose"; | ||
import { WebhookUpdateProductOrchestration } from "./webhook/product/update"; | ||
|
||
type Product = { | ||
admin_graphql_api_id: string; | ||
body_html: string; | ||
created_at: string; | ||
handle: string; | ||
id: number; | ||
product_type: string; | ||
published_at: string; | ||
template_suffix: any; | ||
title: string; | ||
updated_at: string; | ||
vendor: string; | ||
status: string; | ||
published_scope: string; | ||
tags: string; | ||
variants: Array<{ | ||
admin_graphql_api_id: string; | ||
barcode: string; | ||
compare_at_price: string; | ||
created_at: string; | ||
fulfillment_service: string; | ||
id: number; | ||
inventory_management: any; | ||
inventory_policy: string; | ||
position: number; | ||
price: string; | ||
product_id: number; | ||
sku: string; | ||
taxable: boolean; | ||
title: string; | ||
updated_at: string; | ||
option1: string; | ||
option2: any; | ||
option3: any; | ||
grams: number; | ||
image_id: any; | ||
weight: number; | ||
weight_unit: string; | ||
inventory_item_id: number; | ||
inventory_quantity: number; | ||
old_inventory_quantity: number; | ||
requires_shipping: boolean; | ||
}>; | ||
options: Array<{ | ||
name: string; | ||
id: number; | ||
product_id: number; | ||
position: number; | ||
values: Array<string>; | ||
}>; | ||
images: Array<any>; | ||
image: any; | ||
variant_gids: Array<{ | ||
admin_graphql_api_id: string; | ||
updated_at: string; | ||
}>; | ||
}; | ||
|
||
app.http("webhookProductUpdate", { | ||
methods: ["POST"], | ||
authLevel: "anonymous", | ||
route: "webhooks/product/update", | ||
extraInputs: [df.input.durableClient()], | ||
handler: async (request: HttpRequest, context: InvocationContext) => { | ||
try { | ||
await connect(); | ||
const shopifyProduct = (await request.json()) as unknown as Product; | ||
const shouldProcessUpdate = shopifyProduct.tags.includes("update"); | ||
const regex = /userid-(\d+)/; | ||
const match = shopifyProduct.tags.match(regex); | ||
|
||
if (match && shouldProcessUpdate) { | ||
// The user ID is in the first capturing group | ||
const customerId = match[1]; | ||
console.log(`User ID: ${customerId}, - ${shopifyProduct.id}`); | ||
|
||
await WebhookUpdateProductOrchestration( | ||
{ | ||
customerId: parseInt(customerId), | ||
productId: shopifyProduct.id, | ||
}, | ||
context | ||
); | ||
} | ||
|
||
return { body: "" }; | ||
} catch (err) { | ||
return { body: "" }; | ||
} | ||
}, | ||
}); |