all repos — caroster @ 686daf82d3a472d61b9c33b118b7a166033ceb83

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

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

 1'use strict';
 2const axios = require('axios');
 3const moment = require('moment');
 4const uuid = require('uuid');
 5const sendgrid = require('../../../lib/sendgrid');
 6require('moment/locale/fr-ch');
 7
 8const {STRAPI_URL = ''} = process.env;
 9
10module.exports = {
11  lifecycles: {
12    async beforeCreate(event) {
13      if (!event.uuid) {
14        event.uuid = uuid.v4();
15      }
16
17      // If user provides an address, get its lat/lng position using OSM API
18      if (event.address) {
19        const query = encodeURI(event.address);
20        try {
21          const {data} = await axios.get(
22            ` https://nominatim.openstreetmap.org/search?format=json&q=${query}`
23          );
24          if (Array.isArray(data) && data.length > 0) {
25            const [entity] = data;
26            event.position = [entity.lat, entity.lon];
27          } else
28            strapi.log.info(
29              `No location from Nominatim API for ${event.address}`
30            );
31        } catch (error) {
32          strapi.log.error(error);
33        }
34      }
35
36      // If user accepts newsletters, subscribe it
37      if (event.newsletter) sendgrid.subscribe(event.email);
38    },
39    async beforeUpdate(params, event) {
40      const eventInDb = await strapi.services.event.findOne(params);
41
42      if (!eventInDb.uuid) {
43        event.uuid = uuid.v4();
44      }
45
46      if (event.address) {
47        const query = encodeURI(event.address);
48        try {
49          const {data} = await axios.get(
50            ` https://nominatim.openstreetmap.org/search?format=json&q=${query}`
51          );
52          if (Array.isArray(data) && data.length > 0) {
53            const [entity] = data;
54            event.position = [entity.lat, entity.lon];
55          } else
56            strapi.log.info(
57              `No location from Nominatim API for ${event.address}`
58            );
59        } catch (error) {
60          strapi.log.error(error);
61        }
62      }
63    },
64
65    async afterCreate(event) {
66      try {
67        await strapi.plugins[
68          'email-designer'
69        ].services.email.sendTemplatedEmail(
70          {
71            to: event.email,
72          },
73          {
74            templateId: 1,
75            subject: `Caroster: ${event.name}`,
76          },
77          {
78            eventName: event.name,
79            eventTime: event.date
80              ? moment(event.date).format('dddd D MMMM YYYY')
81              : null,
82            eventAddress: event.address,
83            eventLink: `${STRAPI_URL}/e/${event.uuid}`,
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};