Refactor code to send the status to the client

This commit is contained in:
Jonas Lochmann 2020-09-28 02:00:00 +02:00
parent b17bc42a1e
commit b4fd177afe
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
14 changed files with 1007 additions and 625 deletions

View file

@ -0,0 +1,110 @@
/*
* 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 * as Sequelize from 'sequelize'
import { Database } from '../../../database'
import { ServerUserList } from '../../../object/serverdatastatus'
import { FamilyEntry } from './family-entry'
export async function getUserList ({ database, transaction, familyEntry }: {
database: Database
transaction: Sequelize.Transaction
familyEntry: FamilyEntry
}): Promise<ServerUserList> {
const users = (await database.user.findAll({
where: {
familyId: familyEntry.familyId
},
attributes: [
'userId',
'name',
'passwordHash',
'secondPasswordSalt',
'type',
'timeZone',
'disableTimelimitsUntil',
'mail',
'currentDevice',
'categoryForNotAssignedApps',
'relaxPrimaryDeviceRule',
'mailNotificationFlags',
'blockedTimes',
'flags'
],
transaction
})).map((item) => ({
userId: item.userId,
name: item.name,
passwordHash: item.passwordHash,
secondPasswordSalt: item.secondPasswordSalt,
type: item.type,
timeZone: item.timeZone,
disableTimelimitsUntil: item.disableTimelimitsUntil,
mail: item.mail,
currentDevice: item.currentDevice,
categoryForNotAssignedApps: item.categoryForNotAssignedApps,
relaxPrimaryDeviceRule: item.relaxPrimaryDeviceRule,
mailNotificationFlags: item.mailNotificationFlags,
blockedTimes: item.blockedTimes,
flags: item.flags
}))
const limitLoginCategories = (await database.userLimitLoginCategory.findAll({
where: {
familyId: familyEntry.familyId
},
attributes: [
'userId',
'categoryId'
],
transaction
})).map((item) => ({
userId: item.userId,
categoryId: item.categoryId
}))
const getLimitLoginCategory = (userId: string) => {
const item = limitLoginCategories.find((item) => item.userId === userId)
if (item) {
return item.categoryId
} else {
return undefined
}
}
return {
version: familyEntry.userListVersion,
data: users.map((item) => ({
id: item.userId,
name: item.name,
password: item.passwordHash,
secondPasswordSalt: item.secondPasswordSalt,
type: item.type,
timeZone: item.timeZone,
disableLimitsUntil: parseInt(item.disableTimelimitsUntil, 10),
mail: item.mail,
currentDevice: item.currentDevice,
categoryForNotAssignedApps: item.categoryForNotAssignedApps,
relaxPrimaryDevice: item.relaxPrimaryDeviceRule,
mailNotificationFlags: item.mailNotificationFlags,
blockedTimes: item.blockedTimes,
flags: parseInt(item.flags, 10),
llc: getLimitLoginCategory(item.userId)
}))
}
}