(router: Router)
| 7 | import { ControllerError } from '../util/errors' |
| 8 | |
| 9 | export function userController(router: Router): void { |
| 10 | router.post('/api/v1/user', async (ctx) => { |
| 11 | const { body } = ctx.request |
| 12 | |
| 13 | if (!validateCreateAnonymousUserRequest(body)) { |
| 14 | throw new ControllerError(400, 'InvalidUser', 'invalid user') |
| 15 | } |
| 16 | |
| 17 | const result = await ctx.db.transaction().execute(async (trx) => { |
| 18 | return userService.createAnonymousUser(trx, body) |
| 19 | }) |
| 20 | |
| 21 | ctx.status = 201 |
| 22 | ctx.body = { |
| 23 | user: result.user, |
| 24 | authToken: result.authToken.authToken, |
| 25 | refreshToken: result.refreshToken.refreshToken, |
| 26 | } |
| 27 | }) |
| 28 | |
| 29 | router.get( |
| 30 | '/api/v1/user/:userId', |
| 31 | authenticationService.authenticateUser, |
| 32 | async (ctx) => { |
| 33 | const { userId } = ctx.params |
| 34 | const user = await userService.findUserById(ctx.db, userId) |
| 35 | |
| 36 | if (!user) { |
| 37 | throw new ControllerError( |
| 38 | 404, |
| 39 | 'UserNotFound', |
| 40 | `user with id ${userId} was not found`, |
| 41 | ) |
| 42 | } |
| 43 | |
| 44 | ctx.body = { user } |
| 45 | }, |
| 46 | ) |
| 47 | |
| 48 | signInMethodController(router) |
| 49 | } |
no test coverage detected
searching dependent graphs…