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.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import {Asset, AssetType} from '@prisma/client';
|
||||
import type {Asset} from '@prisma/client';
|
||||
import {AssetRepository} from '../repositories/AssetRepository';
|
||||
import {NotFoundError, ForbiddenError, ValidationError} from '../utils/errors';
|
||||
import {NotFoundError, ValidationError} from '../utils/errors';
|
||||
|
||||
type AssetType = 'cash' | 'investment' | 'property' | 'vehicle' | 'other';
|
||||
const VALID_ASSET_TYPES: AssetType[] = ['cash', 'investment', 'property', 'vehicle', 'other'];
|
||||
|
||||
interface CreateAssetDTO {
|
||||
name: string;
|
||||
@@ -54,7 +57,7 @@ export class AssetService {
|
||||
if (data.value !== undefined || data.name !== undefined || data.type !== undefined) {
|
||||
this.validateAssetData({
|
||||
name: data.name || asset.name,
|
||||
type: data.type || asset.type,
|
||||
type: (data.type || asset.type) as AssetType,
|
||||
value: data.value !== undefined ? data.value : asset.value,
|
||||
});
|
||||
}
|
||||
@@ -75,6 +78,18 @@ export class AssetService {
|
||||
return this.assetRepository.getTotalValue(userId);
|
||||
}
|
||||
|
||||
async getByType(userId: string): Promise<Record<string, Asset[]>> {
|
||||
const assets = await this.assetRepository.findAllByUser(userId);
|
||||
return assets.reduce((acc, asset) => {
|
||||
const type = asset.type;
|
||||
if (!acc[type]) {
|
||||
acc[type] = [];
|
||||
}
|
||||
acc[type].push(asset);
|
||||
return acc;
|
||||
}, {} as Record<string, Asset[]>);
|
||||
}
|
||||
|
||||
private validateAssetData(data: CreateAssetDTO): void {
|
||||
if (!data.name || data.name.trim().length === 0) {
|
||||
throw new ValidationError('Asset name is required');
|
||||
@@ -84,7 +99,7 @@ export class AssetService {
|
||||
throw new ValidationError('Asset value cannot be negative');
|
||||
}
|
||||
|
||||
if (!Object.values(AssetType).includes(data.type)) {
|
||||
if (!VALID_ASSET_TYPES.includes(data.type)) {
|
||||
throw new ValidationError('Invalid asset type');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user