2023-11-21 23:32:22 +00:00
|
|
|
import { GraphQLError } from 'graphql'
|
|
|
|
import serialize, { serializeInvoicable } from './serial'
|
|
|
|
import { TERRITORY_COST_MONTHLY, TERRITORY_COST_ONCE, TERRITORY_COST_YEARLY } from '../../lib/constants'
|
|
|
|
import { datePivot } from '../../lib/time'
|
|
|
|
import { ssValidate, territorySchema } from '../../lib/validate'
|
2024-01-03 02:05:49 +00:00
|
|
|
import { nextBilling, nextNextBilling } from '../../lib/territory'
|
2023-11-21 23:32:22 +00:00
|
|
|
|
|
|
|
export function paySubQueries (sub, models) {
|
|
|
|
if (sub.billingType === 'ONCE') {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2024-01-03 02:05:49 +00:00
|
|
|
const billingAt = nextBilling(sub)
|
|
|
|
const billAt = nextNextBilling(sub)
|
2023-11-21 23:32:22 +00:00
|
|
|
const cost = BigInt(sub.billingCost) * BigInt(1000)
|
|
|
|
|
|
|
|
return [
|
|
|
|
models.user.update({
|
|
|
|
where: {
|
|
|
|
id: sub.userId
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
msats: {
|
|
|
|
decrement: cost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
// update 'em
|
|
|
|
models.sub.update({
|
|
|
|
where: {
|
|
|
|
name: sub.name
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
billedLastAt: billingAt,
|
|
|
|
status: 'ACTIVE'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
// record 'em
|
|
|
|
models.subAct.create({
|
|
|
|
data: {
|
|
|
|
userId: sub.userId,
|
|
|
|
subName: sub.name,
|
|
|
|
msats: cost,
|
|
|
|
type: 'BILLING'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
models.$executeRaw`
|
|
|
|
DELETE FROM pgboss.job
|
|
|
|
WHERE name = 'territoryBilling'
|
|
|
|
AND data->>'subName' = ${sub.name}
|
|
|
|
AND completedon IS NULL`,
|
|
|
|
// schedule 'em
|
|
|
|
models.$queryRaw`
|
2024-01-03 02:05:49 +00:00
|
|
|
INSERT INTO pgboss.job (name, data, startafter, keepuntil) VALUES ('territoryBilling',
|
2023-11-21 23:32:22 +00:00
|
|
|
${JSON.stringify({
|
|
|
|
subName: sub.name
|
2024-01-03 02:05:49 +00:00
|
|
|
})}::JSONB, ${billAt}, ${datePivot(billAt, { days: 1 })})`
|
2023-11-21 23:32:22 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2024-01-03 02:05:49 +00:00
|
|
|
export async function getSub (parent, { name }, { models, me }) {
|
|
|
|
if (!name) return null
|
2023-07-23 15:08:43 +00:00
|
|
|
|
2024-01-03 02:05:49 +00:00
|
|
|
return await models.sub.findUnique({
|
|
|
|
where: {
|
|
|
|
name
|
|
|
|
},
|
|
|
|
...(me
|
|
|
|
? {
|
|
|
|
include: {
|
|
|
|
MuteSub: {
|
|
|
|
where: {
|
|
|
|
userId: Number(me?.id)
|
2023-12-31 01:45:41 +00:00
|
|
|
}
|
2023-12-15 18:10:29 +00:00
|
|
|
}
|
2024-01-03 02:05:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
: {})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
Query: {
|
|
|
|
sub: getSub,
|
2023-12-15 18:10:29 +00:00
|
|
|
subs: async (parent, args, { models, me }) => {
|
2023-12-31 00:19:42 +00:00
|
|
|
if (me) {
|
|
|
|
return await models.$queryRaw`
|
|
|
|
SELECT "Sub".*, COALESCE(json_agg("MuteSub".*) FILTER (WHERE "MuteSub"."userId" IS NOT NULL), '[]') AS "MuteSub"
|
|
|
|
FROM "Sub"
|
|
|
|
LEFT JOIN "MuteSub" ON "Sub".name = "MuteSub"."subName" AND "MuteSub"."userId" = ${me.id}::INTEGER
|
|
|
|
WHERE status <> 'STOPPED'
|
|
|
|
GROUP BY "Sub".name, "MuteSub"."userId"
|
2023-12-31 01:41:16 +00:00
|
|
|
ORDER BY "Sub".name ASC
|
2023-12-31 00:19:42 +00:00
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2023-11-21 23:32:22 +00:00
|
|
|
return await models.sub.findMany({
|
|
|
|
where: {
|
|
|
|
status: {
|
|
|
|
not: 'STOPPED'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
name: 'asc'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2022-05-09 17:30:27 +00:00
|
|
|
subLatestPost: async (parent, { name }, { models, me }) => {
|
|
|
|
const latest = await models.item.findFirst({
|
|
|
|
where: {
|
|
|
|
subName: name
|
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
createdAt: 'desc'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-07-30 13:25:46 +00:00
|
|
|
return latest?.createdAt
|
2022-02-17 17:23:43 +00:00
|
|
|
}
|
2023-11-21 23:32:22 +00:00
|
|
|
},
|
|
|
|
Mutation: {
|
|
|
|
upsertSub: async (parent, { hash, hmac, ...data }, { me, models, lnd }) => {
|
|
|
|
if (!me) {
|
|
|
|
throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } })
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX this is because we did the wrong thing and used the subName as a primary key
|
2023-12-09 16:43:18 +00:00
|
|
|
const old = await models.sub.findUnique({
|
2023-11-21 23:32:22 +00:00
|
|
|
where: {
|
|
|
|
name: data.name,
|
|
|
|
userId: me.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await ssValidate(territorySchema, data, { models, me })
|
|
|
|
|
2023-12-09 16:43:18 +00:00
|
|
|
if (old) {
|
2023-12-23 20:26:16 +00:00
|
|
|
return await updateSub(parent, data, { me, models, lnd, hash, hmac })
|
2023-11-21 23:32:22 +00:00
|
|
|
} else {
|
|
|
|
return await createSub(parent, data, { me, models, lnd, hash, hmac })
|
|
|
|
}
|
|
|
|
},
|
|
|
|
paySub: async (parent, { name, hash, hmac }, { me, models, lnd }) => {
|
|
|
|
// check that they own the sub
|
|
|
|
const sub = await models.sub.findUnique({
|
|
|
|
where: {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!sub) {
|
|
|
|
throw new GraphQLError('sub not found', { extensions: { code: 'BAD_INPUT' } })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sub.userId !== me.id) {
|
|
|
|
throw new GraphQLError('you do not own this sub', { extensions: { code: 'BAD_INPUT' } })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sub.status === 'ACTIVE') {
|
|
|
|
return sub
|
|
|
|
}
|
|
|
|
|
|
|
|
const queries = paySubQueries(sub, models)
|
|
|
|
if (queries.length === 0) {
|
|
|
|
return sub
|
|
|
|
}
|
|
|
|
|
|
|
|
const results = await serializeInvoicable(
|
|
|
|
queries,
|
|
|
|
{ models, lnd, hash, hmac, me, enforceFee: sub.billingCost })
|
|
|
|
return results[1]
|
2023-12-15 18:10:29 +00:00
|
|
|
},
|
|
|
|
toggleMuteSub: async (parent, { name }, { me, models }) => {
|
|
|
|
if (!me) {
|
|
|
|
throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } })
|
|
|
|
}
|
|
|
|
|
|
|
|
const lookupData = { userId: Number(me.id), subName: name }
|
|
|
|
const where = { userId_subName: lookupData }
|
|
|
|
const existing = await models.muteSub.findUnique({ where })
|
|
|
|
if (existing) {
|
|
|
|
await models.muteSub.delete({ where })
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
await models.muteSub.create({ data: { ...lookupData } })
|
|
|
|
return true
|
|
|
|
}
|
2023-11-21 23:32:22 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Sub: {
|
|
|
|
user: async (sub, args, { models }) => {
|
|
|
|
if (sub.user) {
|
|
|
|
return sub.user
|
|
|
|
}
|
|
|
|
return await models.user.findUnique({ where: { id: sub.userId } })
|
2023-12-15 18:10:29 +00:00
|
|
|
},
|
|
|
|
meMuteSub: async (sub, args, { models }) => {
|
2023-12-31 01:41:16 +00:00
|
|
|
return sub.meMuteSub || sub.MuteSub?.length > 0
|
2023-11-21 23:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createSub (parent, data, { me, models, lnd, hash, hmac }) {
|
|
|
|
const { billingType } = data
|
|
|
|
let billingCost = TERRITORY_COST_MONTHLY
|
|
|
|
let billAt = datePivot(new Date(), { months: 1 })
|
|
|
|
|
|
|
|
if (billingType === 'ONCE') {
|
|
|
|
billingCost = TERRITORY_COST_ONCE
|
|
|
|
billAt = null
|
|
|
|
} else if (billingType === 'YEARLY') {
|
|
|
|
billingCost = TERRITORY_COST_YEARLY
|
|
|
|
billAt = datePivot(new Date(), { years: 1 })
|
|
|
|
}
|
|
|
|
|
|
|
|
const cost = BigInt(1000) * BigInt(billingCost)
|
|
|
|
|
|
|
|
try {
|
|
|
|
const results = await serializeInvoicable([
|
|
|
|
// bill 'em
|
|
|
|
models.user.update({
|
|
|
|
where: {
|
|
|
|
id: me.id
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
msats: {
|
|
|
|
decrement: cost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
// create 'em
|
|
|
|
models.sub.create({
|
|
|
|
data: {
|
|
|
|
...data,
|
|
|
|
billingCost,
|
|
|
|
rankingType: 'WOT',
|
|
|
|
userId: me.id
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
// record 'em
|
|
|
|
models.subAct.create({
|
|
|
|
data: {
|
|
|
|
userId: me.id,
|
|
|
|
subName: data.name,
|
|
|
|
msats: cost,
|
|
|
|
type: 'BILLING'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
// schedule 'em
|
|
|
|
...(billAt
|
|
|
|
? [models.$queryRaw`
|
2024-01-03 02:05:49 +00:00
|
|
|
INSERT INTO pgboss.job (name, data, startafter, keepuntil) VALUES ('territoryBilling',
|
2023-11-21 23:32:22 +00:00
|
|
|
${JSON.stringify({
|
|
|
|
subName: data.name
|
2024-01-03 02:05:49 +00:00
|
|
|
})}::JSONB, ${billAt}, ${datePivot(billAt, { days: 1 })})`]
|
2023-11-21 23:32:22 +00:00
|
|
|
: [])
|
|
|
|
], { models, lnd, hash, hmac, me, enforceFee: billingCost })
|
|
|
|
|
|
|
|
return results[1]
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'P2002') {
|
|
|
|
throw new GraphQLError('name taken', { extensions: { code: 'BAD_INPUT' } })
|
|
|
|
}
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateSub (parent, { name, ...data }, { me, models, lnd, hash, hmac }) {
|
2023-12-09 16:43:18 +00:00
|
|
|
// prevent modification of billingType
|
|
|
|
delete data.billingType
|
2023-11-21 23:32:22 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const results = await serialize(models,
|
|
|
|
// update 'em
|
|
|
|
models.sub.update({
|
2023-12-09 16:43:18 +00:00
|
|
|
data,
|
2023-11-21 23:32:22 +00:00
|
|
|
where: {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
return results[0]
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'P2002') {
|
|
|
|
throw new GraphQLError('name taken', { extensions: { code: 'BAD_INPUT' } })
|
|
|
|
}
|
|
|
|
throw error
|
2022-02-17 17:23:43 +00:00
|
|
|
}
|
|
|
|
}
|