Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate columns in excel reconciliation service #6523

Merged
merged 6 commits into from
Feb 20, 2025
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
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
Loading