Skip to content

Commit

Permalink
Add jsdoc to bookingController.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Sep 23, 2023
1 parent 5933a23 commit 5879e2a
Showing 1 changed file with 107 additions and 31 deletions.
138 changes: 107 additions & 31 deletions api/src/controllers/bookingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ import * as MailHelper from '../common/MailHelper'
import * as env from '../config/env.config'
import * as bookcarsTypes from 'bookcars-types'

/**
* Create a Booking.
*
* @export
* @async
* @param {Request} req
* @param {Response} res
* @returns {unknown}
*/
export async function create(req: Request, res: Response) {
try {
const body: bookcarsTypes.UpsertBookingPayload = req.body
Expand All @@ -37,6 +46,16 @@ export async function create(req: Request, res: Response) {
}
}

/**
* Notify a supplier.
*
* @async
* @param {env.User} user
* @param {string} bookingId
* @param {env.User} company
* @param {string} notificationMessage
* @returns {void}
*/
async function notifySupplier(user: env.User, bookingId: string, company: env.User, notificationMessage: string) {
strings.setLanguage(company.language)

Expand Down Expand Up @@ -69,6 +88,15 @@ async function notifySupplier(user: env.User, bookingId: string, company: env.Us
await MailHelper.sendMail(mailOptions)
}

/**
* Complete checkout process and create Booking.
*
* @export
* @async
* @param {Request} req
* @param {Response} res
* @returns {unknown}
*/
export async function book(req: Request, res: Response) {
try {
let user: env.User | null
Expand Down Expand Up @@ -199,6 +227,13 @@ export async function book(req: Request, res: Response) {
}
}

/**
* Notify driver and send push notification.
*
* @async
* @param {env.Booking} booking
* @returns {void}
*/
async function notifyDriver(booking: env.Booking) {
const driver = await User.findById(booking.driver)
if (!driver) {
Expand Down Expand Up @@ -291,6 +326,15 @@ async function notifyDriver(booking: env.Booking) {
}
}

/**
* Update Booking.
*
* @export
* @async
* @param {Request} req
* @param {Response} res
* @returns {unknown}
*/
export async function update(req: Request, res: Response) {
try {
const body: bookcarsTypes.UpsertBookingPayload = req.body
Expand Down Expand Up @@ -387,6 +431,15 @@ export async function update(req: Request, res: Response) {
}
}

/**
* Update Booking Status.
*
* @export
* @async
* @param {Request} req
* @param {Response} res
* @returns {unknown}
*/
export async function updateStatus(req: Request, res: Response) {
try {
const body: bookcarsTypes.UpdateStatusPayload = req.body
Expand All @@ -410,6 +463,15 @@ export async function updateStatus(req: Request, res: Response) {
}
}

/**
* Delete Bookings.
*
* @export
* @async
* @param {Request} req
* @param {Response} res
* @returns {unknown}
*/
export async function deleteBookings(req: Request, res: Response) {
try {
const body: string[] = req.body
Expand All @@ -431,6 +493,15 @@ export async function deleteBookings(req: Request, res: Response) {
}
}

/**
* Get Booking by ID.
*
* @export
* @async
* @param {Request} req
* @param {Response} res
* @returns {unknown}
*/
export async function getBooking(req: Request, res: Response) {
const { id } = req.params

Expand Down Expand Up @@ -488,6 +559,15 @@ export async function getBooking(req: Request, res: Response) {
}
}

/**
* Get Bookings.
*
* @export
* @async
* @param {Request} req
* @param {Response} res
* @returns {unknown}
*/
export async function getBookings(req: Request, res: Response) {
try {
const body: bookcarsTypes.GetBookingsPayload = req.body
Expand All @@ -509,14 +589,10 @@ export async function getBookings(req: Request, res: Response) {
}
if ($match.$and) {
if (user) {
$match.$and.push({
'driver._id': { $eq: new mongoose.Types.ObjectId(user) },
})
$match.$and.push({ 'driver._id': { $eq: new mongoose.Types.ObjectId(user) } })
}
if (car) {
$match.$and.push({
'car._id': { $eq: new mongoose.Types.ObjectId(car) },
})
$match.$and.push({ 'car._id': { $eq: new mongoose.Types.ObjectId(car) } })
}
if (from) {
$match.$and.push({ from: { $gte: from } })
Expand All @@ -525,18 +601,10 @@ export async function getBookings(req: Request, res: Response) {
$match.$and.push({ to: { $lte: to } })
} // $to < to
if (pickupLocation) {
$match.$and.push({
'pickupLocation._id': {
$eq: new mongoose.Types.ObjectId(pickupLocation),
},
})
$match.$and.push({ 'pickupLocation._id': { $eq: new mongoose.Types.ObjectId(pickupLocation) } })
}
if (dropOffLocation) {
$match.$and.push({
'dropOffLocation._id': {
$eq: new mongoose.Types.ObjectId(dropOffLocation),
},
})
$match.$and.push({ 'dropOffLocation._id': { $eq: new mongoose.Types.ObjectId(dropOffLocation) } })
}
if (keyword) {
const isObjectId = mongoose.isValidObjectId(keyword)
Expand Down Expand Up @@ -566,9 +634,7 @@ export async function getBookings(req: Request, res: Response) {
let: { companyId: '$company' },
pipeline: [
{
$match: {
$expr: { $eq: ['$_id', '$$companyId'] },
},
$match: {$expr: { $eq: ['$_id', '$$companyId'] }},
},
],
as: 'company',
Expand All @@ -581,9 +647,7 @@ export async function getBookings(req: Request, res: Response) {
let: { carId: '$car' },
pipeline: [
{
$match: {
$expr: { $eq: ['$_id', '$$carId'] },
},
$match: {$expr: { $eq: ['$_id', '$$carId'] }},
},
],
as: 'car',
Expand All @@ -596,9 +660,7 @@ export async function getBookings(req: Request, res: Response) {
let: { driverId: '$driver' },
pipeline: [
{
$match: {
$expr: { $eq: ['$_id', '$$driverId'] },
},
$match: {$expr: { $eq: ['$_id', '$$driverId'] }},
},
],
as: 'driver',
Expand All @@ -611,9 +673,7 @@ export async function getBookings(req: Request, res: Response) {
let: { pickupLocationId: '$pickupLocation' },
pipeline: [
{
$match: {
$expr: { $eq: ['$_id', '$$pickupLocationId'] },
},
$match: {$expr: { $eq: ['$_id', '$$pickupLocationId'] }},
},
{
$lookup: {
Expand Down Expand Up @@ -645,9 +705,7 @@ export async function getBookings(req: Request, res: Response) {
let: { dropOffLocationId: '$dropOffLocation' },
pipeline: [
{
$match: {
$expr: { $eq: ['$_id', '$$dropOffLocationId'] },
},
$match: {$expr: { $eq: ['$_id', '$$dropOffLocationId'] }},
},
{
$lookup: {
Expand Down Expand Up @@ -707,6 +765,15 @@ export async function getBookings(req: Request, res: Response) {
}
}

/**
* Check if a driver has Bookings.
*
* @export
* @async
* @param {Request} req
* @param {Response} res
* @returns {unknown}
*/
export async function hasBookings(req: Request, res: Response) {
const { driver } = req.params

Expand All @@ -728,6 +795,15 @@ export async function hasBookings(req: Request, res: Response) {
}
}

/**
* Cancel a Booking.
*
* @export
* @async
* @param {Request} req
* @param {Response} res
* @returns {unknown}
*/
export async function cancelBooking(req: Request, res: Response) {
const { id } = req.params

Expand Down

0 comments on commit 5879e2a

Please sign in to comment.