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

Added deleteByLogin to the users route of node-api #98

Merged
merged 4 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/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type SafeUserType = z.infer<typeof SafeUserSchema>;
* Type definition for sanitized Users records list,
* inferred from UsersListOutputSchema.
*/
export type UsersListOutputSchema = z.infer<typeof UsersListOutputSchema>;
export type UsersListOutputSchemaType = z.infer<typeof UsersListOutputSchema>;

/**
* Interface defining the properties of UsersDb initialization options.
Expand Down
25 changes: 24 additions & 1 deletion packages/node-api/src/router/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
authAdminProcedure,
} from '../server.js';
import { ACCESS_TOKEN_NAME } from '../constants.js';
import { z } from 'zod';
import { createLogger } from '@windingtree/sdk-logger';

const logger = createLogger('UserRouter');
Expand Down Expand Up @@ -43,7 +44,7 @@ export const userRouter = router({

/**
* List users records.
* Throws an error if the user already exists.
* Allowed for admins only.
*/
list: authAdminProcedure
.output(UsersListOutputSchema)
Expand All @@ -65,6 +66,28 @@ export const userRouter = router({
}
}),

/**
* Deletes user by login name.
* Allowed for admins only.
*/
deleteByLogin: authAdminProcedure
.input(z.string())
.mutation(async ({ input, ctx }) => {
try {
const { users } = ctx;
const user = await users.get(input);
logger.trace(`Found #${user.login} user`);
await users.delete(user.login);
logger.trace(`User ${user.login} has been deleted`);
} catch (error) {
logger.error('user.deleteByLogin', 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
Loading