all repos — caroster @ 9a5f5065875933848a30775a26e9727e87ea6b47

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

:sparkles: send email after email creation
Hadrien Froger hadrien@octree.ch
Fri, 10 Jul 2020 11:31:29 +0100
commit

9a5f5065875933848a30775a26e9727e87ea6b47

parent

fc06dfe675ad6835ee3317e010ac8893d972d5fa

4 files changed, 37 insertions(+), 5 deletions(-)

jump to
M .env.example.env.example

@@ -1,2 +1,3 @@

+STRAPI_URL= SENDGRID_API_KEY= SENDGRID_CONTACT_LIST=
M api/event/models/event.jsapi/event/models/event.js

@@ -1,6 +1,6 @@

'use strict'; const axios = require('axios'); - +const moment = require('moment'); /** * Read the documentation (https://strapi.io/documentation/v3.x/concepts/models.html#lifecycle-hooks) * to customize this model

@@ -49,6 +49,39 @@ );

} catch (error) { strapi.log.error(error); } + } + }, + + async afterCreate(event) { + let eventTime = ''; + if (event.date) { + eventTime = + ', ' + + moment(eventTime.date).locale('fr').format('dddd Do MMMM YYYY'); + } + let eventAddress = ''; + if (event.address) { + eventAddress = ', ' + event.address; + } + const {STRAPI_URL = ''} = process.env; + + try { + await strapi.plugins['sendgrid'].services.email.send({ + to: event.email, + from: 'caroster@octree.ch', + templateId: 'd-a1b5043fb186411ea4b57ea956625093', + dynamic_template_data: { + eventName: event.name, + eventTime, + eventAddress, + eventLink: `${STRAPI_URL}/e/${event.id}`, + }, + }); + } catch (error) { + strapi.log.error( + `Impossible to send email notification to ${event.email} for event#${event.id}:`, + error.message + ); } }, },
M package-lock.jsonpackage-lock.json

@@ -10348,9 +10348,7 @@ }

} }, "strapi-plugin-sendgrid": { - "version": "0.1.0", - "resolved": "https://npm-8ee.hidora.com/strapi-plugin-sendgrid/-/strapi-plugin-sendgrid-0.1.0.tgz", - "integrity": "sha512-CHJn0nKkhQC/dJ+rTo9q0V6H9hBUQCECoG7VtwAEAuf1k9OijF1/dTFL5lzA4lfu2hBDljul3Zo99Q7PvY+G8g==", + "version": "file:../strapi-plugin-sendgrid", "requires": { "axios": "^0.19.2" }
M package.jsonpackage.json

@@ -24,7 +24,7 @@ "strapi-middleware-reactapp": "^0.1.0",

"strapi-plugin-content-manager": "3.0.5", "strapi-plugin-content-type-builder": "3.0.5", "strapi-plugin-documentation": "^3.0.5", - "strapi-plugin-sendgrid": "^0.1.0", + "strapi-plugin-sendgrid": "file:../strapi-plugin-sendgrid", "strapi-plugin-upload": "^3.0.5", "strapi-plugin-users-permissions": "3.0.5", "strapi-utils": "3.0.5"