Skip to content

Commit

Permalink
Update bookingController.ts and booking.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Nov 18, 2024
1 parent 52d5968 commit 3fe0787
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 5 deletions.
4 changes: 2 additions & 2 deletions api/src/controllers/bookingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const notify = async (driver: env.User, bookingId: string, user: env.User
* @param {boolean} payLater
* @returns {unknown}
*/
export const confirm = async (user: env.User, supplier: env.User, booking: env.Booking, payLater: boolean = false) => {
export const confirm = async (user: env.User, supplier: env.User, booking: env.Booking, payLater: boolean) => {
const { language } = user
const locale = language === 'fr' ? 'fr-FR' : 'en-US'
const options: Intl.DateTimeFormatOptions = {
Expand Down Expand Up @@ -140,7 +140,7 @@ export const confirm = async (user: env.User, supplier: env.User, booking: env.B
const dropOffLocationName = dropOffLocation.values.filter((value) => value.language === language)[0].value

let contractFile: string | null = null
if (supplier.contracts) {
if (supplier.contracts && supplier.contracts.length > 0) {
contractFile = supplier.contracts.find((c) => c.language === user.language)?.file || null
if (!contractFile) {
contractFile = supplier.contracts.find((c) => c.language === 'en')?.file || null
Expand Down
114 changes: 112 additions & 2 deletions api/tests/booking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,78 @@ describe('POST /api/checkout', () => {
bookings = await Booking.find({ driver: DRIVER1_ID })
expect(bookings.length).toBeGreaterThan(1)

// test success (driver.enableEmailNotifications disabled)
let driver = await User.findById(DRIVER1_ID)
driver!.enableEmailNotifications = false
await driver!.save()
res = await request(app)
.post('/api/checkout')
.send(payload)
expect(res.statusCode).toBe(200)
driver!.enableEmailNotifications = true
await driver!.save()

// test success (supplier.enableEmailNotifications disabled)
let supplier = await User.findById(SUPPLIER_ID)
supplier!.enableEmailNotifications = false
await supplier!.save()
res = await request(app)
.post('/api/checkout')
.send(payload)
expect(res.statusCode).toBe(200)
supplier!.enableEmailNotifications = true
await supplier!.save()

// test success (without contract)
supplier = await User.findById(SUPPLIER_ID)
let { contracts } = (supplier!)
supplier!.contracts = undefined
await supplier!.save()
await driver!.save()
res = await request(app)
.post('/api/checkout')
.send(payload)
expect(res.statusCode).toBe(200)
supplier!.contracts = contracts
await supplier!.save()

// test success (with contract file not found)
supplier = await User.findById(SUPPLIER_ID)
contracts = supplier!.contracts
supplier!.contracts = [{ language: 'en', file: `${nanoid()}.pdf` }]
await supplier!.save()
await driver!.save()
res = await request(app)
.post('/api/checkout')
.send(payload)
expect(res.statusCode).toBe(200)
supplier!.contracts = contracts
await supplier!.save()

// test success (with contract file null)
supplier = await User.findById(SUPPLIER_ID)
contracts = supplier!.contracts
supplier!.contracts = [{ language: 'en', file: null }]
await supplier!.save()
await driver!.save()
res = await request(app)
.post('/api/checkout')
.send(payload)
expect(res.statusCode).toBe(200)
supplier!.contracts = contracts
await supplier!.save()

// test success (with contract fr language)
driver = await User.findById(DRIVER1_ID)
driver!.language = 'fr'
await driver!.save()
res = await request(app)
.post('/api/checkout')
.send(payload)
expect(res.statusCode).toBe(200)
driver!.language = 'en'
await driver!.save()

// test failure (stripe payment failed)
payload.payLater = false
const receiptEmail = testHelper.GetRandomEmail()
Expand Down Expand Up @@ -270,7 +342,7 @@ describe('POST /api/checkout', () => {
await stripeAPI.paymentIntents.confirm(paymentIntentId, {
payment_method: 'pm_card_visa',
})
const driver = await User.findOne({ _id: DRIVER1_ID })
driver = await User.findOne({ _id: DRIVER1_ID })
driver!.language = 'fr'
await driver?.save()
res = await request(app)
Expand All @@ -280,6 +352,15 @@ describe('POST /api/checkout', () => {
expect(res.statusCode).toBe(200)
bookings = await Booking.find({ driver: DRIVER1_ID })
expect(bookings.length).toBeGreaterThan(2)

// test failure (car not found)
const carId = payload.booking!.car
payload.booking!.car = testHelper.GetRandromObjectIdAsString()
res = await request(app)
.post('/api/checkout')
.send(payload)
expect(res.statusCode).toBe(400)
payload.booking!.car = carId
} catch (err) {
console.error(err)
} finally {
Expand All @@ -301,7 +382,17 @@ describe('POST /api/checkout', () => {
const booking = await Booking.findById(bookingId)
expect(booking?.status).toBe(bookcarsTypes.BookingStatus.Void)
expect(booking?.sessionId).toBe(payload.sessionId)
payload.payLater = true

// test success (checkout session driver not verified)
driver = await User.findById(DRIVER1_ID)
driver!.verified = false
await driver!.save()
res = await request(app)
.post('/api/checkout')
.send(payload)
expect(res.statusCode).toBe(200)
driver!.verified = true
await driver!.save()

// test success (checkout session with no additional driver)
payload.booking!.additionalDriver = false
Expand All @@ -313,6 +404,7 @@ describe('POST /api/checkout', () => {
expect(bookings.length).toBeGreaterThan(3)
payload.booking!.additionalDriver = true

payload.payLater = true
payload.driver = {
fullName: 'driver',
email: testHelper.GetRandomEmail(),
Expand Down Expand Up @@ -430,6 +522,19 @@ describe('POST /api/update-booking', () => {
expect(additionalDriver).not.toBeNull()
expect(additionalDriver?.fullName).toBe(ADDITIONAL_DRIVER.fullName)

// test success (enableEmailNotifications disabled)
payload.booking.status = bookcarsTypes.BookingStatus.Reserved
const driver = await User.findById(DRIVER1_ID)
driver!.enableEmailNotifications = false
await driver!.save()
res = await request(app)
.put('/api/update-booking')
.set(env.X_ACCESS_TOKEN, token)
.send(payload)
expect(res.statusCode).toBe(200)
driver!.enableEmailNotifications = true
await driver!.save()

const booking = await Booking.findById(BOOKING_ID)
expect(booking).not.toBeNull()
booking!._additionalDriver = testHelper.GetRandromObjectId()
Expand Down Expand Up @@ -774,6 +879,11 @@ describe('DELETE /api/delete-temp-booking', () => {
const _driver = await User.findById(driver._id)
expect(_driver).toBeNull()

// test booking not found
res = await request(app)
.delete(`/api/delete-temp-booking/${testHelper.GetRandromObjectIdAsString()}/${sessionId}`)
expect(res.statusCode).toBe(200)

//
// Test failure
//
Expand Down
11 changes: 11 additions & 0 deletions api/tests/testHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ export const initialize = async () => {
expect(admin.id).toBeDefined()
ADMIN_USER_ID = admin.id

const adminFromEnv = env.ADMIN_EMAIL && await User.findOne({ email: env.ADMIN_EMAIL })
if (!adminFromEnv) {
await (new User({
fullName: 'admin',
email: env.ADMIN_EMAIL,
language: LANGUAGE,
password: passwordHash,
type: bookcarsTypes.UserType.Admin,
})).save()
}

// user
const user = new User({
fullName: USER_FULL_NAME,
Expand Down
2 changes: 1 addition & 1 deletion packages/bookcars-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export interface CheckoutPayload {
driver?: User
booking?: Booking
additionalDriver?: AdditionalDriver
payLater?: boolean
payLater: boolean
sessionId?: string
paymentIntentId?: string
customerId?: string
Expand Down

0 comments on commit 3fe0787

Please sign in to comment.