fix: thêm thành viên ngoài hệ thống vào Tour

This commit is contained in:
2026-06-20 17:56:56 +07:00
parent ae97f061e8
commit 52706cab7d
17 changed files with 533 additions and 141 deletions
+77 -15
View File
@@ -555,7 +555,7 @@ let TourController = class TourController {
this.cacheManager = cacheManager;
}
async createTour(body, req) {
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,
@@ -568,10 +568,19 @@ let TourController = 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) => ({
userId: m.userId || null,
displayName: m.displayName || null,
role: m.role || 'MEMBER'
}))
: [])
]
},
legs: {
create: {
@@ -610,6 +619,13 @@ let TourController = 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),
@@ -618,7 +634,7 @@ let TourController = class TourController {
legId: loc.legId,
description: body.expenseDescription || `Chi phí tại ${loc.name}`,
note: body.expenseNote || null,
paidById: body.paidById || null,
paidById,
}
});
}
@@ -794,7 +810,9 @@ let TourController = class TourController {
if (!tour)
throw new common_1.NotFoundException('Không tìm thấy tour');
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}`);
}
}
await this.cacheManager.del(`res-to-tour:${id}`);
for (const leg of tour.legs) {
@@ -898,6 +916,25 @@ let TourController = class TourController {
async addMember(tourId, body, req) {
const validRoles = ['OWNER', 'MANAGER', 'MEMBER', 'MEMBER_NO_FINANCE', 'VIEWER_ONLY'];
const role = validRoles.includes(body.role) ? body.role : '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 common_1.BadRequestException('Vui lòng cung cấp userId hoặc displayName');
}
await this.cacheManager.del(`user-role:${body.userId}:${tourId}`);
const participation = await this.prisma.tourParticipant.findUnique({
where: { tourId_userId: { tourId, userId: body.userId } },
@@ -941,7 +978,18 @@ let TourController = class TourController {
});
}
async updateMemberCounts(tourId, userId, body, req) {
const isSelf = req.user.id === userId;
const participant = await this.prisma.tourParticipant.findFirst({
where: {
OR: [
{ id: userId },
{ tourId, userId }
]
}
});
if (!participant) {
throw new common_1.NotFoundException('Không tìm thấy thành viên trong tour.');
}
const isSelf = req.user.id === participant.userId;
const requesterParticipation = await this.prisma.tourParticipant.findUnique({
where: { tourId_userId: { tourId, userId: req.user.id } },
});
@@ -966,14 +1014,16 @@ let TourController = class TourController {
data.role = body.role;
}
}
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 } } },
});
@@ -1090,15 +1140,27 @@ let TourController = class TourController {
return { success: true, message: 'Đã từ chối yêu cầu tham gia.' };
}
async removeMember(tourId, userId) {
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 common_1.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' };
}
+1 -1
View File
File diff suppressed because one or more lines are too long