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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 2x 2x 2x 2x 2x 2x 2x 6x 6x 2x 3x 3x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Injectable, NotFoundException } from '@nestjs/common';
import { and, desc, eq, isNull, sql } from 'drizzle-orm';
import { db } from '../db/client';
import { transactions } from '../db/schema';
import { AuditService } from '../audit/audit.service';
import { OutboxService } from '../events/outbox.service';
import type { AuthenticatedSession } from '../auth/tenant.guard';
import type { CreateTransactionDto, UpdateTransactionDto } from './dto';
@Injectable()
export class TransactionsService {
constructor(
private readonly outbox: OutboxService,
private readonly audit: AuditService,
) {}
async list(session: AuthenticatedSession, organizationId?: string) {
return db.query.transactions.findMany({
where: and(
eq(transactions.tenantId, session.tenantId),
isNull(transactions.deletedAt),
...(organizationId ? [eq(transactions.organizationId, organizationId)] : []),
),
orderBy: desc(transactions.transactionDate),
});
}
async getById(session: AuthenticatedSession, id: string) {
const row = await db.query.transactions.findFirst({
where: and(
eq(transactions.id, id),
eq(transactions.tenantId, session.tenantId),
isNull(transactions.deletedAt),
),
});
if (!row) throw new NotFoundException('Transaction not found');
return row;
}
async create(session: AuthenticatedSession, dto: CreateTransactionDto) {
return this.outbox.withTransaction(async (tx) => {
const [row] = await tx
.insert(transactions)
.values({
tenantId: session.tenantId,
organizationId: dto.organizationId,
type: dto.type,
amountMinorUnits: String(dto.amountMinorUnits),
currency: dto.currency.toUpperCase(),
transactionDate: new Date(dto.transactionDate),
evidenceStatus: dto.evidenceStatus ?? 'missing',
source: dto.source,
})
.returning();
await this.audit.record(tx, {
tenantId: session.tenantId,
actorId: session.userId,
action: 'transaction.created',
resource: `transaction:${row.id}`,
});
await this.outbox.record(tx, {
tenantId: session.tenantId,
workspaceId: session.workspaceId,
actorId: session.userId,
eventType: 'transaction.created',
aggregateType: 'transaction',
subjectId: row.id,
correlationId: session.correlationId,
payload: { type: row.type, currency: row.currency, evidenceStatus: row.evidenceStatus },
});
return row;
});
}
async update(session: AuthenticatedSession, id: string, dto: UpdateTransactionDto) {
await this.getById(session, id);
return this.outbox.withTransaction(async (tx) => {
const [row] = await tx
.update(transactions)
.set({
...(dto.evidenceStatus !== undefined ? { evidenceStatus: dto.evidenceStatus } : {}),
...(dto.type !== undefined ? { type: dto.type } : {}),
...(dto.source !== undefined ? { source: dto.source } : {}),
updatedAt: new Date(),
version: sql`${transactions.version} + 1`,
})
.where(and(eq(transactions.id, id), eq(transactions.tenantId, session.tenantId)))
.returning();
await this.audit.record(tx, {
tenantId: session.tenantId,
actorId: session.userId,
action: dto.evidenceStatus !== undefined
? `transaction.evidence_updated:${dto.evidenceStatus}`
: 'transaction.updated',
resource: `transaction:${id}`,
});
Eif (dto.evidenceStatus !== undefined) {
await this.outbox.record(tx, {
tenantId: session.tenantId,
workspaceId: session.workspaceId,
actorId: session.userId,
eventType: 'transaction.evidence_updated',
aggregateType: 'transaction',
subjectId: id,
correlationId: session.correlationId,
payload: { evidenceStatus: dto.evidenceStatus },
});
}
return row;
});
}
async softDelete(session: AuthenticatedSession, id: string) {
await this.getById(session, id);
await this.outbox.withTransaction(async (tx) => {
await tx
.update(transactions)
.set({ deletedAt: new Date() })
.where(and(eq(transactions.id, id), eq(transactions.tenantId, session.tenantId)));
await this.audit.record(tx, {
tenantId: session.tenantId,
actorId: session.userId,
action: 'transaction.deleted',
resource: `transaction:${id}`,
});
});
return { deleted: true };
}
}
|