fix email sender all flow
This commit is contained in:
@@ -458,7 +458,14 @@ export const tripService = {
|
||||
return runSerializable(async (tx) => {
|
||||
const trip = await tx.trip.findUnique({
|
||||
where: { id: tripId },
|
||||
select: { id: true, status: true, organizerId: true, date: true },
|
||||
select: {
|
||||
id: true,
|
||||
status: true,
|
||||
organizerId: true,
|
||||
date: true,
|
||||
title: true,
|
||||
organizer: { select: { id: true, email: true, name: true } },
|
||||
},
|
||||
});
|
||||
if (!trip) {
|
||||
throw new Error("Trip tidak ditemukan");
|
||||
@@ -500,6 +507,8 @@ export const tripService = {
|
||||
const refundsCreated: string[] = [];
|
||||
const cancelledBookings: string[] = [];
|
||||
const skippedBookings: string[] = [];
|
||||
// userId → nominal refund yang dibuat untuk dia (untuk email pembatalan).
|
||||
const refundByUser = new Map<string, number>();
|
||||
|
||||
for (const b of bookings) {
|
||||
if (b.status === "CANCELLED" || b.status === "EXPIRED") {
|
||||
@@ -540,6 +549,7 @@ export const tripService = {
|
||||
}
|
||||
);
|
||||
refundsCreated.push(refund.id);
|
||||
refundByUser.set(b.userId, remaining);
|
||||
|
||||
// Trip dibatalkan → cancel payout HELD/RELEASED organizer untuk
|
||||
// booking ini. Payout PAID di-flag clawback otomatis.
|
||||
@@ -560,6 +570,12 @@ export const tripService = {
|
||||
}
|
||||
}
|
||||
|
||||
// Snapshot peserta aktif SEBELUM di-cancel — untuk notifikasi email.
|
||||
const activeParticipants = await tx.tripParticipant.findMany({
|
||||
where: { tripId, status: { not: "CANCELLED" } },
|
||||
select: { user: { select: { id: true, email: true, name: true } } },
|
||||
});
|
||||
|
||||
// Semua participant aktif → CANCELLED (apapun status booking-nya).
|
||||
await tx.tripParticipant.updateMany({
|
||||
where: { tripId, status: { not: "CANCELLED" } },
|
||||
@@ -586,6 +602,17 @@ export const tripService = {
|
||||
refundsCreated,
|
||||
cancelledBookings,
|
||||
skippedBookings,
|
||||
// Data penerima notifikasi — email dikirim oleh action setelah tx commit.
|
||||
notify: {
|
||||
tripTitle: trip.title,
|
||||
organizer: trip.organizer,
|
||||
participants: activeParticipants.map((p) => ({
|
||||
userId: p.user.id,
|
||||
email: p.user.email,
|
||||
name: p.user.name,
|
||||
refundAmount: refundByUser.get(p.user.id) ?? 0,
|
||||
})),
|
||||
},
|
||||
};
|
||||
}, "Gagal membatalkan trip. Coba lagi sebentar.");
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user