Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MatanYadaev committed Mar 11, 2024
1 parent 2c4de9a commit 9d62068
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -489,5 +489,87 @@ describe('#TransactionControllerExternal', () => {
message: [expect.any(String)],
});
});

it('returns 400 when counterparty is missing from the body', async () => {
// Arrange
const apiKey = (customer.authenticationConfiguration as { authValue: string }).authValue;
const validTransaction = getBaseTransactionData();
const transactions = [
{
...validTransaction,
originator: undefined as unknown as TransactionCreateDto['originator'],
},
] as const satisfies readonly TransactionCreateDto[];

// Act
const response = await request(app.getHttpServer())
.post('/external/transactions/bulk')
.send(transactions)
.set('authorization', `Bearer ${apiKey}`);

// Assert
expect(response.status).toBe(400);
expect(response.body).toEqual({
statusCode: 400,
message: 'Originator and beneficiary are required.',
});
});

it('returns 400 when counterparty is missing an entity', async () => {
// Arrange
const apiKey = (customer.authenticationConfiguration as { authValue: string }).authValue;
const validTransaction = getBaseTransactionData();
const transactions = [
{
...validTransaction,
beneficiary: {
correlationId: faker.datatype.uuid(),
// Missing endUserData or businessData
},
},
] as const satisfies readonly TransactionCreateDto[];

// Act
const response = await request(app.getHttpServer())
.post('/external/transactions/bulk')
.send(transactions)
.set('authorization', `Bearer ${apiKey}`);

// Assert
expect(response.status).toBe(400);
expect(response.body).toEqual({
statusCode: 400,
message: 'Counterparty must have either business or end user data.',
});
});
});

it('returns 400 when counterparty has both business and end user', async () => {
// Arrange
const apiKey = (customer.authenticationConfiguration as { authValue: string }).authValue;
const validTransaction = getBaseTransactionData();
const transactions = [
{
...validTransaction,
originator: getBusinessCounterpartyData(),
beneficiary: {
...getBusinessCounterpartyData(),
...getEndUserCounterpartyData(),
},
},
] as const satisfies readonly TransactionCreateDto[];

// Act
const response = await request(app.getHttpServer())
.post('/external/transactions/bulk')
.send(transactions)
.set('authorization', `Bearer ${apiKey}`);

// Assert
expect(response.status).toBe(400);
expect(response.body).toEqual({
statusCode: 400,
message: 'Counterparty must have either business or end user data.',
});
});
});
15 changes: 10 additions & 5 deletions services/workflows-service/src/transaction/transaction.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { HttpException } from '@nestjs/common';

export class TransactionEntityMapper {
static toCreateData({ dto, projectId }: { dto: TransactionCreateDto; projectId: TProjectId }) {
if (!dto.originator || !dto.beneficiary) {
throw new HttpException('Originator and beneficiary are required.', 400);
}

return {
transactionCorrelationId: dto.correlationId,
transactionDate: dto.date,
Expand Down Expand Up @@ -92,16 +96,17 @@ export class TransactionEntityMapper {
counterpartyInfo,
projectId,
}: {
counterpartyInfo: CounterpartyInfo | undefined;
counterpartyInfo: CounterpartyInfo;
projectId: string;
}):
| Prisma.TransactionRecordCreateInput['counterpartyOriginator']
| Prisma.TransactionRecordCreateInput['counterpartyBeneficiary'] {
if (!counterpartyInfo?.businessData && !counterpartyInfo?.endUserData) {
throw new HttpException('Counterparty must have either business or end user data.', 400);
}

if (counterpartyInfo?.businessData && counterpartyInfo?.endUserData) {
throw new HttpException(
'Counterparty must have either business or end user data, not both.',
400,
);
throw new HttpException('Counterparty must have either business or end user data.', 400);
}

return counterpartyInfo
Expand Down
18 changes: 10 additions & 8 deletions services/workflows-service/src/transaction/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ export class TransactionService {
transactionsPayload: TransactionCreateDto[];
projectId: TProjectId;
}) {
const mappedTransactions = transactionsPayload.map(transactionPayload =>
TransactionEntityMapper.toCreateData({
dto: transactionPayload,
projectId,
}),
);

const response: Array<TransactionCreatedDto | { error: Error; correlationId: string }> = [];

for (const transactionPayload of transactionsPayload) {
for (const transactionPayload of mappedTransactions) {
try {
const transaction = await this.repository.create({
data: TransactionEntityMapper.toCreateData({
dto: transactionPayload,
projectId,
}),
});
const transaction = await this.repository.create({ data: transactionPayload });

response.push({
id: transaction.id,
Expand All @@ -50,7 +52,7 @@ export class TransactionService {

response.push({
error: errorToLog,
correlationId: transactionPayload.correlationId,
correlationId: transactionPayload.transactionCorrelationId,
});
}
}
Expand Down

0 comments on commit 9d62068

Please sign in to comment.