Allow blocked time area extension with the self limitation

This commit is contained in:
Jonas Lochmann
2020-08-31 02:00:00 +02:00
parent eb6f668b43
commit a6723430ca
4 changed files with 46 additions and 13 deletions
@@ -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
@@ -15,14 +15,49 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { UpdateCategoryBlockedTimesAction } from '../../../../action'
import { blockedTimesBitmaskLength, UpdateCategoryBlockedTimesAction } from '../../../../action/updatecategoryblockedtimes'
import { validateAndParseBitmask } from '../../../../util/bitmask'
import { Cache } from '../cache'
export async function dispatchUpdateCategoryBlockedTimes ({ action, cache }: {
export async function dispatchUpdateCategoryBlockedTimes ({ action, cache, fromChildSelfLimitAddChildUserId }: {
action: UpdateCategoryBlockedTimesAction
cache: Cache
fromChildSelfLimitAddChildUserId: string | null
}) {
const [affectedRows] = await cache.database.category.update({
const categoryEntryUnsafe = await cache.database.category.findOne({
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction,
attributes: ['childId', 'blockedMinutesInWeek']
})
if (!categoryEntryUnsafe) {
throw new Error('can not update blocked time areas for a category which does not exist')
}
const categoryEntry = {
childId: categoryEntryUnsafe.childId,
blockedMinutesInWeek: categoryEntryUnsafe.blockedMinutesInWeek
}
if (fromChildSelfLimitAddChildUserId !== null) {
if (categoryEntry.childId !== fromChildSelfLimitAddChildUserId) {
throw new Error('can not update blocked time areas for other child users')
}
const oldBlocked = validateAndParseBitmask(categoryEntry.blockedMinutesInWeek, blockedTimesBitmaskLength)
const newBlocked = validateAndParseBitmask(action.blockedTimes, blockedTimesBitmaskLength)
oldBlocked.forEach((value, index) => {
if (value && !newBlocked[index]) {
throw new Error('new blocked time areas are smaller')
}
})
}
await cache.database.category.update({
blockedMinutesInWeek: action.blockedTimes
}, {
where: {
@@ -32,10 +67,6 @@ export async function dispatchUpdateCategoryBlockedTimes ({ action, cache }: {
transaction: cache.transaction
})
if (affectedRows === 0) {
throw new Error('invalid category id provided')
}
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.areChangesImportant = true
}