add cron and partial update refund schema

This commit is contained in:
arifal
2026-05-10 22:27:21 +07:00
parent 9a163c4f13
commit 744ee3446b
7 changed files with 466 additions and 17 deletions
+35
View File
@@ -211,4 +211,39 @@ export const tripRepo = {
async updateStatus(id: string, status: "OPEN" | "FULL" | "CLOSED" | "COMPLETED") {
return prisma.trip.update({ where: { id }, data: { status } });
},
/**
* Bulk transisi trip yang sudah lewat `cutoff` (start of today UTC) dari
* status OPEN/FULL ke COMPLETED. Idempotent — second run tidak akan match
* apa-apa karena status sudah berubah.
*
* Returns daftar id yang ter-update untuk telemetri/log.
*/
async bulkCompletePastTrips(cutoff: Date) {
const trips = await prisma.trip.findMany({
where: {
status: { in: ["OPEN", "FULL"] },
OR: [
{ endDate: { lt: cutoff } },
{ AND: [{ endDate: null }, { date: { lt: cutoff } }] },
],
},
select: { id: true },
});
if (trips.length === 0) {
return { count: 0, ids: [] as string[] };
}
const ids = trips.map((t) => t.id);
const result = await prisma.trip.updateMany({
where: {
id: { in: ids },
status: { in: ["OPEN", "FULL"] },
},
data: { status: "COMPLETED" },
});
return { count: result.count, ids };
},
};
+14
View File
@@ -385,6 +385,20 @@ export const tripService = {
return bookingService.markPaidManual(booking.id, userId);
},
/**
* Auto-complete trip yang sudah lewat. Dipakai cron harian.
*
* Cutoff = start of today UTC. Trip dengan endDate < cutoff (atau, kalau
* endDate null, date < cutoff) di-set status COMPLETED — selama statusnya
* masih OPEN/FULL. CLOSED trip tidak disentuh (organizer eksplisit batalkan).
*
* Idempotent: dua kali run di hari sama, run kedua nge-match 0 row.
*/
async autoCompletePastTrips() {
const cutoff = utcStartOfDay(new Date());
return tripRepo.bulkCompletePastTrips(cutoff);
},
async confirmParticipantPayment(
tripId: string,
participantId: string,