Skip to content

Commit

Permalink
Refactor updateArticle function and fix conditional in webhookProduct…
Browse files Browse the repository at this point in the history
…Update

 • Extract article data construction into a separate object for clarity in updateArticle.
 • Ensure image URLs are stripped of query parameters before updating in updateArticle.
 • Add a check for shopifyProduct.id in the conditional of webhookProductUpdate to prevent processing without a product ID.
  • Loading branch information
jamalsoueidan committed Jul 1, 2024
1 parent 92e6267 commit 872ebe3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,30 @@ export const updateArticle = async ({
tags.push(`gender-${user.gender}`);
}

const data = {
article: {
id: user.articleId,
body_html: user.aboutMeHtml,
tags: tags.join(", "),
summary_html: user.shortDescription,
...(user.images?.profile?.url
? {
image: {
src: user.images?.profile.url.includes("?")
? user.images?.profile.url.split("?")[0]
: user.images?.profile.url,
alt: user.username,
},
}
: {}),
published_at: user.active ? user.createdAt : null,
},
};

const response = await shopifyRest().put(
`blogs/105364226375/articles/${user.articleId}`,
{
data: {
article: {
id: user.articleId,
body_html: user.aboutMeHtml,
tags: tags.join(", "),
summary_html: user.shortDescription,
...(user.images?.profile
? {
image: {
src: user.images?.profile.url,
alt: user.username,
},
}
: {}),
published_at: user.active ? user.createdAt : null,
},
},
data,
}
);

Expand Down
2 changes: 1 addition & 1 deletion src/functions/webook-product.function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ app.http("webhookProductUpdate", {
const regex = /userid-(\d+)/;
const match = shopifyProduct.tags.match(regex);

if (match && shouldProcessUpdate) {
if (match && shouldProcessUpdate && shopifyProduct.id) {
// The user ID is in the first capturing group
const customerId = match[1];
console.log(`User ID: ${customerId}, - ${shopifyProduct.id}`);
Expand Down

0 comments on commit 872ebe3

Please sign in to comment.