admin roadmap trips ops and payment ops
This commit is contained in:
@@ -381,6 +381,49 @@ export const paymentService = {
|
||||
* Auth: caller harus pastikan `userId` adalah owner booking; kita verifikasi
|
||||
* di sini lewat lookup payment → booking.userId.
|
||||
*/
|
||||
/**
|
||||
* Admin variant `reconcileFromGateway` — skip ownership check (admin bypass).
|
||||
* Dipakai dari `/admin/bookings/[id]` saat user lapor "sudah bayar tapi
|
||||
* status belum update". Idempotent: aman dipanggil berulang.
|
||||
*/
|
||||
async adminReconcile(
|
||||
orderId: string
|
||||
): Promise<
|
||||
| {
|
||||
ok: true;
|
||||
status:
|
||||
| "updated"
|
||||
| "skipped"
|
||||
| "ignored"
|
||||
| "booking_conflict"
|
||||
| "not_found";
|
||||
}
|
||||
| { ok: false; reason: "amount_mismatch" | "not_found" }
|
||||
> {
|
||||
const payment = await prisma.payment.findUnique({
|
||||
where: { externalOrderId: orderId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!payment) {
|
||||
return { ok: false, reason: "not_found" };
|
||||
}
|
||||
|
||||
const status = await fetchMidtransTransactionStatus(orderId);
|
||||
if (!status) {
|
||||
return { ok: true, status: "not_found" };
|
||||
}
|
||||
|
||||
return applyGatewayStatus({
|
||||
order_id: status.order_id,
|
||||
gross_amount: status.gross_amount,
|
||||
transaction_status: status.transaction_status,
|
||||
fraud_status: status.fraud_status ?? null,
|
||||
transaction_id: status.transaction_id,
|
||||
payment_type: status.payment_type,
|
||||
rawSource: status as unknown as Prisma.InputJsonValue,
|
||||
});
|
||||
},
|
||||
|
||||
async reconcileFromGateway(
|
||||
orderId: string,
|
||||
userId: string
|
||||
|
||||
@@ -404,9 +404,10 @@ export const tripService = {
|
||||
},
|
||||
|
||||
/**
|
||||
* Organizer batalkan trip (Trip.status = CLOSED). Atomic dalam satu
|
||||
* serializable transaction:
|
||||
* - Set Trip.status = CLOSED.
|
||||
* Batalkan trip (Trip.status = CLOSED). Bisa dipicu organizer sendiri ATAU
|
||||
* admin (intervensi). Atomic dalam satu serializable transaction:
|
||||
* - Set Trip.status = CLOSED (+ `cancelledByAdminId` & `cancelledReason`
|
||||
* kalau actor = ADMIN).
|
||||
* - Untuk setiap peserta aktif:
|
||||
* - Booking PAID → buat Refund ORGANIZER_CANCELLED (auto-approved, full
|
||||
* amount). Booking tetap PAID sampai admin mark SUCCEEDED — jejak
|
||||
@@ -420,7 +421,12 @@ export const tripService = {
|
||||
* Idempotent: trip yang sudah CLOSED/COMPLETED akan ditolak supaya tidak
|
||||
* dobel-buat refund.
|
||||
*/
|
||||
async closeTrip(tripId: string, organizerId: string) {
|
||||
async closeTrip(
|
||||
tripId: string,
|
||||
actor:
|
||||
| { type: "ORGANIZER"; userId: string }
|
||||
| { type: "ADMIN"; adminId: string; reason: string }
|
||||
) {
|
||||
let lastErr: unknown;
|
||||
for (let attempt = 0; attempt < SERIAL_TX_ATTEMPTS; attempt++) {
|
||||
try {
|
||||
@@ -433,7 +439,10 @@ export const tripService = {
|
||||
if (!trip) {
|
||||
throw new Error("Trip tidak ditemukan");
|
||||
}
|
||||
if (trip.organizerId !== organizerId) {
|
||||
if (
|
||||
actor.type === "ORGANIZER" &&
|
||||
trip.organizerId !== actor.userId
|
||||
) {
|
||||
throw new Error(
|
||||
"Hanya organizer trip ini yang bisa membatalkan trip"
|
||||
);
|
||||
@@ -544,7 +553,13 @@ export const tripService = {
|
||||
|
||||
await tx.trip.update({
|
||||
where: { id: tripId },
|
||||
data: { status: "CLOSED" },
|
||||
data: {
|
||||
status: "CLOSED",
|
||||
...(actor.type === "ADMIN" && {
|
||||
cancelledByAdminId: actor.adminId,
|
||||
cancelledReason: actor.reason,
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user