import type { Account, Ref } from '@hcengineering/core' import type { Collection, FindCursor } from 'mongodb' import type { GithubUserRecord } from './types' export class UserManager { userCache = new Map() refUserCache = new Map() constructor (readonly usersCollection: Collection) {} public async getUsers (workspace: string): Promise { return await this.usersCollection .find({ [`accounts.${workspace}`]: { $exists: true } }) .toArray() } async getAccount (login: string): Promise { let res = this.userCache.get(login) if (res !== undefined) { return res } res = (await this.usersCollection.findOne({ _id: login })) ?? undefined if (res !== undefined) { if (this.userCache.size > 1000) { this.userCache.clear() } this.userCache.set(login, res) } return res } async getAccountByRef (workspace: string, ref: Ref): Promise { const key = `${workspace}.${ref}` let rec = this.refUserCache.get(key) if (rec !== undefined) { return rec } rec = (await this.usersCollection.findOne({ [`accounts.${workspace}`]: ref })) ?? undefined if (rec !== undefined) { if (this.refUserCache.size > 1000) { this.refUserCache.clear() } this.refUserCache.set(key, rec) } return rec } async updateUser (dta: GithubUserRecord): Promise { this.userCache.clear() this.refUserCache.clear() await this.usersCollection.updateOne({ _id: dta._id }, { $set: dta } as any) } async insertUser (dta: GithubUserRecord): Promise { await this.usersCollection.insertOne(dta) } async removeUser (login: string): Promise { this.userCache.clear() this.refUserCache.clear() await this.usersCollection.deleteOne({ _id: login }) } getAllUsers (): FindCursor { return this.usersCollection.find({}) } }