- 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.
79 lines
1.9 KiB
TypeScript
79 lines
1.9 KiB
TypeScript
import type {Liability, Prisma} from '@prisma/client';
|
|
import {DatabaseConnection} from '../config/database';
|
|
|
|
const prisma = DatabaseConnection.getInstance();
|
|
|
|
/**
|
|
* Repository for Liability data access
|
|
* Implements Single Responsibility Principle - handles only database operations
|
|
*/
|
|
export class LiabilityRepository {
|
|
async findById(id: string): Promise<Liability | null> {
|
|
return prisma.liability.findUnique({
|
|
where: {id},
|
|
});
|
|
}
|
|
|
|
async findByIdAndUser(id: string, userId: string): Promise<Liability | null> {
|
|
return prisma.liability.findFirst({
|
|
where: {id, userId},
|
|
});
|
|
}
|
|
|
|
async findAllByUser(userId: string): Promise<Liability[]> {
|
|
return prisma.liability.findMany({
|
|
where: {userId},
|
|
orderBy: {createdAt: 'desc'},
|
|
});
|
|
}
|
|
|
|
async create(data: Prisma.LiabilityCreateInput): Promise<Liability> {
|
|
return prisma.liability.create({
|
|
data,
|
|
});
|
|
}
|
|
|
|
async update(id: string, data: Prisma.LiabilityUpdateInput): Promise<Liability> {
|
|
return prisma.liability.update({
|
|
where: {id},
|
|
data,
|
|
});
|
|
}
|
|
|
|
async delete(id: string): Promise<void> {
|
|
await prisma.liability.delete({
|
|
where: {id},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get total value of all liabilities for a user
|
|
*/
|
|
async getTotalValue(userId: string): Promise<number> {
|
|
const result = await prisma.liability.aggregate({
|
|
where: {userId},
|
|
_sum: {
|
|
balance: true,
|
|
},
|
|
});
|
|
|
|
return result._sum.balance || 0;
|
|
}
|
|
|
|
/**
|
|
* Get liabilities grouped by type
|
|
*/
|
|
async getByType(userId: string): Promise<Record<string, Liability[]>> {
|
|
const liabilities = await this.findAllByUser(userId);
|
|
|
|
return liabilities.reduce((acc, liability) => {
|
|
const type = liability.type;
|
|
if (!acc[type]) {
|
|
acc[type] = [];
|
|
}
|
|
acc[type].push(liability);
|
|
return acc;
|
|
}, {} as Record<string, Liability[]>);
|
|
}
|
|
}
|