Add support for custom time warnings

This commit is contained in:
Jonas Lochmann
2022-03-28 02:00:00 +02:00
parent d7799f2d06
commit 2ab23ea811
19 changed files with 330 additions and 9 deletions
+58
View File
@@ -0,0 +1,58 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2022 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 { familyIdColumn, idWithinFamilyColumn } from './columns'
import { SequelizeAttributes } from './types'
export const categoryTimeWarningConstants = {
minMinutes: 1,
maxMinutes: 60 * 24 * 7 - 2
}
export interface CategoryTimeWarningAttributes {
familyId: string
categoryId: string
minutes: number
}
export type CategoryTimeWarningModel = Sequelize.Model<CategoryTimeWarningAttributes> & CategoryTimeWarningAttributes
export type CategoryTimeWarningModelStatic = typeof Sequelize.Model & {
new (values?: object, options?: Sequelize.BuildOptions): CategoryTimeWarningModel;
}
export const attributes: SequelizeAttributes<CategoryTimeWarningAttributes> = {
familyId: {
...familyIdColumn,
primaryKey: true
},
categoryId: {
...idWithinFamilyColumn,
primaryKey: true
},
minutes: {
type: Sequelize.INTEGER,
allowNull: false,
validate: {
min: categoryTimeWarningConstants.minMinutes,
max: categoryTimeWarningConstants.maxMinutes
},
primaryKey: true
}
}
export const createCategoryTimeWarningModel = (sequelize: Sequelize.Sequelize): CategoryTimeWarningModelStatic => sequelize.define('CategoryTimeWarning', attributes) as CategoryTimeWarningModelStatic
+3
View File
@@ -23,6 +23,7 @@ import { AuthTokenModelStatic, createAuthtokenModel } from './authtoken'
import { CategoryModelStatic, createCategoryModel } from './category'
import { CategoryAppModelStatic, createCategoryAppModel } from './categoryapp'
import { CategoryNetworkIdModelStatic, createCategoryNetworkIdModel } from './categorynetworkid'
import { CategoryTimeWarningModelStatic, createCategoryTimeWarningModel } from './categorytimewarning'
import { ChildTaskModelStatic, createChildTaskModel } from './childtask'
import { ConfigModelStatic, createConfigModel } from './config'
import { createDeviceModel, DeviceModelStatic } from './device'
@@ -47,6 +48,7 @@ export interface Database {
category: CategoryModelStatic
categoryApp: CategoryAppModelStatic
categoryNetworkId: CategoryNetworkIdModelStatic
categoryTimeWarning: CategoryTimeWarningModelStatic
childTask: ChildTaskModelStatic
config: ConfigModelStatic
device: DeviceModelStatic
@@ -72,6 +74,7 @@ const createDatabase = (sequelize: Sequelize.Sequelize): Database => ({
categoryApp: createCategoryAppModel(sequelize),
childTask: createChildTaskModel(sequelize),
categoryNetworkId: createCategoryNetworkIdModel(sequelize),
categoryTimeWarning: createCategoryTimeWarningModel(sequelize),
config: createConfigModel(sequelize),
device: createDeviceModel(sequelize),
family: createFamilyModel(sequelize),
@@ -0,0 +1,55 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2022 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'
export async function up (_: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
const dialect = sequelize.getDialect()
const isMysql = dialect === 'mysql' || dialect === 'mariadb'
if (isMysql) {
await sequelize.query(
'CREATE TABLE `CategoryTimeWarnings` ' +
'(`familyId` VARCHAR(10) NOT NULL, `categoryId` VARCHAR(6) NOT NULL,' +
'`minutes` INTEGER NOT NULL, ' +
'PRIMARY KEY(`familyId`, `categoryId`, `minutes`), FOREIGN KEY(`familyId`, `categoryId`)' +
'REFERENCES `Categories`(`familyId`, `categoryId`) ON UPDATE CASCADE ON DELETE CASCADE )',
{ transaction }
)
} else {
await sequelize.query(
'CREATE TABLE "CategoryTimeWarnings" ' +
'("familyId" VARCHAR(10) NOT NULL, "categoryId" VARCHAR(6) NOT NULL,' +
'"minutes" INTEGER NOT NULL, ' +
'PRIMARY KEY("familyId", "categoryId", "minutes"), FOREIGN KEY("familyId", "categoryId")' +
'REFERENCES "Categories"("familyId", "categoryId") ON UPDATE CASCADE ON DELETE CASCADE )',
{ transaction }
)
}
})
}
export async function down (queryInterface: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
await queryInterface.dropTable('CategoryTimeWarnings', { transaction })
})
}