mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
Add support for more limits
This commit is contained in:
@@ -29,6 +29,7 @@ import { createMailLoginTokenModel, MailLoginTokenModelStatic } from './maillogi
|
||||
import { createUmzug } from './migration/umzug'
|
||||
import { createOldDeviceModel, OldDeviceModelStatic } from './olddevice'
|
||||
import { createPurchaseModel, PurchaseModelStatic } from './purchase'
|
||||
import { createSessionDurationModel, SessionDurationModelStatic } from './sessionduration'
|
||||
import { createTimelimitRuleModel, TimelimitRuleModelStatic } from './timelimitrule'
|
||||
import { createUsedTimeModel, UsedTimeModelStatic } from './usedtime'
|
||||
import { createUserModel, UserModelStatic } from './user'
|
||||
@@ -46,6 +47,7 @@ export interface Database {
|
||||
mailLoginToken: MailLoginTokenModelStatic
|
||||
oldDevice: OldDeviceModelStatic
|
||||
purchase: PurchaseModelStatic
|
||||
sessionDuration: SessionDurationModelStatic
|
||||
timelimitRule: TimelimitRuleModelStatic
|
||||
usedTime: UsedTimeModelStatic
|
||||
user: UserModelStatic
|
||||
@@ -65,6 +67,7 @@ const createDatabase = (sequelize: Sequelize.Sequelize): Database => ({
|
||||
mailLoginToken: createMailLoginTokenModel(sequelize),
|
||||
oldDevice: createOldDeviceModel(sequelize),
|
||||
purchase: createPurchaseModel(sequelize),
|
||||
sessionDuration: createSessionDurationModel(sequelize),
|
||||
timelimitRule: createTimelimitRuleModel(sequelize),
|
||||
usedTime: createUsedTimeModel(sequelize),
|
||||
user: createUserModel(sequelize),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2020 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
|
||||
@@ -24,7 +24,7 @@ import { attributes as categoryAppAttributes } from '../../categoryapp'
|
||||
import { attributesVersion1 as deviceAttributes } from '../../device'
|
||||
import { attributes as familyAttributes } from '../../family'
|
||||
import { attributes as purchaseAttributes } from '../../purchase'
|
||||
import { attributes as timelimitruleAttributes } from '../../timelimitrule'
|
||||
import { attributesVersion1 as timelimitruleAttributes } from '../../timelimitrule'
|
||||
import { attributesVersion1 as usedTimeAttribute } from '../../usedtime'
|
||||
import { attributesVersion1 as userAttributes } from '../../user'
|
||||
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2020 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 { MinuteOfDay } from '../../../util/minuteofday'
|
||||
import { attributesVersion1 as sessionDurationAttributes } from '../../sessionduration'
|
||||
import { attributesVersion2 as timelimitRuleAttributes } from '../../timelimitrule'
|
||||
import {
|
||||
attributesVersion1 as usedTimeAttributesVersion1,
|
||||
attributesVersion2 as usedTimeAttributesVersion2,
|
||||
attributesVersion3 as usedTimeAttributesVersion3
|
||||
} from '../../usedtime'
|
||||
|
||||
export async function up (queryInterface: QueryInterface, sequelize: Sequelize) {
|
||||
await sequelize.transaction({
|
||||
type: Transaction.TYPES.EXCLUSIVE
|
||||
}, async (transaction) => {
|
||||
// session durations
|
||||
await queryInterface.createTable('SessionDurations', sessionDurationAttributes, { transaction })
|
||||
|
||||
// timelimit rule table
|
||||
await queryInterface.addColumn('TimelimitRules', 'startMinuteOfDay', {
|
||||
...timelimitRuleAttributes.startMinuteOfDay
|
||||
}, { transaction })
|
||||
|
||||
await queryInterface.addColumn('TimelimitRules', 'endMinuteOfDay', {
|
||||
...timelimitRuleAttributes.endMinuteOfDay
|
||||
}, { transaction })
|
||||
|
||||
await queryInterface.addColumn('TimelimitRules', 'sessionDurationMilliseconds', {
|
||||
...timelimitRuleAttributes.sessionDurationMilliseconds
|
||||
}, { transaction })
|
||||
|
||||
await queryInterface.addColumn('TimelimitRules', 'sessionPauseMilliseconds', {
|
||||
...timelimitRuleAttributes.sessionPauseMilliseconds
|
||||
}, { transaction })
|
||||
|
||||
// used times
|
||||
await queryInterface.renameTable('UsedTimes', 'UsedTimesOld', { transaction })
|
||||
|
||||
await queryInterface.createTable('UsedTimes', {
|
||||
...usedTimeAttributesVersion1,
|
||||
...usedTimeAttributesVersion2,
|
||||
...usedTimeAttributesVersion3
|
||||
}, { transaction })
|
||||
|
||||
await sequelize.query(`
|
||||
INSERT INTO UsedTimes (familyId, categoryId, dayOfEpoch, usedTime, lastUpdate, startMinuteOfDay, endMinuteOfDay)
|
||||
SELECT familyId, categoryId, dayOfEpoch, usedTime, lastUpdate,
|
||||
${MinuteOfDay.MIN} AS startMinuteOfDay, ${MinuteOfDay.MAX} AS endMinuteOfDay
|
||||
FROM UsedTimesOld
|
||||
`, { transaction })
|
||||
|
||||
await queryInterface.dropTable('UsedTimesOld', { transaction })
|
||||
})
|
||||
}
|
||||
|
||||
export async function down (queryInterface: QueryInterface, sequelize: Sequelize) {
|
||||
await sequelize.transaction({
|
||||
type: Transaction.TYPES.EXCLUSIVE
|
||||
}, async (transaction) => {
|
||||
// session durations
|
||||
await queryInterface.dropTable('SessionDurations', { transaction })
|
||||
|
||||
// timelimit rule table
|
||||
await queryInterface.removeColumn('TimelimitRules', 'startMinuteOfDay', { transaction })
|
||||
await queryInterface.removeColumn('TimelimitRules', 'endMinuteOfDay', { transaction })
|
||||
await queryInterface.removeColumn('TimelimitRules', 'sessionDurationMilliseconds', { transaction })
|
||||
await queryInterface.removeColumn('TimelimitRules', 'sessionPauseMilliseconds', { transaction })
|
||||
|
||||
// used times
|
||||
throw new Error('not implemented')
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2020 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 * as Sequelize from 'sequelize'
|
||||
import { MinuteOfDay } from '../util/minuteofday'
|
||||
import { familyIdColumn, idWithinFamilyColumn, timestampColumn } from './columns'
|
||||
import { SequelizeAttributes } from './types'
|
||||
|
||||
interface SessionDurationAttributesVersion1 {
|
||||
familyId: string
|
||||
categoryId: string
|
||||
maxSessionDuration: number
|
||||
sessionPauseDuration: number
|
||||
startMinuteOfDay: number
|
||||
endMinuteOfDay: number
|
||||
lastUsage: string
|
||||
lastSessionDuration: number
|
||||
// used for deleting old items, set by the server
|
||||
roundedLastUpdate: string
|
||||
}
|
||||
|
||||
export type SessionDurationAttributes = SessionDurationAttributesVersion1
|
||||
|
||||
export type SessionDurationModel = Sequelize.Model & SessionDurationAttributes
|
||||
export type SessionDurationModelStatic = typeof Sequelize.Model & {
|
||||
new (values?: object, options?: Sequelize.BuildOptions): SessionDurationModel;
|
||||
}
|
||||
|
||||
export const attributesVersion1: SequelizeAttributes<SessionDurationAttributesVersion1> = {
|
||||
familyId: {
|
||||
...familyIdColumn,
|
||||
primaryKey: true
|
||||
},
|
||||
categoryId: {
|
||||
...idWithinFamilyColumn,
|
||||
primaryKey: true
|
||||
},
|
||||
maxSessionDuration: {
|
||||
type: Sequelize.INTEGER,
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
validate: {
|
||||
min: 1
|
||||
}
|
||||
},
|
||||
sessionPauseDuration: {
|
||||
type: Sequelize.INTEGER,
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
validate: {
|
||||
min: 1
|
||||
}
|
||||
},
|
||||
startMinuteOfDay: {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: false,
|
||||
primaryKey: true,
|
||||
validate: {
|
||||
min: MinuteOfDay.MIN,
|
||||
max: MinuteOfDay.MAX
|
||||
}
|
||||
},
|
||||
endMinuteOfDay: {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: false,
|
||||
primaryKey: true,
|
||||
validate: {
|
||||
min: MinuteOfDay.MIN,
|
||||
max: MinuteOfDay.MAX,
|
||||
customValidator (endMinuteOfDay: unknown) {
|
||||
const startMinuteOfDay = this.startMinuteOfDay
|
||||
|
||||
if (typeof endMinuteOfDay !== 'number' || typeof startMinuteOfDay !== 'number') {
|
||||
throw new Error('wrong data types')
|
||||
}
|
||||
|
||||
if (startMinuteOfDay > endMinuteOfDay) {
|
||||
throw new Error('startMinuteOfDay must not be bigger than endMinuteOfDay')
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
lastUsage: { ...timestampColumn },
|
||||
lastSessionDuration: {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: false,
|
||||
validate: {
|
||||
min: 0
|
||||
}
|
||||
},
|
||||
roundedLastUpdate: { ...timestampColumn }
|
||||
}
|
||||
|
||||
export const attributes: SequelizeAttributes<SessionDurationAttributes> = {
|
||||
...attributesVersion1
|
||||
}
|
||||
|
||||
export const createSessionDurationModel = (sequelize: Sequelize.Sequelize): SessionDurationModelStatic => sequelize.define('SessionDuration', attributes) as SessionDurationModelStatic
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2020 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
|
||||
@@ -16,10 +16,11 @@
|
||||
*/
|
||||
|
||||
import * as Sequelize from 'sequelize'
|
||||
import { MinuteOfDay } from '../util/minuteofday'
|
||||
import { booleanColumn, familyIdColumn, idWithinFamilyColumn } from './columns'
|
||||
import { SequelizeAttributes } from './types'
|
||||
|
||||
export interface TimelimitRuleAttributes {
|
||||
interface TimelimitRuleAttributesVersion1 {
|
||||
familyId: string
|
||||
ruleId: string
|
||||
categoryId: string
|
||||
@@ -28,12 +29,21 @@ export interface TimelimitRuleAttributes {
|
||||
maximumTimeInMillis: number
|
||||
}
|
||||
|
||||
interface TimelimitRuleAttributesVersion2 {
|
||||
startMinuteOfDay: number
|
||||
endMinuteOfDay: number
|
||||
sessionDurationMilliseconds: number
|
||||
sessionPauseMilliseconds: number
|
||||
}
|
||||
|
||||
type TimelimitRuleAttributes = TimelimitRuleAttributesVersion1 & TimelimitRuleAttributesVersion2
|
||||
|
||||
export type TimelimitRuleModel = Sequelize.Model & TimelimitRuleAttributes
|
||||
export type TimelimitRuleModelStatic = typeof Sequelize.Model & {
|
||||
new (values?: object, options?: Sequelize.BuildOptions): TimelimitRuleModel;
|
||||
}
|
||||
|
||||
export const attributes: SequelizeAttributes<TimelimitRuleAttributes> = {
|
||||
export const attributesVersion1: SequelizeAttributes<TimelimitRuleAttributesVersion1> = {
|
||||
familyId: {
|
||||
...familyIdColumn,
|
||||
primaryKey: true
|
||||
@@ -61,4 +71,57 @@ export const attributes: SequelizeAttributes<TimelimitRuleAttributes> = {
|
||||
}
|
||||
}
|
||||
|
||||
export const attributesVersion2: SequelizeAttributes<TimelimitRuleAttributesVersion2> = {
|
||||
startMinuteOfDay: {
|
||||
type: Sequelize.INTEGER,
|
||||
validate: {
|
||||
min: MinuteOfDay.MIN,
|
||||
max: MinuteOfDay.MAX
|
||||
},
|
||||
allowNull: false,
|
||||
defaultValue: MinuteOfDay.MIN
|
||||
},
|
||||
endMinuteOfDay: {
|
||||
type: Sequelize.INTEGER,
|
||||
validate: {
|
||||
min: MinuteOfDay.MIN,
|
||||
max: MinuteOfDay.MAX,
|
||||
customValidator (endMinuteOfDay: unknown) {
|
||||
const startMinuteOfDay = this.startMinuteOfDay
|
||||
|
||||
if (typeof endMinuteOfDay !== 'number' || typeof startMinuteOfDay !== 'number') {
|
||||
throw new Error('wrong data types')
|
||||
}
|
||||
|
||||
if (startMinuteOfDay > endMinuteOfDay) {
|
||||
throw new Error('startMinuteOfDay must not be bigger than endMinuteOfDay')
|
||||
}
|
||||
}
|
||||
},
|
||||
allowNull: false,
|
||||
defaultValue: MinuteOfDay.MAX
|
||||
},
|
||||
sessionDurationMilliseconds: {
|
||||
type: Sequelize.INTEGER,
|
||||
validate: {
|
||||
min: 0
|
||||
},
|
||||
allowNull: false,
|
||||
defaultValue: 0
|
||||
},
|
||||
sessionPauseMilliseconds: {
|
||||
type: Sequelize.INTEGER,
|
||||
validate: {
|
||||
min: 0
|
||||
},
|
||||
allowNull: false,
|
||||
defaultValue: 0
|
||||
}
|
||||
}
|
||||
|
||||
export const attributes: SequelizeAttributes<TimelimitRuleAttributes> = {
|
||||
...attributesVersion1,
|
||||
...attributesVersion2
|
||||
}
|
||||
|
||||
export const createTimelimitRuleModel = (sequelize: Sequelize.Sequelize): TimelimitRuleModelStatic => sequelize.define('TimelimitRule', attributes) as TimelimitRuleModelStatic
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2020 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
|
||||
@@ -16,6 +16,7 @@
|
||||
*/
|
||||
|
||||
import * as Sequelize from 'sequelize'
|
||||
import { MinuteOfDay } from '../util/minuteofday'
|
||||
import { familyIdColumn, idWithinFamilyColumn, timestampColumn } from './columns'
|
||||
import { SequelizeAttributes } from './types'
|
||||
|
||||
@@ -30,7 +31,13 @@ export interface UsedTimeAttributesVersion2 {
|
||||
lastUpdate: string
|
||||
}
|
||||
|
||||
export type UsedTimeAttributes = UsedTimeAttributesVersion1 & UsedTimeAttributesVersion2
|
||||
export interface UsedTimeAttributesVersion3 {
|
||||
startMinuteOfDay: number
|
||||
endMinuteOfDay: number
|
||||
}
|
||||
|
||||
export type UsedTimeAttributes = UsedTimeAttributesVersion1 &
|
||||
UsedTimeAttributesVersion2 & UsedTimeAttributesVersion3
|
||||
|
||||
export type UsedTimeModel = Sequelize.Model & UsedTimeAttributes
|
||||
export type UsedTimeModelStatic = typeof Sequelize.Model & {
|
||||
@@ -70,9 +77,44 @@ export const attributesVersion2: SequelizeAttributes<UsedTimeAttributesVersion2>
|
||||
}
|
||||
}
|
||||
|
||||
export const attributes = {
|
||||
export const attributesVersion3: SequelizeAttributes<UsedTimeAttributesVersion3> = {
|
||||
startMinuteOfDay: {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: false,
|
||||
defaultValue: MinuteOfDay.MIN,
|
||||
primaryKey: true,
|
||||
validate: {
|
||||
min: MinuteOfDay.MIN,
|
||||
max: MinuteOfDay.MAX
|
||||
}
|
||||
},
|
||||
endMinuteOfDay: {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: false,
|
||||
defaultValue: MinuteOfDay.MAX,
|
||||
primaryKey: true,
|
||||
validate: {
|
||||
min: MinuteOfDay.MIN,
|
||||
max: MinuteOfDay.MAX,
|
||||
customValidator (endMinuteOfDay: unknown) {
|
||||
const startMinuteOfDay = this.startMinuteOfDay
|
||||
|
||||
if (typeof endMinuteOfDay !== 'number' || typeof startMinuteOfDay !== 'number') {
|
||||
throw new Error('wrong data types')
|
||||
}
|
||||
|
||||
if (startMinuteOfDay > endMinuteOfDay) {
|
||||
throw new Error('startMinuteOfDay must not be bigger than endMinuteOfDay')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const attributes: SequelizeAttributes<UsedTimeAttributesVersion3> = {
|
||||
...attributesVersion1,
|
||||
...attributesVersion2
|
||||
...attributesVersion2,
|
||||
...attributesVersion3
|
||||
}
|
||||
|
||||
export const createUsedTimeModel = (sequelize: Sequelize.Sequelize): UsedTimeModelStatic => sequelize.define('UsedTime', attributes) as UsedTimeModelStatic
|
||||
|
||||
Reference in New Issue
Block a user