mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2025-10-03 01:39:31 +02:00
Add support for sorting categories
This commit is contained in:
parent
62d39a8fdd
commit
6fc9597d20
11 changed files with 213 additions and 7 deletions
|
@ -55,6 +55,7 @@ export { UpdateAppActivitiesAction } from './updateappactivities'
|
|||
export { UpdateCategoryBatteryLimitAction } from './updatecategorybatterylimit'
|
||||
export { UpdateCategoryBlockAllNotificationsAction } from './updatecategoryblockallnotifications'
|
||||
export { UpdateCategoryBlockedTimesAction } from './updatecategoryblockedtimes'
|
||||
export { UpdateCategorySortingAction } from './updatecategorysorting'
|
||||
export { UpdateCategoryTemporarilyBlockedAction } from './updatecategorytemporarilyblocked'
|
||||
export { UpdateCategoryTimeWarningsAction } from './updatecategorytimewarnings'
|
||||
export { UpdateCategoryTitleAction } from './updatecategorytitle'
|
||||
|
|
|
@ -45,6 +45,7 @@ import { SerializedSetUserTimezoneAction, SetUserTimezoneAction } from '../setus
|
|||
import { SerializedUpdateCategoryBatteryLimitAction, UpdateCategoryBatteryLimitAction } from '../updatecategorybatterylimit'
|
||||
import { SerializedUpdateCategoryBlockAllNotificationsAction, UpdateCategoryBlockAllNotificationsAction } from '../updatecategoryblockallnotifications'
|
||||
import { SerializedUpdateCategoryBlockedTimesAction, UpdateCategoryBlockedTimesAction } from '../updatecategoryblockedtimes'
|
||||
import { SerializedUpdateCategorySortingAction, UpdateCategorySortingAction } from '../updatecategorysorting'
|
||||
import { SerializedUpdateCategoryTemporarilyBlockedAction, UpdateCategoryTemporarilyBlockedAction } from '../updatecategorytemporarilyblocked'
|
||||
import { SerializedUpdateCategoryTimeWarningsAction, UpdateCategoryTimeWarningsAction } from '../updatecategorytimewarnings'
|
||||
import { SerializedUpdateCategoryTitleAction, UpdateCategoryTitleAction } from '../updatecategorytitle'
|
||||
|
@ -85,6 +86,7 @@ export type SerializedParentAction =
|
|||
SerializedUpdateCategoryBatteryLimitAction |
|
||||
SerializedUpdateCategoryBlockAllNotificationsAction |
|
||||
SerializedUpdateCategoryBlockedTimesAction |
|
||||
SerializedUpdateCategorySortingAction |
|
||||
SerializedUpdateCategoryTemporarilyBlockedAction |
|
||||
SerializedUpdateCategoryTimeWarningsAction |
|
||||
SerializedUpdateCategoryTitleAction |
|
||||
|
@ -154,6 +156,8 @@ export const parseParentAction = (action: SerializedParentAction): ParentAction
|
|||
return UpdateCategoryBlockAllNotificationsAction.parse(action)
|
||||
} else if (action.type === 'UPDATE_CATEGORY_BLOCKED_TIMES') {
|
||||
return UpdateCategoryBlockedTimesAction.parse(action)
|
||||
} else if (action.type === 'UPDATE_CATEGORY_SORTING') {
|
||||
return UpdateCategorySortingAction.parse(action)
|
||||
} else if (action.type === 'UPDATE_CATEGORY_TIME_WARNINGS') {
|
||||
return UpdateCategoryTimeWarningsAction.parse(action)
|
||||
} else if (action.type === 'UPDATE_CATEGORY_TITLE') {
|
||||
|
|
56
src/action/updatecategorysorting.ts
Normal file
56
src/action/updatecategorysorting.ts
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* 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 { uniq } from 'lodash'
|
||||
import { assertIdWithinFamily } from '../util/token'
|
||||
import { ParentAction } from './basetypes'
|
||||
|
||||
export class UpdateCategorySortingAction extends ParentAction {
|
||||
readonly categoryIds: Array<string>
|
||||
|
||||
constructor ({ categoryIds }: {
|
||||
categoryIds: Array<string>
|
||||
}) {
|
||||
super()
|
||||
|
||||
if (categoryIds.length === 0) {
|
||||
throw new Error('empty category sorting list')
|
||||
}
|
||||
|
||||
if (uniq(categoryIds).length !== categoryIds.length) {
|
||||
throw new Error('category sorting list has duplicates')
|
||||
}
|
||||
|
||||
categoryIds.forEach((categoryId) => assertIdWithinFamily(categoryId))
|
||||
|
||||
this.categoryIds = categoryIds
|
||||
}
|
||||
|
||||
serialize = (): SerializedUpdateCategorySortingAction => ({
|
||||
type: 'UPDATE_CATEGORY_SORTING',
|
||||
categoryIds: this.categoryIds
|
||||
})
|
||||
|
||||
static parse = ({ categoryIds }: SerializedUpdateCategorySortingAction) => (
|
||||
new UpdateCategorySortingAction({ categoryIds })
|
||||
)
|
||||
}
|
||||
|
||||
export interface SerializedUpdateCategorySortingAction {
|
||||
type: 'UPDATE_CATEGORY_SORTING'
|
||||
categoryIds: Array<string>
|
||||
}
|
|
@ -839,6 +839,28 @@ const definitions = {
|
|||
"type"
|
||||
]
|
||||
},
|
||||
"SerializedUpdateCategorySortingAction": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"type": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"UPDATE_CATEGORY_SORTING"
|
||||
]
|
||||
},
|
||||
"categoryIds": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"categoryIds",
|
||||
"type"
|
||||
]
|
||||
},
|
||||
"SerializedUpdateCategoryTemporarilyBlockedAction": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
@ -1699,6 +1721,9 @@ export const isSerializedParentAction: (value: object) => value is SerializedPar
|
|||
{
|
||||
"$ref": "#/definitions/SerializedUpdateCategoryBlockedTimesAction"
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/SerializedUpdateCategorySortingAction"
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/SerializedUpdateCategoryTemporarilyBlockedAction"
|
||||
},
|
||||
|
|
|
@ -57,9 +57,13 @@ export interface CategoryAttributesVersion6 {
|
|||
temporarilyBlockedEndTime: number
|
||||
}
|
||||
|
||||
export interface CategoryAttributesVersion7 {
|
||||
sort: number
|
||||
}
|
||||
|
||||
export type CategoryAttributes = CategoryAttributesVersion1 & CategoryAttributesVersion2 &
|
||||
CategoryAttributesVersion3 & CategoryAttributesVersion4 & CategoryAttributesVersion5 &
|
||||
CategoryAttributesVersion6
|
||||
CategoryAttributesVersion6 & CategoryAttributesVersion7
|
||||
|
||||
export type CategoryModel = Sequelize.Model & CategoryAttributes
|
||||
export type CategoryModelStatic = typeof Sequelize.Model & {
|
||||
|
@ -157,13 +161,25 @@ export const attributesVersion6: SequelizeAttributes<CategoryAttributesVersion6>
|
|||
}
|
||||
}
|
||||
|
||||
export const attributesVersion7: SequelizeAttributes<CategoryAttributesVersion7> = {
|
||||
sort: {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: false,
|
||||
defaultValue: 0,
|
||||
validate: {
|
||||
min: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const attributes: SequelizeAttributes<CategoryAttributes> = {
|
||||
...attributesVersion1,
|
||||
...attributesVersion2,
|
||||
...attributesVersion3,
|
||||
...attributesVersion4,
|
||||
...attributesVersion5,
|
||||
...attributesVersion6
|
||||
...attributesVersion6,
|
||||
...attributesVersion7
|
||||
}
|
||||
|
||||
export const createCategoryModel = (sequelize: Sequelize.Sequelize): CategoryModelStatic => sequelize.define('Category', attributes) as CategoryModelStatic
|
||||
|
|
|
@ -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 { attributesVersion7 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', 'sort', {
|
||||
...categoryAttributes.sort
|
||||
}, {
|
||||
transaction
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export async function down (queryInterface: QueryInterface, sequelize: Sequelize) {
|
||||
await sequelize.transaction({
|
||||
type: Transaction.TYPES.EXCLUSIVE
|
||||
}, async (transaction) => {
|
||||
await queryInterface.removeColumn('Categories', 'sort', { transaction })
|
||||
})
|
||||
}
|
|
@ -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
|
||||
|
@ -37,6 +37,17 @@ export async function dispatchCreateCategory ({ action, cache }: {
|
|||
throw new Error('missing child for new category')
|
||||
}
|
||||
|
||||
const oldMaxSort: number = await cache.database.category.max('sort', {
|
||||
transaction: cache.transaction,
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
childId: action.childId
|
||||
}
|
||||
})
|
||||
|
||||
// if there are no categories, then this is not a number
|
||||
const sort = Number.isSafeInteger(oldMaxSort + 1) ? (oldMaxSort + 1) : 0
|
||||
|
||||
// no version number needs to be updated
|
||||
await cache.database.category.create({
|
||||
familyId: cache.familyId,
|
||||
|
@ -53,7 +64,8 @@ export async function dispatchCreateCategory ({ action, cache }: {
|
|||
usedTimesVersion: generateVersionId(),
|
||||
parentCategoryId: '',
|
||||
blockAllNotifications: false,
|
||||
timeWarningFlags: 0
|
||||
timeWarningFlags: 0,
|
||||
sort
|
||||
}, { transaction: cache.transaction })
|
||||
|
||||
// update the cache
|
||||
|
|
|
@ -46,6 +46,7 @@ import {
|
|||
UpdateCategoryBatteryLimitAction,
|
||||
UpdateCategoryBlockAllNotificationsAction,
|
||||
UpdateCategoryBlockedTimesAction,
|
||||
UpdateCategorySortingAction,
|
||||
UpdateCategoryTemporarilyBlockedAction,
|
||||
UpdateCategoryTimeWarningsAction,
|
||||
UpdateCategoryTitleAction,
|
||||
|
@ -86,6 +87,7 @@ import { dispatchSetUserTimezone } from './setusertimezone'
|
|||
import { dispatchUpdateCategoryBatteryLimit } from './updatecategorybatterylimit'
|
||||
import { dispatchUpdateCategoryBlockAllNotifications } from './updatecategoryblockallnotifications'
|
||||
import { dispatchUpdateCategoryBlockedTimes } from './updatecategoryblockedtimes'
|
||||
import { dispatchUpdateCategorySorting } from './updatecategorysorting'
|
||||
import { dispatchUpdateCategoryTemporarilyBlocked } from './updatecategorytemporarilyblocked'
|
||||
import { dispatchUpdateCategoryTimeWarnings } from './updatecategorytimewarnings'
|
||||
import { dispatchUpdateCategoryTitle } from './updatecategorytitle'
|
||||
|
@ -148,6 +150,8 @@ export const dispatchParentAction = async ({ action, cache, parentUserId, source
|
|||
await dispatchUpdateCategoryBlockAllNotifications({ action, cache })
|
||||
} else if (action instanceof UpdateCategoryBlockedTimesAction) {
|
||||
await dispatchUpdateCategoryBlockedTimes({ action, cache })
|
||||
} else if (action instanceof UpdateCategorySortingAction) {
|
||||
await dispatchUpdateCategorySorting({ action, cache })
|
||||
} else if (action instanceof IncrementCategoryExtraTimeAction) {
|
||||
await dispatchIncrementCategoryExtraTime({ action, cache })
|
||||
} else if (action instanceof UpdateCategoryTemporarilyBlockedAction) {
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* 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 { UpdateCategorySortingAction } from '../../../../action'
|
||||
import { Cache } from '../cache'
|
||||
|
||||
export async function dispatchUpdateCategorySorting ({ action, cache }: {
|
||||
action: UpdateCategorySortingAction
|
||||
cache: Cache
|
||||
}) {
|
||||
// no validation here:
|
||||
// - only parents can do it
|
||||
// - using it over categories which don't belong together destroys the sorting for both,
|
||||
// but does not cause any trouble
|
||||
|
||||
for (let i = 0; i < action.categoryIds.length; i++) {
|
||||
const categoryId = action.categoryIds[i]
|
||||
|
||||
await cache.database.category.update({
|
||||
sort: i
|
||||
}, {
|
||||
transaction: cache.transaction,
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
categoryId
|
||||
}
|
||||
})
|
||||
|
||||
cache.categoriesWithModifiedBaseData.push(categoryId)
|
||||
}
|
||||
}
|
|
@ -342,7 +342,8 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
|
|||
'timeWarningFlags',
|
||||
'minBatteryCharging',
|
||||
'minBatteryMobile',
|
||||
'temporarilyBlockedEndTime'
|
||||
'temporarilyBlockedEndTime',
|
||||
'sort'
|
||||
],
|
||||
transaction
|
||||
})).map((item) => ({
|
||||
|
@ -358,7 +359,8 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
|
|||
timeWarningFlags: item.timeWarningFlags,
|
||||
minBatteryCharging: item.minBatteryCharging,
|
||||
minBatteryMobile: item.minBatteryMobile,
|
||||
temporarilyBlockedEndTime: item.temporarilyBlockedEndTime
|
||||
temporarilyBlockedEndTime: item.temporarilyBlockedEndTime,
|
||||
sort: item.sort
|
||||
}))
|
||||
|
||||
result.categoryBase = dataForSyncing.map((item): ServerUpdatedCategoryBaseData => ({
|
||||
|
@ -374,7 +376,8 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
|
|||
timeWarnings: item.timeWarningFlags,
|
||||
mblMobile: item.minBatteryMobile,
|
||||
mblCharging: item.minBatteryCharging,
|
||||
tempBlockTime: item.temporarilyBlockedEndTime
|
||||
tempBlockTime: item.temporarilyBlockedEndTime,
|
||||
sort: item.sort
|
||||
}))
|
||||
}
|
||||
|
||||
|
|
|
@ -108,6 +108,7 @@ export interface ServerUpdatedCategoryBaseData {
|
|||
// mbl = minimum battery level
|
||||
mblCharging: number
|
||||
mblMobile: number
|
||||
sort: number
|
||||
}
|
||||
|
||||
export interface ServerUpdatedCategoryAssignedApps {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue