Skip to content

Commit

Permalink
Fix duplicate columns in excel reconciliation service (#6523)
Browse files Browse the repository at this point in the history
Co-authored-by: Ruben <vandervalk@geoit.nl>
  • Loading branch information
RubenGeo and Ruben authored Feb 20, 2025
1 parent f0824b4 commit 13fc275
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions services/121-service/src/metrics/metrics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@ export class MetricsService {
maxPaymentId: number,
registrationDataOptions: RegistrationDataOptions[],
): Promise<unknown[]> {
// TODO: This should use the latestTransactionEntity instead of the custom query here to decide what is the lastest transaction
const latestTransactionPerPa = await this.transactionScopedRepository
.createQueryBuilder('transaction')
.select('transaction.registrationId', 'registrationId')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ export class ExcelRecociliationService {
const matchColumn = await this.excelService.getImportMatchColumn(
fspConfig.id,
);
this.validateNoDuplicateValuesInMatchColumn({
importRecords: validatedExcelImport,
matchColumn,
});
const importResultForFspConfig = await this.reconciliatePayments({
programId,
payment,
Expand Down Expand Up @@ -390,6 +394,22 @@ export class ExcelRecociliationService {
return resultFeedback;
}

// Duplicate values are not allowed in the match column because it would be ambiguous which registration to match with and create a transaction for
private validateNoDuplicateValuesInMatchColumn({
importRecords,
matchColumn,
}: {
importRecords: object[];
matchColumn: string;
}): void {
const matchColumnValues = importRecords.map((r) => r[matchColumn]);
const uniqueMatchColumnValues = new Set(matchColumnValues);
if (uniqueMatchColumnValues.size !== matchColumnValues.length) {
const errors = `The match column '${matchColumn}' contains duplicate values.`;
throw new HttpException({ errors }, HttpStatus.BAD_REQUEST);
}
}

private createTransactionResult(
registrationWithAmount: ExcelFspInstructions,
importResponseRecord: any,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ exports[`Do payment with Excel FSP Import FSP reconciliation data Should give an
}
`;

exports[`Do payment with Excel FSP Import FSP reconciliation data Should give an error when there are duplicate values in the toMatch column 1`] = `
{
"errors": "The match column 'phoneNumber' contains duplicate values.",
}
`;

exports[`Do payment with Excel FSP Import FSP reconciliation data should give me a CSV template when I request it 1`] = `
[
{
Expand Down
27 changes: 27 additions & 0 deletions services/121-service/test/payment/do-payment-fsp-excel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,33 @@ describe('Do payment with Excel FSP', () => {
}
});

it(`Should give an error when there are duplicate values in the toMatch column`, async () => {
// Arrange
const matchColumn = FinancialServiceProviderAttributes.phoneNumber;
const reconciliationData = [
{
[matchColumn]: registrationWesteros1.phoneNumber,
status: TransactionStatusEnum.success,
},
{
[matchColumn]: registrationWesteros1.phoneNumber,
status: TransactionStatusEnum.error,
},
];

// Act
const importResult = await importFspReconciliationData(
programIdWesteros,
paymentNr,
accessToken,
reconciliationData,
);

// Assert
expect(importResult.statusCode).toBe(HttpStatus.BAD_REQUEST);
expect(importResult.body).toMatchSnapshot();
});

it('should give me a CSV template when I request it', async () => {
const response =
await getImportFspReconciliationTemplate(programIdWesteros);
Expand Down

0 comments on commit 13fc275

Please sign in to comment.