Skip to content
Open
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
1 change: 1 addition & 0 deletions src/handlers/revenueHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ describe('RevenueHandler', () => {
expect(mockRevenueService.submitReport).toHaveBeenCalledWith({
offeringId: 'offering-1',
issuerId: 'issuer-1',
reportedBy: 'issuer-1',
amount: '500.50',
periodStart: new Date('2024-02-01'),
periodEnd: new Date('2024-02-28'),
Expand Down
2 changes: 2 additions & 0 deletions src/handlers/revenueHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class RevenueHandler {
const report = await this.revenueService.submitReport({
offeringId,
issuerId,
reportedBy: issuerId,
amount,
periodStart: new Date(periodStart),
periodEnd: new Date(periodEnd),
Expand Down Expand Up @@ -139,6 +140,7 @@ export class RevenueHandler {
const report = await this.revenueService.submitReport({
offeringId,
issuerId,
reportedBy: issuerId,
amount,
periodStart: new Date(periodStart),
periodEnd: new Date(periodEnd),
Expand Down
52 changes: 52 additions & 0 deletions src/services/revenueService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,58 @@ describe('RevenueService', () => {
);
});

// ── NOT NULL columns (migration 016) ────────────────────────────────────

it('passes issuer_id and reported_by to the repository', async () => {
mockOfferingRepo.findById.mockResolvedValue(mockOffering);
mockRevenueReportRepo.findOverlappingReport.mockResolvedValue(null);
mockLedgerLockRepo.isPeriodLocked.mockResolvedValue(false);
mockRevenueReportRepo.create.mockResolvedValue(mockReport);

await service.submitReport(validInput);

expect(mockRevenueReportRepo.create).toHaveBeenCalledWith(
expect.objectContaining({
issuer_id: 'issuer-abc',
reported_by: 'issuer-abc',
}),
mockTransactionClient
);
});

it('uses reportedBy for reported_by when provided', async () => {
mockOfferingRepo.findById.mockResolvedValue(mockOffering);
mockRevenueReportRepo.findOverlappingReport.mockResolvedValue(null);
mockLedgerLockRepo.isPeriodLocked.mockResolvedValue(false);
mockRevenueReportRepo.create.mockResolvedValue(mockReport);

await service.submitReport({ ...validInput, reportedBy: 'user-xyz' });

expect(mockRevenueReportRepo.create).toHaveBeenCalledWith(
expect.objectContaining({
issuer_id: 'issuer-abc',
reported_by: 'user-xyz',
}),
mockTransactionClient
);
});

it('satisfies a repository that enforces the NOT NULL columns', async () => {
mockOfferingRepo.findById.mockResolvedValue(mockOffering);
mockRevenueReportRepo.findOverlappingReport.mockResolvedValue(null);
mockLedgerLockRepo.isPeriodLocked.mockResolvedValue(false);
mockRevenueReportRepo.create.mockImplementation(async (data: Record<string, unknown>) => {
for (const column of ['issuer_id', 'reported_by']) {
if (data[column] == null) {
throw new Error(`null value in column "${column}" violates not-null constraint`);
}
}
return mockReport;
});

await expect(service.submitReport(validInput)).resolves.toBe(mockReport);
});

// ── Offering not found ──────────────────────────────────────────────────

it('throws NOT_FOUND when offering does not exist', async () => {
Expand Down
5 changes: 5 additions & 0 deletions src/services/revenueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '../db/repositories/revenueReportRepository';
import { LedgerPeriodLockRepository } from '../db/repositories/ledgerPeriodLockRepository';
import { AppError, Errors } from '../lib/errors';
import { isPositiveDecimalAmount } from '../lib/amountValidation';
import { Logger } from '../lib/logger';
import { LogLevel } from '../lib/logger';
import { Pool } from 'pg';
Expand All @@ -15,6 +16,8 @@ import { TransactionError, withTransaction } from '../db/transaction';
export interface SubmitRevenueReportInput {
offeringId: string;
issuerId: string;
/** Authenticated user submitting the report; defaults to `issuerId`. */
reportedBy?: string;
amount: string;
periodStart: Date;
periodEnd: Date;
Expand Down Expand Up @@ -128,6 +131,8 @@ export class RevenueService {
// 4. Build the insert payload before opening a transaction.
const reportData: CreateRevenueReportInput = {
offering_id: input.offeringId,
issuer_id: input.issuerId,
reported_by: input.reportedBy ?? input.issuerId,
amount: input.amount,
period_start: input.periodStart,
period_end: input.periodEnd,
Expand Down
Loading