2024-09-19 18:13:14 +00:00
|
|
|
import { Prisma } from '@prisma/client'
|
|
|
|
|
|
|
|
export async function expireBoost ({ data: { id }, models }) {
|
|
|
|
// reset boost 30 days after last boost
|
|
|
|
// run in serializable because we use an aggregate here
|
|
|
|
// and concurrent boosts could be double counted
|
|
|
|
// serialization errors will cause pgboss retries
|
|
|
|
await models.$transaction(
|
|
|
|
[
|
|
|
|
models.$executeRaw`
|
|
|
|
WITH boost AS (
|
|
|
|
SELECT sum(msats) FILTER (WHERE created_at <= now() - interval '30 days') as old_msats,
|
|
|
|
sum(msats) FILTER (WHERE created_at > now() - interval '30 days') as cur_msats
|
|
|
|
FROM "ItemAct"
|
|
|
|
WHERE act = 'BOOST'
|
|
|
|
AND "itemId" = ${Number(id)}::INTEGER
|
|
|
|
)
|
|
|
|
UPDATE "Item"
|
2024-09-19 21:10:04 +00:00
|
|
|
SET boost = COALESCE(boost.cur_msats, 0) / 1000, "oldBoost" = COALESCE(boost.old_msats, 0) / 1000
|
2024-09-19 18:13:14 +00:00
|
|
|
FROM boost
|
|
|
|
WHERE "Item".id = ${Number(id)}::INTEGER`
|
|
|
|
],
|
|
|
|
{
|
|
|
|
isolationLevel: Prisma.TransactionIsolationLevel.Serializable
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|