yapi/server/controllers/group.js

386 lines
13 KiB
JavaScript
Raw Normal View History

const groupModel = require('../models/group.js');
const yapi = require('../yapi.js');
const baseController = require('./base.js');
const projectModel = require('../models/project.js');
const userModel = require('../models/user.js');
const interfaceModel = require('../models/interface.js');
const interfaceColModel = require('../models/interfaceCol.js');
const interfaceCaseModel = require('../models/interfaceCase.js');
2017-07-27 14:12:43 +08:00
class groupController extends baseController {
constructor(ctx) {
super(ctx);
}
2017-08-15 18:36:48 +08:00
/**
* 添加项目分组
* @interface /group/get
* @method GET
* @category group
* @foldnumber 10
* @param {String} id 项目分组ID
* @returns {Object}
* @example
*/
async get(ctx) {
try {
let params = ctx.request.query;
if (!params.id) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组id不能为空');
}
let groupInst = yapi.getInst(groupModel);
let result = await groupInst.getGroupById(params.id);
result = result.toObject();
result.role = await this.getProjectRole(params.id, 'group');
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 400, e.message)
}
}
2017-07-06 19:21:54 +08:00
/**
* 添加项目分组
* @interface /group/add
* @method POST
* @category group
* @foldnumber 10
* @param {String} group_name 项目分组名称不能为空
2017-08-15 18:23:30 +08:00
* @param {String} [group_desc] 项目分组描述
2017-08-08 14:45:19 +08:00
* @param {String} owner_uid 组长uid
2017-08-15 18:23:30 +08:00
* @returns {Object}
2017-07-06 20:53:26 +08:00
* @example ./api/group/add.json
2017-07-06 19:21:54 +08:00
*/
async add(ctx) {
let params = ctx.request.body;
2017-07-27 14:12:43 +08:00
2017-07-26 14:22:59 +08:00
params = yapi.commons.handleParams(params, {
group_name: 'string',
2017-08-08 14:45:19 +08:00
group_desc: 'string',
owner_uid: 'number'
2017-07-27 14:12:43 +08:00
});
if (this.getRole() !== 'admin') {
return ctx.body = yapi.commons.resReturn(null, 401, '没有权限');
2017-07-19 19:38:48 +08:00
}
2017-07-27 14:12:43 +08:00
if (!params.group_name) {
2017-07-06 19:21:54 +08:00
return ctx.body = yapi.commons.resReturn(null, 400, '项目分组名不能为空');
}
2017-07-27 14:12:43 +08:00
2017-08-15 18:36:48 +08:00
if (!params.owner_uid) {
2017-08-08 14:45:19 +08:00
return ctx.body = yapi.commons.resReturn(null, 400, '项目分组必须添加一个组长');
}
let groupUserdata = await this.getUserdata(params.owner_uid, 'owner');
2017-08-15 18:36:48 +08:00
if (groupUserdata === null) {
2017-08-08 14:45:19 +08:00
return ctx.body = yapi.commons.resReturn(null, 400, '组长uid不存在')
}
2017-07-27 14:12:43 +08:00
let groupInst = yapi.getInst(groupModel);
let checkRepeat = await groupInst.checkRepeat(params.group_name);
if (checkRepeat > 0) {
return ctx.body = yapi.commons.resReturn(null, 401, '项目分组名已存在');
2017-07-04 17:19:20 +08:00
}
2017-07-27 14:12:43 +08:00
let data = {
group_name: params.group_name,
group_desc: params.group_desc,
2017-07-26 14:22:59 +08:00
uid: this.getUid(),
add_time: yapi.commons.time(),
2017-08-08 14:45:19 +08:00
up_time: yapi.commons.time(),
members: [groupUserdata]
2017-07-27 14:12:43 +08:00
};
try {
let result = await groupInst.save(data);
2017-07-27 14:12:43 +08:00
2017-08-08 14:45:19 +08:00
result = yapi.commons.fieldSelect(result, ['_id', 'group_name', 'group_desc', 'uid', 'members']);
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
2017-08-15 18:36:48 +08:00
async getUserdata(uid, role) {
2017-08-08 14:45:19 +08:00
role = role || 'dev';
let userInst = yapi.getInst(userModel);
let userData = await userInst.findById(uid);
2017-08-15 18:36:48 +08:00
if (!userData) {
2017-08-08 14:45:19 +08:00
return null;
}
return {
_role: userData.role,
2017-08-08 14:45:19 +08:00
role: role,
uid: userData._id,
username: userData.username,
email: userData.email
}
}
/**
* 添加项目分组成员
* @interface /group/add_member
* @method POST
* @category group
* @foldnumber 10
* @param {String} id 项目分组id
* @param {String} member_uid 项目分组成员uid
* @param {String} role 成员角色owner or dev
2017-08-15 18:23:30 +08:00
* @returns {Object}
* @example
*/
2017-08-15 18:38:36 +08:00
2017-08-25 16:25:34 +08:00
async addMember(ctx) {
2017-08-15 18:38:36 +08:00
2017-08-08 14:45:19 +08:00
let params = ctx.request.body;
let groupInst = yapi.getInst(groupModel);
if (!params.member_uid) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组成员uid不能为空');
}
if (!params.id) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组id不能为空');
}
params.role = params.role === 'owner' ? 'owner' : 'dev';
2017-08-08 14:45:19 +08:00
var check = await groupInst.checkMemberRepeat(params.id, params.member_uid);
if (check > 0) {
return ctx.body = yapi.commons.resReturn(null, 400, '成员已存在');
}
let groupUserdata = await this.getUserdata(params.member_uid, params.role);
2017-08-15 18:36:48 +08:00
if (groupUserdata === null) {
2017-08-08 14:45:19 +08:00
return ctx.body = yapi.commons.resReturn(null, 400, '组长uid不存在')
}
2017-08-15 18:36:48 +08:00
if (groupUserdata._role === 'admin') {
return ctx.body = yapi.commons.resReturn(null, 400, '不能邀请管理员')
}
delete groupUserdata._role;
2017-08-08 14:45:19 +08:00
try {
let result = await groupInst.addMember(params.id, groupUserdata);
ctx.body = yapi.commons.resReturn(result);
2017-07-27 14:12:43 +08:00
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
2017-08-08 14:45:19 +08:00
}
/**
* 修改项目分组成员角色
* @interface /group/change_member_role
* @method POST
* @category group
* @foldnumber 10
* @param {String} id 项目分组id
* @param {String} member_uid 项目分组成员uid
2017-08-15 18:23:30 +08:00
* @param {String} role 权限 ['owner'|'dev']
* @returns {Object}
* @example
*/
2017-08-15 18:36:48 +08:00
async changeMemberRole(ctx) {
let params = ctx.request.body;
let groupInst = yapi.getInst(groupModel);
if (!params.member_uid) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组成员uid不能为空');
}
if (!params.id) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组id不能为空');
}
var check = await groupInst.checkMemberRepeat(params.id, params.member_uid);
if (check === 0) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组成员不存在');
}
if (await this.checkAuth(params.id, 'group', 'danger') !== true) {
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
params.role = params.role === 'owner' ? 'owner' : 'dev';
try {
let result = await groupInst.changeMemberRole(params.id, params.member_uid, params.role);
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 获取所有项目成员
* @interface /group/get_member_list
* @method GET
* @category group
* @foldnumber 10
* @param {String} id 项目分组id
2017-08-15 18:23:30 +08:00
* @returns {Object}
* @example
*/
2017-08-08 14:45:19 +08:00
async getMemberList(ctx) {
let params = ctx.request.query;
if (!params.id) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
2017-07-27 14:12:43 +08:00
2017-08-08 14:45:19 +08:00
try {
let groupInst = yapi.getInst(groupModel);
let group = await groupInst.get(params.id);
ctx.body = yapi.commons.resReturn(group.members);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 删除项目成员
* @interface /group/del_member
* @method POST
* @category group
* @foldnumber 10
* @param {String} id 项目分组id
* @param {String} member_uid 项目分组成员uid
2017-08-15 18:23:30 +08:00
* @returns {Object}
* @example
*/
2017-08-08 14:45:19 +08:00
async delMember(ctx) {
let params = ctx.request.body;
let groupInst = yapi.getInst(groupModel);
if (!params.member_uid) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组成员uid不能为空');
}
if (!params.id) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组id不能为空');
}
var check = await groupInst.checkMemberRepeat(params.id, params.member_uid);
if (check === 0) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组成员不存在');
}
if (await this.checkAuth(params.id, 'group', 'danger') !== true) {
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
2017-08-08 14:45:19 +08:00
try {
let result = await groupInst.delMember(params.id, params.member_uid);
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
2017-07-06 20:53:26 +08:00
/**
2017-07-10 11:56:53 +08:00
* 获取项目分组列表
2017-07-06 20:53:26 +08:00
* @interface /group/list
* @method get
* @category group
* @foldnumber 10
2017-08-15 18:23:30 +08:00
* @returns {Object}
2017-07-10 11:56:53 +08:00
* @example ./api/group/list.json
2017-07-06 20:53:26 +08:00
*/
async list(ctx) {
2017-07-27 14:12:43 +08:00
try {
var groupInst = yapi.getInst(groupModel);
let result = await groupInst.list();
2017-09-13 17:09:06 +08:00
let newResult = [];
if(result && result.length > 0){
for(let i=0; i< result.length; i++){
result[i] = result[i].toObject();
2017-09-14 21:12:45 +08:00
result[i].role = await this.getProjectRole(result[i]._id, 'group');
if(result[i].role !== 'member'){
2017-09-13 17:09:06 +08:00
newResult.unshift(result[i]);
}else{
newResult.push(result[i]);
}
}
}
ctx.body = yapi.commons.resReturn(newResult);
2017-07-27 14:12:43 +08:00
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
2017-07-04 17:19:20 +08:00
}
}
2017-07-10 11:56:53 +08:00
/**
* 删除项目分组
* @interface /group/del
* @method post
* @param {String} id 项目分组id
* @category group
* @foldnumber 10
2017-08-15 18:23:30 +08:00
* @returns {Object}
2017-07-10 11:56:53 +08:00
* @example ./api/group/del.json
*/
2017-07-27 14:12:43 +08:00
async del(ctx) {
if (this.getRole() !== 'admin') {
return ctx.body = yapi.commons.resReturn(null, 401, '没有权限');
}
2017-07-10 11:56:53 +08:00
2017-07-27 14:12:43 +08:00
try {
let groupInst = yapi.getInst(groupModel);
let projectInst = yapi.getInst(projectModel);
2017-08-16 10:56:42 +08:00
let interfaceInst = yapi.getInst(interfaceModel);
let interfaceColInst = yapi.getInst(interfaceColModel);
let interfaceCaseInst = yapi.getInst(interfaceCaseModel);
2017-07-27 14:12:43 +08:00
let id = ctx.request.body.id;
if (!id) {
2017-07-10 20:02:44 +08:00
return ctx.body = yapi.commons.resReturn(null, 402, 'id不能为空');
}
2017-08-25 16:25:34 +08:00
let projectList = await projectInst.list(id, true);
2017-08-16 10:56:42 +08:00
projectList.forEach(async (p) => {
await interfaceInst.delByProjectId(p._id)
await interfaceCaseInst.delByProjectId(p._id)
await interfaceColInst.delByProjectId(p._id)
})
await projectInst.delByGroupid(id);
let result = await groupInst.del(id);
2017-07-27 14:12:43 +08:00
ctx.body = yapi.commons.resReturn(result);
} catch (err) {
ctx.body = yapi.commons.resReturn(null, 402, err.message);
2017-07-04 17:19:20 +08:00
}
}
2017-07-10 11:56:53 +08:00
/**
* 更新项目分组
* @interface /group/up
* @method post
* @param {String} id 项目分组id
* @param {String} group_name 项目分组名称
* @param {String} group_desc 项目分组描述
* @category group
* @foldnumber 10
2017-08-15 18:23:30 +08:00
* @returns {Object}
2017-07-10 11:56:53 +08:00
* @example ./api/group/up.json
*/
2017-07-27 14:12:43 +08:00
async up(ctx) {
2017-08-25 16:25:34 +08:00
let groupInst = yapi.getInst(groupModel);
let id = ctx.request.body.id;
let data = {};
if (await this.checkAuth(id, 'group', 'danger') !== true) {
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
2017-07-19 19:38:48 +08:00
}
2017-07-27 14:12:43 +08:00
try {
2017-07-26 14:22:59 +08:00
ctx.request.body = yapi.commons.handleParams(ctx.request.body, {
id: 'number',
group_name: 'string',
group_desc: 'string'
2017-07-27 14:12:43 +08:00
});
2017-08-25 16:25:34 +08:00
2017-07-27 14:12:43 +08:00
ctx.request.body.group_name && (data.group_name = ctx.request.body.group_name);
ctx.request.body.group_desc && (data.group_desc = ctx.request.body.group_desc);
if (Object.keys(data).length === 0) {
2017-07-06 19:21:54 +08:00
ctx.body = yapi.commons.resReturn(null, 404, '分组名和分组描述不能为空');
2017-07-04 17:19:20 +08:00
}
let result = await groupInst.up(id, data);
2017-07-27 14:12:43 +08:00
ctx.body = yapi.commons.resReturn(result);
} catch (err) {
ctx.body = yapi.commons.resReturn(null, 402, err.message);
2017-07-04 17:19:20 +08:00
}
}
}
2017-08-15 18:23:30 +08:00
module.exports = groupController;