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

129 lines
3.1 KiB

2 years ago
'use strict';
const moment = require('moment')
2 years ago
async function bindAnxin2pep (ctx) {
const transaction = await ctx.fs.dc.orm.transaction();
2 years ago
try {
const models = ctx.fs.dc.models;
const { clickHouse } = ctx.app.fs
const { userId, pepUserId } = ctx.fs.api
const { bindId, name, pepProjectId, anxinProjectId = [], appId = [] } = ctx.request.body
2 years ago
if(name && pepProjectId){
throw '项企项目与自定义项目名称仅可选择一项'
}
let bindId_ = bindId
const now = moment()
const existRes = await models.ProjectCorrelation.findOne({
where: {
pepProjectId: pepProjectId
},
include: {
model: models.ProjectApp
}
})
let storageData = {
name, pepProjectId, anxinProjectId,
del: false,
}
if (bindId) {
// 修改
if (!existRes) {
throw '尚无已绑定的项企项目'
}
if (name) {
}
await models.ProjectCorrelation.update(storageData, {
where: {
pepProjectId: pepProjectId
},
transaction
})
} else {
// 新增
if (existRes) {
// 但是有之前的数据
if (existRes.del) {
// 不过之前的删除了
await models.ProjectCorrelation.update(storageData, {
where: {
pepProjectId: pepProjectId
},
transaction
})
} else {
// 没有删除 重复添加
throw '当前项企项目已绑定'
}
} else {
storageData.createUser = userId;
storageData.createTime = now.format()
const createRes = await models.ProjectCorrelation.create(storageData, {
transaction
})
bindId_ = createRes.id
}
}
if (bindId) {
await models.ProjectApp.destroy({
where: {
projectId: bindId
},
transaction
})
}
await models.ProjectApp.bulkCreate(appId.map(aid => {
return {
projectId: bindId_,
appId: aid
}
}), {
transaction
})
2 years ago
await transaction.commit();
ctx.status = 204;
2 years ago
} catch (error) {
await transaction.rollback();
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
}
}
}
async function del (ctx) {
try {
const models = ctx.fs.dc.models;
const { bindId } = ctx.query
await models.ProjectCorrelation.update({
del: true
}, {
where: {
id: { $in: bindId.split(',') }
}
})
ctx.status = 20;
} 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
module.exports = {
bindAnxin2pep,
del
2 years ago
};