Add support for extra time for a specific day

This commit is contained in:
Jonas Lochmann 2020-03-02 01:00:00 +01:00
parent af17e6cc8e
commit 0ecb7e4830
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
10 changed files with 121 additions and 46 deletions

View file

@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * 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 * 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
@ -21,8 +21,9 @@ import { ParentAction } from './basetypes'
export class IncrementCategoryExtraTimeAction extends ParentAction { export class IncrementCategoryExtraTimeAction extends ParentAction {
readonly categoryId: string readonly categoryId: string
readonly addedExtraTime: number readonly addedExtraTime: number
readonly day: number
constructor ({ categoryId, addedExtraTime }: {categoryId: string, addedExtraTime: number}) { constructor ({ categoryId, addedExtraTime, day }: {categoryId: string, addedExtraTime: number, day: number}) {
super() super()
assertIdWithinFamily(categoryId) assertIdWithinFamily(categoryId)
@ -31,18 +32,24 @@ export class IncrementCategoryExtraTimeAction extends ParentAction {
throw new Error('must add some extra time with IncrementCategoryExtraTimeAction') throw new Error('must add some extra time with IncrementCategoryExtraTimeAction')
} }
if (day < -1 || (!Number.isSafeInteger(day))) {
throw Error('day must be valid')
}
this.categoryId = categoryId this.categoryId = categoryId
this.addedExtraTime = addedExtraTime this.addedExtraTime = addedExtraTime
this.day = day
} }
serialize = (): SerializedIncrementCategoryExtraTimeAction => ({ serialize = (): SerializedIncrementCategoryExtraTimeAction => ({
type: 'INCREMENT_CATEGORY_EXTRATIME', type: 'INCREMENT_CATEGORY_EXTRATIME',
categoryId: this.categoryId, categoryId: this.categoryId,
addedExtraTime: this.addedExtraTime addedExtraTime: this.addedExtraTime,
day: this.day
}) })
static parse = ({ categoryId, addedExtraTime }: SerializedIncrementCategoryExtraTimeAction) => ( static parse = ({ categoryId, addedExtraTime, day }: SerializedIncrementCategoryExtraTimeAction) => (
new IncrementCategoryExtraTimeAction({ categoryId, addedExtraTime }) new IncrementCategoryExtraTimeAction({ categoryId, addedExtraTime, day: day ?? -1 })
) )
} }
@ -50,4 +57,5 @@ export interface SerializedIncrementCategoryExtraTimeAction {
type: 'INCREMENT_CATEGORY_EXTRATIME' type: 'INCREMENT_CATEGORY_EXTRATIME'
categoryId: string categoryId: string
addedExtraTime: number addedExtraTime: number
day?: number
} }

View file

@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * 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 * 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
@ -21,8 +21,9 @@ import { ParentAction } from './basetypes'
export class SetCategoryExtraTimeAction extends ParentAction { export class SetCategoryExtraTimeAction extends ParentAction {
readonly categoryId: string readonly categoryId: string
readonly newExtraTime: number readonly newExtraTime: number
readonly day: number
constructor ({ categoryId, newExtraTime }: {categoryId: string, newExtraTime: number}) { constructor ({ categoryId, newExtraTime, day }: {categoryId: string, newExtraTime: number, day: number}) {
super() super()
assertIdWithinFamily(categoryId) assertIdWithinFamily(categoryId)
@ -31,18 +32,24 @@ export class SetCategoryExtraTimeAction extends ParentAction {
throw Error('newExtraTime must be >= 0') throw Error('newExtraTime must be >= 0')
} }
if (day < -1 || (!Number.isSafeInteger(day))) {
throw Error('day must be valid')
}
this.categoryId = categoryId this.categoryId = categoryId
this.newExtraTime = newExtraTime this.newExtraTime = newExtraTime
this.day = day
} }
serialize = (): SerializedSetCategoryExtraTimeAction => ({ serialize = (): SerializedSetCategoryExtraTimeAction => ({
type: 'SET_CATEGORY_EXTRA_TIME', type: 'SET_CATEGORY_EXTRA_TIME',
categoryId: this.categoryId, categoryId: this.categoryId,
newExtraTime: this.newExtraTime newExtraTime: this.newExtraTime,
day: this.day
}) })
static parse = ({ categoryId, newExtraTime }: SerializedSetCategoryExtraTimeAction) => ( static parse = ({ categoryId, newExtraTime, day }: SerializedSetCategoryExtraTimeAction) => (
new SetCategoryExtraTimeAction({ categoryId, newExtraTime }) new SetCategoryExtraTimeAction({ categoryId, newExtraTime, day: day ?? -1 })
) )
} }
@ -50,4 +57,5 @@ export interface SerializedSetCategoryExtraTimeAction {
type: 'SET_CATEGORY_EXTRA_TIME' type: 'SET_CATEGORY_EXTRA_TIME'
categoryId: string categoryId: string
newExtraTime: number newExtraTime: number
day?: number
} }

View file

@ -370,6 +370,9 @@ const definitions = {
}, },
"addedExtraTime": { "addedExtraTime": {
"type": "number" "type": "number"
},
"day": {
"type": "number"
} }
}, },
"additionalProperties": false, "additionalProperties": false,
@ -483,6 +486,9 @@ const definitions = {
}, },
"newExtraTime": { "newExtraTime": {
"type": "number" "type": "number"
},
"day": {
"type": "number"
} }
}, },
"additionalProperties": false, "additionalProperties": false,

View file

@ -61,9 +61,13 @@ export interface CategoryAttributesVersion7 {
sort: number sort: number
} }
export interface CategoryAttributesVersion8 {
extraTimeDay: number
}
export type CategoryAttributes = CategoryAttributesVersion1 & CategoryAttributesVersion2 & export type CategoryAttributes = CategoryAttributesVersion1 & CategoryAttributesVersion2 &
CategoryAttributesVersion3 & CategoryAttributesVersion4 & CategoryAttributesVersion5 & CategoryAttributesVersion3 & CategoryAttributesVersion4 & CategoryAttributesVersion5 &
CategoryAttributesVersion6 & CategoryAttributesVersion7 CategoryAttributesVersion6 & CategoryAttributesVersion7 & CategoryAttributesVersion8
export type CategoryModel = Sequelize.Model & CategoryAttributes export type CategoryModel = Sequelize.Model & CategoryAttributes
export type CategoryModelStatic = typeof Sequelize.Model & { export type CategoryModelStatic = typeof Sequelize.Model & {
@ -172,6 +176,17 @@ export const attributesVersion7: SequelizeAttributes<CategoryAttributesVersion7>
} }
} }
export const attributesVersion8: SequelizeAttributes<CategoryAttributesVersion8> = {
extraTimeDay: {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: -1,
validate: {
min: -1
}
}
}
export const attributes: SequelizeAttributes<CategoryAttributes> = { export const attributes: SequelizeAttributes<CategoryAttributes> = {
...attributesVersion1, ...attributesVersion1,
...attributesVersion2, ...attributesVersion2,
@ -179,7 +194,8 @@ export const attributes: SequelizeAttributes<CategoryAttributes> = {
...attributesVersion4, ...attributesVersion4,
...attributesVersion5, ...attributesVersion5,
...attributesVersion6, ...attributesVersion6,
...attributesVersion7 ...attributesVersion7,
...attributesVersion8
} }
export const createCategoryModel = (sequelize: Sequelize.Sequelize): CategoryModelStatic => sequelize.define('Category', attributes) as CategoryModelStatic export const createCategoryModel = (sequelize: Sequelize.Sequelize): CategoryModelStatic => sequelize.define('Category', attributes) as CategoryModelStatic

View file

@ -0,0 +1,39 @@
/*
* 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 { attributesVersion8 as categoryAttributes } from '../../category'
export async function up (queryInterface: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
await queryInterface.addColumn('Categories', 'extraTimeDay', {
...categoryAttributes.extraTimeDay
}, {
transaction
})
})
}
export async function down (queryInterface: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
await queryInterface.removeColumn('Categories', 'extraTimeDay', { transaction })
})
}

View file

@ -58,6 +58,7 @@ export async function dispatchCreateCategory ({ action, cache }: {
temporarilyBlocked: false, temporarilyBlocked: false,
temporarilyBlockedEndTime: 0, temporarilyBlockedEndTime: 0,
extraTimeInMillis: 0, extraTimeInMillis: 0,
extraTimeDay: -1,
timeLimitRulesVersion: generateVersionId(), timeLimitRulesVersion: generateVersionId(),
baseVersion: generateVersionId(), baseVersion: generateVersionId(),
assignedAppsVersion: generateVersionId(), assignedAppsVersion: generateVersionId(),

View file

@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * 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 * 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,8 +15,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import * as Sequelize from 'sequelize'
import { IncrementCategoryExtraTimeAction } from '../../../../action' import { IncrementCategoryExtraTimeAction } from '../../../../action'
import { CategoryModel } from '../../../../database/category'
import { Cache } from '../cache' import { Cache } from '../cache'
export async function dispatchIncrementCategoryExtraTime ({ action, cache }: { export async function dispatchIncrementCategoryExtraTime ({ action, cache }: {
@ -27,30 +27,22 @@ export async function dispatchIncrementCategoryExtraTime ({ action, cache }: {
throw new Error('action requires full version') throw new Error('action requires full version')
} }
const categoryEntryUnsafe = await cache.database.category.findOne({ async function handleCategory (category: CategoryModel) {
where: { if (action.day === category.extraTimeDay || category.extraTimeDay === -1) {
familyId: cache.familyId, category.extraTimeInMillis += action.addedExtraTime
categoryId: action.categoryId } else {
}, category.extraTimeInMillis = action.addedExtraTime
transaction: cache.transaction, }
attributes: [
'childId',
'parentCategoryId'
]
})
if (!categoryEntryUnsafe) { category.extraTimeDay = action.day
throw new Error(`tried to add extra time to ${action.categoryId} but it does not exist`)
await category.save({ transaction: cache.transaction })
cache.categoriesWithModifiedBaseData.push(category.categoryId)
cache.areChangesImportant = true
} }
const categoryEntry = { const categoryEntry = await cache.database.category.findOne({
childId: categoryEntryUnsafe.childId,
parentCategoryId: categoryEntryUnsafe.parentCategoryId
}
await cache.database.category.update({
extraTimeInMillis: Sequelize.literal(`extraTimeInMillis + ${action.addedExtraTime}`) as any
}, {
where: { where: {
familyId: cache.familyId, familyId: cache.familyId,
categoryId: action.categoryId categoryId: action.categoryId
@ -58,23 +50,23 @@ export async function dispatchIncrementCategoryExtraTime ({ action, cache }: {
transaction: cache.transaction transaction: cache.transaction
}) })
cache.categoriesWithModifiedBaseData.push(action.categoryId) if (!categoryEntry) {
cache.areChangesImportant = true throw new Error(`tried to add extra time to ${action.categoryId} but it does not exist`)
}
await handleCategory(categoryEntry)
if (categoryEntry.parentCategoryId !== '') { if (categoryEntry.parentCategoryId !== '') {
const [affectedRows] = await cache.database.category.update({ const parentCategoryEntry = await cache.database.category.findOne({
extraTimeInMillis: Sequelize.literal(`extraTimeInMillis + ${action.addedExtraTime}`) as any
}, {
where: { where: {
familyId: cache.familyId, familyId: cache.familyId,
categoryId: categoryEntry.parentCategoryId, categoryId: categoryEntry.parentCategoryId
childId: categoryEntry.childId
}, },
transaction: cache.transaction transaction: cache.transaction
}) })
if (affectedRows !== 0) { if (parentCategoryEntry) {
cache.categoriesWithModifiedBaseData.push(categoryEntry.parentCategoryId) await handleCategory(parentCategoryEntry)
} }
} }
} }

View file

@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * 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 * 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
@ -27,7 +27,8 @@ export async function dispatchSetCategoryExtraTime ({ action, cache }: {
} }
const [affectedRows] = await cache.database.category.update({ const [affectedRows] = await cache.database.category.update({
extraTimeInMillis: action.newExtraTime extraTimeInMillis: action.newExtraTime,
extraTimeDay: action.day
}, { }, {
where: { where: {
familyId: cache.familyId, familyId: cache.familyId,

View file

@ -335,6 +335,7 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
'title', 'title',
'blockedMinutesInWeek', 'blockedMinutesInWeek',
'extraTimeInMillis', 'extraTimeInMillis',
'extraTimeDay',
'temporarilyBlocked', 'temporarilyBlocked',
'baseVersion', 'baseVersion',
'parentCategoryId', 'parentCategoryId',
@ -352,6 +353,7 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
title: item.title, title: item.title,
blockedMinutesInWeek: item.blockedMinutesInWeek, blockedMinutesInWeek: item.blockedMinutesInWeek,
extraTimeInMillis: item.extraTimeInMillis, extraTimeInMillis: item.extraTimeInMillis,
extraTimeDay: item.extraTimeDay,
temporarilyBlocked: item.temporarilyBlocked, temporarilyBlocked: item.temporarilyBlocked,
baseVersion: item.baseVersion, baseVersion: item.baseVersion,
parentCategoryId: item.parentCategoryId, parentCategoryId: item.parentCategoryId,
@ -369,6 +371,7 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
title: item.title, title: item.title,
blockedTimes: item.blockedMinutesInWeek, blockedTimes: item.blockedMinutesInWeek,
extraTime: item.extraTimeInMillis, extraTime: item.extraTimeInMillis,
extraTimeDay: item.extraTimeDay,
tempBlocked: item.temporarilyBlocked, tempBlocked: item.temporarilyBlocked,
version: item.baseVersion, version: item.baseVersion,
parentCategoryId: item.parentCategoryId, parentCategoryId: item.parentCategoryId,

View file

@ -99,6 +99,7 @@ export interface ServerUpdatedCategoryBaseData {
title: string title: string
blockedTimes: string // blockedMinutesInWeek blockedTimes: string // blockedMinutesInWeek
extraTime: number extraTime: number
extraTimeDay: number
tempBlocked: boolean tempBlocked: boolean
tempBlockTime: number tempBlockTime: number
version: string version: string