Files
personal-finance/backend-api/src/controllers/DebtPaymentController.ts
Alexander Zinn 700832550c Update backend API configuration and schema for improved functionality
- Modified TypeScript configuration to disable strict mode and allow importing TypeScript extensions.
- Updated Prisma schema to enhance the User model with a new debtAccounts field and refined asset/liability types.
- Adjusted environment variable parsing for PORT to use coercion for better type handling.
- Refactored various controllers and repositories to utilize type imports for better clarity and maintainability.
- Enhanced service layers with new methods for retrieving assets by type and calculating invoice statistics.
- Introduced new types for invoice statuses and asset types to ensure consistency across the application.
2025-12-08 02:57:38 -05:00

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