Files
personal-finance/backend-api/src/controllers/DebtPaymentController.ts
Alexander Zinn df2cf418ea Enhance ESLint configuration and improve code consistency
- Added '@typescript-eslint/no-unused-vars' rule to ESLint configuration for better variable management in TypeScript files.
- Updated database.ts to ensure consistent logging format.
- Refactored AuthController and CashflowController to improve variable naming and maintainability.
- Added spacing for better readability in multiple controller methods.
- Adjusted error handling in middleware and repository files for improved clarity.
- Enhanced various service and repository methods to ensure consistent return types and error handling.
- Made minor formatting adjustments across frontend components for improved user experience.
2025-12-11 02:19:05 -05:00

94 lines
2.5 KiB
TypeScript

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});
}
}