add unpaid14 to the premium unlock api

This commit is contained in:
Jonas Lochmann
2026-06-15 02:00:00 +02:00
parent b2bcc3715f
commit 235115168f
4 changed files with 42 additions and 11 deletions
+30 -6
View File
@@ -21,13 +21,14 @@ import { notifyClientsAboutChangesDelayed } from '../../function/websocket'
import { WebsocketApi } from '../../websocket'
const day = 1000 * 60 * 60 * 24
const week = day * 7
const month = day * 31
const year = day * 366
export const addPurchase = async ({ database, familyId, type, service, transactionId, websocket, transaction }: {
database: Database
familyId: string
type: 'month' | 'year'
type: 'month' | 'year' | 'unpaid14'
service: 'googleplay' | 'directpurchase'
transactionId: string
websocket: WebsocketApi
@@ -59,16 +60,39 @@ export const addPurchase = async ({ database, familyId, type, service, transacti
const previousFullVersionEndTime = familyEntry.fullVersionUntil
const previousFullVersionDebts = parseInt(familyEntry.fullVersionDebts, 10)
const typeDuration = type === 'year' ? year : month
if (type === 'month' || type === 'year') {
const typeDuration = type === 'year' ? year : month
if (typeDuration > previousFullVersionDebts) {
const newFullVersionUntil = Math.max(parseInt(familyEntry.fullVersionUntil, 10), Date.now()) + typeDuration - previousFullVersionDebts
if (typeDuration > previousFullVersionDebts) {
const newFullVersionUntil = Math.max(parseInt(familyEntry.fullVersionUntil, 10), Date.now()) + typeDuration - previousFullVersionDebts
familyEntry.fullVersionUntil = newFullVersionUntil.toString(10)
familyEntry.fullVersionDebts = '0'
familyEntry.hasFullVersion = true
} else {
familyEntry.fullVersionDebts = (previousFullVersionDebts - typeDuration).toString(10)
}
} else if (type === 'unpaid14') {
const debtsAdd = 2 * week
const debtsMax = 3 * week
const newDebts = Math.min(debtsMax, previousFullVersionDebts + debtsAdd)
if (newDebts <= previousFullVersionDebts) {
// do not save anything
return
}
const durationToAdd = newDebts - previousFullVersionDebts
const newFullVersionUntil = Math.max(parseInt(familyEntry.fullVersionUntil, 10), Date.now()) + durationToAdd
familyEntry.fullVersionUntil = newFullVersionUntil.toString(10)
familyEntry.fullVersionDebts = '0'
familyEntry.fullVersionDebts = newDebts.toString(10)
familyEntry.hasFullVersion = true
} else {
familyEntry.fullVersionDebts = (previousFullVersionDebts - typeDuration).toString(10)
throw new Error()
}
await familyEntry.save({ transaction })