Skip to content

Commit

Permalink
Add updateArticle activity and handler to customer update orchestration
Browse files Browse the repository at this point in the history
  • Loading branch information
jamalsoueidan committed May 27, 2024
1 parent c5e855d commit bcfafdf
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/functions/customer/orchestrations/customer/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ import * as df from "durable-functions";
import { OrchestrationContext } from "durable-functions";
import { User } from "~/functions/user";
import { activityType } from "~/library/orchestration";
import { updateArticle } from "./update/update-article";
import { updateUserMetaobject } from "./update/update-user-metaobject";

const updateUserMetaobjectName = "updateUserMetaobject";
df.app.activity(updateUserMetaobjectName, {
handler: updateUserMetaobject,
});

const updateArticleName = "updateArticle";
df.app.activity(updateArticleName, {
handler: updateArticle,
});

const orchestrator: df.OrchestrationHandler = function* (
context: OrchestrationContext
) {
Expand All @@ -23,8 +29,17 @@ const orchestrator: df.OrchestrationHandler = function* (
})
);

const article: Awaited<ReturnType<typeof updateArticle>> =
yield context.df.callActivity(
updateArticleName,
activityType<typeof updateArticle>({
user,
})
);

return {
userField,
article,
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { CustomerLocationServiceList } from "~/functions/customer/services/location/list";
import { ScheduleModel } from "~/functions/schedule";
import { User } from "~/functions/user";
import { shopifyRest } from "~/library/shopify/rest";

interface RootObject {
article: Article;
}
interface Article {
id: number;
title: string;
created_at: string;
body_html: string;
blog_id: number;
author: string;
user_id?: any;
published_at: string;
updated_at: string;
summary_html: string;
template_suffix?: any;
handle: string;
tags: string;
admin_graphql_api_id: string;
image: Image;
}
interface Image {
created_at: string;
alt: string;
width: number;
height: number;
src: string;
}

export const updateArticle = async ({
user,
}: {
user: User;
}): Promise<RootObject> => {
const schedules = await ScheduleModel.find({
customerId: user.customerId,
});

const parentIds = schedules.flatMap((schedule) =>
schedule.products.map((product) => product.parentId)
);

const days = schedules.flatMap((schedule) =>
schedule.slots.map((slot) => slot.day.toLowerCase())
);

const tags = [user.username];

if (user.professions) {
tags.push(user.professions?.join(","));
}

if (parentIds.length > 0) {
tags.push(`parentid-${parentIds.join(", parentid-")}`);
}

if (days.length > 0) {
tags.push(`day-${days.join(", day-")}`);
}

const locations = await CustomerLocationServiceList({
customerId: user.customerId,
});

if (locations.length > 0) {
tags.push(
`city-${locations.map((l) => l.city.toLowerCase()).join(", city-")}`
);
}

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,
},
},
}
);

if (!response.ok) {
throw new Error(`Failed to create article for user ${user.username}`);
}

return await response.json();
};
14 changes: 13 additions & 1 deletion src/functions/customer/services/location/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LocationModel } from "~/functions/location";
import { NotFoundError } from "~/library/handler";

export type CustomerLocationServiceListProps = {
customerId: number;
Expand All @@ -7,5 +8,16 @@ export type CustomerLocationServiceListProps = {
export const CustomerLocationServiceList = (
props: CustomerLocationServiceListProps
) => {
return LocationModel.find({ customerId: props.customerId, deletedAt: null });
return LocationModel.find({
customerId: props.customerId,
deletedAt: null,
}).orFail(
new NotFoundError([
{
path: ["locationId", "customerId"],
message: "LOCATION_NOT_FOUND_IN_USER",
code: "custom",
},
])
);
};
5 changes: 0 additions & 5 deletions src/functions/customer/services/product/list-ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,3 @@ export const CustomerProductsServiceListIds = async (
schedule.products.map((product) => product.productId)
);
};

type CustomerProductsServiceListProps = {
customerId: Schedule["customerId"];
scheduleId?: Schedule["_id"];
};

0 comments on commit bcfafdf

Please sign in to comment.