运维服务中台
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.

110 lines
2.5 KiB

'use strict';
const moment = require('moment')
async function groupList (ctx) {
try {
const { models } = ctx.fs.dc;
const { userId } = ctx.fs.api
const res = await models.ProjectGroup.findAll({
where: {
pomsUserId: userId
},
order: [['id', 'DESC']]
})
ctx.status = 200;
ctx.body = res
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function editGroup (ctx) {
try {
const { models } = ctx.fs.dc;
const { userId } = ctx.fs.api
const { id, name, pomsProjectIds = [] } = ctx.request.body
if (!name || !pomsProjectIds || !pomsProjectIds.length) {
throw '参数错误!'
}
let repeatNameRes = await models.ProjectGroup.findOne({
where: {
pomsUserId: userId,
name,
}
})
let repeatProjectRes = await models.ProjectGroup.findOne({
where: {
pomsUserId: userId,
pomsProjectIds
}
})
if (repeatNameRes && (!id || (id && repeatNameRes.id != id))) {
throw '已有相同名称的分组信息!'
}
if (repeatProjectRes && (!id || (id && repeatProjectRes.id != id))) {
throw '已有相同项目的分组信息!'
}
if (id) {
await models.ProjectGroup.update({
name,
pomsProjectIds,
}, {
where: {
id
}
})
} else {
await models.ProjectGroup.create({
name,
pomsProjectIds,
pomsUserId: userId,
}, {
where: {
id
}
})
}
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
}
}
}
async function delGroup (ctx) {
try {
const { models } = ctx.fs.dc;
await models.ProjectGroup.destroy({
where: {
id: ctx.query.groupId
}
})
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
}
}
}
module.exports = {
groupList,
editGroup,
delGroup,
};