import type {FastifyRequest, FastifyReply} from 'fastify'; import {DebtPaymentService} from '../services/DebtPaymentService'; import {getUserId} from '../middleware/auth'; import {z} from 'zod'; const createPaymentSchema = z.object({ accountId: z.string().uuid(), amount: z.number().min(0.01), paymentDate: z.string().transform(str => new Date(str)), notes: z.string().optional(), }); /** * Controller for DebtPayment endpoints * Implements Single Responsibility Principle - handles only HTTP layer */ export class DebtPaymentController { constructor(private paymentService: DebtPaymentService) {} /** * Create a new debt payment */ async create(request: FastifyRequest, reply: FastifyReply) { const userId = getUserId(request); const data = createPaymentSchema.parse(request.body); const payment = await this.paymentService.create(userId, data); return reply.status(201).send({payment}); } /** * Get all debt payments */ async getAll(request: FastifyRequest, reply: FastifyReply) { const userId = getUserId(request); const {accountId, startDate, endDate} = request.query as { accountId?: string; startDate?: string; endDate?: string; }; if (accountId) { const payments = await this.paymentService.getByAccount(accountId, userId); return reply.send({payments}); } if (startDate && endDate) { const payments = await this.paymentService.getByDateRange( userId, new Date(startDate), new Date(endDate) ); return reply.send({payments}); } const payments = await this.paymentService.getAllByUser(userId); return reply.send({payments}); } /** * Get a single debt payment */ async getOne(request: FastifyRequest, reply: FastifyReply) { const userId = getUserId(request); const {id} = request.params as {id: string}; const payment = await this.paymentService.getById(id, userId); return reply.send({payment}); } /** * Delete a debt payment */ async delete(request: FastifyRequest, reply: FastifyReply) { const userId = getUserId(request); const {id} = request.params as {id: string}; await this.paymentService.delete(id, userId); return reply.status(204).send(); } /** * Get total payments */ async getTotalPayments(request: FastifyRequest, reply: FastifyReply) { const userId = getUserId(request); const totalPayments = await this.paymentService.getTotalPayments(userId); return reply.send({totalPayments}); } }