feat(F-048): completed feature
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import type { FastifySchema } from 'fastify';
|
||||
import type pg from 'pg';
|
||||
import { z } from 'zod';
|
||||
import type { Authenticate } from '../../../shared/auth.js';
|
||||
import { requireRole } from '../../../shared/auth.js';
|
||||
import { parseJson } from '../../../shared/http-input.js';
|
||||
import { errorSchema } from '../../../shared/swagger.js';
|
||||
import { NotificationsService } from '../application/notifications-service.js';
|
||||
import { PgNotificationsRepository } from '../infrastructure/pg-notifications-repository.js';
|
||||
|
||||
@@ -32,7 +34,13 @@ export async function registerNotificationsRoutes(
|
||||
deps.emailProvider,
|
||||
);
|
||||
|
||||
app.post('/notifications/dispatch', async (request, reply) => {
|
||||
const dispatchSwaggerSchema: FastifySchema = {
|
||||
tags: ['Admin'],
|
||||
summary: 'Dispatch notification (admin)',
|
||||
body: { type: 'object' },
|
||||
response: { 401: errorSchema, 403: errorSchema },
|
||||
};
|
||||
app.post('/notifications/dispatch', { schema: dispatchSwaggerSchema }, async (request, reply) => {
|
||||
const user = await deps.authenticate(request);
|
||||
requireRole(user, 'admin');
|
||||
const input = parseJson(dispatchSchema, request.body);
|
||||
@@ -40,18 +48,28 @@ export async function registerNotificationsRoutes(
|
||||
return reply.send({ outcome });
|
||||
});
|
||||
|
||||
app.get('/notifications/messages/:eventId', async (request, reply) => {
|
||||
const user = await deps.authenticate(request);
|
||||
requireRole(user, 'admin');
|
||||
const params = z.object({ eventId: z.string().min(1).max(120) }).parse(request.params);
|
||||
const repo = new PgNotificationsRepository(deps.pool);
|
||||
const found = await repo.findByEventId(params.eventId);
|
||||
if (!found)
|
||||
throw new (await import('../../../shared/errors.js')).AppError(
|
||||
404,
|
||||
'NOT_FOUND',
|
||||
'Message not found',
|
||||
);
|
||||
return reply.send(found);
|
||||
});
|
||||
const getMessageSchema: FastifySchema = {
|
||||
tags: ['Admin'],
|
||||
summary: 'Get notification message (admin)',
|
||||
params: { type: 'object', required: ['eventId'], properties: { eventId: { type: 'string' } } },
|
||||
response: { 401: errorSchema, 403: errorSchema, 404: errorSchema },
|
||||
};
|
||||
app.get(
|
||||
'/notifications/messages/:eventId',
|
||||
{ schema: getMessageSchema },
|
||||
async (request, reply) => {
|
||||
const user = await deps.authenticate(request);
|
||||
requireRole(user, 'admin');
|
||||
const params = z.object({ eventId: z.string().min(1).max(120) }).parse(request.params);
|
||||
const repo = new PgNotificationsRepository(deps.pool);
|
||||
const found = await repo.findByEventId(params.eventId);
|
||||
if (!found)
|
||||
throw new (await import('../../../shared/errors.js')).AppError(
|
||||
404,
|
||||
'NOT_FOUND',
|
||||
'Message not found',
|
||||
);
|
||||
return reply.send(found);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user