运维服务中台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
7.0 KiB

2 years ago
'use strict';
2 years ago
const moment = require('moment')
2 years ago
async function allDeps (ctx) {
try {
const models = ctx.fs.dc.models;
const { redis } = ctx.app
2 years ago
const start = moment()
2 years ago
let depRes = await redis.get('allDepartments')
2 years ago
console.log(`DurationCalc: 1 用时 ${moment().diff(start, 'milliseconds')}`);
2 years ago
if (depRes) {
depRes = JSON.parse(depRes)
2 years ago
console.log(`DurationCalc: 2 用时 ${moment().diff(start, 'milliseconds')}`);
2 years ago
depRes = depRes.departments
2 years ago
console.log(`DurationCalc: 3 用时 ${moment().diff(start, 'milliseconds')}`);
2 years ago
}
2 years ago
console.log(`DurationCalc: 4 用时 ${moment().diff(start, 'milliseconds')}`);
2 years ago
ctx.status = 200;
ctx.body = depRes || []
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
}
}
}
async function editUser (ctx) {
try {
const models = ctx.fs.dc.models;
const { pomsUserId, pepUserId, role = [], correlationProject = [] } = ctx.request.body
2 years ago
const existUserRes = await models.User.findOne({
where: {
pepUserId
}
})
if (
existUserRes && !existUserRes.deleted && !pomsUserId &&
(
(existUserRes.role.includes('admin') && role.includes('admin'))
|| (!existUserRes.role.includes('admin') && !role.includes('admin'))
// 都有或都没有管理员
)
) {
2 years ago
// 新增已存在未删除
throw '人员账号已添加'
}
2 years ago
let storageData = {
pepUserId,
role,
correlationProject,
updateTime: moment().format()
}
if (existUserRes) {
// 存在且传递id 或者 不传id也存在
// 修改 update
storageData.deleted = false
2 years ago
if (
role.includes('admin')
2 years ago
) {
2 years ago
if (existUserRes.role.includes('admin')) {
// 已是管理员
throw '当前人员已是管理员'
}
// 正在修改为管理员
storageData.disabled = true
storageData.role = [...new Set([...existUserRes.role, ...role])]
} else if (existUserRes.role.includes('admin')) {
// 正在修改成员 但是此时还是管理员
storageData.disabled = true
storageData.role = [...role, 'admin']
2 years ago
}
await models.User.update(storageData, {
where: {
pepUserId
}
})
} else {
// 新增
await models.User.create(storageData)
}
ctx.status = 204
2 years ago
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
2 years ago
message: typeof error == 'string' ? error : undefined
2 years ago
}
}
}
async function putUser (ctx) {
try {
const models = ctx.fs.dc.models;
const { pomsUserId } = ctx.params
const { disabled = undefined, deleted = undefined } = ctx.request.body
const existUserRes = await models.User.findOne({
where: {
id: pomsUserId
}
})
if (existUserRes && existUserRes.role.includes('admin') && disabled == false) {
2 years ago
throw '成员不能既是管理员又是普通成员'
}
2 years ago
const updateData = {
disabled,
deleted,
}
for (let k in updateData) {
if (updateData[k] == undefined) {
delete updateData[k]
}
}
2 years ago
await models.User.update(updateData, {
where: {
id: pomsUserId
}
})
ctx.status = 204
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
2 years ago
}
}
}
2 years ago
async function delAdmin (ctx) {
try {
const models = ctx.fs.dc.models;
const { pomsUserId } = ctx.params
const existUserRes = await models.User.findOne({
where: {
id: pomsUserId
}
})
if (existUserRes) {
let updateValues = existUserRes.dataValues
let adminIndex = updateValues.role.findIndex(r => r == 'admin')
if (adminIndex > -1) {
updateValues.role.splice(adminIndex, 1)
}
await models.User.update(updateValues, {
where: {
id: pomsUserId
}
})
}
ctx.status = 204;
} catch (error) {
2 years ago
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
2 years ago
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
2 years ago
async function user (ctx) {
try {
const models = ctx.fs.dc.models;
2 years ago
const { clickHouse } = ctx.app.fs
2 years ago
const { role, limit, page, } = ctx.query
const excludeField = ['lastInTime', 'inTimes', 'onlineDuration', 'lastInAddress', 'deleted', 'updateTime']
let findOption = {
attributes: {
exclude: excludeField,
},
where: {
deleted: false,
2 years ago
$not: {
role: { $contained: ['SuperAdmin', 'admin'] }
}
2 years ago
},
order: [['updateTime', 'DESC']]
}
if (role) {
findOption.where.role = { $contains: [role] }
}
if (limit) {
findOption.limit = limit
}
if (page && limit) {
findOption.offset = page * limit
}
const userRes = await models.User.findAndCountAll(findOption)
const adminRes = await models.User.findAll({
where: {
role: { $contains: ['admin'] }
},
attributes: {
exclude: excludeField,
},
order: [['updateTime', 'DESC']]
})
let userIds = new Set()
for (let u of userRes.rows.concat(adminRes)) {
userIds.add(u.pepUserId)
}
let userPepRes = userIds.size ?
await clickHouse.pepEmis.query(`SELECT DISTINCT user.id AS id, "user"."name" AS name, department.name AS depName, department.id AS depId FROM department_user LEFT JOIN user ON department_user.user=user.id LEFT JOIN department ON department.id=department_user.department WHERE user.id IN (${[...userIds].join(',')}) AND department.delete=false`).toPromise() :
[]
2 years ago
for (let u of userRes.rows.concat(adminRes)) {
const corUsers = userPepRes.filter(up => up.id == u.pepUserId)
u.dataValues.name = corUsers.length ? corUsers[0].name : ''
u.dataValues.departments = corUsers.length ? corUsers.map(cu => {
return {
name: cu.depName,
id: cu.depId
}
}) : []
}
2 years ago
2 years ago
ctx.status = 200
ctx.body = {
admin: adminRes,
users: userRes
}
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
}
}
}
module.exports = {
allDeps,
editUser,
putUser,
2 years ago
delAdmin,
2 years ago
user,
};