email service and template using resend
This commit is contained in:
@@ -6,6 +6,8 @@ import { authOptions } from "@/lib/auth";
|
||||
import { isAdminEmail } from "@/lib/admin";
|
||||
import { userService } from "@/server/services/user.service";
|
||||
import { auditLog } from "@/server/services/audit-log.service";
|
||||
import { emailService } from "@/lib/email/send";
|
||||
import { userRepo } from "@/server/repositories/user.repo";
|
||||
|
||||
export async function suspendUserAction(userId: string, reason: string) {
|
||||
const session = await getServerSession(authOptions);
|
||||
@@ -29,6 +31,10 @@ export async function suspendUserAction(userId: string, reason: string) {
|
||||
entityId: userId,
|
||||
payload: { reason: reason.trim() },
|
||||
});
|
||||
|
||||
// Notif email user — due process: kasih tahu alasan + cara appeal.
|
||||
void notifySuspended(userId, reason.trim());
|
||||
|
||||
revalidatePath("/admin/users");
|
||||
revalidatePath(`/admin/users/${userId}`);
|
||||
return { success: true as const };
|
||||
@@ -37,6 +43,21 @@ export async function suspendUserAction(userId: string, reason: string) {
|
||||
}
|
||||
}
|
||||
|
||||
async function notifySuspended(userId: string, reason: string) {
|
||||
const target = await userRepo.findById(userId);
|
||||
if (!target) return;
|
||||
await emailService.send({
|
||||
to: target.email,
|
||||
// Suspend bisa di-trigger berulang — sertakan timestamp supaya tiap suspend
|
||||
// baru dapat email baru.
|
||||
idempotencyKey: `account_suspended-${userId}-${Date.now()}`,
|
||||
template: {
|
||||
template: "account_suspended",
|
||||
data: { userName: target.name, reason },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function unsuspendUserAction(userId: string) {
|
||||
const session = await getServerSession(authOptions);
|
||||
if (!session?.user) {
|
||||
|
||||
@@ -10,6 +10,9 @@ import {
|
||||
type ReuploadField,
|
||||
} from "@/server/services/organizer.service";
|
||||
import { auditLog } from "@/server/services/audit-log.service";
|
||||
import { emailService } from "@/lib/email/send";
|
||||
import { organizerRepo } from "@/server/repositories/organizer.repo";
|
||||
import { userRepo } from "@/server/repositories/user.repo";
|
||||
import { submitVerificationSchema, reviewVerificationSchema } from "./schemas";
|
||||
|
||||
export async function submitVerificationAction(formData: FormData) {
|
||||
@@ -86,6 +89,10 @@ export async function reviewVerificationAction(formData: FormData) {
|
||||
? { rejectionReason: result.data.rejectionReason ?? null }
|
||||
: undefined,
|
||||
});
|
||||
|
||||
// Notif email — fire and forget, jangan blok response.
|
||||
void notifyVerificationDecision(result.data.verificationId, result.data.decision, result.data.rejectionReason);
|
||||
|
||||
revalidatePath("/admin/verifications");
|
||||
revalidatePath("/verify");
|
||||
revalidatePath("/profile");
|
||||
@@ -95,6 +102,41 @@ export async function reviewVerificationAction(formData: FormData) {
|
||||
}
|
||||
}
|
||||
|
||||
async function notifyVerificationDecision(
|
||||
verificationId: string,
|
||||
decision: "APPROVED" | "REJECTED",
|
||||
rejectionReason?: string
|
||||
) {
|
||||
const verification = await organizerRepo.findById(verificationId);
|
||||
if (!verification) return;
|
||||
const user = await userRepo.findById(verification.userId);
|
||||
if (!user) return;
|
||||
|
||||
if (decision === "APPROVED") {
|
||||
await emailService.send({
|
||||
to: user.email,
|
||||
idempotencyKey: `kyc_approved-${verificationId}`,
|
||||
template: {
|
||||
template: "kyc_approved",
|
||||
data: { userName: user.name },
|
||||
},
|
||||
});
|
||||
} else {
|
||||
await emailService.send({
|
||||
to: user.email,
|
||||
// submissionCount supaya kalau reject berulang masing-masing dapat email.
|
||||
idempotencyKey: `kyc_rejected-${verificationId}-${verification.submissionCount}`,
|
||||
template: {
|
||||
template: "kyc_rejected",
|
||||
data: {
|
||||
userName: user.name,
|
||||
rejectionReason: rejectionReason ?? "(tidak ada alasan tercatat)",
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Admin reopen pengajuan REJECTED ke PENDING — supaya organizer bisa
|
||||
* di-review ulang tanpa drop & recreate row. Note wajib min 10 char untuk audit.
|
||||
@@ -159,6 +201,10 @@ export async function requestReuploadAction(
|
||||
entityId: verificationId,
|
||||
payload: { fields: valid, note: note.trim() },
|
||||
});
|
||||
|
||||
// Notif email organizer — urgent, action required.
|
||||
void notifyReuploadRequest(verificationId, valid, note.trim());
|
||||
|
||||
revalidatePath("/admin/verifications");
|
||||
revalidatePath("/verify");
|
||||
return { success: true as const };
|
||||
@@ -167,6 +213,30 @@ export async function requestReuploadAction(
|
||||
}
|
||||
}
|
||||
|
||||
async function notifyReuploadRequest(
|
||||
verificationId: string,
|
||||
fields: ReuploadField[],
|
||||
note: string
|
||||
) {
|
||||
const verification = await organizerRepo.findById(verificationId);
|
||||
if (!verification) return;
|
||||
const user = await userRepo.findById(verification.userId);
|
||||
if (!user) return;
|
||||
await emailService.send({
|
||||
to: user.email,
|
||||
// Allow re-trigger kalau admin minta lagi setelah submit ulang.
|
||||
idempotencyKey: `kyc_reupload_request-${verificationId}-${verification.submissionCount}`,
|
||||
template: {
|
||||
template: "kyc_reupload_request",
|
||||
data: {
|
||||
userName: user.name,
|
||||
fields,
|
||||
note,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Phase 4: admin verify user tanpa upload KYC (partner trusted referral).
|
||||
* Bikin row APPROVED dengan flag `isManualOverride = true`.
|
||||
|
||||
@@ -6,6 +6,8 @@ import { authOptions } from "@/lib/auth";
|
||||
import { isAdminEmail } from "@/lib/admin";
|
||||
import { refundService } from "@/server/services/refund.service";
|
||||
import { auditLog } from "@/server/services/audit-log.service";
|
||||
import { emailService } from "@/lib/email/send";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { createRefundSchema, refundDecisionSchema } from "./schemas";
|
||||
|
||||
async function requireAdmin() {
|
||||
@@ -51,6 +53,9 @@ export async function createRefundAction(formData: FormData) {
|
||||
reason: parsed.data.reason,
|
||||
},
|
||||
});
|
||||
|
||||
void notifyRefundCreated(refund.id);
|
||||
|
||||
revalidatePath("/admin/refunds");
|
||||
return { success: true };
|
||||
} catch (err) {
|
||||
@@ -58,6 +63,77 @@ export async function createRefundAction(formData: FormData) {
|
||||
}
|
||||
}
|
||||
|
||||
async function notifyRefundCreated(refundId: string) {
|
||||
const refund = await prisma.refund.findUnique({
|
||||
where: { id: refundId },
|
||||
include: {
|
||||
booking: {
|
||||
include: {
|
||||
user: { select: { email: true, name: true } },
|
||||
trip: { select: { title: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!refund) return;
|
||||
await emailService.send({
|
||||
to: refund.booking.user.email,
|
||||
idempotencyKey: `refund_created-${refund.id}`,
|
||||
template: {
|
||||
template: "refund_created",
|
||||
data: {
|
||||
userName: refund.booking.user.name,
|
||||
tripTitle: refund.booking.trip.title,
|
||||
amount: refund.amount,
|
||||
reason: refund.reason,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function notifyRefundDecision(
|
||||
refundId: string,
|
||||
decision: "SUCCEEDED" | "FAILED",
|
||||
adminNote: string
|
||||
) {
|
||||
const refund = await prisma.refund.findUnique({
|
||||
where: { id: refundId },
|
||||
include: {
|
||||
booking: {
|
||||
include: {
|
||||
user: { select: { email: true, name: true } },
|
||||
trip: { select: { title: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!refund) return;
|
||||
await emailService.send({
|
||||
to: refund.booking.user.email,
|
||||
idempotencyKey: `refund_${decision.toLowerCase()}-${refund.id}`,
|
||||
template:
|
||||
decision === "SUCCEEDED"
|
||||
? {
|
||||
template: "refund_succeeded",
|
||||
data: {
|
||||
userName: refund.booking.user.name,
|
||||
tripTitle: refund.booking.trip.title,
|
||||
amount: refund.amount,
|
||||
adminNote,
|
||||
},
|
||||
}
|
||||
: {
|
||||
template: "refund_failed",
|
||||
data: {
|
||||
userName: refund.booking.user.name,
|
||||
tripTitle: refund.booking.trip.title,
|
||||
amount: refund.amount,
|
||||
adminNote,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function decideRefundAction(formData: FormData) {
|
||||
const admin = await requireAdmin();
|
||||
if (!admin) return { error: "Tidak memiliki akses admin" };
|
||||
@@ -110,6 +186,13 @@ export async function decideRefundAction(formData: FormData) {
|
||||
entityId: refundId,
|
||||
payload: adminNote ? { adminNote } : undefined,
|
||||
});
|
||||
|
||||
// Notif email user kalau decision final (SUCCEEDED/FAILED) — APPROVE/REJECT
|
||||
// intermediate, refund_created sudah dikirim sebelumnya.
|
||||
if (decision === "SUCCEEDED" || decision === "FAILED") {
|
||||
void notifyRefundDecision(refundId, decision, adminNote ?? "");
|
||||
}
|
||||
|
||||
revalidatePath("/admin/refunds");
|
||||
return { success: true };
|
||||
} catch (err) {
|
||||
|
||||
@@ -13,6 +13,8 @@ import { revalidatePath } from "next/cache";
|
||||
import { tripStoredInstantFromYmd } from "@/lib/trip-dates";
|
||||
import { requireActiveUser } from "@/lib/auth-guards";
|
||||
import { auditLog } from "@/server/services/audit-log.service";
|
||||
import { emailService } from "@/lib/email/send";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export async function createTripAction(formData: FormData) {
|
||||
const session = await getServerSession(authOptions);
|
||||
@@ -172,6 +174,9 @@ export async function confirmParticipantAction(
|
||||
participantId,
|
||||
session.user.id
|
||||
);
|
||||
|
||||
void notifyBookingApproved(participantId);
|
||||
|
||||
revalidatePath(`/trips/${tripId}`);
|
||||
revalidatePath("/trips");
|
||||
revalidatePath("/");
|
||||
@@ -182,6 +187,31 @@ export async function confirmParticipantAction(
|
||||
}
|
||||
}
|
||||
|
||||
async function notifyBookingApproved(participantId: string) {
|
||||
const participant = await prisma.tripParticipant.findUnique({
|
||||
where: { id: participantId },
|
||||
include: {
|
||||
user: { select: { email: true, name: true } },
|
||||
trip: { select: { id: true, title: true } },
|
||||
booking: { select: { id: true, amount: true } },
|
||||
},
|
||||
});
|
||||
if (!participant || !participant.booking) return;
|
||||
await emailService.send({
|
||||
to: participant.user.email,
|
||||
idempotencyKey: `booking_approved-${participant.booking.id}`,
|
||||
template: {
|
||||
template: "booking_approved",
|
||||
data: {
|
||||
userName: participant.user.name,
|
||||
tripTitle: participant.trip.title,
|
||||
tripId: participant.trip.id,
|
||||
amount: participant.booking.amount,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function rejectParticipantAction(
|
||||
tripId: string,
|
||||
participantId: string
|
||||
|
||||
Reference in New Issue
Block a user