Browse Source

项目关系绑定

dev
巴林闲侠 2 years ago
parent
commit
c69ab08c21
  1. 4
      api/app/lib/controllers/alarm/app.js
  2. 192
      api/app/lib/controllers/project/bind.js
  3. 17
      api/app/lib/controllers/project/index.js
  4. 2
      api/app/lib/controllers/push/config.js
  5. 8
      api/app/lib/middlewares/authenticator.js

4
api/app/lib/controllers/alarm/app.js

@ -108,10 +108,10 @@ async function notedInspection (ctx) {
try {
const models = ctx.fs.dc.models;
const { inspectionId } = ctx.request.body
const { userId } = ctx.fs.api
const { userId, pepUserId } = ctx.fs.api
await models.AppInspection.update({
notedPepUserId: userId,
notedPepUserId: pepUserId,
notedTime: moment().format()
}, {
where: {

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

@ -1,74 +1,148 @@
'use strict';
const moment = require('moment')
async function bindAnxin2pep (ctx) {
const transaction = await ctx.fs.dc.orm.transaction();
try {
// const models = ctx.fs.dc.models;
// const { clickHouse } = ctx.app.fs
// const { bindId, name, pepProjectId, anxinProjectId = [], app = [] } = ctx.request.body
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
// let bindId_ = bindId
// const existRes = await models.ProjectCorrelation.findOne({
// where: {
// pepProjectId: pepProjectId
// }
// })
let bindId_ = bindId
const now = moment()
const existRes = await models.ProjectCorrelation.findOne({
where: {
pepProjectId: pepProjectId
},
include: {
model: models.ProjectApp
}
})
// let storageData = {
// name, pepProjectId, anxinProjectId,
// }
// if (bindId_) {
// if (!existRes) {
// throw '尚无已绑定的项企项目'
// }
// // 修改
// await models.ProjectCorrelation.update(storageData, {
// where: {
// pepProjectId: pepProjectId
// },
// transaction
// })
// } else {
// // 新增
// if (existRes) {
// // 但是有之前的数据
// if (existRes.del) {
// // 不过之前的删除了
let storageData = {
name, pepProjectId, anxinProjectId,
del: false,
}
// 已经创建过的 app
let existApp = []
// 新增的
let createAppData = []
// url 相同需要更新的 解除 lock 状态
let updateAppData = []
// 没有出现但存在的 叠加 lock 状态
let lockAppData = []
// } else {
// // 没有删除 重复添加
// throw '当前项企项目已绑定'
// }
// } else {
// const createRes = await models.ProjectCorrelation.create(storageData, {
// transaction
// })
// bindId_ = createRes.id
if (bindId_) {
if (!existRes) {
throw '尚无已绑定的项企项目'
}
// 修改
await models.ProjectCorrelation.update(storageData, {
where: {
pepProjectId: pepProjectId
},
transaction
})
existApp = existRes.projectApps
} else {
// 新增
if (existRes) {
// 但是有之前的数据
if (existRes.del) {
// 不过之前的删除了
await models.ProjectCorrelation.update(storageData, {
where: {
pepProjectId: pepProjectId
},
transaction
})
existApp = existRes.projectApps
} else {
// 没有删除 重复添加
throw '当前项企项目已绑定'
}
} else {
storageData.createUser = userId;
storageData.createTime = now.format()
const createRes = await models.ProjectCorrelation.create(storageData, {
transaction
})
bindId_ = createRes.id
}
}
// await models.ProjectApp.bulkCreate(app.map((a, i, arr) => {
// 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 地址错误`
// }
// for (let ii = i; ii < arr.length; ii++) {
// let
// }
app.forEach((a, i, arr) => {
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} 地址重复`
}
}
// return {
// name: a.name,
// url: a.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,
})
}
lockAppData = existApp.filter(esa => !esa.addAgain).map(esa => {
return {
id: esa.id,
// name: esa.name,
// url: esa.url,
// projectId: bindId_,
// lock: false,
// }
// }))
// }
// }
lock: true,
}
})
})
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;
await transaction.commit();
ctx.status = 204;
} catch (error) {
await transaction.rollback();
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);

17
api/app/lib/controllers/project/index.js

@ -24,13 +24,14 @@ async function pomsProject (ctx) {
try {
const models = ctx.fs.dc.models;
const { clickHouse } = ctx.app.fs
const { userId } = ctx.fs.api
const { userId, pepUserId } = ctx.fs.api
const { limit, page } = ctx.query
let findOption = {
where: {
del: false
},
distinct: true,
include: {
model: models.ProjectApp,
where: {
@ -49,14 +50,11 @@ async function pomsProject (ctx) {
findOption.offset = page * limit
}
const proRes = await models.ProjectCorrelation.findAll(findOption)
delete findOption.limit
delete findOption.offset
const proCount = await models.ProjectCorrelation.count(findOption)
const proRes = await models.ProjectCorrelation.findAndCountAll(findOption)
let pepProjectIds = new Set()
let anxinProjectIds = new Set()
for (let p of proRes) {
for (let p of proRes.rows) {
pepProjectIds.add(p.pepProjectId)
for (let ap of p.anxinProjectId) {
anxinProjectIds.add(ap)
@ -71,7 +69,7 @@ async function pomsProject (ctx) {
[]
for (let p of proRes) {
for (let p of proRes.rows) {
const corPro = pepProjectRes.find(pp => pp.id == p.pepProjectId)
p.dataValues.pepProjectName = corPro.project_name
let nextAnxinProject = anxinProjectRes.filter(ap => p.anxinProjectId.includes(ap.id))
@ -79,10 +77,7 @@ async function pomsProject (ctx) {
delete p.dataValues.anxinProjectId
}
ctx.status = 200;
ctx.body = {
count: proCount,
rows: proRes
}
ctx.body = proRes
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: error`);
ctx.status = 400;

2
api/app/lib/controllers/push/config.js

@ -48,7 +48,7 @@ async function list (ctx) {
async function edit (ctx) {
try {
const models = ctx.fs.dc.models;
const { userId } = ctx.fs.api
const { userId, pepUserId } = ctx.fs.api
const { pushId, name, pepProjectId = [], alarmType = [], receiverPepUserId = [], timeType = [], disable } = ctx.request.body
let storageData = {

8
api/app/lib/middlewares/authenticator.js

@ -76,11 +76,17 @@ let authorizeToken = async function (ctx, token) {
})
const { userInfo, expired } = authorizeRes;
if (expired && moment().valueOf() <= moment(expired).valueOf()) {
const pomsUser = await ctx.app.fs.dc.models.User.findOne({
where: {
pepUserId: userInfo.id
}
})
rslt = {
'authorized': userInfo.authorized,
'resources': (userInfo || {}).resources || [],
};
ctx.fs.api.userId = userInfo.id;
ctx.fs.api.userId = pomsUser.id;
ctx.fs.api.pepUserId = userInfo.id;
ctx.fs.api.userInfo = userInfo;
ctx.fs.api.token = token;
}

Loading…
Cancel
Save