Allow adding category network ids

This commit is contained in:
Jonas Lochmann
2020-08-24 02:00:00 +02:00
parent 9730d13b2e
commit eb6f668b43
34 changed files with 1257 additions and 62 deletions
@@ -0,0 +1,73 @@
/*
* 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 { AddCategoryNetworkIdAction } from '../../../../action'
import { maxNetworkIdsPerCategory } from '../../../../database/categorynetworkid'
import { Cache } from '../cache'
export async function dispatchAddCategoryNetworkId ({ action, cache }: {
action: AddCategoryNetworkIdAction
cache: Cache
}) {
const categoryEntryUnsafe = await cache.database.category.findOne({
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction,
attributes: ['childId']
})
if (!categoryEntryUnsafe) {
throw new Error('invalid category id for new rule')
}
const count = await cache.database.categoryNetworkId.count({
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction
})
if (count + 1 > maxNetworkIdsPerCategory) {
throw new Error('category network limit reached')
}
const hasOldItem = (await cache.database.categoryNetworkId.count({
where: {
familyId: cache.familyId,
categoryId: action.categoryId,
networkItemId: action.itemId
},
transaction: cache.transaction
})) !== 0
if (hasOldItem) {
throw new Error('id already used')
}
await cache.database.categoryNetworkId.create({
familyId: cache.familyId,
categoryId: action.categoryId,
networkItemId: action.itemId,
hashedNetworkId: action.hashedNetworkId
}, { transaction: cache.transaction })
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.areChangesImportant = true
}
@@ -17,6 +17,7 @@
import {
AddCategoryAppsAction,
AddCategoryNetworkIdAction,
AddUserAction,
ChangeParentPasswordAction,
CreateCategoryAction,
@@ -29,6 +30,7 @@ import {
RemoveCategoryAppsAction,
RemoveUserAction,
RenameChildAction,
ResetCategoryNetworkIdsAction,
ResetParentBlockedTimesAction,
SetCategoryExtraTimeAction,
SetCategoryForUnassignedAppsAction,
@@ -61,6 +63,7 @@ import {
} from '../../../../action'
import { Cache } from '../cache'
import { dispatchAddCategoryApps } from './addcategoryapps'
import { dispatchAddCategoryNetworkId } from './addcategorynetworkid'
import { dispatchAddUser } from './adduser'
import { dispatchChangeParentPassword } from './changeparentpassword'
import { dispatchCreateCategory } from './createcategory'
@@ -72,6 +75,7 @@ import { dispatchIncrementCategoryExtraTime } from './incrementcategoryextratime
import { dispatchRemoveCategoryApps } from './removecategoryapps'
import { dispatchRemoveUser } from './removeuser'
import { dispatchRenameChild } from './renamechild'
import { dispatchResetCategoryNetworkIds } from './resetcategorynetworkids'
import { dispatchResetParentBlockedTimes } from './resetparentblockedtimes'
import { dispatchSetCategoryExtraTime } from './setcategoryextratime'
import { dispatchSetCategoryForUnassignedApps } from './setcategoryforunassignedapps'
@@ -124,7 +128,9 @@ export const dispatchParentAction = async ({ action, cache, parentUserId, source
}
if (fromChildSelfLimitAddChildUserId === null) {
if (action instanceof AddUserAction) {
if (action instanceof AddCategoryNetworkIdAction) {
return dispatchAddCategoryNetworkId({ action, cache })
} else if (action instanceof AddUserAction) {
return dispatchAddUser({ action, cache })
} else if (action instanceof RemoveCategoryAppsAction) {
return dispatchRemoveCategoryApps({ action, cache })
@@ -178,6 +184,8 @@ export const dispatchParentAction = async ({ action, cache, parentUserId, source
return dispatchUpdateTimelimitRule({ action, cache })
} else if (action instanceof RemoveUserAction) {
return dispatchRemoveUser({ action, cache, parentUserId })
} else if (action instanceof ResetCategoryNetworkIdsAction) {
return dispatchResetCategoryNetworkIds({ action, cache })
} else if (action instanceof RenameChildAction) {
return dispatchRenameChild({ action, cache })
} else if (action instanceof ChangeParentPasswordAction) {
@@ -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 { ResetCategoryNetworkIdsAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchResetCategoryNetworkIds ({ action, cache }: {
action: ResetCategoryNetworkIdsAction
cache: Cache
}) {
const categoryEntryUnsafe = await cache.database.category.findOne({
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction,
attributes: ['childId']
})
if (!categoryEntryUnsafe) {
throw new Error('invalid category id for new rule')
}
await cache.database.categoryNetworkId.destroy({
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction
})
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.areChangesImportant = true
}
+26 -1
View File
@@ -397,6 +397,25 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
sort: item.sort
}))
const networkIdsForSyncing = (await database.categoryNetworkId.findAll({
where: {
familyId,
categoryId: {
[Sequelize.Op.in]: categoryIdsToSyncBaseData
}
},
attributes: [
'categoryId',
'networkItemId',
'hashedNetworkId'
],
transaction
})).map((item) => ({
categoryId: item.categoryId,
networkItemId: item.networkItemId,
hashedNetworkId: item.hashedNetworkId
}))
result.categoryBase = dataForSyncing.map((item): ServerUpdatedCategoryBaseData => ({
categoryId: item.categoryId,
childId: item.childId,
@@ -412,7 +431,13 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
mblMobile: item.minBatteryMobile,
mblCharging: item.minBatteryCharging,
tempBlockTime: item.temporarilyBlockedEndTime,
sort: item.sort
sort: item.sort,
networks: networkIdsForSyncing
.filter((network) => network.categoryId === item.categoryId)
.map((network) => ({
itemId: network.networkItemId,
hashedNetworkId: network.hashedNetworkId
}))
}))
}