add cron and partial update refund schema
This commit is contained in:
@@ -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 };
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user