fix: fix bugs

This commit is contained in:
xion 2025-02-28 14:13:11 +08:00
parent 425b0d5286
commit ae829c6181
3 changed files with 64 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@kevisual/code-center-module",
"version": "0.0.9",
"version": "0.0.10",
"description": "",
"main": "dist/system.mjs",
"module": "dist/system.mjs",

View File

@ -23,6 +23,7 @@ export class Org extends Model {
async addUser(user: User, opts?: { operateId?: string; role: string; needPermission?: boolean; isAdmin?: boolean }) {
const hasUser = this.users.find((u) => u.uid === user.id);
if (hasUser) {
console.log('current org', this.username, 'has', user.username);
return;
}
if (user.type !== 'user') {
@ -39,9 +40,34 @@ export class Org extends Model {
}
}
}
await user.expireOrgs();
try {
await user.expireOrgs();
} catch (e) {
console.error('expireOrgs', e);
}
const users = [...this.users];
users.push({ role: opts?.role || 'member', uid: user.id });
if (opts?.role === 'owner') {
const orgOwner = users.find((u) => u.role === 'owner');
if (opts.isAdmin) {
} else {
if (!opts.operateId) {
throw Error('operateId is required');
}
const owner = await User.findByPk(opts?.operateId);
if (!owner) {
throw Error('operateId is not found');
}
if (orgOwner?.uid !== owner.id) {
throw Error('No permission');
}
}
if (orgOwner) {
orgOwner.role = 'admin';
}
users.push({ role: 'owner', uid: user.id });
} else {
users.push({ role: opts?.role || 'member', uid: user.id });
}
await Org.update({ users }, { where: { id: this.id } });
}
/**
@ -62,7 +88,7 @@ export class Org extends Model {
}
}
await user.expireOrgs();
const users = this.users.filter((u) => u.uid !== user.id && u.role !== 'owner');
const users = this.users.filter((u) => u.uid !== user.id || u.role !== 'owner');
await Org.update({ users }, { where: { id: this.id } });
}
/**

View File

@ -24,10 +24,11 @@ export const main = async () => {
});
console.log('org', org.toJSON());
const res = await org.addUser(me, {
operateId: rootId,
// operateId: rootId,
// operateId: id,
role: 'admin',
needPermission: true,
// isAdmin: true,
// needPermission: true,
});
// await org.removeUser(me, {
// operateId: rootId,
@ -35,8 +36,20 @@ export const main = async () => {
process.exit(0);
};
// main();
export const remove = async () => {
await UserInit();
await OrgInit();
const me = await User.findByPk(rootId);
const org = await Org.findOne({
where: {
username: 'system',
},
});
await org.removeUser(me, {
isAdmin: true,
});
process.exit(0);
};
const updatePassword = async () => {
await UserInit();
await OrgInit();
@ -45,4 +58,20 @@ const updatePassword = async () => {
process.exit(0);
};
updatePassword();
// updatePassword();
const getList = async () => {
await UserInit();
await OrgInit();
const orgs = await Org.findAll();
for (const user of orgs) {
console.log('user-name', user.username);
console.log('user', user.id, user.users, '\n');
}
process.exit(0);
};
// await main();
await getList();
// remove();