Skip to content

Commit

Permalink
Merge pull request #95 from windingtree/develop
Browse files Browse the repository at this point in the history
feat: 🎸 Added user.list route to the node-api
  • Loading branch information
kostysh authored Feb 16, 2024
2 parents 8658194 + af485bc commit d2451fd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/db/src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,25 @@ export class UsersDb {
return user;
}

/**
* Retrieves the user with the given login from storage.
*
* @returns {Promise<User[]>} The User object associated with the given login
* @throws Will throw an error if the user is not found
* @memberof UsersDb
*/
async getAll(): Promise<User[]> {
const records: User[] = [];

for (const record of await this.storage.entries<User>()) {
if (record[0].startsWith(this.prefix)) {
records.push(record[1]);
}
}

return records;
}

/**
* Adds a new user to the storage.
*
Expand Down
19 changes: 19 additions & 0 deletions packages/node-api/src/router/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit d2451fd

Please sign in to comment.