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

refactor: get balance via fspconfigid of registration AB#32518 #6358

Merged
merged 6 commits into from
Jan 10, 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
Original file line number Diff line number Diff line change
Expand Up @@ -563,28 +563,21 @@ export class IntersolveVoucherService
programId: number,
): Promise<number> {
const voucher = await this.getVoucher(referenceId, payment, programId);
return await this.getAndStoreBalance(voucher, programId);
const credentials =
await this.programFspConfigurationRepository.getUsernamePasswordPropertiesByVoucherId(
voucher.id,
);
return await this.getAndStoreBalance(voucher, programId, credentials);
}

private async getAndStoreBalance(
intersolveVoucher: IntersolveVoucherEntity,
programId: number,
credentials: UsernamePasswordInterface,
): Promise<number> {
const financialServiceProviderName = intersolveVoucher.whatsappPhoneNumber
? FinancialServiceProviders.intersolveVoucherWhatsapp
: FinancialServiceProviders.intersolveVoucherPaper;
const programFinancialServiceProviderConfiguration =
await this.programFspConfigurationRepository.getByProgramIdAndFinancialServiceProviderName(
{ programId, financialServiceProviderName },
);

const credentials =
await this.programFspConfigurationRepository.getUsernamePasswordProperties(
programFinancialServiceProviderConfiguration[0].id, // TODO: take the 0-th element, because the above method returns an array of entities as e.g. multiple Excel FSPs can be defined per program. For Intersolve-voucher this is not currently the case, so this is needed and works, but should be improved.
);
if (!credentials?.username || !credentials?.password) {
throw new Error(
`Could not retrieve configuration of FSP: "${financialServiceProviderName}", for program: ${programId}. Please contact the 121 platform team.`,
`Could not retrieve configuration of FSP Intersolve Voucher for program: ${programId}. Please contact the 121 platform team.`,
);
}

Expand Down Expand Up @@ -685,8 +678,12 @@ export class IntersolveVoucherService
programId,
})
.getMany();
const credentials =
await this.programFspConfigurationRepository.getUsernamePasswordPropertiesByVoucherId(
previouslyUnusedVouchers[0].id,
);
for await (const voucher of previouslyUnusedVouchers) {
await this.getAndStoreBalance(voucher, programId);
await this.getAndStoreBalance(voucher, programId, credentials);
}
id += 1000;
}
Expand Down Expand Up @@ -868,9 +865,12 @@ export class IntersolveVoucherService
});

const vouchersToUpdate = await q.getMany();

const credentials =
await this.programFspConfigurationRepository.getUsernamePasswordPropertiesByVoucherId(
vouchersToUpdate[0].id,
);
for await (const voucher of vouchersToUpdate) {
await this.getAndStoreBalance(voucher, programId);
await this.getAndStoreBalance(voucher, programId, credentials);
}
id += 1000;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,32 @@ export class ProgramFinancialServiceProviderConfigurationRepository extends Repo
return response;
}

public async getUsernamePasswordPropertiesByVoucherId(
intersolveVoucherId: number,
): Promise<UsernamePasswordInterface> {
const programFspConfig = await this.baseRepository
.createQueryBuilder('configuration')
.leftJoin('configuration.transactions', 'transactions')
.innerJoin('transactions.latestTransaction', 'latestTransaction')
.leftJoin('latestTransaction.registration', 'registration')
.leftJoin('registration.images', 'images')
.leftJoin('images.voucher', 'voucher')
.where('voucher.id = :intersolveVoucherId', {
intersolveVoucherId,
})
.andWhere('voucher.payment = transactions.payment') // TODO: REFACTOR: this filter is needed as it is not taken care of by the joins above. Better to refactor the entity relations here, probably together with whole Voucher refactor. Also look at module responsiblity then.
.select('configuration.id AS id')
.getRawOne(); // use getRawOne (+select) instead of getOne for performance reasons

if (!programFspConfig) {
throw new Error(
`ProgramFspConfig not found based onintersolveVoucherId ${intersolveVoucherId}`,
);
}

return this.getUsernamePasswordProperties(programFspConfig.id);
}

// This methods specfically does not throw as it also used to check if the property exists
public async getPropertyValueByName({
programFinancialServiceProviderConfigurationId,
Expand Down
46 changes: 46 additions & 0 deletions services/121-service/test/helpers/intersolve-voucher.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as request from 'supertest';

import { TransactionStatusEnum } from '@121-service/src/payments/transactions/enums/transaction-status.enum';
import { waitFor } from '@121-service/src/utils/waitFor.helper';
import { getTransactions } from '@121-service/test/helpers/program.helper';
import { getServer } from '@121-service/test/helpers/utility.helper';

export async function getTransactionsIntersolveVoucher(
programId: number,
payment: number,
referenceId: string,
accessToken: string,
): Promise<any[]> {
let getTransactionsBody: any[] = [];
let attempts = 0;
while (attempts <= 10) {
attempts++;
getTransactionsBody = (
await getTransactions(programId, payment, referenceId, accessToken)
).body;

if (
getTransactionsBody.length > 0 &&
getTransactionsBody[0].status === TransactionStatusEnum.success
) {
break;
}

await waitFor(2_000);
}
return getTransactionsBody;
}

export async function getVoucherBalance(
programId: number,
payment: number,
referenceId: string | null,
accessToken: string,
): Promise<request.Response> {
return await getServer()
.get(
`/programs/${programId}/financial-service-providers/intersolve-voucher/vouchers/balance`,
)
.set('Cookie', [accessToken])
.query({ payment, referenceId });
}
33 changes: 8 additions & 25 deletions services/121-service/test/payment/do-payment-fsp-voucher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import { TransactionStatusEnum } from '@121-service/src/payments/transactions/en
import { RegistrationStatusEnum } from '@121-service/src/registration/enum/registration-status.enum';
import { SeedScript } from '@121-service/src/scripts/seed-script.enum';
import { LanguageEnum } from '@121-service/src/shared/enum/language.enums';
import { waitFor } from '@121-service/src/utils/waitFor.helper';
import { adminOwnerDto } from '@121-service/test/fixtures/user-owner';
import { getTransactionsIntersolveVoucher } from '@121-service/test/helpers/intersolve-voucher.helper';
import {
doPayment,
getTransactions,
waitForMessagesToComplete,
} from '@121-service/test/helpers/program.helper';
import {
Expand Down Expand Up @@ -67,30 +66,14 @@ describe('Do payment to 1 PA', () => {
accessToken,
);

// Assert
let getTransactionsBody: any[] = [];
let attempts = 0;
while (attempts <= 10) {
attempts++;
getTransactionsBody = (
await getTransactions(
programId,
payment,
registrationAh.referenceId,
accessToken,
)
).body;

if (
getTransactionsBody.length > 0 &&
getTransactionsBody[0].status === TransactionStatusEnum.success
) {
break;
}

await waitFor(2_000);
}
const getTransactionsBody = await getTransactionsIntersolveVoucher(
programId,
payment,
registrationAh.referenceId,
accessToken,
);

// Assert
expect(doPaymentResponse.status).toBe(HttpStatus.ACCEPTED);
expect(doPaymentResponse.body.applicableCount).toBe(
paymentReferenceIds.length,
Expand Down
75 changes: 75 additions & 0 deletions services/121-service/test/voucher/get-balance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { TransactionStatusEnum } from '@121-service/src/payments/transactions/enums/transaction-status.enum';
import { RegistrationStatusEnum } from '@121-service/src/registration/enum/registration-status.enum';
import { SeedScript } from '@121-service/src/scripts/seed-script.enum';
import { waitFor } from '@121-service/src/utils/waitFor.helper';
import {
getTransactionsIntersolveVoucher,
getVoucherBalance,
} from '@121-service/test/helpers/intersolve-voucher.helper';
import { doPayment } from '@121-service/test/helpers/program.helper';
import {
awaitChangePaStatus,
importRegistrations,
} from '@121-service/test/helpers/registration.helper';
import {
getAccessToken,
resetDB,
} from '@121-service/test/helpers/utility.helper';
import {
programIdPV,
registrationPV5,
} from '@121-service/test/registrations/pagination/pagination-data';

describe('Get Intersolve voucher balance', () => {
let accessToken: string;

const payment = 1;
const amount = 22;

beforeEach(async () => {
await waitFor(1_000);
await resetDB(SeedScript.nlrcMultiple);
accessToken = await getAccessToken();
await waitFor(3_000);
});

it('should succesfully get balance', async () => {
// Arrange
await importRegistrations(programIdPV, [registrationPV5], accessToken);
await awaitChangePaStatus(
programIdPV,
[registrationPV5.referenceId],
RegistrationStatusEnum.included,
accessToken,
);
const paymentReferenceIds = [registrationPV5.referenceId];
await doPayment(
programIdPV,
payment,
amount,
paymentReferenceIds,
accessToken,
);

// make sure to wait for the transaction to be completed
const getTransactionsBody = await getTransactionsIntersolveVoucher(
programIdPV,
payment,
registrationPV5.referenceId,
accessToken,
);

// Act
const getVoucherBalanceResponse = await getVoucherBalance(
programIdPV,
payment,
registrationPV5.referenceId,
accessToken,
);

// Assert
expect(getTransactionsBody[0].status).toBe(TransactionStatusEnum.success);
expect(getVoucherBalanceResponse.status).toBe(200);
expect(getVoucherBalanceResponse.text).toBe('12.5'); // This is the number our mock gives back
});
});
Loading