Skip to content

Commit

Permalink
Merge pull request #98 from windingtree/develop
Browse files Browse the repository at this point in the history
Added deleteByLogin to the users route of node-api
  • Loading branch information
kostysh authored Feb 19, 2024
2 parents 93ca6a9 + 29f5f6f commit 7769767
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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

0 comments on commit 7769767

Please sign in to comment.