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:
Jonas Lochmann
2026-06-08 02:00:00 +02:00
parent 0b6ece0d28
commit 48d6a061f8
8 changed files with 95 additions and 20 deletions
+5 -2
View File
@@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * 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 * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
@@ -228,7 +228,10 @@ export const createAdminRouter = ({ database, websocket, eventHandler }: {
error: 'family not found' error: 'family not found'
} }
const canDoPurchase = canDoNextPurchase({ fullVersionUntil: parseInt(familyEntry.fullVersionUntil) }) const canDoPurchase = canDoNextPurchase({
fullVersionUntil: parseInt(familyEntry.fullVersionUntil),
fullVersionDebts: parseInt(familyEntry.fullVersionDebts),
})
if (!canDoPurchase) { if (!canDoPurchase) {
const lastPurchase = await database.purchase.findOne({ const lastPurchase = await database.purchase.findOne({
+5 -2
View File
@@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * 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 * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
@@ -54,7 +54,10 @@ export const createPurchaseRouter = ({ database, websocket }: {
transaction transaction
}) })
return canDoNextPurchase({ fullVersionUntil: parseInt(familyEntry.fullVersionUntil, 10) }) return canDoNextPurchase({
fullVersionUntil: parseInt(familyEntry.fullVersionUntil, 10),
fullVersionDebts: parseInt(familyEntry.fullVersionDebts, 10),
})
}) })
res.json({ res.json({
+20 -3
View File
@@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * 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 * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
@@ -37,8 +37,13 @@ export interface FamilyAttributesVersion3 {
u2fKeysVersion: string u2fKeysVersion: string
} }
export interface FamilyAttributesVersion4 {
fullVersionDebts: string
}
export type FamilyAttributes = FamilyAttributesVersion1 & export type FamilyAttributes = FamilyAttributesVersion1 &
FamilyAttributesVersion2 & FamilyAttributesVersion3 FamilyAttributesVersion2 & FamilyAttributesVersion3 &
FamilyAttributesVersion4
export type FamilyModel = Sequelize.Model<FamilyAttributes> & FamilyAttributes export type FamilyModel = Sequelize.Model<FamilyAttributes> & FamilyAttributes
export type FamilyModelStatic = typeof Sequelize.Model & { export type FamilyModelStatic = typeof Sequelize.Model & {
@@ -76,10 +81,22 @@ export const attributesVersion3: SequelizeAttributes<FamilyAttributesVersion3> =
} }
} }
export const attributesVersion4: SequelizeAttributes<FamilyAttributesVersion4> = {
fullVersionDebts: {
type: Sequelize.BIGINT,
allowNull: false,
defaultValue: 0,
validate: {
min: 0
}
}
}
export const attributes: SequelizeAttributes<FamilyAttributes> = { export const attributes: SequelizeAttributes<FamilyAttributes> = {
...attributesVersion1, ...attributesVersion1,
...attributesVersion2, ...attributesVersion2,
...attributesVersion3 ...attributesVersion3,
...attributesVersion4
} }
export const createFamilyModel = (sequelize: Sequelize.Sequelize): FamilyModelStatic => sequelize.define('Family', attributes) as FamilyModelStatic export const createFamilyModel = (sequelize: Sequelize.Sequelize): FamilyModelStatic => sequelize.define('Family', attributes) as FamilyModelStatic
@@ -0,0 +1,39 @@
/*
* server component for the TimeLimit App
* 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
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { QueryInterface, Sequelize, Transaction } from 'sequelize'
import { attributesVersion4 as familyAttributes } from '../../family'
export async function up (queryInterface: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
await queryInterface.addColumn('Families', 'fullVersionDebts', {
...familyAttributes.fullVersionDebts
}, {
transaction
})
})
}
export async function down (queryInterface: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
await queryInterface.removeColumn('Families', 'fullVersionDebts', { transaction })
})
}
+3 -2
View File
@@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * 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 * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * 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), fullVersionUntil: (Date.now() + 1000 * 60 * 60 * 24 * 14).toString(10),
hasFullVersion: true, hasFullVersion: true,
nextServerKeyRequestSeq: '1', nextServerKeyRequestSeq: '1',
u2fKeysVersion: generateVersionId() u2fKeysVersion: generateVersionId(),
fullVersionDebts: '0'
}, { transaction }) }, { transaction })
// create parent user // create parent user
+13 -5
View File
@@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * 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 * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * 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 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) if (typeDuration > previousFullVersionDebts) {
familyEntry.hasFullVersion = true 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 }) await familyEntry.save({ transaction })
@@ -72,7 +80,7 @@ export const addPurchase = async ({ database, familyId, type, service, transacti
type, type,
loggedAt: Date.now().toString(10), loggedAt: Date.now().toString(10),
previousFullVersionEndTime, previousFullVersionEndTime,
newFullVersionEndTime: newFullVersionUntil.toString(10) newFullVersionEndTime: familyEntry.fullVersionUntil
}, { }, {
transaction transaction
}) })
@@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * 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 * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
export const canDoNextPurchase = ({ fullVersionUntil }: {fullVersionUntil: number}) => ( export const canDoNextPurchase = ({ fullVersionUntil, fullVersionDebts }: {
fullVersionUntil < (Date.now() + 1000 * 60 * 60 * 24 * 31) // 31 days 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 * 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 * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
@@ -43,7 +43,7 @@ export const requireFamilyEntry = async ({ database, deviceAuthToken, transactio
where: { where: {
familyId: deviceEntry.familyId familyId: deviceEntry.familyId
}, },
attributes: ['fullVersionUntil'], attributes: ['fullVersionUntil', 'fullVersionDebts'],
transaction transaction
}) })
@@ -52,7 +52,8 @@ export const requireFamilyEntry = async ({ database, deviceAuthToken, transactio
} }
const familyEntry = { const familyEntry = {
fullVersionUntil: familyEntryUnsafe.fullVersionUntil fullVersionUntil: familyEntryUnsafe.fullVersionUntil,
fullVersionDebts: familyEntryUnsafe.fullVersionDebts,
} }
return familyEntry return familyEntry