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 { try {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
const { inspectionId } = ctx.request.body const { inspectionId } = ctx.request.body
const { userId } = ctx.fs.api const { userId, pepUserId } = ctx.fs.api
await models.AppInspection.update({ await models.AppInspection.update({
notedPepUserId: userId, notedPepUserId: pepUserId,
notedTime: moment().format() notedTime: moment().format()
}, { }, {
where: { where: {

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

@ -1,74 +1,148 @@
'use strict'; 'use strict';
const moment = require('moment')
async function bindAnxin2pep (ctx) { async function bindAnxin2pep (ctx) {
const transaction = await ctx.fs.dc.orm.transaction(); const transaction = await ctx.fs.dc.orm.transaction();
try { try {
// const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
// const { clickHouse } = ctx.app.fs const { clickHouse } = ctx.app.fs
// const { bindId, name, pepProjectId, anxinProjectId = [], app = [] } = ctx.request.body const { userId, pepUserId } = ctx.fs.api
const { bindId, name, pepProjectId, anxinProjectId = [], app = [] } = ctx.request.body
// let bindId_ = bindId let bindId_ = bindId
// const existRes = await models.ProjectCorrelation.findOne({ const now = moment()
// where: { const existRes = await models.ProjectCorrelation.findOne({
// pepProjectId: pepProjectId where: {
// } pepProjectId: pepProjectId
// }) },
include: {
model: models.ProjectApp
}
})
// let storageData = { let storageData = {
// name, pepProjectId, anxinProjectId, name, pepProjectId, anxinProjectId,
// } del: false,
// if (bindId_) { }
// if (!existRes) { // 已经创建过的 app
// throw '尚无已绑定的项企项目' let existApp = []
// } // 新增的
// // 修改 let createAppData = []
// await models.ProjectCorrelation.update(storageData, { // url 相同需要更新的 解除 lock 状态
// where: { let updateAppData = []
// pepProjectId: pepProjectId // 没有出现但存在的 叠加 lock 状态
// }, let lockAppData = []
// transaction
// })
// } else {
// // 新增
// if (existRes) {
// // 但是有之前的数据
// if (existRes.del) {
// // 不过之前的删除了
// } else { if (bindId_) {
// // 没有删除 重复添加 if (!existRes) {
// throw '当前项企项目已绑定' throw '尚无已绑定的项企项目'
// } }
// } else { // 修改
// const createRes = await models.ProjectCorrelation.create(storageData, { await models.ProjectCorrelation.update(storageData, {
// transaction where: {
// }) pepProjectId: pepProjectId
// bindId_ = createRes.id },
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) => { app.forEach((a, i, arr) => {
// if (!a.name || !a.url) { if (!a.name || !a.url) {
// throw `${a.name} ${a.url} 缺少必要参数` throw `${a.name} ${a.url} 缺少必要参数`
// } }
// let curUrlArr = a.url.split('://') let curUrlArr = a.url.split('://')
// if(curUrlArr.length < 2){ if (curUrlArr.length < 2) {
// throw `${a.name} ${a.url} url 地址错误` throw `${a.name} ${a.url} url 地址错误`
// } }
// for (let ii = i; ii < arr.length; ii++) { curUrlArr.shift()
// let 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, let existSameApp = existApp.find(ea => {
// url: a.url, 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_, // 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(); await transaction.commit();
// ctx.status = 204; ctx.status = 204;
} catch (error) { } catch (error) {
await transaction.rollback(); await transaction.rollback();
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); 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 { try {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
const { clickHouse } = ctx.app.fs const { clickHouse } = ctx.app.fs
const { userId } = ctx.fs.api const { userId, pepUserId } = ctx.fs.api
const { limit, page } = ctx.query const { limit, page } = ctx.query
let findOption = { let findOption = {
where: { where: {
del: false del: false
}, },
distinct: true,
include: { include: {
model: models.ProjectApp, model: models.ProjectApp,
where: { where: {
@ -49,14 +50,11 @@ async function pomsProject (ctx) {
findOption.offset = page * limit findOption.offset = page * limit
} }
const proRes = await models.ProjectCorrelation.findAll(findOption) const proRes = await models.ProjectCorrelation.findAndCountAll(findOption)
delete findOption.limit
delete findOption.offset
const proCount = await models.ProjectCorrelation.count(findOption)
let pepProjectIds = new Set() let pepProjectIds = new Set()
let anxinProjectIds = new Set() let anxinProjectIds = new Set()
for (let p of proRes) { for (let p of proRes.rows) {
pepProjectIds.add(p.pepProjectId) pepProjectIds.add(p.pepProjectId)
for (let ap of p.anxinProjectId) { for (let ap of p.anxinProjectId) {
anxinProjectIds.add(ap) 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) const corPro = pepProjectRes.find(pp => pp.id == p.pepProjectId)
p.dataValues.pepProjectName = corPro.project_name p.dataValues.pepProjectName = corPro.project_name
let nextAnxinProject = anxinProjectRes.filter(ap => p.anxinProjectId.includes(ap.id)) let nextAnxinProject = anxinProjectRes.filter(ap => p.anxinProjectId.includes(ap.id))
@ -79,10 +77,7 @@ async function pomsProject (ctx) {
delete p.dataValues.anxinProjectId delete p.dataValues.anxinProjectId
} }
ctx.status = 200; ctx.status = 200;
ctx.body = { ctx.body = proRes
count: proCount,
rows: proRes
}
} catch (error) { } catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.fs.logger.error(`path: ${ctx.path}, error: error`);
ctx.status = 400; ctx.status = 400;

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

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

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

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

Loading…
Cancel
Save