mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
add unpaid14 to the premium unlock api
This commit is contained in:
@@ -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 })
|
||||
|
||||
Reference in New Issue
Block a user