all repos — caroster @ 8bbbeae604eacc3a0f3e559c22dcb694822bf0b1

[Octree] Group carpool to your event https://caroster.io

api/event/models/event.js (view raw)

 1'use strict';
 2const axios = require('axios');
 3const moment = require('moment');
 4require('moment/locale/fr-ch');
 5
 6const {STRAPI_URL = '', CAROSTER_TEMPLATEID_EVENTCREATION} = process.env;
 7
 8/**
 9 * Read the documentation (https://strapi.io/documentation/v3.x/concepts/models.html#lifecycle-hooks)
10 * to customize this model
11 */
12
13module.exports = {
14  lifecycles: {
15    async beforeCreate(event) {
16      // If user provides an address, get its lat/lng position using OSM API
17      if (event.address) {
18        const query = encodeURI(event.address);
19        try {
20          const {data} = await axios.get(
21            ` https://nominatim.openstreetmap.org/search?format=json&q=${query}`
22          );
23          if (Array.isArray(data) && data.length > 0) {
24            const [entity] = data;
25            event.position = [entity.lat, entity.lon];
26          } else
27            strapi.log.info(
28              `No location from Nominatim API for ${event.address}`
29            );
30        } catch (error) {
31          strapi.log.error(error);
32        }
33      }
34
35      // If user accepts newsletters, subscribe it
36      if (event.newsletter)
37        try {
38          strapi.plugins['email'].services.contact.subscribe({
39            email: event.email,
40          });
41        } catch (error) {
42          console.error(error);
43          strapi.log.error(
44            `Impossible to save email ${
45              event.email
46            } in contact list for event#${event.id}. Error: ${JSON.stringify(
47              error
48            )}`
49          );
50        }
51    },
52    async beforeUpdate(params, event) {
53      if (event.address) {
54        const query = encodeURI(event.address);
55        try {
56          const {data} = await axios.get(
57            ` https://nominatim.openstreetmap.org/search?format=json&q=${query}`
58          );
59          if (Array.isArray(data) && data.length > 0) {
60            const [entity] = data;
61            event.position = [entity.lat, entity.lon];
62          } else
63            strapi.log.info(
64              `No location from Nominatim API for ${event.address}`
65            );
66        } catch (error) {
67          strapi.log.error(error);
68        }
69      }
70    },
71
72    async afterCreate(event) {
73      try {
74        await strapi.plugins['email'].services.email.send({
75          to: event.email,
76          templateId: CAROSTER_TEMPLATEID_EVENTCREATION,
77          templateData: {
78            eventName: event.name,
79            eventTime: event.date
80              ? moment(event.date).format('dddd Do MMMM YYYY')
81              : null,
82            eventAddress: event.address,
83            eventLink: `${STRAPI_URL}/e/${event.id}`,
84          },
85        });
86      } catch (error) {
87        console.error(error);
88        strapi.log.error(
89          `Impossible to send email notification to ${event.email} for event#${
90            event.id
91          }. Error: ${JSON.stringify(error)}`
92        );
93      }
94    },
95  },
96};