Skip to content

Commit

Permalink
add interval to event
Browse files Browse the repository at this point in the history
  • Loading branch information
lulu committed May 8, 2024
1 parent 0e3b077 commit 3907619
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ You can listen to this event in your plugin to perform additional actions with y
}

```
Check medusa docs https://docs.medusajs.com/development/events/create-subscriber

Check medusa docs <https://docs.medusajs.com/development/events/create-subscriber>

#### Example

Expand All @@ -252,7 +253,7 @@ export default async function abandonedEmailHandler({
const abandonedCartService: AbandonedCartService = container.resolve("abandonedCartService")
const sendGridService: SendGridService = container.resolve("sendGridService")

const { id } = data
const { id, interval } = data

const notNullCartsPromise = await cartRepo.findOne({
where: {
Expand All @@ -261,7 +262,7 @@ export default async function abandonedEmailHandler({
order: {
created_at: "DESC",
},
select: ["id", "email", "created_at", "region", "context", "abandoned_cart_notification_count"],
select: ["id", "email", "created_at", "region", "context", "abandoned_count"],
relations: ["items", "region", "shipping_address"],
})

Expand All @@ -283,15 +284,18 @@ export default async function abandonedEmailHandler({
}

// Send email using sendgrid
await this.sendGridService.sendEmail(emailData)
const sendGridPromise = this.sendGridService.sendEmail(emailData)

// Update the cart to reflect that the email has been sent
await cartRepo.update(cart.id, {
abandoned_cart_notification_sent: true,
abandoned_cart_notification_date: new Date().toISOString(),
abandoned_cart_notification_count: (notNullCartsPromise?.abandoned_cart_notification_count || 0) + 1,
})

const repoPromise = cartRepo.update(cart.id, {
abandoned_lastdate: new Date().toISOString(),
abandoned_count: (notNullCartsPromise?.abandoned_count || 0) + 1,
abandoned_last_interval: interval || undefined,
abandoned_completed_at: interval && pluginOptions.intervals[pluginOptions.intervals.length - 1].interval === interval ? new Date().toISOString() : undefined,
});

await Promise.all([sendGridPromise, repoPromise])

}

export const config: SubscriberConfig = {
Expand All @@ -302,4 +306,3 @@ export const config: SubscriberConfig = {
}

```

2 changes: 2 additions & 0 deletions src/services/abandoned-cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default class AbandonedCartService extends TransactionBaseService {
this.logger.info("SendGrid is not enabled, emitting event")
await this.eventBusService.emit("cart.send-abandoned-email", {
id,
interval,
});
return {
success: true,
Expand Down Expand Up @@ -174,6 +175,7 @@ export default class AbandonedCartService extends TransactionBaseService {

const eventPromise = this.eventBusService.emit("cart.send-abandoned-email", {
id,
interval,
});
this.logger.info(`Sending email for cart ${id}`);
await Promise.all([emailPromise, cartPromise, eventPromise]);
Expand Down

0 comments on commit 3907619

Please sign in to comment.