feat(backend): add fastify api, auth, prisma schema and jobs
This commit is contained in:
30
apps/backend/src/modules/profile/profile.routes.ts
Normal file
30
apps/backend/src/modules/profile/profile.routes.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import { updateProfileSchema } from '@family-wishlist/shared';
|
||||
import { ConflictError, NotFoundError } from '../../utils/errors.js';
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
export default async function profileRoutes(app: FastifyInstance) {
|
||||
app.addHook('preHandler', app.authenticate);
|
||||
|
||||
app.get('/', async (request) => {
|
||||
const profile = await app.prisma.user.findUnique({ where: { id: request.user.id } });
|
||||
if (!profile) throw new NotFoundError('Profile');
|
||||
return profile;
|
||||
});
|
||||
|
||||
app.patch('/', async (request) => {
|
||||
const body = updateProfileSchema.parse(request.body);
|
||||
try {
|
||||
const updated = await app.prisma.user.update({
|
||||
where: { id: request.user.id },
|
||||
data: body,
|
||||
});
|
||||
return updated;
|
||||
} catch (err) {
|
||||
if (err instanceof Prisma.PrismaClientKnownRequestError && err.code === 'P2002') {
|
||||
throw new ConflictError('Slug is already taken');
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user