Skip to content

Commit af485bc

Browse files
committed
feat: 🎸 Added user.list route to the node-api
1 parent 9341b87 commit af485bc

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

‎packages/db/src/users.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,25 @@ export class UsersDb {
101101
return user;
102102
}
103103

104+
/**
105+
* Retrieves the user with the given login from storage.
106+
*
107+
* @returns {Promise<User[]>} The User object associated with the given login
108+
* @throws Will throw an error if the user is not found
109+
* @memberof UsersDb
110+
*/
111+
async getAll(): Promise<User[]> {
112+
const records: User[] = [];
113+
114+
for (const record of await this.storage.entries<User>()) {
115+
if (record[0].startsWith(this.prefix)) {
116+
records.push(record[1]);
117+
}
118+
}
119+
120+
return records;
121+
}
122+
104123
/**
105124
* Adds a new user to the storage.
106125
*

‎packages/node-api/src/router/user.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ export const userRouter = router({
3636
}
3737
}),
3838

39+
/**
40+
* List users records.
41+
* Throws an error if the user already exists.
42+
*/
43+
list: authAdminProcedure.query(async ({ ctx }) => {
44+
try {
45+
const { users } = ctx;
46+
const records = await users.getAll();
47+
logger.trace(`Listed #${records.length} users`);
48+
return records;
49+
} catch (error) {
50+
logger.error('user.list', error);
51+
throw new TRPCError({
52+
code: 'BAD_REQUEST',
53+
message: (error as Error).message,
54+
});
55+
}
56+
}),
57+
3958
/**
4059
* Log in an existing user.
4160
* If successful, generates a new access token and sends it in the response.

0 commit comments

Comments
 (0)