mirror of
https://github.com/jorgev259/soc_site-astro.git
synced 2025-06-29 07:57:41 +00:00
Migrate donator requests to limit of 5
This commit is contained in:
parent
1cd11fea2b
commit
042758d27c
5 changed files with 99 additions and 2 deletions
36
prisma/data_migrations/202505091015_donator_request_limit.ts
Normal file
36
prisma/data_migrations/202505091015_donator_request_limit.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { RequestState, type Prisma } from '@prisma/client'
|
||||
import prismaClient from 'utils/prisma-client'
|
||||
|
||||
const LIMIT_PENDING = 5
|
||||
|
||||
export default async function MigrationFn(tx: Prisma.TransactionClient) {
|
||||
const donatorRequests = await tx.requests.findMany({
|
||||
where: { donator: true, state: RequestState.PENDING, userID: { not: null } }
|
||||
})
|
||||
const donatorMap = new Map<string, typeof donatorRequests>()
|
||||
|
||||
donatorRequests.forEach((request) => {
|
||||
const userID = request.userID as string
|
||||
donatorMap.set(userID, [...(donatorMap.get(userID) || []), request])
|
||||
})
|
||||
|
||||
const holdRequests = new Set<number>()
|
||||
|
||||
for (const requests of donatorMap.values()) {
|
||||
if (requests.length <= LIMIT_PENDING) continue
|
||||
|
||||
const end = requests.length - LIMIT_PENDING
|
||||
const sortedRequests = requests.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime()).slice(0, end)
|
||||
|
||||
sortedRequests.forEach((r) => {
|
||||
holdRequests.add(r.id)
|
||||
})
|
||||
}
|
||||
|
||||
await prismaClient.requests.updateMany({
|
||||
where: { id: { in: Array.from(holdRequests) } },
|
||||
data: { state: RequestState.HOLD }
|
||||
})
|
||||
|
||||
console.log(`Updated ${holdRequests.size} requests to HOLD state`)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue