Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed users list route #97

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/db/src/deals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class DealsDb {
};
let cursor = 0;
const from = page.start >= 0 ? page.start : 0;
const to = from + page.skip ?? 0;
const to = from + (page.skip ?? 0);
const records: DealRecord[] = [];

for (const record of await this.storage.entries<DealRecord>()) {
Expand Down
35 changes: 32 additions & 3 deletions packages/db/src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export interface User {
* It validates that the input contains a nonempty `login` and `password`.
*/
export const UserInputSchema = z.object({
login: z.string().nonempty(),
password: z.union([z.string().nonempty(), z.string().startsWith('0x')]),
login: z.string().min(1),
password: z.union([z.string().min(1), z.string().startsWith('0x')]),
});

/**
Expand All @@ -33,6 +33,31 @@ export const UserInputSchema = z.object({
*/
export type UserInputType = z.infer<typeof UserInputSchema>;

/**
* User output schema
*/
export const SafeUserSchema = z.object({
login: z.string().min(1),
isAdmin: z.boolean().optional(),
});

/**
* Users list output schema
*/
export const UsersListOutputSchema = z.array(SafeUserSchema);

/**
* Type definition for sanitized User record,
* inferred from SafeUserSchema.
*/
export type SafeUserType = z.infer<typeof SafeUserSchema>;

/**
* Type definition for sanitized Users records list,
* inferred from UsersListOutputSchema.
*/
export type UsersListOutputSchema = z.infer<typeof UsersListOutputSchema>;

/**
* Interface defining the properties of UsersDb initialization options.
*/
Expand Down Expand Up @@ -130,7 +155,11 @@ export class UsersDb {
* @throws Will throw an error if a user with the same login already exists
* @memberof UsersDb
*/
async add(login: string, password: string, isAdmin = false): Promise<void> {
async add(
login: string,
password: string,
isAdmin: boolean = false,
): Promise<void> {
const knownUser = await this.storage.get<User>(`${this.prefix}${login}`);

// Check if the user already exists
Expand Down
40 changes: 25 additions & 15 deletions packages/node-api/src/router/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { TRPCError } from '@trpc/server';
import { User, UserInputSchema, comparePassword } from '@windingtree/sdk-db';
import {
User,
UserInputSchema,
UsersListOutputSchema,
comparePassword,
} from '@windingtree/sdk-db';
import {
router,
procedure,
Expand Down Expand Up @@ -40,20 +45,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,
});
}
}),
list: authAdminProcedure
.output(UsersListOutputSchema)
.query(async ({ ctx }) => {
try {
const { users } = ctx;
const records = await users.getAll();
logger.trace(`Listed #${records.length} users`);
return records.map(({ login, isAdmin }) => ({
login,
isAdmin,
}));
} catch (error) {
logger.error('user.list', error);
throw new TRPCError({
code: 'BAD_REQUEST',
message: (error as Error).message,
});
}
}),

/**
* Log in an existing user.
Expand Down
Loading