( fastify, _options, done )
| 77 | * @param done Callback to signal that the logic has completed. |
| 78 | */ |
| 79 | export const userRoutes: FastifyPluginCallbackTypebox = ( |
| 80 | fastify, |
| 81 | _options, |
| 82 | done |
| 83 | ) => { |
| 84 | fastify.post( |
| 85 | '/account/delete', |
| 86 | { |
| 87 | schema: schemas.deleteMyAccount |
| 88 | }, |
| 89 | async (req, reply) => { |
| 90 | const logger = fastify.log.child({ req, res: reply }); |
| 91 | logger.info(`User ${req.user?.id} requested account deletion`); |
| 92 | await fastify.prisma.userToken.deleteMany({ |
| 93 | where: { userId: req.user!.id } |
| 94 | }); |
| 95 | await fastify.prisma.msUsername.deleteMany({ |
| 96 | where: { userId: req.user!.id } |
| 97 | }); |
| 98 | await fastify.prisma.survey.deleteMany({ |
| 99 | where: { userId: req.user!.id } |
| 100 | }); |
| 101 | try { |
| 102 | await fastify.prisma.user.delete({ |
| 103 | where: { id: req.user!.id } |
| 104 | }); |
| 105 | } catch (err) { |
| 106 | if ( |
| 107 | err instanceof PrismaClientKnownRequestError && |
| 108 | err.code === 'P2025' |
| 109 | ) { |
| 110 | logger.warn( |
| 111 | err, |
| 112 | `User with id ${req.user?.id} not found for deletion.` |
| 113 | ); |
| 114 | } else { |
| 115 | logger.error(err, 'Error deleting user account'); |
| 116 | throw err; |
| 117 | } |
| 118 | } |
| 119 | reply.clearOurCookies(); |
| 120 | |
| 121 | return {}; |
| 122 | } |
| 123 | ); |
| 124 | |
| 125 | fastify.delete( |
| 126 | '/users/:userId', |
| 127 | { |
| 128 | schema: schemas.deleteUser |
| 129 | }, |
| 130 | async (req, reply) => { |
| 131 | const logger = fastify.log.child({ req, res: reply }); |
| 132 | const { userId } = req.params; |
| 133 | |
| 134 | if (userId !== req.user?.id) { |
| 135 | logger.warn( |
| 136 | { requestedUserId: userId, authUserId: req.user?.id }, |
nothing calls this directly
no test coverage detected