fix: thêm thành viên ngoài hệ thống vào Tour
This commit is contained in:
+81
-16
@@ -531,7 +531,7 @@ class TourController {
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post()
|
||||
async createTour(@Body() body: any, @Req() req: any) {
|
||||
const { title, description, startDate, endDate, adultCount, childCount, childDiscount, tags } = body;
|
||||
const { title, description, startDate, endDate, adultCount, childCount, childDiscount, tags, members } = body;
|
||||
const tour = await this.prisma.tour.create({
|
||||
data: {
|
||||
title,
|
||||
@@ -544,10 +544,19 @@ class TourController {
|
||||
childDiscount: childDiscount || 0,
|
||||
createdById: req.user.id,
|
||||
participants: {
|
||||
create: {
|
||||
userId: req.user.id,
|
||||
role: 'OWNER'
|
||||
}
|
||||
create: [
|
||||
{
|
||||
userId: req.user.id,
|
||||
role: 'OWNER'
|
||||
},
|
||||
...(members && Array.isArray(members)
|
||||
? members.map((m: any) => ({
|
||||
userId: m.userId || null,
|
||||
displayName: m.displayName || null,
|
||||
role: m.role || 'MEMBER'
|
||||
}))
|
||||
: [])
|
||||
]
|
||||
},
|
||||
legs: {
|
||||
create: {
|
||||
@@ -594,6 +603,13 @@ class TourController {
|
||||
}
|
||||
}).then(async (loc) => {
|
||||
if (body.expenseAmount && Number(body.expenseAmount) > 0) {
|
||||
let paidById = body.paidById || null;
|
||||
if (paidById) {
|
||||
const userExists = await this.prisma.user.findUnique({ where: { id: paidById } });
|
||||
if (!userExists) {
|
||||
paidById = null;
|
||||
}
|
||||
}
|
||||
await this.prisma.expense.create({
|
||||
data: {
|
||||
amount: Number(body.expenseAmount),
|
||||
@@ -602,7 +618,7 @@ class TourController {
|
||||
legId: loc.legId,
|
||||
description: body.expenseDescription || `Chi phí tại ${loc.name}`,
|
||||
note: body.expenseNote || null,
|
||||
paidById: body.paidById || null,
|
||||
paidById,
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -832,7 +848,9 @@ class TourController {
|
||||
// --- BẮT ĐẦU DỌN DẸP CACHE ---
|
||||
// a. Xóa cache vai trò của tất cả thành viên trong tour này
|
||||
for (const participant of tour.participants) {
|
||||
await this.cacheManager.del(`user-role:${participant.userId}:${id}`);
|
||||
if (participant.userId) {
|
||||
await this.cacheManager.del(`user-role:${participant.userId}:${id}`);
|
||||
}
|
||||
}
|
||||
|
||||
// b. Xóa cache mapping tài nguyên (Leg, Location, Photo) về Tour này
|
||||
@@ -961,10 +979,31 @@ class TourController {
|
||||
@Roles(ParticipantRole.OWNER, ParticipantRole.MANAGER) // Only owner/manager can add members
|
||||
@UseGuards(JwtAuthGuard, TourRoleGuard)
|
||||
@Post(':tourId/members')
|
||||
async addMember(@Param('tourId', ParseUUIDPipe) tourId: string, @Body() body: { userId: string; role?: string }, @Req() req: any) {
|
||||
async addMember(@Param('tourId', ParseUUIDPipe) tourId: string, @Body() body: { userId?: string; displayName?: string; role?: string }, @Req() req: any) {
|
||||
const validRoles = ['OWNER', 'MANAGER', 'MEMBER', 'MEMBER_NO_FINANCE', 'VIEWER_ONLY'] as const;
|
||||
const role = validRoles.includes(body.role as any) ? body.role as 'OWNER' | 'MANAGER' | 'MEMBER' | 'MEMBER_NO_FINANCE' | 'VIEWER_ONLY' : 'MEMBER';
|
||||
|
||||
if (body.displayName) {
|
||||
const participation = await this.prisma.tourParticipant.create({
|
||||
data: {
|
||||
tourId,
|
||||
role,
|
||||
displayName: body.displayName,
|
||||
},
|
||||
include: { user: { select: { id: true, name: true, email: true } } },
|
||||
});
|
||||
await Promise.all([
|
||||
this.cacheManager.del(tourId),
|
||||
this.cacheManager.del(`/api/v1/tours/${tourId}`),
|
||||
this.cacheManager.del(`/api/v1/tours/${tourId}/public`),
|
||||
]);
|
||||
return participation;
|
||||
}
|
||||
|
||||
if (!body.userId) {
|
||||
throw new BadRequestException('Vui lòng cung cấp userId hoặc displayName');
|
||||
}
|
||||
|
||||
// Xóa cache khi thay đổi quyền hạn hoặc thêm thành viên mới
|
||||
await this.cacheManager.del(`user-role:${body.userId}:${tourId}`);
|
||||
|
||||
@@ -1021,7 +1060,19 @@ class TourController {
|
||||
@Body() body: { adultCount?: number; childCount?: number; role?: string },
|
||||
@Req() req: any
|
||||
) {
|
||||
const isSelf = req.user.id === userId;
|
||||
const participant = await this.prisma.tourParticipant.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ id: userId },
|
||||
{ tourId, userId }
|
||||
]
|
||||
}
|
||||
});
|
||||
if (!participant) {
|
||||
throw new NotFoundException('Không tìm thấy thành viên trong tour.');
|
||||
}
|
||||
|
||||
const isSelf = req.user.id === participant.userId;
|
||||
|
||||
// Tìm quyền hạn của người gửi yêu cầu trong tour này
|
||||
const requesterParticipation = await this.prisma.tourParticipant.findUnique({
|
||||
@@ -1050,15 +1101,17 @@ class TourController {
|
||||
}
|
||||
|
||||
// Xóa cache liên quan
|
||||
if (participant.userId) {
|
||||
await this.cacheManager.del(`user-role:${participant.userId}:${tourId}`);
|
||||
}
|
||||
await Promise.all([
|
||||
this.cacheManager.del(`user-role:${userId}:${tourId}`),
|
||||
this.cacheManager.del(tourId),
|
||||
this.cacheManager.del(`/api/v1/tours/${tourId}`),
|
||||
this.cacheManager.del(`/api/v1/tours/${tourId}/public`),
|
||||
]);
|
||||
|
||||
return this.prisma.tourParticipant.update({
|
||||
where: { tourId_userId: { tourId, userId } },
|
||||
where: { id: participant.id },
|
||||
data,
|
||||
include: { user: { select: { id: true, name: true, email: true } } },
|
||||
});
|
||||
@@ -1208,17 +1261,29 @@ class TourController {
|
||||
@UseGuards(JwtAuthGuard, TourRoleGuard)
|
||||
@Delete(':tourId/members/:userId')
|
||||
async removeMember(@Param('tourId', ParseUUIDPipe) tourId: string, @Param('userId', ParseUUIDPipe) userId: string) {
|
||||
const participation = await this.prisma.tourParticipant.findUnique({
|
||||
where: { tourId_userId: { tourId, userId } },
|
||||
const participant = await this.prisma.tourParticipant.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ id: userId },
|
||||
{ tourId, userId }
|
||||
]
|
||||
}
|
||||
});
|
||||
if (!participation) {
|
||||
if (!participant) {
|
||||
throw new NotFoundException('Thành viên này không có trong tour');
|
||||
}
|
||||
|
||||
await this.cacheManager.del(`user-role:${userId}:${tourId}`);
|
||||
if (participant.userId) {
|
||||
await this.cacheManager.del(`user-role:${participant.userId}:${tourId}`);
|
||||
}
|
||||
await Promise.all([
|
||||
this.cacheManager.del(tourId),
|
||||
this.cacheManager.del(`/api/v1/tours/${tourId}`),
|
||||
this.cacheManager.del(`/api/v1/tours/${tourId}/public`),
|
||||
]);
|
||||
|
||||
await this.prisma.tourParticipant.delete({
|
||||
where: { tourId_userId: { tourId, userId } },
|
||||
where: { id: participant.id },
|
||||
});
|
||||
return { message: 'Đã xóa thành viên khỏi tour' };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user