diff --git a/package.json b/package.json index 0de005f..27a4f83 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/models/org.ts b/src/models/org.ts index d25c7ae..beaeb59 100644 --- a/src/models/org.ts +++ b/src/models/org.ts @@ -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 } }); } /** diff --git a/src/scripts/org-test.ts b/src/scripts/org-test.ts index 4e32c3f..c051d21 100644 --- a/src/scripts/org-test.ts +++ b/src/scripts/org-test.ts @@ -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(); \ No newline at end of file +// 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(); \ No newline at end of file