Skip to content

Commit 08e235f

Browse files
author
Valera Rozuvan
authored
[be] Add booking_price end point. (windingtree#113)
1 parent 11c5582 commit 08e235f

File tree

12 files changed

+148
-3
lines changed

12 files changed

+148
-3
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { HotelRepo } from '../../data/hotel/HotelRepo'
2+
import { calculateOfferPrice } from '../../app/rate_modifier'
3+
4+
import { RoomTypeRepo } from '../../data/room_type/RoomTypeRepo'
5+
6+
import { CONSTANTS } from '../../common/constants'
7+
import { IHotel, IRoomType, IProfile, IGetBookingPricePayload, IBookingPrice } from '../../common/types'
8+
9+
const { SUPER_ADMIN } = CONSTANTS.PROFILE_ROLE
10+
11+
const hotelRepo = new HotelRepo()
12+
const roomTypeRepo = new RoomTypeRepo()
13+
14+
async function getBookingPrice(requester: IProfile, data: IGetBookingPricePayload): Promise<IBookingPrice> {
15+
let hotel: IHotel
16+
17+
if (requester.role === SUPER_ADMIN) {
18+
hotel = await hotelRepo.readHotel(data.hotelId)
19+
} else {
20+
hotel = await hotelRepo.readHotelByOwnerId(data.hotelId, requester.id)
21+
}
22+
23+
let roomType: IRoomType
24+
25+
if (requester.role === SUPER_ADMIN) {
26+
roomType = await roomTypeRepo.readRoomType(data.roomTypeId)
27+
} else {
28+
roomType = await roomTypeRepo.readRoomTypeByHotelId(data.roomTypeId, requester.hotelId)
29+
}
30+
31+
const price = await calculateOfferPrice(hotel, roomType, data.arrival, data.departure, roomType.price)
32+
33+
return { price }
34+
}
35+
36+
export { getBookingPrice }

api/_lib/app/booking/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export { deleteBooking } from './deleteBooking'
33
export { getAllBookings } from './getAllBookings'
44
export { getBooking } from './getBooking'
55
export { updateBooking } from './updateBooking'
6+
export { getBookingPrice } from './getBookingPrice'

api/_lib/app/orgid/offerSearch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
ILocationRectangleDbType,
1919
IOrgDetails
2020
} from '../../common/types'
21-
import { calculateOfferPrice } from "../rate_modifier/pricingEngine";
21+
import { calculateOfferPrice } from "../../app/rate_modifier";
2222

2323
const moment = extendMoment(Moment)
2424

api/_lib/app/rate_modifier/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export { deleteRateModifier } from './deleteRateModifier'
33
export { getAllRateModifiers } from './getAllRateModifiers'
44
export { getRateModifier } from './getRateModifier'
55
export { updateRateModifier } from './updateRateModifier'
6+
export { calculateOfferPrice } from './pricingEngine'

api/_lib/common/types/api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
IRateModifier,
2020
IRateModifierCollection,
2121
IUploadImage,
22+
IBookingPrice,
2223
} from '../types'
2324

2425
type TAvailableResultTypes =
@@ -40,6 +41,7 @@ type TAvailableResultTypes =
4041
|IRateModifier
4142
|IRateModifierCollection
4243
|IUploadImage
44+
|IBookingPrice
4345
|string
4446

4547
type TMethodFunc = (request: NowRequest, response: NowResponse) => Promise<TAvailableResultTypes>

api/_lib/common/types/booking.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ interface IPatchBookingPayloadDbData {
9191

9292
type IBookingCollectionDbData = Array<IBookingDbData>
9393

94+
interface IBookingPrice {
95+
price: number
96+
}
97+
98+
interface IGetBookingPricePayload {
99+
hotelId: string
100+
roomTypeId: string
101+
arrival: string,
102+
departure: string
103+
}
104+
94105
export {
95106
TBookingDbDataFields,
96107
IBookingDbDataProjection,
@@ -103,4 +114,6 @@ export {
103114
IBookingDbData,
104115
IPatchBookingPayloadDbData,
105116
IBookingCollectionDbData,
117+
IBookingPrice,
118+
IGetBookingPricePayload,
106119
}

api/_lib/common/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export {
3838
IBookingDbData,
3939
IPatchBookingPayloadDbData,
4040
IBookingCollectionDbData,
41+
IBookingPrice,
42+
IGetBookingPricePayload,
4143
} from './booking'
4244

4345
export {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { NowRequest } from '@vercel/node'
2+
3+
import { validateRequiredString } from '../../../interface/validators/_helpers'
4+
5+
import { CONSTANTS } from '../../../common/constants'
6+
import { CError } from '../../../common/tools'
7+
import { IGetBookingPricePayload } from '../../../common/types'
8+
9+
const { BAD_REQUEST } = CONSTANTS.HTTP_STATUS
10+
11+
async function getBookingPricePayloadValidator(request: NowRequest): Promise<IGetBookingPricePayload> {
12+
if (!request.body) {
13+
throw new CError(BAD_REQUEST, 'Must provide a valid body with request.')
14+
}
15+
16+
const payload: IGetBookingPricePayload = {
17+
hotelId: '',
18+
roomTypeId: '',
19+
arrival: '',
20+
departure: '',
21+
}
22+
23+
const ALLOWED_PROPS: Array<keyof IGetBookingPricePayload> = [
24+
'hotelId',
25+
'roomTypeId',
26+
'arrival',
27+
'departure',
28+
]
29+
30+
for (const [key] of Object.entries(request.body)) {
31+
if (!ALLOWED_PROPS.includes(key as keyof IGetBookingPricePayload)) {
32+
throw new CError(BAD_REQUEST, `Property '${key}' on 'GET /booking_price' is not allowed.`)
33+
}
34+
}
35+
36+
const hotelId = request.body.hotelId
37+
await validateRequiredString('hotelId', hotelId)
38+
payload.hotelId = hotelId
39+
40+
const roomTypeId = request.body.roomTypeId
41+
await validateRequiredString('roomTypeId', roomTypeId)
42+
payload.roomTypeId = roomTypeId
43+
44+
const arrival = request.body.arrival
45+
await validateRequiredString('arrival', arrival)
46+
payload.arrival = arrival
47+
48+
const departure = request.body.departure
49+
await validateRequiredString('departure', departure)
50+
payload.departure = departure
51+
52+
return payload
53+
}
54+
55+
export { getBookingPricePayloadValidator }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { postBookingPayloadValidator } from './postBookingPayloadValidator'
22
export { patchBookingPayloadValidator } from './patchBookingPayloadValidator'
3+
export { getBookingPricePayloadValidator } from './getBookingPricePayloadValidator'
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
export { postProfilePayloadValidator, patchProfilePayloadValidator } from './Profile'
22
export { postHotelPayloadValidator, patchHotelPayloadValidator } from './Hotel'
33
export { patchRoomTypePayloadValidator, postRoomTypePayloadValidator } from './RoomType'
4-
export { patchBookingPayloadValidator, postBookingPayloadValidator } from './Booking'
4+
export {
5+
patchBookingPayloadValidator,
6+
postBookingPayloadValidator,
7+
getBookingPricePayloadValidator,
8+
} from './Booking'
59
export { postLoginPayloadValidator } from './Login'
610
export { postOneTimePasswordPayloadValidator } from './OneTimePassword'
711
export { postCreateOrderPayloadValidator } from './Order'
812
export { postJwtPayloadValidator } from './Jwt'
9-
export { patchRateModifierPayloadValidator, postRateModifierPayloadValidator } from './RateModifier'
13+
export {
14+
patchRateModifierPayloadValidator,
15+
postRateModifierPayloadValidator,
16+
} from './RateModifier'
1017
export { postUploadImagePayloadValidator } from './UploadImage'

0 commit comments

Comments
 (0)