-
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.
Add updateArticle activity and handler to customer update orchestration
- Loading branch information
1 parent
c5e855d
commit bcfafdf
Showing
4 changed files
with
131 additions
and
6 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
103 changes: 103 additions & 0 deletions
103
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
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(); | ||
}; |
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