Files
personal-finance/backend-api/src/controllers/DebtCategoryController.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

98 lines
2.6 KiB
TypeScript

import type {FastifyRequest, FastifyReply} from 'fastify';
import {DebtCategoryService} from '../services/DebtCategoryService';
import {getUserId} from '../middleware/auth';
import {z} from 'zod';
const createCategorySchema = z.object({
name: z.string().min(1).max(255),
description: z.string().optional(),
color: z
.string()
.regex(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
.optional()
});
const updateCategorySchema = z.object({
name: z.string().min(1).max(255).optional(),
description: z.string().optional(),
color: z
.string()
.regex(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
.optional()
});
/**
* Controller for DebtCategory endpoints
* Implements Single Responsibility Principle - handles only HTTP layer
*/
export class DebtCategoryController {
constructor(private categoryService: DebtCategoryService) {}
/**
* Create a new debt category
*/
async create(request: FastifyRequest, reply: FastifyReply) {
const userId = getUserId(request);
const data = createCategorySchema.parse(request.body);
const category = await this.categoryService.create(userId, data);
return reply.status(201).send({category});
}
/**
* Get all debt categories
*/
async getAll(request: FastifyRequest, reply: FastifyReply) {
const userId = getUserId(request);
const {withStats} = request.query as {withStats?: string};
if (withStats === 'true') {
const categories = await this.categoryService.getWithStats(userId);
return reply.send({categories});
}
const categories = await this.categoryService.getAllByUser(userId);
return reply.send({categories});
}
/**
* Get a single debt category
*/
async getOne(request: FastifyRequest, reply: FastifyReply) {
const userId = getUserId(request);
const {id} = request.params as {id: string};
const category = await this.categoryService.getById(id, userId);
return reply.send({category});
}
/**
* Update a debt category
*/
async update(request: FastifyRequest, reply: FastifyReply) {
const userId = getUserId(request);
const {id} = request.params as {id: string};
const data = updateCategorySchema.parse(request.body);
const category = await this.categoryService.update(id, userId, data);
return reply.send({category});
}
/**
* Delete a debt category
*/
async delete(request: FastifyRequest, reply: FastifyReply) {
const userId = getUserId(request);
const {id} = request.params as {id: string};
await this.categoryService.delete(id, userId);
return reply.status(204).send();
}
}