diff --git a/packages/db/src/users.ts b/packages/db/src/users.ts index e6118f4..0611dd0 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 156e884..3e340a5 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.