2024-03-20 00:37:31 +00:00
|
|
|
import serialize from '@/api/resolvers/serial'
|
|
|
|
import { paySubQueries } from '@/api/resolvers/sub'
|
|
|
|
import { nextBillingWithGrace } from '@/lib/territory'
|
|
|
|
import { datePivot } from '@/lib/time'
|
2023-11-21 23:32:22 +00:00
|
|
|
|
|
|
|
export async function territoryBilling ({ data: { subName }, boss, models }) {
|
|
|
|
const sub = await models.sub.findUnique({
|
|
|
|
where: {
|
|
|
|
name: subName
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-12-08 20:02:00 +00:00
|
|
|
async function territoryStatusUpdate () {
|
2024-01-03 02:05:49 +00:00
|
|
|
if (sub.status !== 'STOPPED') {
|
|
|
|
await models.sub.update({
|
|
|
|
where: {
|
|
|
|
name: subName
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
status: nextBillingWithGrace(sub) >= new Date() ? 'GRACE' : 'STOPPED',
|
|
|
|
statusUpdatedAt: new Date()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-21 23:32:22 +00:00
|
|
|
// retry billing in one day
|
|
|
|
await boss.send('territoryBilling', { subName }, { startAfter: datePivot(new Date(), { days: 1 }) })
|
|
|
|
}
|
2023-12-08 20:02:00 +00:00
|
|
|
|
|
|
|
if (!sub.billingAutoRenew) {
|
|
|
|
await territoryStatusUpdate()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const queries = paySubQueries(sub, models)
|
2024-04-10 00:49:20 +00:00
|
|
|
await serialize(queries, { models })
|
2023-12-08 20:02:00 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
await territoryStatusUpdate()
|
|
|
|
}
|
2023-11-21 23:32:22 +00:00
|
|
|
}
|
2024-03-01 16:28:55 +00:00
|
|
|
|
|
|
|
export async function territoryRevenue ({ models }) {
|
2024-04-10 00:49:20 +00:00
|
|
|
await serialize(
|
2024-03-01 16:28:55 +00:00
|
|
|
models.$executeRaw`
|
|
|
|
WITH revenue AS (
|
|
|
|
SELECT coalesce(sum(msats), 0) as revenue, "subName", "userId"
|
|
|
|
FROM (
|
|
|
|
SELECT ("ItemAct".msats - COALESCE("ReferralAct".msats, 0)) * (1 - (COALESCE("Sub"."rewardsPct", 100) * 0.01)) as msats,
|
|
|
|
"Sub"."name" as "subName", "Sub"."userId" as "userId"
|
|
|
|
FROM "ItemAct"
|
|
|
|
JOIN "Item" ON "Item"."id" = "ItemAct"."itemId"
|
|
|
|
LEFT JOIN "Item" root ON "Item"."rootId" = root.id
|
|
|
|
JOIN "Sub" ON "Sub"."name" = COALESCE(root."subName", "Item"."subName")
|
|
|
|
LEFT JOIN "ReferralAct" ON "ReferralAct"."itemActId" = "ItemAct".id
|
2024-04-01 22:45:53 +00:00
|
|
|
WHERE date_trunc('day', "ItemAct".created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = date_trunc('day', (now() AT TIME ZONE 'America/Chicago' - interval '1 day'))
|
2024-03-01 16:28:55 +00:00
|
|
|
AND "ItemAct".act <> 'TIP'
|
|
|
|
AND "Sub".status <> 'STOPPED'
|
|
|
|
) subquery
|
|
|
|
GROUP BY "subName", "userId"
|
|
|
|
),
|
|
|
|
"SubActResult" AS (
|
|
|
|
INSERT INTO "SubAct" (msats, "subName", "userId", type)
|
|
|
|
SELECT revenue, "subName", "userId", 'REVENUE'
|
|
|
|
FROM revenue
|
|
|
|
WHERE revenue > 1000
|
|
|
|
RETURNING *
|
|
|
|
)
|
|
|
|
UPDATE users SET msats = users.msats + "SubActResult".msats
|
|
|
|
FROM "SubActResult"
|
2024-04-10 00:49:20 +00:00
|
|
|
WHERE users.id = "SubActResult"."userId"`,
|
|
|
|
{ models }
|
2024-03-01 16:28:55 +00:00
|
|
|
)
|
|
|
|
}
|