From af485bc1b18e5d7518a2867d1d22f6f9a2991780 Mon Sep 17 00:00:00 2001 From: Kostiantyn Smyrnov Date: Fri, 16 Feb 2024 21:41:27 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Added=20user.list=20rout?= =?UTF-8?q?e=20to=20the=20node-api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/db/src/users.ts | 19 +++++++++++++++++++ packages/node-api/src/router/user.ts | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/packages/db/src/users.ts b/packages/db/src/users.ts index e6118f4d..0611dd03 100644 --- a/packages/db/src/users.ts +++ b/packages/db/src/users.ts @@ -101,6 +101,25 @@ export class UsersDb { return user; } + /** + * Retrieves the user with the given login from storage. + * + * @returns {Promise} The User object associated with the given login + * @throws Will throw an error if the user is not found + * @memberof UsersDb + */ + async getAll(): Promise { + const records: User[] = []; + + for (const record of await this.storage.entries()) { + if (record[0].startsWith(this.prefix)) { + records.push(record[1]); + } + } + + return records; + } + /** * Adds a new user to the storage. * diff --git a/packages/node-api/src/router/user.ts b/packages/node-api/src/router/user.ts index 156e8841..3e340a53 100644 --- a/packages/node-api/src/router/user.ts +++ b/packages/node-api/src/router/user.ts @@ -36,6 +36,25 @@ export const userRouter = router({ } }), + /** + * List users records. + * Throws an error if the user already exists. + */ + list: authAdminProcedure.query(async ({ ctx }) => { + try { + const { users } = ctx; + const records = await users.getAll(); + logger.trace(`Listed #${records.length} users`); + return records; + } catch (error) { + logger.error('user.list', error); + throw new TRPCError({ + code: 'BAD_REQUEST', + message: (error as Error).message, + }); + } + }), + /** * Log in an existing user. * If successful, generates a new access token and sends it in the response.