Sửa hiển thị điểm đầu và điểm cuối của chặng
This commit is contained in:
@@ -207,6 +207,51 @@ class TourController {
|
||||
});
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post(':tourId/legs/batch')
|
||||
async initializeLegs(@Param('tourId', ParseUUIDPipe) tourId: string, @Body() body: { count: number }) {
|
||||
const { count } = body;
|
||||
if (count <= 0 || count > 20) throw new BadRequestException('Số lượng chặng không hợp lệ (1-20)');
|
||||
|
||||
// 1. Lấy danh sách chặng hiện có
|
||||
const existingLegs = await this.prisma.leg.findMany({
|
||||
where: { tourId },
|
||||
orderBy: { sequence: 'asc' }
|
||||
});
|
||||
|
||||
// 2. Tạo thêm chặng nếu số lượng hiện tại chưa đủ 'count'
|
||||
const needed = count - existingLegs.length;
|
||||
if (needed > 0) {
|
||||
const createData = Array.from({ length: needed }).map((_, i) => ({
|
||||
tourId,
|
||||
sequence: existingLegs.length + i + 1,
|
||||
note: `Chặng ${existingLegs.length + i + 1}`
|
||||
}));
|
||||
await this.prisma.leg.createMany({ data: createData });
|
||||
}
|
||||
|
||||
// 3. Lấy chặng cuối cùng sau khi đã cập nhật
|
||||
const allLegs = await this.prisma.leg.findMany({
|
||||
where: { tourId },
|
||||
orderBy: { sequence: 'asc' }
|
||||
});
|
||||
const lastLeg = allLegs[allLegs.length - 1];
|
||||
|
||||
// 4. Tự động di chuyển Điểm kết thúc sang Chặng cuối cùng (nếu đã khai báo điểm kết thúc)
|
||||
const endPoint = await this.prisma.location.findFirst({
|
||||
where: { leg: { tourId }, plannedEnd: new Date(0) }
|
||||
});
|
||||
|
||||
if (endPoint && lastLeg && endPoint.legId !== lastLeg.id) {
|
||||
await this.prisma.location.update({
|
||||
where: { id: endPoint.id },
|
||||
data: { legId: lastLeg.id }
|
||||
});
|
||||
}
|
||||
|
||||
return allLegs;
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post(':tourId/legs')
|
||||
async addLeg(@Param('tourId', ParseUUIDPipe) tourId: string, @Body() body: any) {
|
||||
|
||||
Reference in New Issue
Block a user