feat: 初始化项目用户初始化修改
This commit is contained in:
@@ -12,7 +12,9 @@ app
|
||||
.define(async (ctx) => {
|
||||
const state = ctx.state?.tokenUser || {};
|
||||
const { id } = state;
|
||||
const user = await User.findByPk(id);
|
||||
const user = await User.findByPk(id, {
|
||||
logging: false,
|
||||
});
|
||||
if (!user) {
|
||||
throw new CustomError(500, 'user not found');
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ app
|
||||
],
|
||||
},
|
||||
},
|
||||
logging: false,
|
||||
});
|
||||
|
||||
ctx.body = list;
|
||||
@@ -30,21 +31,6 @@ app
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'org',
|
||||
key: 'get',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const id = ctx.query.id;
|
||||
if (!id) {
|
||||
throw new CustomError('id is required');
|
||||
}
|
||||
ctx.body = await Org.findByPk(id);
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'org',
|
||||
@@ -53,10 +39,27 @@ app
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { username, description } = ctx.query.data;
|
||||
const { username, description, id } = ctx.query.data;
|
||||
if (!username) {
|
||||
throw new CustomError('username is required');
|
||||
}
|
||||
if (id) {
|
||||
const org = await Org.findByPk(id);
|
||||
if (!org) {
|
||||
throw new CustomError('org not found');
|
||||
}
|
||||
org.description = description;
|
||||
await org.save();
|
||||
const user = await User.findOne({ where: { username } });
|
||||
user.description = description;
|
||||
await user.save();
|
||||
ctx.body = {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
description: user.description,
|
||||
};
|
||||
return;
|
||||
}
|
||||
const user = await User.createOrg(username, tokenUser.id, description);
|
||||
ctx.body = {
|
||||
id: user.id,
|
||||
@@ -96,3 +99,47 @@ app
|
||||
ctx.body = 'success';
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'org',
|
||||
key: 'get',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const id = ctx.query.id;
|
||||
if (!id) {
|
||||
throw new CustomError('id is required');
|
||||
}
|
||||
const org = await Org.findByPk(id);
|
||||
if (!org) {
|
||||
throw new CustomError('org not found');
|
||||
}
|
||||
const usersIds = org.users;
|
||||
const me = usersIds.find((u) => u.uid === tokenUser.id);
|
||||
if (!me) {
|
||||
throw new CustomError('Permission denied');
|
||||
}
|
||||
const _users = await User.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: usersIds.map((u) => u.uid),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const users = _users.map((u) => {
|
||||
const role = usersIds.find((r) => r.uid === u.id)?.role;
|
||||
return {
|
||||
id: u.id,
|
||||
username: u.username,
|
||||
role: role,
|
||||
};
|
||||
});
|
||||
ctx.body = {
|
||||
org,
|
||||
users,
|
||||
};
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
Reference in New Issue
Block a user