Skip to content
Snippets Groups Projects
Commit d5bf219b authored by Simon Mulquin's avatar Simon Mulquin
Browse files

Merge pull request 'feat: :sparkles: Send notif when event has ended' (#9) from...

Merge pull request 'feat: :sparkles: Send notif when event has ended' (#9) from send-end-event-notif into main

Reviewed-on: https://codeberg.org/Octree/Caroster/pulls/9
parents bfdcefc8 39032cf0
Branches
Tags v3.0
No related merge requests found
......@@ -20,4 +20,23 @@ export default {
concurrency: 5,
});
},
/**
* Send event recap when it has ended
* Only to events with a provided 'date' field
* Everyday at 08:30
*/
"30 8 * * *": async ({ strapi }) => {
const events = await strapi.entityService.findMany("api::event.event", {
filters: {
date: {
$eq: DateTime.now().toISODate(),
},
},
populate: ["travels", "passengers", "passengers.travel"],
limit: -1,
});
await pMap(events, strapi.service("api::event.event").sendEndRecap, {
concurrency: 5,
});
},
};
......@@ -3,6 +3,7 @@ import { DateTime } from "luxon";
import { factories } from "@strapi/strapi";
const TEMPLATE_NAME_RECAP = "event_recap";
const TEMPLATE_NAME_END_EVENT = "event_end";
const { STRAPI_URL = "" } = process.env;
export default factories.createCoreService(
......@@ -80,5 +81,52 @@ export default factories.createCoreService(
}
}
},
sendEndRecap: async (event) => {
try {
const template = await strapi
.plugin("email-designer")
.services.template.findOne({
name: TEMPLATE_NAME_END_EVENT,
});
if (!template) {
strapi.log.error(
`No email template with name ${TEMPLATE_NAME_END_EVENT}`
);
return null;
}
const travelsCount = event.travels?.length || 0;
const passengersCount = event.passengers?.filter(
(passenger) => passenger.travel
).length;
await strapi.service("plugin::email-designer.email").sendTemplatedEmail(
{
to: event.email,
},
{
templateReferenceId: template.templateReferenceId,
},
{
event,
travelsCount,
passengersCount,
eventLink: `${STRAPI_URL}/e/${event.uuid}`,
}
);
strapi.log.info(
`Email with template '${TEMPLATE_NAME_END_EVENT}' sent to ${event.email}`
);
} catch (error) {
console.error(error);
strapi.log.error(
`Impossible to send end event notification to ${
event.email
} for event #${event.id}. Error: ${JSON.stringify(error)}`
);
}
},
})
);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment