mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
add full version debts system
foundation for granting the full version while consuming the same amount automatically at the next purchase
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2022 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2026 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
@@ -82,7 +82,8 @@ export async function createFamily ({
|
||||
fullVersionUntil: (Date.now() + 1000 * 60 * 60 * 24 * 14).toString(10),
|
||||
hasFullVersion: true,
|
||||
nextServerKeyRequestSeq: '1',
|
||||
u2fKeysVersion: generateVersionId()
|
||||
u2fKeysVersion: generateVersionId(),
|
||||
fullVersionDebts: '0'
|
||||
}, { transaction })
|
||||
|
||||
// create parent user
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2022 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2026 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
@@ -57,11 +57,19 @@ export const addPurchase = async ({ database, familyId, type, service, transacti
|
||||
}
|
||||
|
||||
const previousFullVersionEndTime = familyEntry.fullVersionUntil
|
||||
const previousFullVersionDebts = parseInt(familyEntry.fullVersionDebts, 10)
|
||||
|
||||
const newFullVersionUntil = Math.max(parseInt(familyEntry.fullVersionUntil, 10), Date.now()) + (type === 'year' ? year : month)
|
||||
const typeDuration = type === 'year' ? year : month
|
||||
|
||||
familyEntry.fullVersionUntil = newFullVersionUntil.toString(10)
|
||||
familyEntry.hasFullVersion = true
|
||||
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)
|
||||
}
|
||||
|
||||
await familyEntry.save({ transaction })
|
||||
|
||||
@@ -72,7 +80,7 @@ export const addPurchase = async ({ database, familyId, type, service, transacti
|
||||
type,
|
||||
loggedAt: Date.now().toString(10),
|
||||
previousFullVersionEndTime,
|
||||
newFullVersionEndTime: newFullVersionUntil.toString(10)
|
||||
newFullVersionEndTime: familyEntry.fullVersionUntil
|
||||
}, {
|
||||
transaction
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2026 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
@@ -15,6 +15,9 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export const canDoNextPurchase = ({ fullVersionUntil }: {fullVersionUntil: number}) => (
|
||||
fullVersionUntil < (Date.now() + 1000 * 60 * 60 * 24 * 31) // 31 days
|
||||
export const canDoNextPurchase = ({ fullVersionUntil, fullVersionDebts }: {
|
||||
fullVersionUntil: number
|
||||
fullVersionDebts: number
|
||||
}) => (
|
||||
(fullVersionUntil - fullVersionDebts) < (Date.now() + 1000 * 60 * 60 * 24 * 31) // 31 days
|
||||
)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2026 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
@@ -43,7 +43,7 @@ export const requireFamilyEntry = async ({ database, deviceAuthToken, transactio
|
||||
where: {
|
||||
familyId: deviceEntry.familyId
|
||||
},
|
||||
attributes: ['fullVersionUntil'],
|
||||
attributes: ['fullVersionUntil', 'fullVersionDebts'],
|
||||
transaction
|
||||
})
|
||||
|
||||
@@ -52,7 +52,8 @@ export const requireFamilyEntry = async ({ database, deviceAuthToken, transactio
|
||||
}
|
||||
|
||||
const familyEntry = {
|
||||
fullVersionUntil: familyEntryUnsafe.fullVersionUntil
|
||||
fullVersionUntil: familyEntryUnsafe.fullVersionUntil,
|
||||
fullVersionDebts: familyEntryUnsafe.fullVersionDebts,
|
||||
}
|
||||
|
||||
return familyEntry
|
||||
|
||||
Reference in New Issue
Block a user