Skip to content

Commit eeff45d

Browse files
committed
--update: save booking
1 parent 4a49d05 commit eeff45d

File tree

5 files changed

+113
-14
lines changed

5 files changed

+113
-14
lines changed

apps/api/src/app.controller.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { NewAmenityDto } from './dtos/new-amenity.dto';
2525
import { NewRoomTypeDto } from './dtos/new-RoomType.dto';
2626
import { FileFieldsInterceptor } from '@nestjs/platform-express';
2727
import { NewRoomDto } from './dtos/new-room.dto';
28+
import { BookingDto } from './dtos/booking.dto';
2829

2930
@Controller()
3031
export class AppController {
@@ -166,4 +167,46 @@ export class AppController {
166167
},
167168
);
168169
}
170+
@Post('bookings')
171+
async booking(@Body() bookingDto: BookingDto) {
172+
const {
173+
firstName,
174+
lastName,
175+
phone,
176+
email,
177+
country,
178+
address,
179+
city,
180+
zipCode,
181+
note,
182+
start,
183+
end,
184+
adults,
185+
children,
186+
fee,
187+
totalPrice,
188+
rooms,
189+
} = bookingDto;
190+
return this.bookingService.send(
191+
{ cmd: 'booking' },
192+
{
193+
firstName,
194+
lastName,
195+
phone,
196+
email,
197+
country,
198+
address,
199+
city,
200+
zipCode,
201+
note,
202+
start,
203+
end,
204+
adults,
205+
children,
206+
fee,
207+
totalPrice,
208+
rooms,
209+
},
210+
);
211+
}
169212
}

apps/api/src/dtos/booking.dto.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { IsArray, IsEmail, IsEmpty, IsNotEmpty } from 'class-validator';
2+
3+
export class BookingDto {
4+
@IsNotEmpty()
5+
firstName: string;
6+
@IsNotEmpty()
7+
lastName: string;
8+
@IsNotEmpty()
9+
phone: string;
10+
@IsNotEmpty()
11+
@IsEmail()
12+
email: string;
13+
@IsNotEmpty()
14+
country: string;
15+
@IsNotEmpty()
16+
address: string;
17+
@IsNotEmpty()
18+
city: string;
19+
@IsNotEmpty()
20+
zipCode: string;
21+
note?: string;
22+
@IsNotEmpty()
23+
start: string;
24+
@IsNotEmpty()
25+
end: string;
26+
@IsNotEmpty()
27+
adults: number;
28+
@IsNotEmpty()
29+
children: number;
30+
@IsNotEmpty()
31+
fee: number;
32+
@IsNotEmpty()
33+
totalPrice: number;
34+
@IsNotEmpty()
35+
@IsArray()
36+
rooms: string[];
37+
}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { Controller, Get, Inject } from '@nestjs/common';
22
import { BookingService } from './booking.service';
33
import { SharedService } from '@app/shared';
4-
import { Ctx, MessagePattern, RmqContext } from '@nestjs/microservices';
4+
import {
5+
Ctx,
6+
MessagePattern,
7+
Payload,
8+
RmqContext,
9+
} from '@nestjs/microservices';
510

611
@Controller()
712
export class BookingController {
@@ -10,12 +15,9 @@ export class BookingController {
1015
@Inject('SharedServiceInterface')
1116
private readonly sharedService: SharedService,
1217
) {}
13-
@MessagePattern({ cmd: 'search-room-for-booking' })
14-
async searchRoomForBooking(
15-
@Ctx() context: RmqContext,
16-
payload: { start: string; end: string },
17-
): Promise<any> {
18+
@MessagePattern({ cmd: 'booking' })
19+
async booking(@Ctx() context: RmqContext, @Payload() payload) {
1820
this.sharedService.acknowledgeMessage(context);
19-
return this.bookingService.searchRoomForBooking(payload);
21+
return this.bookingService.booking(payload);
2022
}
2123
}

apps/booking/src/booking.service.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ConfigService } from '@nestjs/config';
33
import { RoomRepositoryInterface } from '@app/shared/interfaces/room.repository.interface';
44

55
import { BookingRepositoryInterface } from '@app/shared/interfaces/booking.repository.interface';
6+
const moment = require('moment');
67

78
@Injectable()
89
export class BookingService {
@@ -13,5 +14,19 @@ export class BookingService {
1314
@Inject('BookingRepositoryInterface')
1415
private readonly BookingRepository: BookingRepositoryInterface,
1516
) {}
16-
async searchRoomForBooking(payload) {}
17+
async booking(payload) {
18+
const formatStartToDate = moment(payload.start, 'DD-MM-YYYY').toDate();
19+
const formatEndToDate = moment(payload.end, 'DD-MM-YYYY').toDate();
20+
const { start, end, ...others } = payload;
21+
try {
22+
const saveBooking = await this.BookingRepository.create({
23+
start: formatStartToDate,
24+
end: formatEndToDate,
25+
...others,
26+
});
27+
return { status: 'Đặt phòng thành công', code: 200 };
28+
} catch (e) {
29+
return e;
30+
}
31+
}
1732
}

libs/shared/src/schemas/booking.schema.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
1010
},
1111
})
1212
export class Booking extends BaseEntity {
13-
@Prop({ type: String, required: true })
14-
name: string;
1513
@Prop({ type: String, required: true })
1614
firstName: string;
1715
@Prop({ type: String, required: true })
16+
lastName: string;
17+
@Prop({ type: String, required: true })
1818
phone: string;
1919
@Prop({
2020
type: String,
@@ -30,14 +30,16 @@ export class Booking extends BaseEntity {
3030
city: string;
3131
@Prop({ type: String, required: true })
3232
zipCode: string;
33-
@Prop({ type: String, required: true })
33+
@Prop({ type: String })
3434
note: string;
3535
@Prop({ type: Date, required: true })
36-
check_in: Date;
36+
start: Date;
3737
@Prop({ type: Date, required: true })
38-
check_out: Date;
38+
end: Date;
39+
@Prop({ type: Number, required: true })
40+
adults: number;
3941
@Prop({ type: Number, required: true })
40-
slot: number;
42+
children: number;
4143
@Prop({ type: Number, required: true })
4244
fee: number;
4345
@Prop({ type: Number, required: true })

0 commit comments

Comments
 (0)