feat: add switch orgs in org
This commit is contained in:
		| @@ -26,6 +26,10 @@ export class User extends Model { | |||||||
|   declare orgId: string; |   declare orgId: string; | ||||||
|   declare email: string; |   declare email: string; | ||||||
|   declare avatar: string; |   declare avatar: string; | ||||||
|  |   tokenUser: any; | ||||||
|  |   setTokenUser(tokenUser: any) { | ||||||
|  |     this.tokenUser = tokenUser; | ||||||
|  |   } | ||||||
|   async createToken(uid?: string) { |   async createToken(uid?: string) { | ||||||
|     const { id, username, type } = this; |     const { id, username, type } = this; | ||||||
|     const expireTime = 60 * 60 * 24 * 7; // 7 days |     const expireTime = 60 * 60 * 24 * 7; // 7 days | ||||||
| @@ -91,7 +95,12 @@ export class User extends Model { | |||||||
|     }; |     }; | ||||||
|   } |   } | ||||||
|   async getOrgs() { |   async getOrgs() { | ||||||
|     const id = this.id; |     let id = this.id; | ||||||
|  |     if (this.type === 'org') { | ||||||
|  |       if (this.tokenUser && this.tokenUser.uid) { | ||||||
|  |         id = this.tokenUser.uid; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|     const cache = await redis.get(`user:${id}:orgs`); |     const cache = await redis.get(`user:${id}:orgs`); | ||||||
|     if (cache) { |     if (cache) { | ||||||
|       return JSON.parse(cache); |       return JSON.parse(cache); | ||||||
| @@ -109,9 +118,14 @@ export class User extends Model { | |||||||
|       }, |       }, | ||||||
|     }); |     }); | ||||||
|     const orgNames = orgs.map((org) => org.username); |     const orgNames = orgs.map((org) => org.username); | ||||||
|     await redis.set(`user:${id}:orgs`, JSON.stringify(orgNames), 'EX', 60 * 60); // 1 hour |     if (orgNames.length > 0) { | ||||||
|  |       await redis.set(`user:${id}:orgs`, JSON.stringify(orgNames), 'EX', 60 * 60); // 1 hour | ||||||
|  |     } | ||||||
|     return orgNames; |     return orgNames; | ||||||
|   } |   } | ||||||
|  |   async expireOrgs() { | ||||||
|  |     await redis.del(`user:${this.id}:orgs`); | ||||||
|  |   } | ||||||
| } | } | ||||||
| User.init( | User.init( | ||||||
|   { |   { | ||||||
|   | |||||||
| @@ -10,14 +10,15 @@ app | |||||||
|     middleware: ['auth'], |     middleware: ['auth'], | ||||||
|   }) |   }) | ||||||
|   .define(async (ctx) => { |   .define(async (ctx) => { | ||||||
|     const state = ctx.state?.tokenUser || {}; |     const tokenUser = ctx.state?.tokenUser || {}; | ||||||
|     const { id } = state; |     const { id } = tokenUser; | ||||||
|     const user = await User.findByPk(id, { |     const user = await User.findByPk(id, { | ||||||
|       logging: false, |       logging: false, | ||||||
|     }); |     }); | ||||||
|     if (!user) { |     if (!user) { | ||||||
|       throw new CustomError(500, 'user not found'); |       throw new CustomError(500, 'user not found'); | ||||||
|     } |     } | ||||||
|  |     user.setTokenUser(tokenUser); | ||||||
|     ctx.body = await user.getInfo(); |     ctx.body = await user.getInfo(); | ||||||
|   }) |   }) | ||||||
|   .addTo(app); |   .addTo(app); | ||||||
| @@ -68,12 +69,13 @@ app | |||||||
|   }) |   }) | ||||||
|   .define(async (ctx) => { |   .define(async (ctx) => { | ||||||
|     const { username, password, description, avatar, email } = ctx.query.data || {}; |     const { username, password, description, avatar, email } = ctx.query.data || {}; | ||||||
|     const state = ctx.state?.tokenUser || {}; |     const tokenUser = ctx.state?.tokenUser || {}; | ||||||
|     const { id } = state; |     const { id } = tokenUser; | ||||||
|     const user = await User.findByPk(id); |     const user = await User.findByPk(id); | ||||||
|     if (!user) { |     if (!user) { | ||||||
|       throw new CustomError(500, 'user not found'); |       throw new CustomError(500, 'user not found'); | ||||||
|     } |     } | ||||||
|  |     user.setTokenUser(tokenUser); | ||||||
|     if (username) { |     if (username) { | ||||||
|       user.username = username; |       user.username = username; | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user