Add new API to increment the used time

This commit is contained in:
Jonas Lochmann
2020-01-27 01:00:00 +01:00
parent f4046e0fc3
commit b55c375506
8 changed files with 257 additions and 4 deletions
+98
View File
@@ -0,0 +1,98 @@
/*
* 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 { AppLogicAction } from './basetypes'
export class AddUsedTimeActionVersion2 extends AppLogicAction {
readonly dayOfEpoch: number
readonly items: Array<{
readonly categoryId: string
readonly timeToAdd: number
readonly extraTimeToSubtract: number
}>
constructor ({ dayOfEpoch, items }: {
dayOfEpoch: number
items: Array<{
categoryId: string
timeToAdd: number
extraTimeToSubtract: number
}>
}) {
super()
if (dayOfEpoch < 0 || (!Number.isSafeInteger(dayOfEpoch))) {
throw new Error('illegal dayOfEpoch')
}
if (items.length === 0) {
throw new Error('missing items')
}
if (items.length !== uniq(items.map((item) => item.categoryId)).length) {
throw new Error('duplicate category ids')
}
items.forEach((item) => {
assertIdWithinFamily(item.categoryId)
if (item.timeToAdd < 0 || (!Number.isSafeInteger(item.timeToAdd))) {
throw new Error('illegal timeToAdd')
}
if (item.extraTimeToSubtract < 0 || (!Number.isSafeInteger(item.extraTimeToSubtract))) {
throw new Error('illegal extra time to subtract')
}
})
this.dayOfEpoch = dayOfEpoch
this.items = items
}
serialize = (): SerializedAddUsedTimeActionVersion2 => ({
type: 'ADD_USED_TIME_V2',
d: this.dayOfEpoch,
i: this.items.map((item) => ({
categoryId: item.categoryId,
tta: item.timeToAdd,
etts: item.extraTimeToSubtract
}))
})
static parse = ({ d, i }: SerializedAddUsedTimeActionVersion2) => (
new AddUsedTimeActionVersion2({
dayOfEpoch: d,
items: i.map((item) => ({
categoryId: item.categoryId,
timeToAdd: item.tta,
extraTimeToSubtract: item.etts
}))
})
)
}
export interface SerializedAddUsedTimeActionVersion2 {
type: 'ADD_USED_TIME_V2'
d: number
i: Array<{
categoryId: string
tta: number
etts: number
}>
}
+1
View File
@@ -21,6 +21,7 @@ export { AddCategoryAppsAction } from './addcategoryapps'
export { AddUserAction } from './adduser'
export { AddInstalledAppsAction } from './addinstalledapps'
export { AddUsedTimeAction } from './addusedtime'
export { AddUsedTimeActionVersion2 } from './addusedtime2'
export { ChangeParentPasswordAction } from './changeparentpassword'
export { ChildChangePasswordAction } from './childchangepassword'
export { ChildSignInAction } from './childsignin'
+5 -1
View File
@@ -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
@@ -17,6 +17,7 @@
import { AddInstalledAppsAction, SerializedAddInstalledAppsAction } from '../addinstalledapps'
import { AddUsedTimeAction, SerializedAddUsedTimeAction } from '../addusedtime'
import { AddUsedTimeActionVersion2, SerializedAddUsedTimeActionVersion2 } from '../addusedtime2'
import { AppLogicAction } from '../basetypes'
import { RemoveInstalledAppsAction, SerializedRemoveInstalledAppsAction } from '../removeinstalledapps'
import { SerializedSignOutAtDeviceAction, SignOutAtDeviceAction } from '../signoutatdevice'
@@ -27,6 +28,7 @@ import { SerializedUpdateDeviceStatusAction, UpdateDeviceStatusAction } from '..
export type SerializedAppLogicAction =
SerializedAddInstalledAppsAction |
SerializedAddUsedTimeAction |
SerializedAddUsedTimeActionVersion2 |
SerializedRemoveInstalledAppsAction |
SerializedSignOutAtDeviceAction |
SerialiezdTriedDisablingDeviceAdminAction |
@@ -36,6 +38,8 @@ export type SerializedAppLogicAction =
export const parseAppLogicAction = (serialized: SerializedAppLogicAction): AppLogicAction => {
if (serialized.type === 'ADD_USED_TIME') {
return AddUsedTimeAction.parse(serialized)
} else if (serialized.type === 'ADD_USED_TIME_V2') {
return AddUsedTimeActionVersion2.parse(serialized)
} else if (serialized.type === 'ADD_INSTALLED_APPS') {
return AddInstalledAppsAction.parse(serialized)
} else if (serialized.type === 'REMOVE_INSTALLED_APPS') {