mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
Add support for child users adding limits themself
This commit is contained in:
@@ -16,11 +16,13 @@
|
||||
*/
|
||||
|
||||
import { SetParentCategoryAction } from '../../../../action'
|
||||
import { getCategoryWithParentCategories } from '../../../../util/category'
|
||||
import { Cache } from '../cache'
|
||||
|
||||
export async function dispatchSetParentCategory ({ action, cache }: {
|
||||
export async function dispatchSetParentCategory ({ action, cache, fromChildSelfLimitAddChildUserId }: {
|
||||
action: SetParentCategoryAction
|
||||
cache: Cache
|
||||
fromChildSelfLimitAddChildUserId: string | null
|
||||
}) {
|
||||
const categoryEntry = await cache.database.category.findOne({
|
||||
where: {
|
||||
@@ -34,6 +36,12 @@ export async function dispatchSetParentCategory ({ action, cache }: {
|
||||
throw new Error('tried to set parent category of non existent category')
|
||||
}
|
||||
|
||||
if (fromChildSelfLimitAddChildUserId !== null) {
|
||||
if (categoryEntry.childId !== fromChildSelfLimitAddChildUserId) {
|
||||
throw new Error('can not set parent category for other user')
|
||||
}
|
||||
}
|
||||
|
||||
if (action.parentCategory !== '') {
|
||||
const categoriesByUserId = (await cache.database.category.findAll({
|
||||
where: {
|
||||
@@ -74,6 +82,16 @@ export async function dispatchSetParentCategory ({ action, cache }: {
|
||||
if (childCategoryIds.has(action.parentCategory) || action.parentCategory === action.categoryId) {
|
||||
throw new Error('can not set a category as parent which is a child of the category')
|
||||
}
|
||||
|
||||
if (fromChildSelfLimitAddChildUserId !== null) {
|
||||
const ownParentCategory = categoriesByUserId.find((item) => item.categoryId === categoryEntry.parentCategoryId)
|
||||
const enableDueToLimitAddingWhenChild = ownParentCategory === undefined ||
|
||||
getCategoryWithParentCategories(categoriesByUserId, action.parentCategory).indexOf(ownParentCategory.categoryId) !== -1
|
||||
|
||||
if (!enableDueToLimitAddingWhenChild) {
|
||||
throw new Error('can not change parent categories in a way which reduces limits')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await cache.database.category.update({
|
||||
|
||||
Reference in New Issue
Block a user