Skip to content

Commit

Permalink
Update admin notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed May 12, 2024
1 parent e5d57b0 commit 25625e1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
75 changes: 42 additions & 33 deletions api/src/controllers/bookingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<p>
${i18n.t('HELLO')}${user.fullName},<br><br>
${message}<br><br>
${helper.joinURL(env.BACKEND_HOST, `update-booking?b=${bookingId}`)}<br><br>
${i18n.t('REGARDS')}<br>
</p>`,
}

const mailOptions = {
from: env.SMTP_FROM,
to,
subject: message,
html: `<p>${i18n.t('HELLO')}${agency.fullName},
<br><br>${message}
<br><br>${helper.joinURL(env.BACKEND_HOST, `update-booking?b=${bookingId}`)}
<br><br>${i18n.t('REGARDS')}
<br></p>`,
await mailHelper.sendMail(mailOptions)
}

await mailHelper.sendMail(mailOptions)
}

/**
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 5 additions & 4 deletions api/src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion api/src/lang/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion api/src/lang/fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 25625e1

Please sign in to comment.