diff --git a/api/coverage/cobertura-coverage.xml b/api/coverage/cobertura-coverage.xml index c93fd7fc..e12c3f65 100644 --- a/api/coverage/cobertura-coverage.xml +++ b/api/coverage/cobertura-coverage.xml @@ -1,6 +1,6 @@ - + C:\dev\movinin\src\api @@ -53,14 +53,14 @@ - + - + - + - + @@ -70,8 +70,8 @@ - - + + @@ -127,14 +127,14 @@ - + - + - + - + @@ -176,16 +176,16 @@ - - - - + + + + - - - + + + - + @@ -195,31 +195,31 @@ - + - + - + - + - + - + - - - - - + + + + + - + @@ -320,7 +320,7 @@ - + @@ -459,91 +459,91 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -557,23 +557,23 @@ - - - - - - - + + + + + + + - - - + + + - - - - + + + + @@ -584,202 +584,193 @@ - - + + - - - - - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1992,23 +1983,23 @@ - - - - - - - - - - - - + + + + + + + + + + + - - - + + + + @@ -2043,45 +2034,45 @@ - + - + - + - + - + - + - + - + - + - + - - - - + + + + - + diff --git a/api/src/controllers/bookingController.ts b/api/src/controllers/bookingController.ts index f851a6e5..7389d748 100644 --- a/api/src/controllers/bookingController.ts +++ b/api/src/controllers/bookingController.ts @@ -132,11 +132,8 @@ export const checkout = async (req: Request, res: Response) => { return res.sendStatus(204) } - let language = env.DEFAULT_LANGUAGE - if (user.language) { - language = user.language - i18n.locale = user.language - } + const { language } = user + i18n.locale = language const booking = new Booking(body.booking) @@ -218,9 +215,7 @@ const notifyRenter = async (booking: env.Booking) => { return } - if (renter.language) { - i18n.locale = renter.language - } + i18n.locale = renter.language const message = `${i18n.t('BOOKING_UPDATED_NOTIFICATION_PART1')} ${booking._id} ${i18n.t('BOOKING_UPDATED_NOTIFICATION_PART2')}` const notification = new Notification({ @@ -454,34 +449,20 @@ export const getBooking = async (req: Request, res: Response) => { if (booking) { const { language } = req.params - if (booking.agency) { - const { - _id, - fullName, - avatar, - payLater, - } = booking.agency - booking.agency = { - _id, - fullName, - avatar, - payLater, - } + booking.agency = { + _id: booking.agency._id, + fullName: booking.agency.fullName, + avatar: booking.agency.avatar, + payLater: booking.agency.payLater, } - if (booking.property.agency) { - const { - _id, - fullName, - avatar, - payLater, - } = booking.property.agency - booking.property.agency = { - _id, - fullName, - avatar, - payLater, - } + + booking.property.agency = { + _id: booking.property.agency._id, + fullName: booking.property.agency.fullName, + avatar: booking.property.agency.avatar, + payLater: booking.property.agency.payLater, } + booking.location.name = booking.location.values.filter((value) => value.language === language)[0].value return res.json(booking) } @@ -523,44 +504,43 @@ export const getBookings = async (req: Request, res: Response) => { const $match: mongoose.FilterQuery = { $and: [{ 'agency._id': { $in: agencies } }, { status: { $in: statuses } }], } - if ($match.$and) { - if (user) { - $match.$and.push({ - 'renter._id': { $eq: new mongoose.Types.ObjectId(user) }, + + if (user) { + $match.$and!.push({ + 'renter._id': { $eq: new mongoose.Types.ObjectId(user) }, + }) + } + if (property) { + $match.$and!.push({ + 'property._id': { $eq: new mongoose.Types.ObjectId(property) }, + }) + } + if (location) { + $match.$and!.push({ 'location._id': { $eq: new mongoose.Types.ObjectId(location) } }) + } + if (from) { + $match.$and!.push({ from: { $gte: from } }) + } // $from > from + if (to) { + $match.$and!.push({ to: { $lte: to } }) + } // $to < to + if (keyword) { + const isObjectId = helper.isValidObjectId(keyword) + if (isObjectId) { + $match.$and!.push({ + _id: { $eq: new mongoose.Types.ObjectId(keyword) }, }) - } - if (property) { - $match.$and.push({ - 'property._id': { $eq: new mongoose.Types.ObjectId(property) }, + } else { + keyword = escapeStringRegexp(keyword) + $match.$and!.push({ + $or: [ + { 'agency.fullName': { $regex: keyword, $options: options } }, + { 'renter.fullName': { $regex: keyword, $options: options } }, + { 'property.name': { $regex: keyword, $options: options } }, + { 'location.name': { $regex: keyword, $options: options } }, + ], }) } - if (location) { - $match.$and.push({ 'location._id': { $eq: new mongoose.Types.ObjectId(location) } }) - } - if (from) { - $match.$and.push({ from: { $gte: from } }) - } // $from > from - if (to) { - $match.$and.push({ to: { $lte: to } }) - } // $to < to - if (keyword) { - const isObjectId = helper.isValidObjectId(keyword) - if (isObjectId) { - $match.$and.push({ - _id: { $eq: new mongoose.Types.ObjectId(keyword) }, - }) - } else { - keyword = escapeStringRegexp(keyword) - $match.$and.push({ - $or: [ - { 'agency.fullName': { $regex: keyword, $options: options } }, - { 'renter.fullName': { $regex: keyword, $options: options } }, - { 'property.name': { $regex: keyword, $options: options } }, - { 'location.name': { $regex: keyword, $options: options } }, - ], - }) - } - } } const { language } = req.params @@ -662,13 +642,11 @@ export const getBookings = async (req: Request, res: Response) => { }, ]) - if (data.length > 0) { - const bookings: env.BookingInfo[] = data[0].resultData + const bookings: env.BookingInfo[] = data[0].resultData - for (const booking of bookings) { - const { _id, fullName, avatar } = booking.agency - booking.agency = { _id, fullName, avatar } - } + for (const booking of bookings) { + const { _id, fullName, avatar } = booking.agency + booking.agency = { _id, fullName, avatar } } return res.json(data) diff --git a/api/src/controllers/userController.ts b/api/src/controllers/userController.ts index 12c5a4a5..e4d736ef 100644 --- a/api/src/controllers/userController.ts +++ b/api/src/controllers/userController.ts @@ -1268,6 +1268,7 @@ export const deleteUsers = async (req: Request, res: Response) => { const properties = await Property.find({ agency: id }) await Property.deleteMany({ agency: id }) for (const property of properties) { + // delete main image if (property.image) { const image = path.join(env.CDN_PROPERTIES, property.image) if (await helper.exists(image)) { diff --git a/api/tests/booking.test.ts b/api/tests/booking.test.ts index f5566a43..1ed81a1f 100644 --- a/api/tests/booking.test.ts +++ b/api/tests/booking.test.ts @@ -157,6 +157,16 @@ describe('POST /api/checkout', () => { bookings = await Booking.find({ renter: RENTER1_ID }) expect(bookings.length).toBeGreaterThan(1) + const renter = await User.findOne({ _id: RENTER1_ID }) + renter!.language = 'fr' + await renter?.save() + res = await request(app) + .post('/api/checkout') + .send(payload) + expect(res.statusCode).toBe(200) + bookings = await Booking.find({ renter: RENTER1_ID }) + expect(bookings.length).toBeGreaterThan(2) + payload.booking.agency = testHelper.GetRandromObjectIdAsString() res = await request(app) .post('/api/checkout') @@ -299,7 +309,15 @@ describe('POST /api/update-booking-status', () => { .set(env.X_ACCESS_TOKEN, token) .send(payload) expect(res.statusCode).toBe(200) - const booking = await Booking.findById(BOOKING_ID) + let booking = await Booking.findById(BOOKING_ID) + expect(booking?.status).toBe(movininTypes.BookingStatus.Reserved) + + res = await request(app) + .post('/api/update-booking-status') + .set(env.X_ACCESS_TOKEN, token) + .send(payload) + expect(res.statusCode).toBe(200) + booking = await Booking.findById(BOOKING_ID) expect(booking?.status).toBe(movininTypes.BookingStatus.Reserved) res = await request(app) @@ -358,6 +376,19 @@ describe('POST /api/bookings/:page/:size/:language', () => { expect(res.statusCode).toBe(200) expect(res.body[0].resultData.length).toBe(1) + payload.user = undefined + payload.property = undefined + payload.filter!.from = undefined + payload.filter!.to = undefined + payload.filter!.location = undefined + payload.filter!.keyword = undefined + res = await request(app) + .post(`/api/bookings/${testHelper.PAGE}/${testHelper.SIZE}/${testHelper.LANGUAGE}`) + .set(env.X_ACCESS_TOKEN, token) + .send(payload) + expect(res.statusCode).toBe(200) + expect(res.body[0].resultData.length).toBe(1) + payload.filter!.keyword = BOOKING_ID res = await request(app) .post(`/api/bookings/${testHelper.PAGE}/${testHelper.SIZE}/${testHelper.LANGUAGE}`) @@ -441,7 +472,7 @@ describe('DELETE /api/delete-bookings', () => { .set(env.X_ACCESS_TOKEN, token) .send(payload) expect(res.statusCode).toBe(200) - bookings = await Booking.find({ driver: { $in: renters } }) + bookings = await Booking.find({ renter: { $in: renters } }) expect(bookings.length).toBe(0) res = await request(app)