Add support for sorting categories

This commit is contained in:
Jonas Lochmann 2020-02-10 01:00:00 +01:00
parent 62d39a8fdd
commit 6fc9597d20
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
11 changed files with 213 additions and 7 deletions

View file

@ -55,6 +55,7 @@ export { UpdateAppActivitiesAction } from './updateappactivities'
export { UpdateCategoryBatteryLimitAction } from './updatecategorybatterylimit' export { UpdateCategoryBatteryLimitAction } from './updatecategorybatterylimit'
export { UpdateCategoryBlockAllNotificationsAction } from './updatecategoryblockallnotifications' export { UpdateCategoryBlockAllNotificationsAction } from './updatecategoryblockallnotifications'
export { UpdateCategoryBlockedTimesAction } from './updatecategoryblockedtimes' export { UpdateCategoryBlockedTimesAction } from './updatecategoryblockedtimes'
export { UpdateCategorySortingAction } from './updatecategorysorting'
export { UpdateCategoryTemporarilyBlockedAction } from './updatecategorytemporarilyblocked' export { UpdateCategoryTemporarilyBlockedAction } from './updatecategorytemporarilyblocked'
export { UpdateCategoryTimeWarningsAction } from './updatecategorytimewarnings' export { UpdateCategoryTimeWarningsAction } from './updatecategorytimewarnings'
export { UpdateCategoryTitleAction } from './updatecategorytitle' export { UpdateCategoryTitleAction } from './updatecategorytitle'

View file

@ -45,6 +45,7 @@ import { SerializedSetUserTimezoneAction, SetUserTimezoneAction } from '../setus
import { SerializedUpdateCategoryBatteryLimitAction, UpdateCategoryBatteryLimitAction } from '../updatecategorybatterylimit' import { SerializedUpdateCategoryBatteryLimitAction, UpdateCategoryBatteryLimitAction } from '../updatecategorybatterylimit'
import { SerializedUpdateCategoryBlockAllNotificationsAction, UpdateCategoryBlockAllNotificationsAction } from '../updatecategoryblockallnotifications' import { SerializedUpdateCategoryBlockAllNotificationsAction, UpdateCategoryBlockAllNotificationsAction } from '../updatecategoryblockallnotifications'
import { SerializedUpdateCategoryBlockedTimesAction, UpdateCategoryBlockedTimesAction } from '../updatecategoryblockedtimes' import { SerializedUpdateCategoryBlockedTimesAction, UpdateCategoryBlockedTimesAction } from '../updatecategoryblockedtimes'
import { SerializedUpdateCategorySortingAction, UpdateCategorySortingAction } from '../updatecategorysorting'
import { SerializedUpdateCategoryTemporarilyBlockedAction, UpdateCategoryTemporarilyBlockedAction } from '../updatecategorytemporarilyblocked' import { SerializedUpdateCategoryTemporarilyBlockedAction, UpdateCategoryTemporarilyBlockedAction } from '../updatecategorytemporarilyblocked'
import { SerializedUpdateCategoryTimeWarningsAction, UpdateCategoryTimeWarningsAction } from '../updatecategorytimewarnings' import { SerializedUpdateCategoryTimeWarningsAction, UpdateCategoryTimeWarningsAction } from '../updatecategorytimewarnings'
import { SerializedUpdateCategoryTitleAction, UpdateCategoryTitleAction } from '../updatecategorytitle' import { SerializedUpdateCategoryTitleAction, UpdateCategoryTitleAction } from '../updatecategorytitle'
@ -85,6 +86,7 @@ export type SerializedParentAction =
SerializedUpdateCategoryBatteryLimitAction | SerializedUpdateCategoryBatteryLimitAction |
SerializedUpdateCategoryBlockAllNotificationsAction | SerializedUpdateCategoryBlockAllNotificationsAction |
SerializedUpdateCategoryBlockedTimesAction | SerializedUpdateCategoryBlockedTimesAction |
SerializedUpdateCategorySortingAction |
SerializedUpdateCategoryTemporarilyBlockedAction | SerializedUpdateCategoryTemporarilyBlockedAction |
SerializedUpdateCategoryTimeWarningsAction | SerializedUpdateCategoryTimeWarningsAction |
SerializedUpdateCategoryTitleAction | SerializedUpdateCategoryTitleAction |
@ -154,6 +156,8 @@ export const parseParentAction = (action: SerializedParentAction): ParentAction
return UpdateCategoryBlockAllNotificationsAction.parse(action) return UpdateCategoryBlockAllNotificationsAction.parse(action)
} else if (action.type === 'UPDATE_CATEGORY_BLOCKED_TIMES') { } else if (action.type === 'UPDATE_CATEGORY_BLOCKED_TIMES') {
return UpdateCategoryBlockedTimesAction.parse(action) return UpdateCategoryBlockedTimesAction.parse(action)
} else if (action.type === 'UPDATE_CATEGORY_SORTING') {
return UpdateCategorySortingAction.parse(action)
} else if (action.type === 'UPDATE_CATEGORY_TIME_WARNINGS') { } else if (action.type === 'UPDATE_CATEGORY_TIME_WARNINGS') {
return UpdateCategoryTimeWarningsAction.parse(action) return UpdateCategoryTimeWarningsAction.parse(action)
} else if (action.type === 'UPDATE_CATEGORY_TITLE') { } else if (action.type === 'UPDATE_CATEGORY_TITLE') {

View 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>
}

View file

@ -839,6 +839,28 @@ const definitions = {
"type" "type"
] ]
}, },
"SerializedUpdateCategorySortingAction": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"UPDATE_CATEGORY_SORTING"
]
},
"categoryIds": {
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false,
"required": [
"categoryIds",
"type"
]
},
"SerializedUpdateCategoryTemporarilyBlockedAction": { "SerializedUpdateCategoryTemporarilyBlockedAction": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -1699,6 +1721,9 @@ export const isSerializedParentAction: (value: object) => value is SerializedPar
{ {
"$ref": "#/definitions/SerializedUpdateCategoryBlockedTimesAction" "$ref": "#/definitions/SerializedUpdateCategoryBlockedTimesAction"
}, },
{
"$ref": "#/definitions/SerializedUpdateCategorySortingAction"
},
{ {
"$ref": "#/definitions/SerializedUpdateCategoryTemporarilyBlockedAction" "$ref": "#/definitions/SerializedUpdateCategoryTemporarilyBlockedAction"
}, },

View file

@ -57,9 +57,13 @@ export interface CategoryAttributesVersion6 {
temporarilyBlockedEndTime: number temporarilyBlockedEndTime: number
} }
export interface CategoryAttributesVersion7 {
sort: number
}
export type CategoryAttributes = CategoryAttributesVersion1 & CategoryAttributesVersion2 & export type CategoryAttributes = CategoryAttributesVersion1 & CategoryAttributesVersion2 &
CategoryAttributesVersion3 & CategoryAttributesVersion4 & CategoryAttributesVersion5 & CategoryAttributesVersion3 & CategoryAttributesVersion4 & CategoryAttributesVersion5 &
CategoryAttributesVersion6 CategoryAttributesVersion6 & CategoryAttributesVersion7
export type CategoryModel = Sequelize.Model & CategoryAttributes export type CategoryModel = Sequelize.Model & CategoryAttributes
export type CategoryModelStatic = typeof Sequelize.Model & { 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> = { export const attributes: SequelizeAttributes<CategoryAttributes> = {
...attributesVersion1, ...attributesVersion1,
...attributesVersion2, ...attributesVersion2,
...attributesVersion3, ...attributesVersion3,
...attributesVersion4, ...attributesVersion4,
...attributesVersion5, ...attributesVersion5,
...attributesVersion6 ...attributesVersion6,
...attributesVersion7
} }
export const createCategoryModel = (sequelize: Sequelize.Sequelize): CategoryModelStatic => sequelize.define('Category', attributes) as CategoryModelStatic export const createCategoryModel = (sequelize: Sequelize.Sequelize): CategoryModelStatic => sequelize.define('Category', attributes) as CategoryModelStatic

View file

@ -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 })
})
}

View file

@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * 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 * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * 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') 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 // no version number needs to be updated
await cache.database.category.create({ await cache.database.category.create({
familyId: cache.familyId, familyId: cache.familyId,
@ -53,7 +64,8 @@ export async function dispatchCreateCategory ({ action, cache }: {
usedTimesVersion: generateVersionId(), usedTimesVersion: generateVersionId(),
parentCategoryId: '', parentCategoryId: '',
blockAllNotifications: false, blockAllNotifications: false,
timeWarningFlags: 0 timeWarningFlags: 0,
sort
}, { transaction: cache.transaction }) }, { transaction: cache.transaction })
// update the cache // update the cache

View file

@ -46,6 +46,7 @@ import {
UpdateCategoryBatteryLimitAction, UpdateCategoryBatteryLimitAction,
UpdateCategoryBlockAllNotificationsAction, UpdateCategoryBlockAllNotificationsAction,
UpdateCategoryBlockedTimesAction, UpdateCategoryBlockedTimesAction,
UpdateCategorySortingAction,
UpdateCategoryTemporarilyBlockedAction, UpdateCategoryTemporarilyBlockedAction,
UpdateCategoryTimeWarningsAction, UpdateCategoryTimeWarningsAction,
UpdateCategoryTitleAction, UpdateCategoryTitleAction,
@ -86,6 +87,7 @@ import { dispatchSetUserTimezone } from './setusertimezone'
import { dispatchUpdateCategoryBatteryLimit } from './updatecategorybatterylimit' import { dispatchUpdateCategoryBatteryLimit } from './updatecategorybatterylimit'
import { dispatchUpdateCategoryBlockAllNotifications } from './updatecategoryblockallnotifications' import { dispatchUpdateCategoryBlockAllNotifications } from './updatecategoryblockallnotifications'
import { dispatchUpdateCategoryBlockedTimes } from './updatecategoryblockedtimes' import { dispatchUpdateCategoryBlockedTimes } from './updatecategoryblockedtimes'
import { dispatchUpdateCategorySorting } from './updatecategorysorting'
import { dispatchUpdateCategoryTemporarilyBlocked } from './updatecategorytemporarilyblocked' import { dispatchUpdateCategoryTemporarilyBlocked } from './updatecategorytemporarilyblocked'
import { dispatchUpdateCategoryTimeWarnings } from './updatecategorytimewarnings' import { dispatchUpdateCategoryTimeWarnings } from './updatecategorytimewarnings'
import { dispatchUpdateCategoryTitle } from './updatecategorytitle' import { dispatchUpdateCategoryTitle } from './updatecategorytitle'
@ -148,6 +150,8 @@ export const dispatchParentAction = async ({ action, cache, parentUserId, source
await dispatchUpdateCategoryBlockAllNotifications({ action, cache }) await dispatchUpdateCategoryBlockAllNotifications({ action, cache })
} else if (action instanceof UpdateCategoryBlockedTimesAction) { } else if (action instanceof UpdateCategoryBlockedTimesAction) {
await dispatchUpdateCategoryBlockedTimes({ action, cache }) await dispatchUpdateCategoryBlockedTimes({ action, cache })
} else if (action instanceof UpdateCategorySortingAction) {
await dispatchUpdateCategorySorting({ action, cache })
} else if (action instanceof IncrementCategoryExtraTimeAction) { } else if (action instanceof IncrementCategoryExtraTimeAction) {
await dispatchIncrementCategoryExtraTime({ action, cache }) await dispatchIncrementCategoryExtraTime({ action, cache })
} else if (action instanceof UpdateCategoryTemporarilyBlockedAction) { } else if (action instanceof UpdateCategoryTemporarilyBlockedAction) {

View file

@ -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)
}
}

View file

@ -342,7 +342,8 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
'timeWarningFlags', 'timeWarningFlags',
'minBatteryCharging', 'minBatteryCharging',
'minBatteryMobile', 'minBatteryMobile',
'temporarilyBlockedEndTime' 'temporarilyBlockedEndTime',
'sort'
], ],
transaction transaction
})).map((item) => ({ })).map((item) => ({
@ -358,7 +359,8 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
timeWarningFlags: item.timeWarningFlags, timeWarningFlags: item.timeWarningFlags,
minBatteryCharging: item.minBatteryCharging, minBatteryCharging: item.minBatteryCharging,
minBatteryMobile: item.minBatteryMobile, minBatteryMobile: item.minBatteryMobile,
temporarilyBlockedEndTime: item.temporarilyBlockedEndTime temporarilyBlockedEndTime: item.temporarilyBlockedEndTime,
sort: item.sort
})) }))
result.categoryBase = dataForSyncing.map((item): ServerUpdatedCategoryBaseData => ({ result.categoryBase = dataForSyncing.map((item): ServerUpdatedCategoryBaseData => ({
@ -374,7 +376,8 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
timeWarnings: item.timeWarningFlags, timeWarnings: item.timeWarningFlags,
mblMobile: item.minBatteryMobile, mblMobile: item.minBatteryMobile,
mblCharging: item.minBatteryCharging, mblCharging: item.minBatteryCharging,
tempBlockTime: item.temporarilyBlockedEndTime tempBlockTime: item.temporarilyBlockedEndTime,
sort: item.sort
})) }))
} }

View file

@ -108,6 +108,7 @@ export interface ServerUpdatedCategoryBaseData {
// mbl = minimum battery level // mbl = minimum battery level
mblCharging: number mblCharging: number
mblMobile: number mblMobile: number
sort: number
} }
export interface ServerUpdatedCategoryAssignedApps { export interface ServerUpdatedCategoryAssignedApps {