All files / src/modules/mortgages mortgages.service.ts

76.19% Statements 16/21
87.5% Branches 7/8
42.85% Functions 3/7
70.58% Lines 12/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3313x 13x     13x 2x             2x 2x 2x 2x 2x               2x 2x 1x            
import { ForbiddenException, Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '@app/modules/prisma/prisma.service';
 
@Injectable()
export class MortgagesService {
  constructor(private readonly prisma: PrismaService) {}
 
  listLenders() { return this.prisma.client.lender.findMany({ orderBy: { name: 'asc' } }); }
  listProducts() { return this.prisma.client.mortgageProduct.findMany({ include: { lender: true } }); }
  listRates() { return this.prisma.client.rateSnapshot.findMany({ orderBy: { capturedAt: 'desc' }, take: 100 }); }
 
  calc(dto: { amountCents: number; termMonths: number; aprBps: number }) {
    const monthlyRate = dto.aprBps / 10000 / 12;
    const n = dto.termMonths;
    const p = dto.amountCents / 100;
    const payment = monthlyRate === 0 ? p / n : (p * monthlyRate) / (1 - Math.pow(1 + monthlyRate, -n));
    return { monthlyPayment: Math.round(payment * 100) }; // in cents
  }
 
  startApp(userId: number, body: any) {
    return this.prisma.client.mortgageApplication.create({ data: { userId, status: 'DRAFT', data: body } });
  }
 
  async getApp(id: number, userId: number) {
    const app = await this.prisma.client.mortgageApplication.findUnique({ where: { id } });
    if (!app) throw new NotFoundException('Application not found');
    if (app.userId !== userId) throw new ForbiddenException('Not your application');
    return app;
  }
}