stacker.news/worker/expireBoost.js
Keyan 5f0494de30
rethinking boost (#1408)
* reuse boost for jobs

* wip

* allow job stopping

* restore upvote.js

* expire boost

* boost beyond edit window

* fix boost bolt styling

* rank comments with boost

* no random sort for jobs

* top boost for month at top of territory

* boost hints

* more boost help

* squash migrations

* for same boost, prioritize older

* show ad only if active

* fix itemCreate/Update boost expiration jobs

* fix fee button precedence
2024-09-19 13:13:14 -05:00

28 lines
974 B
JavaScript

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"
SET boost = COALESCE(boost.cur_msats, 0), "oldBoost" = COALESCE(boost.old_msats, 0)
FROM boost
WHERE "Item".id = ${Number(id)}::INTEGER`
],
{
isolationLevel: Prisma.TransactionIsolationLevel.Serializable
}
)
}