This commit is contained in:
Matthieu Bessat 2020-07-12 10:48:44 +02:00
parent 7ddd2b3e3d
commit bc70ca4e05

View file

@ -5,6 +5,7 @@ import EmailService from '../EmailService'
import fs from 'fs' import fs from 'fs'
import Mustache from 'mustache' import Mustache from 'mustache'
import slugify from 'slugify' import slugify from 'slugify'
import Twig from 'twig'
export default class AdminOrganizationController { export default class AdminOrganizationController {
static getMany(req: express.Request, res: express.Response) { static getMany(req: express.Request, res: express.Response) {
@ -23,7 +24,7 @@ export default class AdminOrganizationController {
Organization.create({ Organization.create({
token: cryptoRandomString({ length: 10, type: 'distinguishable' }), token: cryptoRandomString({ length: 10, type: 'distinguishable' }),
createdAt: new Date(), createdAt: new Date(),
slug: req.body.name === undefined ? undefined : slugify(req.body.name) slug: req.body.name === undefined ? undefined : slugify(req.body.name),
...req.body ...req.body
}).then(data => { }).then(data => {
res.json({ res.json({
@ -76,16 +77,20 @@ export default class AdminOrganizationController {
} }
static sendEmailToken(req: express.Request, res: express.Response) { static sendEmailToken(req: express.Request, res: express.Response) {
const path: string = __dirname + '/../../templates/email/token.html' const path: string = __dirname + '/../../views/email/token.twig'
Organization.findById(req.params.id).then(data => { Organization.findById(req.params.id).then(data => {
if (data === null) { if (data === null) {
return res.status(404).json({ success: false, errors: [ { code: 'not-found', message: 'Organization not found' } ] }) return res.status(404).json({ success: false, errors: [ { code: 'not-found', message: 'Organization not found' } ] })
} }
Twig.renderFile(path, { filename: '', settings: { foo: 'bar' } }, (err: any, html: any) => {
console.log(html)
console.log(err)
EmailService.send( EmailService.send(
data.get('email'), data.get('email'),
"Votre lien secret pour modifier votre association - Forum des associations 2020", "Votre lien secret pour modifier votre association - Forum des associations 2020",
Mustache.render(fs.readFileSync(path).toString(), {}) html
) )
})
res.json({ success: true }) res.json({ success: true })
}) })
} }