Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/api/src/announcements/announcements.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ function setup() {
findUniqueOrThrow: jest.fn().mockImplementation(({ where }: { where: { id: string } }) => Promise.resolve(rows.get(where.id) ?? stored({ id: where.id }))),
updateMany: jest.fn().mockResolvedValue({ count: 1 }),
findFirst: jest.fn().mockResolvedValue(stored({ notifiedAt: new Date('2026-11-18T09:00:00Z'), publishedAt: new Date('2026-11-18T09:00:00Z') })),
findFirstOrThrow: jest.fn().mockImplementation(() => Promise.resolve(stored({ notifiedAt: new Date() }))),
// A fixed past time: a fresh new Date() here could land a millisecond after the service's own clock and read as scheduled.
findFirstOrThrow: jest.fn().mockImplementation(() => Promise.resolve(stored({ notifiedAt: new Date('2026-11-18T09:00:00Z'), publishedAt: new Date('2026-11-18T09:00:00Z') }))),
},
enrollment: { findMany: jest.fn().mockResolvedValue([place('sec-a', 3, 'dept-cse')]) },
class: { findMany: jest.fn().mockResolvedValue([place('sec-b', 5, 'dept-cse')]) },
Expand Down
40 changes: 24 additions & 16 deletions apps/api/src/fees/fees.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { BadRequestException } from '@nestjs/common';
import { InvoiceStatus, PaymentMethod } from '@prisma/client';

/** Bad input is the client's mistake: a 400 with the reason, not a plain Error that surfaced as a 500. */
function invalid(message: string): never {
throw new BadRequestException({ code: 'INVALID_INPUT', message });
}

/** Methods Razorpay can take online. Cash and bank transfers are paid at the accounts office, not through an order. */
export const ONLINE_METHODS: PaymentMethod[] = [PaymentMethod.ONLINE, PaymentMethod.UPI, PaymentMethod.CARD];

export type CreateFeeStructureDto = {
name: string;
amount: number;
Expand Down Expand Up @@ -40,16 +49,16 @@ export function parseCreateFeeStructureDto(body: unknown): CreateFeeStructureDto
const b = (body ?? {}) as Record<string, unknown>;

if (typeof b.name !== 'string' || !b.name.trim()) {
throw new Error('Fee structure name is required');
invalid('Fee structure name is required');
}

const amount = Number(b.amount);
if (isNaN(amount) || amount <= 0) {
throw new Error('amount must be a positive number');
invalid('amount must be a positive number');
}

if (typeof b.activeFrom !== 'string' || isNaN(Date.parse(b.activeFrom))) {
throw new Error('Valid activeFrom date is required');
invalid('Valid activeFrom date is required');
}

let activeUntil: string | undefined;
Expand All @@ -72,12 +81,12 @@ export function parseCreateInvoiceDto(body: unknown): CreateInvoiceDto {
const b = (body ?? {}) as Record<string, unknown>;

if (typeof b.studentId !== 'string' || !b.studentId.trim()) {
throw new Error('studentId is required');
invalid('studentId is required');
}

const amountDue = Number(b.amountDue);
if (isNaN(amountDue) || amountDue <= 0) {
throw new Error('amountDue must be a positive number');
invalid('amountDue must be a positive number');
}

let dueOn: string | undefined;
Expand All @@ -97,15 +106,14 @@ export function parseCreateRazorpayOrderDto(body: unknown): CreateRazorpayOrderD
const b = (body ?? {}) as Record<string, unknown>;

if (typeof b.invoiceId !== 'string' || !b.invoiceId.trim()) {
throw new Error('invoiceId is required');
invalid('invoiceId is required');
}

let method: PaymentMethod = PaymentMethod.ONLINE;
if (typeof b.method === 'string') {
const m = b.method.toUpperCase();
if (Object.values(PaymentMethod).includes(m as PaymentMethod)) {
method = m as PaymentMethod;
}
if (b.method !== undefined && b.method !== null && b.method !== '') {
const m = typeof b.method === 'string' ? b.method.toUpperCase() : '';
if (!ONLINE_METHODS.includes(m as PaymentMethod)) invalid('An online payment must use ONLINE, UPI or CARD.');
method = m as PaymentMethod;
}

return {
Expand All @@ -118,16 +126,16 @@ export function parseVerifyRazorpayPaymentDto(body: unknown): VerifyRazorpayPaym
const b = (body ?? {}) as Record<string, unknown>;

if (typeof b.paymentId !== 'string' || !b.paymentId.trim()) {
throw new Error('paymentId is required');
invalid('paymentId is required');
}
if (typeof b.razorpayOrderId !== 'string' || !b.razorpayOrderId.trim()) {
throw new Error('razorpayOrderId is required');
invalid('razorpayOrderId is required');
}
if (typeof b.razorpayPaymentId !== 'string' || !b.razorpayPaymentId.trim()) {
throw new Error('razorpayPaymentId is required');
invalid('razorpayPaymentId is required');
}
if (typeof b.razorpaySignature !== 'string' || !b.razorpaySignature.trim()) {
throw new Error('razorpaySignature is required');
invalid('razorpaySignature is required');
}

return {
Expand All @@ -144,7 +152,7 @@ export function parseFeesListQuery(query: unknown): FeesListQuery {
const pageSize = q.pageSize ? Number(q.pageSize) : 20;

if (!Number.isInteger(page) || page < 1 || !Number.isInteger(pageSize) || pageSize < 1 || pageSize > 100) {
throw new Error('Invalid pagination parameters');
invalid('Invalid pagination parameters');
}

let status: InvoiceStatus | undefined;
Expand Down
151 changes: 145 additions & 6 deletions apps/api/src/fees/fees.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { createHmac } from 'node:crypto';
import { BadRequestException, ForbiddenException, NotFoundException, ServiceUnavailableException } from '@nestjs/common';
import { BadRequestException, ConflictException, ForbiddenException, NotFoundException, ServiceUnavailableException } from '@nestjs/common';
import { InvoiceStatus, PaymentStatus } from '@prisma/client';
import { FeesService } from './fees.service';
import type { AuthorizationContext } from '../authorization/authorization.types';
import type { DatabaseService } from '../database.service';
import {
parseCreateFeeStructureDto,
parseCreateInvoiceDto,
parseCreateRazorpayOrderDto,
parseFeesListQuery,
parseVerifyRazorpayPaymentDto,
} from './fees.dto';

Expand All @@ -26,11 +28,15 @@ describe('FeesService', () => {
findMany: jest.fn(),
count: jest.fn(),
update: jest.fn(),
updateMany: jest.fn().mockResolvedValue({ count: 1 }),
},
payment: {
create: jest.fn(),
findFirst: jest.fn(),
update: jest.fn(),
updateMany: jest.fn().mockResolvedValue({ count: 1 }),
aggregate: jest.fn().mockResolvedValue({ _sum: { amount: null } }),
findUniqueOrThrow: jest.fn().mockResolvedValue({ id: 'pay-1', status: PaymentStatus.COMPLETED }),
},
receipt: {
create: jest.fn(),
Expand Down Expand Up @@ -141,6 +147,8 @@ describe('FeesService', () => {
invoiceId: 'inv-1',
amount: 25000,
status: PaymentStatus.PENDING,
transactionRef: 'order_123',
invoice: { amountDue: 25000 },
});

const dto = parseVerifyRazorpayPaymentDto({
Expand All @@ -151,10 +159,12 @@ describe('FeesService', () => {
});

await expect(service.verifyRazorpayPayment(dto, studentContext)).rejects.toThrow(BadRequestException);
expect(database.payment.update).toHaveBeenCalledWith({
where: { id: 'pay-1' },
// Failed only while still pending.
expect(database.payment.updateMany).toHaveBeenCalledWith({
where: { id: 'pay-1', collegeId: 'col-a', status: PaymentStatus.PENDING },
data: { status: PaymentStatus.FAILED },
});
expect(database.receipt.create).not.toHaveBeenCalled();
});

it('settles payment and generates receipt on valid test signature', async () => {
Expand All @@ -164,7 +174,10 @@ describe('FeesService', () => {
invoiceId: 'inv-1',
amount: 25000,
status: PaymentStatus.PENDING,
transactionRef: 'order_123',
invoice: { amountDue: 25000 },
});
database.payment.aggregate.mockResolvedValueOnce({ _sum: { amount: 25000 } });
database.payment.update.mockResolvedValue({
id: 'pay-1',
status: PaymentStatus.COMPLETED,
Expand All @@ -190,6 +203,125 @@ describe('FeesService', () => {

expect(result.success).toBe(true);
expect(result.receipt).toHaveProperty('id', 'rec-1');
expect(database.payment.updateMany).toHaveBeenCalledWith({
where: { id: 'pay-1', collegeId: 'col-a', status: PaymentStatus.PENDING },
data: { status: PaymentStatus.COMPLETED, transactionRef: 'pay_rzp_123', paidAt: expect.any(Date) },
});
expect(database.invoice.update).toHaveBeenCalledWith({ where: { id: 'inv-1' }, data: { status: InvoiceStatus.PAID } });
expect(database.receipt.create).toHaveBeenCalledWith({
data: expect.objectContaining({ paymentId: 'pay-1', receiptNumber: expect.stringMatching(/^REC-\d{4}-[0-9A-F]{10}$/) }),
});
});
});

describe('payment rules', () => {
const pending = { id: 'pay-1', collegeId: 'col-a', invoiceId: 'inv-1', amount: 15000, status: PaymentStatus.PENDING, transactionRef: 'order_1', invoice: { amountDue: 25000 } };
const verify = (overrides: Record<string, string> = {}) =>
parseVerifyRazorpayPaymentDto({ paymentId: 'pay-1', razorpayOrderId: 'order_1', razorpayPaymentId: 'pay_rzp_1', razorpaySignature: 'test_sig_pay_rzp_1', ...overrides });
beforeEach(() => database.student.findFirst.mockResolvedValue({ id: 'stu-1' }));

it.each([InvoiceStatus.DRAFT, InvoiceStatus.VOID])('refuses an order for a %s invoice', async (status) => {
database.invoice.findFirst.mockResolvedValue({ id: 'inv-1', status, amountDue: 25000, invoiceNumber: 'INV-1' });
await expect(service.createRazorpayOrder({ invoiceId: 'inv-1' }, studentContext)).rejects.toMatchObject({ response: { code: 'INVOICE_NOT_PAYABLE' } });
expect(database.payment.create).not.toHaveBeenCalled();
});

it('orders only what is still owed after a part payment', async () => {
database.invoice.findFirst.mockResolvedValue({ id: 'inv-1', status: InvoiceStatus.PARTIALLY_PAID, amountDue: 25000, invoiceNumber: 'INV-1' });
database.payment.aggregate.mockResolvedValueOnce({ _sum: { amount: 10000 } });
database.payment.findFirst.mockResolvedValueOnce(null);
database.payment.create.mockResolvedValue({ id: 'pay-1' });
await expect(service.createRazorpayOrder({ invoiceId: 'inv-1' }, studentContext)).resolves.toMatchObject({ amount: 15000 });
expect(database.payment.aggregate).toHaveBeenCalledWith({ where: { collegeId: 'col-a', invoiceId: 'inv-1', status: PaymentStatus.COMPLETED }, _sum: { amount: true } });
expect(database.payment.create).toHaveBeenCalledWith({ data: expect.objectContaining({ amount: 15000, status: PaymentStatus.PENDING }) });
});

it('gives a second tab the open order instead of a second one, and closes any others', async () => {
database.invoice.findFirst.mockResolvedValue({ id: 'inv-1', status: InvoiceStatus.ISSUED, amountDue: 25000, invoiceNumber: 'INV-1' });
database.payment.findFirst.mockResolvedValueOnce({ id: 'pay-open', amount: 25000, status: PaymentStatus.PENDING, transactionRef: 'order_open' });
await expect(service.createRazorpayOrder({ invoiceId: 'inv-1' }, studentContext)).resolves.toMatchObject({ paymentId: 'pay-open', razorpayOrderId: 'order_open', amount: 25000 });
expect(database.payment.create).not.toHaveBeenCalled();
// The invoice is locked first, and every other open order for it is closed.
expect(database.invoice.updateMany).toHaveBeenCalledWith({ where: { id: 'inv-1', collegeId: 'col-a' }, data: { updatedAt: expect.any(Date) } });
expect(database.payment.updateMany).toHaveBeenCalledWith({
where: { collegeId: 'col-a', invoiceId: 'inv-1', status: PaymentStatus.PENDING, id: { not: 'pay-open' } },
data: { status: PaymentStatus.FAILED },
});
});

it('replaces an open order for an out-of-date amount', async () => {
database.invoice.findFirst.mockResolvedValue({ id: 'inv-1', status: InvoiceStatus.PARTIALLY_PAID, amountDue: 25000, invoiceNumber: 'INV-1' });
database.payment.aggregate.mockResolvedValueOnce({ _sum: { amount: 10000 } });
database.payment.findFirst.mockResolvedValueOnce({ id: 'pay-old', amount: 25000, status: PaymentStatus.PENDING, transactionRef: 'order_old' });
database.payment.create.mockResolvedValue({ id: 'pay-new', transactionRef: 'order_new' });
await expect(service.createRazorpayOrder({ invoiceId: 'inv-1' }, studentContext)).resolves.toMatchObject({ paymentId: 'pay-new', razorpayOrderId: 'order_new', amount: 15000 });
expect(database.payment.updateMany).toHaveBeenCalledWith({ where: { collegeId: 'col-a', invoiceId: 'inv-1', status: PaymentStatus.PENDING }, data: { status: PaymentStatus.FAILED } });
expect(database.payment.create).toHaveBeenCalledWith({ data: expect.objectContaining({ amount: 15000, transactionRef: expect.stringMatching(/^order_/) }) });
});

it('refuses an order when the payments already cover the invoice', async () => {
database.invoice.findFirst.mockResolvedValue({ id: 'inv-1', status: InvoiceStatus.ISSUED, amountDue: 25000, invoiceNumber: 'INV-1' });
database.payment.aggregate.mockResolvedValueOnce({ _sum: { amount: 25000 } });
await expect(service.createRazorpayOrder({ invoiceId: 'inv-1' }, studentContext)).rejects.toMatchObject({ response: { code: 'INVOICE_ALREADY_PAID' } });
expect(database.payment.create).not.toHaveBeenCalled();
});

it.each([PaymentStatus.COMPLETED, PaymentStatus.FAILED])('never changes a %s payment, whatever the signature', async (status) => {
database.payment.findFirst.mockResolvedValue({ ...pending, status });
await expect(service.verifyRazorpayPayment(verify(), studentContext)).rejects.toMatchObject({ response: { code: 'PAYMENT_NOT_PENDING' } });
await expect(service.verifyRazorpayPayment(verify({ razorpaySignature: 'bad' }), studentContext)).rejects.toBeInstanceOf(ConflictException);
expect(database.payment.updateMany).not.toHaveBeenCalled();
expect(database.receipt.create).not.toHaveBeenCalled();
});

it('refuses a signature for a different order', async () => {
database.payment.findFirst.mockResolvedValue(pending);
await expect(service.verifyRazorpayPayment(verify({ razorpayOrderId: 'order_other' }), studentContext)).rejects.toMatchObject({ response: { code: 'ORDER_MISMATCH' } });
expect(database.payment.updateMany).not.toHaveBeenCalled();
});

it('settles a payment once even if two verifications race', async () => {
database.payment.findFirst.mockResolvedValue(pending);
database.payment.updateMany.mockResolvedValueOnce({ count: 0 });
await expect(service.verifyRazorpayPayment(verify(), studentContext)).rejects.toMatchObject({ response: { code: 'PAYMENT_NOT_PENDING' } });
expect(database.invoice.update).not.toHaveBeenCalled();
expect(database.receipt.create).not.toHaveBeenCalled();
});

it('refuses a Razorpay payment id that was already recorded for another order', async () => {
database.payment.findFirst.mockResolvedValue(pending);
database.payment.updateMany.mockRejectedValueOnce({ code: 'P2002', meta: { target: ['collegeId', 'transactionRef'] } });
await expect(service.verifyRazorpayPayment(verify(), studentContext)).rejects.toMatchObject({ response: { code: 'PAYMENT_ALREADY_RECORDED' } });
expect(database.receipt.create).not.toHaveBeenCalled();
});

it('marks the invoice partly paid while money is still owed', async () => {
database.payment.findFirst.mockResolvedValue(pending);
database.payment.aggregate.mockResolvedValueOnce({ _sum: { amount: 15000 } });
database.receipt.create.mockResolvedValue({ id: 'rec-1' });
await expect(service.verifyRazorpayPayment(verify(), studentContext)).resolves.toMatchObject({ success: true });
// The invoice row is locked before its payments are added up.
expect(database.invoice.updateMany).toHaveBeenCalledWith({ where: { id: 'inv-1', collegeId: 'col-a' }, data: { updatedAt: expect.any(Date) } });
expect(database.invoice.update).toHaveBeenCalledWith({ where: { id: 'inv-1' }, data: { status: InvoiceStatus.PARTIALLY_PAID } });
});

it('gives bad input a 400 and takes only online methods for an order', () => {
expect(() => parseCreateRazorpayOrderDto({})).toThrow(BadRequestException);
expect(() => parseCreateRazorpayOrderDto({ invoiceId: 'inv-1', method: 'CASH' })).toThrow(BadRequestException);
expect(() => parseCreateRazorpayOrderDto({ invoiceId: 'inv-1', method: 'bank_transfer' })).toThrow(BadRequestException);
expect(parseCreateRazorpayOrderDto({ invoiceId: 'inv-1', method: 'upi' })).toEqual({ invoiceId: 'inv-1', method: 'UPI' });
expect(parseCreateRazorpayOrderDto({ invoiceId: 'inv-1' })).toEqual({ invoiceId: 'inv-1', method: 'ONLINE' });
expect(() => parseVerifyRazorpayPaymentDto({ paymentId: 'pay-1' })).toThrow(BadRequestException);
expect(() => parseCreateInvoiceDto({ studentId: 'stu-1', amountDue: -5 })).toThrow(BadRequestException);
expect(() => parseCreateFeeStructureDto({ name: '', amount: 1, activeFrom: '2026-01-01' })).toThrow(BadRequestException);
expect(() => parseFeesListQuery({ page: 'abc' })).toThrow(BadRequestException);
});

it('numbers invoices so that a clash is practically impossible', async () => {
database.student.findFirst.mockResolvedValue({ id: 'stu-1' });
database.invoice.create.mockResolvedValue({ id: 'inv-9' });
await service.createInvoice(parseCreateInvoiceDto({ studentId: 'stu-1', amountDue: 1000 }), admin);
expect(database.invoice.create).toHaveBeenCalledWith(expect.objectContaining({ data: expect.objectContaining({ invoiceNumber: expect.stringMatching(/^INV-\d{4}-[0-9A-F]{10}$/) }) }));
});
});
});
Expand Down Expand Up @@ -230,8 +362,15 @@ describe('FeesService access and payment checks', () => {
const hod: AuthorizationContext = { userId: 'hod-1', collegeId: 'col-a', roles: ['HOD'], permissions: [], departmentId: 'dept-a' };
const database = {
student: { findFirst: jest.fn().mockResolvedValue({ id: 'stu-1' }) },
invoice: { findFirst: jest.fn(), update: jest.fn() },
payment: { create: jest.fn().mockResolvedValue({ id: 'pay-1' }), findFirst: jest.fn(), update: jest.fn() },
invoice: { findFirst: jest.fn(), update: jest.fn(), updateMany: jest.fn() },
payment: {
create: jest.fn().mockResolvedValue({ id: 'pay-1' }),
findFirst: jest.fn(),
update: jest.fn(),
updateMany: jest.fn().mockResolvedValue({ count: 1 }),
aggregate: jest.fn().mockResolvedValue({ _sum: { amount: null } }),
findUniqueOrThrow: jest.fn().mockResolvedValue({ id: 'pay-1' }),
},
receipt: { findFirst: jest.fn(), create: jest.fn() },
feeStructure: { create: jest.fn() },
$transaction: jest.fn(),
Expand Down Expand Up @@ -261,7 +400,7 @@ describe('FeesService access and payment checks', () => {

it('rejects the simulated signature once a real secret is configured, and accepts the real one', async () => {
process.env.RAZORPAY_KEY_SECRET = 'real-secret';
database.payment.findFirst.mockResolvedValue({ id: 'pay-1', invoiceId: 'inv-1', amount: 100, status: PaymentStatus.PENDING });
database.payment.findFirst.mockResolvedValue({ id: 'pay-1', invoiceId: 'inv-1', amount: 100, status: PaymentStatus.PENDING, transactionRef: 'order_1', invoice: { amountDue: 100 } });
await expect(service.verifyRazorpayPayment(dto('test_sig_pay_rzp_1'), studentContext)).rejects.toBeInstanceOf(BadRequestException);
const real = createHmac('sha256', 'real-secret').update('order_1|pay_rzp_1').digest('hex');
database.receipt.findFirst.mockResolvedValue(null);
Expand Down
Loading