🐛 Fix travel alerts
Tim Izzo tim@octree.ch
Tue, 20 Feb 2024 10:27:49 +0100
1 files changed,
8 insertions(+),
11 deletions(-)
M
backend/src/api/trip-alert/services/trip-alert.ts
→
backend/src/api/trip-alert/services/trip-alert.ts
@@ -9,17 +9,6 @@ export default factories.createCoreService(
"api::trip-alert.trip-alert", ({ strapi }) => ({ async sendTripAlerts(eventId: string, travel) { - if (!travel.meeting_latitude || !travel.meeting_longitude) { - strapi.log.warn( - `Can't send trip alert for travel ${travel.id}. No coordinates found.` - ); - return; - } - - const travelCoordinates: Coordinates = [ - travel.meeting_latitude, - travel.meeting_longitude, - ]; const eventTripAlerts = await strapi.entityService.findMany( "api::trip-alert.trip-alert", {@@ -34,12 +23,19 @@ const filteredTripAlerts = eventTripAlerts.filter((tripAlert) => {
// If alert has no geographical info, send alert on each new trip if (!tripAlert.latitude || !tripAlert.longitude || !tripAlert.radius) return true; + // If travel has no geographical info, send alert to all + else if (!travel.meeting_latitude || !travel.meeting_longitude) + return true; // Else, check if new travel is in alert area const alertCoordinates: Coordinates = [ tripAlert.latitude, tripAlert.longitude, ]; + const travelCoordinates: Coordinates = [ + travel.meeting_latitude, + travel.meeting_longitude, + ]; const distance = calculateHaversineDistance( travelCoordinates, alertCoordinates@@ -56,6 +52,7 @@ data: {
type: "NewTrip", event: eventId, user: tripAlert.user.id, + payload: { travel }, }, }); });