|
1 | 1 | import db from '../database/models'; |
2 | 2 | import Response from '../helpers/Response'; |
| 3 | +import SearchDatabase from '../helpers/SearchDatabase'; |
| 4 | +import EmailNotifications from '../helpers/EmailNotifications'; |
| 5 | +import addNotification from '../helpers/addNotification'; |
| 6 | +import inAppBot from '../helpers/inAppBot'; |
3 | 7 |
|
4 | 8 | const { |
5 | 9 | Trip, Branch, User, Stop, Accomodation |
@@ -76,13 +80,34 @@ class TripController { |
76 | 80 | ] |
77 | 81 | } |
78 | 82 | ); |
79 | | - const response = new Response( |
80 | | - true, |
81 | | - 201, |
82 | | - 'Travel request successfully created', |
83 | | - { trip } |
84 | | - ); |
85 | | - return res.status(response.code).json(response); |
| 83 | + if (trip.id) { |
| 84 | + const admins = await SearchDatabase.findAdminUsersWithNotificationOpt( |
| 85 | + ['travel admin', 'manager', 'super admin'], { emailOpt: true } |
| 86 | + ); |
| 87 | + const emails = admins.map(admin => admin.email); |
| 88 | + if (emails) { |
| 89 | + const data = await SearchDatabase.findTrip(trip.id); |
| 90 | + const tripData = { |
| 91 | + type, |
| 92 | + reason, |
| 93 | + departureDate, |
| 94 | + returnDate |
| 95 | + }; |
| 96 | + const message = `${data.user.firstName} ${data.user.lastName} requested for a ${type} trip`; |
| 97 | + data.trips = { ...data.trips, ...tripData }; |
| 98 | + EmailNotifications.sendNewTrip(emails, 'New Trip', data); |
| 99 | + inAppBot.send({ tripId: trip.id, message }); |
| 100 | + await addNotification(trip.id, message); |
| 101 | + const response = new Response( |
| 102 | + true, |
| 103 | + 201, |
| 104 | + 'Travel request successfully created', |
| 105 | + { trip } |
| 106 | + ); |
| 107 | + return res.status(response.code).json(response); |
| 108 | + } |
| 109 | + |
| 110 | + } |
86 | 111 | } catch (error) { |
87 | 112 | const response = new Response( |
88 | 113 | false, |
|
0 commit comments