mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
Add child task support
This commit is contained in:
@@ -76,7 +76,8 @@ export async function dispatchCreateCategory ({ action, cache, fromChildSelfLimi
|
||||
blockAllNotifications: false,
|
||||
timeWarningFlags: 0,
|
||||
sort,
|
||||
disableLimitsUntil: 0
|
||||
disableLimitsUntil: 0,
|
||||
taskListVersion: generateVersionId()
|
||||
}, { transaction: cache.transaction })
|
||||
|
||||
// update the cache
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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 { DeleteChildTaskAction } from '../../../../action'
|
||||
import { Cache } from '../cache'
|
||||
import { MissingTaskException } from '../exception/missing-item'
|
||||
|
||||
export async function dispatchDeleteChildTaskAction ({ action, cache }: {
|
||||
action: DeleteChildTaskAction
|
||||
cache: Cache
|
||||
}) {
|
||||
const taskInfoUnsafe = await cache.database.childTask.findOne({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
taskId: action.taskId
|
||||
},
|
||||
transaction: cache.transaction,
|
||||
attributes: ['categoryId']
|
||||
})
|
||||
|
||||
if (taskInfoUnsafe === null) throw new MissingTaskException()
|
||||
|
||||
const taskInfo = { categoryId: taskInfoUnsafe.categoryId }
|
||||
|
||||
await cache.database.childTask.destroy({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
taskId: action.taskId
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
cache.categoriesWithModifiedTasks.add(taskInfo.categoryId)
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import {
|
||||
CreateCategoryAction,
|
||||
CreateTimeLimitRuleAction,
|
||||
DeleteCategoryAction,
|
||||
DeleteChildTaskAction,
|
||||
DeleteTimeLimitRuleAction,
|
||||
IgnoreManipulationAction,
|
||||
IncrementCategoryExtraTimeAction,
|
||||
@@ -32,6 +33,7 @@ import {
|
||||
RenameChildAction,
|
||||
ResetCategoryNetworkIdsAction,
|
||||
ResetParentBlockedTimesAction,
|
||||
ReviewChildTaskAction,
|
||||
SetCategoryExtraTimeAction,
|
||||
SetCategoryForUnassignedAppsAction,
|
||||
SetChildPasswordAction,
|
||||
@@ -53,6 +55,7 @@ import {
|
||||
UpdateCategoryTemporarilyBlockedAction,
|
||||
UpdateCategoryTimeWarningsAction,
|
||||
UpdateCategoryTitleAction,
|
||||
UpdateChildTaskAction,
|
||||
UpdateDeviceNameAction,
|
||||
UpdateEnableActivityLevelBlockingAction,
|
||||
UpdateNetworkTimeVerificationAction,
|
||||
@@ -72,6 +75,7 @@ import { dispatchChangeParentPassword } from './changeparentpassword'
|
||||
import { dispatchCreateCategory } from './createcategory'
|
||||
import { dispatchCreateTimeLimitRule } from './createtimelimitrule'
|
||||
import { dispatchDeleteCategory } from './deletecategory'
|
||||
import { dispatchDeleteChildTaskAction } from './deletechildtaskaction'
|
||||
import { dispatchDeleteTimeLimitRule } from './deletetimelimitrule'
|
||||
import { dispatchIgnoreManipulation } from './ignoremanipulation'
|
||||
import { dispatchIncrementCategoryExtraTime } from './incrementcategoryextratime'
|
||||
@@ -80,6 +84,7 @@ import { dispatchRemoveUser } from './removeuser'
|
||||
import { dispatchRenameChild } from './renamechild'
|
||||
import { dispatchResetCategoryNetworkIds } from './resetcategorynetworkids'
|
||||
import { dispatchResetParentBlockedTimes } from './resetparentblockedtimes'
|
||||
import { dispatchReviewChildTaskAction } from './reviewchildtaskaction'
|
||||
import { dispatchSetCategoryExtraTime } from './setcategoryextratime'
|
||||
import { dispatchSetCategoryForUnassignedApps } from './setcategoryforunassignedapps'
|
||||
import { dispatchSetChildPassword } from './setchildpassword'
|
||||
@@ -101,6 +106,7 @@ import { dispatchUpdateCategorySorting } from './updatecategorysorting'
|
||||
import { dispatchUpdateCategoryTemporarilyBlocked } from './updatecategorytemporarilyblocked'
|
||||
import { dispatchUpdateCategoryTimeWarnings } from './updatecategorytimewarnings'
|
||||
import { dispatchUpdateCategoryTitle } from './updatecategorytitle'
|
||||
import { dispatchUpdateChildTaskAction } from './updatechildtaskaction'
|
||||
import { dispatchUpdateDeviceName } from './updatedevicename'
|
||||
import { dispatchUpdateEnableActivityLevelBlocking } from './updateenableactivitylevelblocking'
|
||||
import { dispatchUpdateNetworkTimeVerification } from './updatenetworktimeverification'
|
||||
@@ -210,6 +216,12 @@ export const dispatchParentAction = async ({ action, cache, parentUserId, source
|
||||
return dispatchUpdateUserFlagsAction({ action, cache })
|
||||
} else if (action instanceof UpdateUserLimitLoginCategory) {
|
||||
return dispatchUpdateUserLimitLoginCategoryAction({ action, cache, parentUserId })
|
||||
} else if (action instanceof DeleteChildTaskAction) {
|
||||
await dispatchDeleteChildTaskAction({ action, cache })
|
||||
} else if (action instanceof ReviewChildTaskAction) {
|
||||
await dispatchReviewChildTaskAction({ action, cache })
|
||||
} else if (action instanceof UpdateChildTaskAction) {
|
||||
await dispatchUpdateChildTaskAction({ action, cache })
|
||||
} else {
|
||||
throw new ActionObjectTypeNotHandledException()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* 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 { ReviewChildTaskAction } from '../../../../action'
|
||||
import { Cache } from '../cache'
|
||||
import { IllegalStateException } from '../exception/illegal-state'
|
||||
import { MissingTaskException } from '../exception/missing-item'
|
||||
|
||||
export async function dispatchReviewChildTaskAction ({ action, cache }: {
|
||||
action: ReviewChildTaskAction
|
||||
cache: Cache
|
||||
}) {
|
||||
const taskInfo = await cache.database.childTask.findOne({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
taskId: action.taskId
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
if (taskInfo === null) throw new MissingTaskException()
|
||||
|
||||
if (taskInfo.pendingRequest === 0) throw new IllegalStateException({ staticMessage: 'no task review pending' })
|
||||
|
||||
if (action.ok) {
|
||||
const categoryInfoUnsafe = await cache.database.category.findOne({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
categoryId: taskInfo.categoryId
|
||||
},
|
||||
attributes: ['extraTimeInMillis', 'extraTimeDay'],
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
if (categoryInfoUnsafe === null) {
|
||||
throw new IllegalStateException({ staticMessage: 'category referenced from task not found' })
|
||||
}
|
||||
|
||||
const categoryInfo = {
|
||||
extraTimeInMillis: categoryInfoUnsafe.extraTimeInMillis,
|
||||
extraTimeDay: categoryInfoUnsafe.extraTimeDay
|
||||
}
|
||||
|
||||
if (categoryInfo.extraTimeDay !== 0 && categoryInfo.extraTimeInMillis > 0) {
|
||||
// if the current time is daily, then extend the daily time only
|
||||
await cache.database.category.update({
|
||||
extraTimeInMillis: categoryInfo.extraTimeInMillis + taskInfo.extraTimeDuration
|
||||
}, {
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
categoryId: taskInfo.categoryId
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
} else {
|
||||
await cache.database.category.update({
|
||||
extraTimeInMillis: categoryInfo.extraTimeInMillis + taskInfo.extraTimeDuration,
|
||||
extraTimeDay: -1
|
||||
}, {
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
categoryId: taskInfo.categoryId
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
}
|
||||
|
||||
cache.categoriesWithModifiedBaseData.add(taskInfo.categoryId)
|
||||
|
||||
await cache.database.childTask.update({
|
||||
pendingRequest: 0,
|
||||
lastGrantTimestamp: action.time.toString(10)
|
||||
}, {
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
taskId: action.taskId
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
} else {
|
||||
await cache.database.childTask.update({
|
||||
pendingRequest: 0
|
||||
}, {
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
taskId: action.taskId
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
}
|
||||
|
||||
cache.categoriesWithModifiedTasks.add(taskInfo.categoryId)
|
||||
}
|
||||
@@ -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 { UpdateChildTaskAction } from '../../../../action'
|
||||
import { Cache } from '../cache'
|
||||
import { IllegalStateException } from '../exception/illegal-state'
|
||||
import { MissingCategoryException, MissingTaskException } from '../exception/missing-item'
|
||||
|
||||
export async function dispatchUpdateChildTaskAction ({ action, cache }: {
|
||||
action: UpdateChildTaskAction
|
||||
cache: Cache
|
||||
}) {
|
||||
const categoryInfoUnsafe = await cache.database.category.findOne({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
categoryId: action.categoryId
|
||||
},
|
||||
attributes: ['childId'],
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
if (categoryInfoUnsafe === null) throw new MissingCategoryException()
|
||||
|
||||
const taskInfo = await cache.database.childTask.findOne({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
taskId: action.taskId
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
const notFound = taskInfo === null
|
||||
|
||||
if (notFound !== action.isNew) {
|
||||
if (action.isNew) {
|
||||
throw new IllegalStateException({
|
||||
staticMessage: 'can not create task which exists already'
|
||||
})
|
||||
} else {
|
||||
throw new MissingTaskException()
|
||||
}
|
||||
}
|
||||
|
||||
if (taskInfo === null) {
|
||||
await cache.database.childTask.create({
|
||||
familyId: cache.familyId,
|
||||
taskId: action.taskId,
|
||||
categoryId: action.categoryId,
|
||||
taskTitle: action.taskTitle,
|
||||
extraTimeDuration: action.extraTimeDuration,
|
||||
pendingRequest: 0,
|
||||
lastGrantTimestamp: '0'
|
||||
}, {
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
cache.categoriesWithModifiedTasks.add(action.categoryId)
|
||||
} else {
|
||||
await cache.database.childTask.update({
|
||||
taskTitle: action.taskTitle,
|
||||
categoryId: action.categoryId,
|
||||
extraTimeDuration: action.extraTimeDuration
|
||||
}, {
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
taskId: action.taskId
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
cache.categoriesWithModifiedTasks.add(taskInfo.categoryId)
|
||||
cache.categoriesWithModifiedTasks.add(action.categoryId)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user