Add support for child users adding limits themself

This commit is contained in:
Jonas Lochmann
2020-08-03 02:00:00 +02:00
parent a73af1338e
commit 007b1f2ec9
11 changed files with 375 additions and 99 deletions
@@ -18,16 +18,30 @@
import { CreateTimeLimitRuleAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchCreateTimeLimitRule ({ action, cache }: {
export async function dispatchCreateTimeLimitRule ({ action, cache, fromChildSelfLimitAddChildUserId }: {
action: CreateTimeLimitRuleAction
cache: Cache
fromChildSelfLimitAddChildUserId: string | null
}) {
const doesCategoryExist = await cache.doesCategoryExist(action.rule.categoryId)
const categoryEntryUnsafe = await cache.database.category.findOne({
where: {
familyId: cache.familyId,
categoryId: action.rule.categoryId
},
transaction: cache.transaction,
attributes: ['childId']
})
if (!doesCategoryExist) {
if (!categoryEntryUnsafe) {
throw new Error('invalid category id for new rule')
}
if (fromChildSelfLimitAddChildUserId !== null) {
if (fromChildSelfLimitAddChildUserId !== categoryEntryUnsafe.childId) {
throw new Error('can not add rules for other users')
}
}
await cache.database.timelimitRule.create({
familyId: cache.familyId,
ruleId: action.rule.ruleId,