Skip to content

Commit

Permalink
Add admin email notification
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed May 11, 2024
1 parent 465c831 commit e5d57b0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ MI_MINIMUM_AGE=21
MI_EXPO_ACCESS_TOKEN=EXPO_ACCESS_TOKEN
MI_STRIPE_SECRET_KEY=STRIPE_SECRET_KEY
MI_STRIPE_SESSION_EXPIRE_AT=82800
MI_ADMIN_EMAIL=admin@movinin.io
7 changes: 7 additions & 0 deletions api/src/config/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@ export const STRIPE_SESSION_EXPIRE_AT = stripeSessionExpireAt
*/
export const BOOKING_EXPIRE_AT = STRIPE_SESSION_EXPIRE_AT + (10 * 60)

/**
* Private SSL key filepath.
*
* @type {string}
*/
export const ADMIN_EMAIL = __env__('MI_ADMIN_EMAIL', false)

/**
* User Document.
*
Expand Down
16 changes: 12 additions & 4 deletions api/src/controllers/bookingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mongoose from 'mongoose'
import escapeStringRegexp from 'escape-string-regexp'
import { Expo, ExpoPushMessage, ExpoPushTicket } from 'expo-server-sdk'
import { Request, Response } from 'express'
import nodemailer from 'nodemailer'
import * as movininTypes from ':movinin-types'
import i18n from '../lang/i18n'
import Booking from '../models/Booking'
Expand Down Expand Up @@ -72,13 +73,19 @@ const notifyAgency = async (user: env.User, bookingId: string, agency: env.User,
// 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)
}

const mailOptions = {
from: env.SMTP_FROM,
to: agency.email,
to,
subject: message,
html: `<p>${i18n.t('HELLO')}${agency.fullName},
<br><br>${message}
<br><br>${helper.joinURL(env.BACKEND_HOST, `booking?b=${bookingId}`)}
<br><br>${helper.joinURL(env.BACKEND_HOST, `update-booking?b=${bookingId}`)}
<br><br>${i18n.t('REGARDS')}
<br></p>`,
}
Expand Down Expand Up @@ -210,7 +217,7 @@ export const checkout = async (req: Request, res: Response) => {
return res.sendStatus(204)
}

const mailOptions = {
const mailOptions: nodemailer.SendMailOptions = {
from: env.SMTP_FROM,
to: user.email,
subject: `${i18n.t('BOOKING_CONFIRMED_SUBJECT_PART1')} ${booking._id} ${i18n.t('BOOKING_CONFIRMED_SUBJECT_PART2')}`,
Expand All @@ -236,7 +243,8 @@ export const checkout = async (req: Request, res: Response) => {
return res.sendStatus(204)
}
i18n.locale = agency.language
await notifyAgency(user, booking._id.toString(), agency, i18n.t('BOOKING_NOTIFICATION'))
const message = body.payLater ? i18n.t('BOOKING_PAY_LATER_NOTIFICATION') : i18n.t('BOOKING_PAID_NOTIFICATION')
await notifyAgency(user, booking._id.toString(), agency, message)

return res.status(200).send({ bookingId: booking._id })
} catch (err) {
Expand Down
3 changes: 2 additions & 1 deletion api/src/lang/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ 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_NOTIFICATION: 'paid the booking',
BOOKING_PAY_LATER_NOTIFICATION: 'confirmed (pay later) 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',
BOOKING_UPDATED_NOTIFICATION_PART2: 'was updated.',
Expand Down
3 changes: 2 additions & 1 deletion api/src/lang/fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ 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_NOTIFICATION: 'a payé la réservation',
BOOKING_PAY_LATER_NOTIFICATION: 'a confirmé (payer plus tard) 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',
BOOKING_UPDATED_NOTIFICATION_PART2: 'a été mis à jour.',
Expand Down

0 comments on commit e5d57b0

Please sign in to comment.