Browse Source

项目绑定信息与应用信息绑定

dev
巴林闲侠 2 years ago
parent
commit
1e1cd4cd20
  1. 93
      api/app/lib/controllers/project/bind.js

93
api/app/lib/controllers/project/bind.js

@ -7,7 +7,7 @@ async function bindAnxin2pep (ctx) {
const models = ctx.fs.dc.models;
const { clickHouse } = ctx.app.fs
const { userId, pepUserId } = ctx.fs.api
const { bindId, name, pepProjectId, anxinProjectId = [], app = [] } = ctx.request.body
const { bindId, name, pepProjectId, anxinProjectId = [], appId = [] } = ctx.request.body
let bindId_ = bindId
const now = moment()
@ -24,16 +24,8 @@ async function bindAnxin2pep (ctx) {
name, pepProjectId, anxinProjectId,
del: false,
}
// 已经创建过的 app
let existApp = []
// 新增的
let createAppData = []
// url 相同需要更新的 解除 lock 状态
let updateAppData = []
// 没有出现但存在的 叠加 lock 状态
let lockAppData = []
if (bindId_) {
if (bindId) {
// 修改
if (!existRes) {
throw '尚无已绑定的项企项目'
@ -47,7 +39,6 @@ async function bindAnxin2pep (ctx) {
},
transaction
})
existApp = existRes.projectApps
} else {
// 新增
if (existRes) {
@ -60,7 +51,6 @@ async function bindAnxin2pep (ctx) {
},
transaction
})
existApp = existRes.projectApps
} else {
// 没有删除 重复添加
throw '当前项企项目已绑定'
@ -75,80 +65,23 @@ async function bindAnxin2pep (ctx) {
}
}
app.forEach((a, i, arr) => {
if (!a.name && !a.url) {
return
}
if (!a.name || !a.url) {
throw `${a.name} ${a.url} 缺少必要参数`
}
let curUrlArr = a.url.split('://')
if (curUrlArr.length < 2) {
throw `${a.name} ${a.url} url 地址错误`
}
curUrlArr.shift()
let curUrl = curUrlArr.join('://')
// 先判断传过来的数据有没有重复
for (let ii = i + 1; ii < arr.length; ii++) {
let curForUrlArr = arr[ii].url.split('://')
curForUrlArr.shift()
if (curUrl == curForUrlArr.join('://')) {
throw `${a.name} ${a.url}${arr[ii].name} ${arr[ii].url} 地址重复`
}
}
// 再判断和已有的有没有重复
let existSameApp = existApp.find(ea => {
let curForUrlArr = ea.url.split('://')
curForUrlArr.shift()
return curUrl == curForUrlArr.join('://')
})
if (existSameApp) {
// 有重复的将已有信息解除锁定并更新
updateAppData.push({
id: existSameApp.id,
name: a.name,
url: a.url,
projectId: bindId_,
lock: false,
})
existSameApp.addAgain = true
} else {
// 没有就创建
createAppData.push({
name: a.name,
url: a.url,
projectId: bindId_,
lock: false,
if (bindId) {
await models.ProjectApp.destroy({
where: {
projectId: bindId
},
transaction
})
}
// 筛选剩余的已有信息并将之锁定
lockAppData = existApp.filter(esa => !esa.addAgain).map(esa => {
await models.ProjectApp.bulkCreate(appId.map(aid => {
return {
id: esa.id,
// name: esa.name,
// url: esa.url,
// projectId: bindId_,
lock: true,
projectId: bindId_,
appId: aid
}
})
})
createAppData.length ?
await models.ProjectApp.bulkCreate(createAppData, {
transaction
}) : null
updateAppData.length || lockAppData.length ?
await Promise.all(updateAppData.concat(lockAppData).map(ud => {
return models.ProjectApp.update(ud, {
where: {
id: ud.id
},
}), {
transaction
})
}))
: null
await transaction.commit();
ctx.status = 204;

Loading…
Cancel
Save