76 lines
2.4 KiB
TypeScript
76 lines
2.4 KiB
TypeScript
import type { FastifyInstance } from 'fastify';
|
|
import {
|
|
createWishSchema,
|
|
updateWishSchema,
|
|
wishStatusQuery,
|
|
NEW_BADGE_DAYS,
|
|
} from '@family-wishlist/shared';
|
|
import { WishesService } from './wishes.service.js';
|
|
import { enqueueOgFetch } from '../images/og.service.js';
|
|
|
|
export default async function wishesRoutes(app: FastifyInstance) {
|
|
app.addHook('preHandler', app.authenticate);
|
|
const service = new WishesService(app.prisma);
|
|
|
|
app.get('/', async (request) => {
|
|
const qs = wishStatusQuery.parse((request.query as { status?: string })?.status ?? 'active');
|
|
const wishes = await service.list(request.user.id, qs);
|
|
return wishes.map((w) => ({
|
|
...w,
|
|
isNewForOwner:
|
|
w.status === 'ACTIVE' &&
|
|
Date.now() - w.createdAt.getTime() < NEW_BADGE_DAYS * 24 * 60 * 60 * 1000,
|
|
}));
|
|
});
|
|
|
|
app.get('/:id', async (request) => {
|
|
const { id } = request.params as { id: string };
|
|
return service.getOwned(request.user.id, id);
|
|
});
|
|
|
|
app.post('/', async (request, reply) => {
|
|
const input = createWishSchema.parse(request.body);
|
|
const wish = await service.create(request.user.id, input);
|
|
if (wish.url) enqueueOgFetch(app, wish.id, wish.url);
|
|
reply.code(201);
|
|
return wish;
|
|
});
|
|
|
|
app.patch('/:id', async (request) => {
|
|
const { id } = request.params as { id: string };
|
|
const input = updateWishSchema.parse(request.body);
|
|
const updated = await service.update(request.user.id, id, input);
|
|
if (input.url !== undefined && updated.url) {
|
|
enqueueOgFetch(app, updated.id, updated.url);
|
|
}
|
|
return updated;
|
|
});
|
|
|
|
app.delete('/:id', async (request) => {
|
|
const { id } = request.params as { id: string };
|
|
return service.softDelete(request.user.id, id);
|
|
});
|
|
|
|
app.post('/:id/archive', async (request) => {
|
|
const { id } = request.params as { id: string };
|
|
return service.archive(request.user.id, id);
|
|
});
|
|
|
|
app.post('/:id/complete', async (request) => {
|
|
const { id } = request.params as { id: string };
|
|
return service.complete(request.user.id, id);
|
|
});
|
|
|
|
app.post('/:id/restore', async (request) => {
|
|
const { id } = request.params as { id: string };
|
|
return service.restore(request.user.id, id);
|
|
});
|
|
|
|
app.post('/:id/duplicate', async (request, reply) => {
|
|
const { id } = request.params as { id: string };
|
|
const wish = await service.duplicate(request.user.id, id);
|
|
reply.code(201);
|
|
return wish;
|
|
});
|
|
}
|