diff --git a/api/src/controllers/bookingController.ts b/api/src/controllers/bookingController.ts index 8f173fe1..c34a0fcc 100644 --- a/api/src/controllers/bookingController.ts +++ b/api/src/controllers/bookingController.ts @@ -42,55 +42,56 @@ export const create = async (req: Request, res: Response) => { } /** - * Notify a agency. + * Notify a supplier or admin. * * @async - * @param {env.User} user + * @param {env.User} driver * @param {string} bookingId - * @param {env.User} agency - * @param {string} notificationMessage - * @returns {*} + * @param {env.User} user + * @param {boolean} notificationMessage + * @returns {void} */ -const notifyAgency = async (user: env.User, bookingId: string, agency: env.User, notificationMessage: string) => { +const notify = async (driver: env.User, bookingId: string, user: env.User, notificationMessage: string) => { + if (user.type !== movininTypes.UserType.Agency && user.type !== movininTypes.UserType.Admin) { + return + } + + i18n.locale = user.language + // notification - const message = `${user.fullName} ${notificationMessage} ${bookingId}.` + const message = `${driver.fullName} ${notificationMessage} ${bookingId}.` const notification = new Notification({ - user: agency._id, + user: user._id, message, booking: bookingId, }) await notification.save() - let counter = await NotificationCounter.findOne({ user: agency._id }) + let counter = await NotificationCounter.findOne({ user: user._id }) if (counter && typeof counter.count !== 'undefined') { counter.count += 1 await counter.save() } else { - counter = new NotificationCounter({ user: agency._id, count: 1 }) + counter = new NotificationCounter({ user: user._id, count: 1 }) await counter.save() } // mail - i18n.locale = agency.language - - const to = [agency.email] - const admin = !!env.ADMIN_EMAIL && await User.exists({ email: env.ADMIN_EMAIL, type: movininTypes.UserType.Admin, active: true, verified: true, enableEmailNotifications: true }) - if (admin) { - to.push(env.ADMIN_EMAIL) - } + if (user.enableEmailNotifications) { + const mailOptions: nodemailer.SendMailOptions = { + from: env.SMTP_FROM, + to: user.email, + subject: message, + html: `

+ ${i18n.t('HELLO')}${user.fullName},

+ ${message}

+ ${helper.joinURL(env.BACKEND_HOST, `update-booking?b=${bookingId}`)}

+ ${i18n.t('REGARDS')}
+

`, + } - const mailOptions = { - from: env.SMTP_FROM, - to, - subject: message, - html: `

${i18n.t('HELLO')}${agency.fullName}, -

${message} -

${helper.joinURL(env.BACKEND_HOST, `update-booking?b=${bookingId}`)} -

${i18n.t('REGARDS')} -

`, + await mailHelper.sendMail(mailOptions) } - - await mailHelper.sendMail(mailOptions) } /** @@ -159,7 +160,7 @@ export const checkout = async (req: Request, res: Response) => { i18n.locale = user.language - const mailOptions = { + const mailOptions: nodemailer.SendMailOptions = { from: env.SMTP_FROM, to: user.email, subject: i18n.t('ACCOUNT_ACTIVATION_SUBJECT'), @@ -243,8 +244,16 @@ export const checkout = async (req: Request, res: Response) => { return res.sendStatus(204) } i18n.locale = agency.language - const message = body.payLater ? i18n.t('BOOKING_PAY_LATER_NOTIFICATION') : i18n.t('BOOKING_PAID_NOTIFICATION') - await notifyAgency(user, booking._id.toString(), agency, message) + let message = body.payLater ? i18n.t('BOOKING_PAY_LATER_NOTIFICATION') : i18n.t('BOOKING_PAID_NOTIFICATION') + await notify(user, booking._id.toString(), agency, message) + + // Notify admin + const admin = !!env.ADMIN_EMAIL && await User.findOne({ email: env.ADMIN_EMAIL, type: movininTypes.UserType.Admin }) + if (admin) { + i18n.locale = admin.language + message = body.payLater ? i18n.t('BOOKING_PAY_LATER_NOTIFICATION') : i18n.t('BOOKING_PAID_NOTIFICATION') + await notify(user, booking._id.toString(), admin, message) + } return res.status(200).send({ bookingId: booking._id }) } catch (err) { @@ -288,7 +297,7 @@ const notifyRenter = async (booking: env.Booking) => { } // mail - const mailOptions = { + const mailOptions: nodemailer.SendMailOptions = { from: env.SMTP_FROM, to: renter.email, subject: message, @@ -793,7 +802,7 @@ export const cancelBooking = async (req: Request, res: Response) => { await booking.save() // Notify agency - await notifyAgency(booking.renter, booking.id.toString(), booking.agency, i18n.t('CANCEL_BOOKING_NOTIFICATION')) + await notify(booking.renter, booking.id.toString(), booking.agency, i18n.t('CANCEL_BOOKING_NOTIFICATION')) return res.sendStatus(200) } diff --git a/api/src/controllers/userController.ts b/api/src/controllers/userController.ts index 2bdacb6b..3964fb99 100644 --- a/api/src/controllers/userController.ts +++ b/api/src/controllers/userController.ts @@ -6,6 +6,7 @@ import { v1 as uuid } from 'uuid' import escapeStringRegexp from 'escape-string-regexp' import mongoose from 'mongoose' import { CookieOptions, Request, Response } from 'express' +import nodemailer from 'nodemailer' import * as movininTypes from ':movinin-types' import i18n from '../lang/i18n' import * as env from '../config/env.config' @@ -89,7 +90,7 @@ const _signup = async (req: Request, res: Response, userType: movininTypes.UserT // Send email i18n.locale = user.language - const mailOptions = { + const mailOptions: nodemailer.SendMailOptions = { from: env.SMTP_FROM, to: user.email, subject: i18n.t('ACCOUNT_ACTIVATION_SUBJECT'), @@ -193,7 +194,7 @@ export const create = async (req: Request, res: Response) => { // Send email i18n.locale = user.language - const mailOptions = { + const mailOptions: nodemailer.SendMailOptions = { from: env.SMTP_FROM, to: user.email, subject: i18n.t('ACCOUNT_ACTIVATION_SUBJECT'), @@ -334,7 +335,7 @@ export const resend = async (req: Request, res: Response) => { const reset = req.params.reset === 'true' - const mailOptions = { + const mailOptions: nodemailer.SendMailOptions = { from: env.SMTP_FROM, to: user.email, subject: reset ? i18n.t('PASSWORD_RESET_SUBJECT') : i18n.t('ACCOUNT_ACTIVATION_SUBJECT'), @@ -751,7 +752,7 @@ export const resendLink = async (req: Request, res: Response) => { // Send email i18n.locale = user.language - const mailOptions = { + const mailOptions: nodemailer.SendMailOptions = { from: env.SMTP_FROM, to: user.email, subject: i18n.t('ACCOUNT_ACTIVATION_SUBJECT'), diff --git a/api/src/lang/en.ts b/api/src/lang/en.ts index 46134935..fecef2b4 100644 --- a/api/src/lang/en.ts +++ b/api/src/lang/en.ts @@ -32,7 +32,7 @@ export const en = { BOOKING_CONFIRMED_PART12: ' (local time).', BOOKING_CONFIRMED_PART13: 'Please respect the time.', BOOKING_CONFIRMED_PART14: 'You can follow your booking on: ', - BOOKING_PAY_LATER_NOTIFICATION: 'confirmed (pay later) the booking', + BOOKING_PAY_LATER_NOTIFICATION: 'confirmed the booking', BOOKING_PAID_NOTIFICATION: 'paid the booking', CANCEL_BOOKING_NOTIFICATION: 'made a request to cancel the booking', BOOKING_UPDATED_NOTIFICATION_PART1: 'The status of the booking', diff --git a/api/src/lang/fr.ts b/api/src/lang/fr.ts index 1ace19e7..e1789de4 100644 --- a/api/src/lang/fr.ts +++ b/api/src/lang/fr.ts @@ -32,7 +32,7 @@ export const fr = { BOOKING_CONFIRMED_PART12: ' (heure locale).', BOOKING_CONFIRMED_PART13: 'Veuillez respecter les horaires.', BOOKING_CONFIRMED_PART14: 'Vous pouvez suivre votre réservation sur : ', - BOOKING_PAY_LATER_NOTIFICATION: 'a confirmé (payer plus tard) la réservation', + BOOKING_PAY_LATER_NOTIFICATION: 'a confirmé la réservation', BOOKING_PAID_NOTIFICATION: 'a payé la réservation', CANCEL_BOOKING_NOTIFICATION: "a fait une demande d'annulation de la réservation", BOOKING_UPDATED_NOTIFICATION_PART1: 'Le statut de la réservation',