Skip to content

Commit

Permalink
Finish with generate voucher module
Browse files Browse the repository at this point in the history
  • Loading branch information
Frdrcpeter007 committed Dec 20, 2023
1 parent f24c678 commit 87431cf
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 117 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",
"pg": "^8.10.0",
"randomized-string": "^2.0.0",
"randomstring": "^1.3.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0",
"slugify": "^1.6.6",
Expand Down
1 change: 1 addition & 0 deletions src/common/constants/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export enum SenderType {
PAYER = 'PAYER',
PATIENT = 'PATIENT',
PROVIDER = 'PROVIDER',
WIIQARE_MANAGER = 'WIIQARE_MANAGER'
}

export enum ReceiverType {
Expand Down
16 changes: 8 additions & 8 deletions src/modules/patient-svc/patient-svc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class PatientSvcService {
private readonly patientRepository: Repository<Patient>,
@InjectRepository(Transaction)
private readonly transactionRepository: Repository<Transaction>,
) {}
) { }

/**
* This function is used to register a patient account
Expand All @@ -43,9 +43,9 @@ export class PatientSvcService {

const updateData = patientDto;
delete updateData.id;
await this.patientRepository.save( {...patient, ...updateData } );
await this.patientRepository.save({ ...patient, ...updateData });

Check warning on line 46 in src/modules/patient-svc/patient-svc.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/patient-svc/patient-svc.service.ts#L46

Added line #L46 was not covered by tests

return { ...patient,...updateData};
return { ...patient, ...updateData };

Check warning on line 48 in src/modules/patient-svc/patient-svc.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/patient-svc/patient-svc.service.ts#L48

Added line #L48 was not covered by tests
}

/**
Expand Down Expand Up @@ -95,18 +95,18 @@ export class PatientSvcService {
.select('patient.id', 'id')
.where('patient.added_by = :payerId', { payerId })
.getRawMany();
const addedByUserUniqueIds = addedByUser.map( result => result.id );
const addedByUserUniqueIds = addedByUser.map(result => result.id);

const uniquePatientIds = uniquePatientIdsQuery.map(
(result) => result.ownerId,
);

const combinedPatientIds = Object.values( [...uniquePatientIds, ...addedByUserUniqueIds ].reduce( ( acc, item ) => {
acc[ item ] = item;
const combinedPatientIds = Object.values([...uniquePatientIds, ...addedByUserUniqueIds].reduce((acc, item) => {
acc[item] = item;
return acc;
}, {} ));
}, {}));

console.log('combined', combinedPatientIds );
console.log('combined', combinedPatientIds);

const patients = await this.patientRepository
.createQueryBuilder('patient')
Expand Down
15 changes: 14 additions & 1 deletion src/modules/payer-svc/payer-svc.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class PayerSvcController {
private readonly cachingService: CachingService,
private readonly sessionService: SessionService,
private readonly patientService: PatientSvcService,
) {}
) { }

@Get('patient')
@Roles(UserRole.PAYER)
Expand Down Expand Up @@ -126,6 +126,19 @@ export class PayerSvcController {
return await this.patientService.registerPatient(createPatientAccountDto);
}

@Post('patient/admin')
@Roles(UserRole.WIIQARE_MANAGER)
@ApiOperation({
summary: 'This API is used register new Patient by WIIQARE MANAGER.',
})
async registerNewPatientForAdmin(

Check warning on line 134 in src/modules/payer-svc/payer-svc.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/payer-svc/payer-svc.controller.ts#L134

Added line #L134 was not covered by tests
@Body() createPatientAccountDto: CreatePatientDto,
@AuthUser() authUser: JwtClaimsDataDto,

Check warning on line 136 in src/modules/payer-svc/payer-svc.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/payer-svc/payer-svc.controller.ts#L136

Added line #L136 was not covered by tests
): Promise<PatientResponseDto> {
console.log(authUser);
return await this.patientService.registerPatient(createPatientAccountDto);

Check warning on line 139 in src/modules/payer-svc/payer-svc.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/payer-svc/payer-svc.controller.ts#L138-L139

Added lines #L138 - L139 were not covered by tests
}

@Put('patient')
@Roles(UserRole.PAYER)
@ApiOperation({
Expand Down
198 changes: 90 additions & 108 deletions src/modules/smart-contract/payment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { Voucher } from './entities/voucher.entity';
import { operationService } from '../operation-saving/operation.service';
import { OperationType } from '../operation-saving/entities/operation.entity';
import { PaymentWithoutStripe } from './dto/mint-voucher.dto';
import randomstring from 'randomstring';

Check warning on line 42 in src/modules/smart-contract/payment.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/smart-contract/payment.controller.ts#L41-L42

Added lines #L41 - L42 were not covered by tests

@ApiTags('payment')
@Controller('payment')
Expand Down Expand Up @@ -81,6 +82,8 @@ export class PaymentController {
@Body() event: Stripe.Event,
@Req() req: RawBodyRequest<Request>,
) {
console.log("test");

Check warning on line 85 in src/modules/smart-contract/payment.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/smart-contract/payment.controller.ts#L85

Added line #L85 was not covered by tests

try {
// Verify the webhook event with Stripe to ensure it is authentic
const webhookSecret = this.appConfigService.stripeWebHookSecret;
Expand Down Expand Up @@ -208,7 +211,8 @@ export class PaymentController {
}

@Post('notification/admin-pmt')
@Public()
@Roles(UserRole.WIIQARE_MANAGER)
// @Public()
@ApiOperation({
summary: 'This API receive payment notification without stripe webhook',
})
Expand All @@ -217,116 +221,94 @@ export class PaymentController {
) {
try {

Check warning on line 222 in src/modules/smart-contract/payment.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/smart-contract/payment.controller.ts#L222

Added line #L222 was not covered by tests

let status = 'payment_intent.succeeded';

// Handle the event based on its type
switch (status) {
case 'payment_intent.succeeded':

// const {
// id: stripePaymentId,
// amount: senderAmount,
// currency: senderCurrency,
// } = verifiedEvent.data.object as Stripe.PaymentIntent;

// const { metadata } = verifiedEvent.data.object as unknown as Record<
// string,
// any
// >;

//TODO: Please use our own BE exchange rate API to get the latest exchange rate!!
const {
senderId,
patientId,
currencyPatientAmount,
currencyPatient,
currencyRate,
senderAmount,
senderCurrency
} = event;

const voucherData = await this.smartContractService.mintVoucher({
amount: Math.round(currencyPatientAmount),
ownerId: patientId,
currency: currencyPatient,
patientId: patientId,
});

// console.log('vdata', voucherData.events.mintVoucherEvent.returnValues.voucherID );

const voucherJSON = {
id: _.get(voucherData, 'events.mintVoucherEvent.returnValues.0'),
amount: _.get(
voucherData,
'events.mintVoucherEvent.returnValues.1.[0]',
),
currency: _.get(
voucherData,
'events.mintVoucherEvent.returnValues.1.[1]',
),
ownerId: _.get(
voucherData,
'events.mintVoucherEvent.returnValues.1.[2]',
),
hospitalId: _.get(
voucherData,
'events.mintVoucherEvent.returnValues.1.[3]',
),
patientId: _.get(
voucherData,
'events.mintVoucherEvent.returnValues.1.[4]',
),
status: _.get(
voucherData,
'events.mintVoucherEvent.returnValues.1.[5]',
)
};

const transactionHash = _.get(
voucherData,
'events.mintVoucherEvent.transactionHash',
);

const shortenHash = transactionHash.slice(0, 8);
//TODO: Please use our own BE exchange rate API to get the latest exchange rate!!
const {
senderId,
patientId,
currencyPatientAmount,
currencyPatient,
currencyRate,
senderAmount,
senderCurrency
} = event;

Check warning on line 233 in src/modules/smart-contract/payment.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/smart-contract/payment.controller.ts#L233

Added line #L233 was not covered by tests

const voucherData = await this.smartContractService.mintVoucher({

Check warning on line 235 in src/modules/smart-contract/payment.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/smart-contract/payment.controller.ts#L235

Added line #L235 was not covered by tests
amount: Math.round(currencyPatientAmount),
ownerId: patientId,
currency: currencyPatient,
patientId: patientId,
});

// console.log('vdata', voucherData.events.mintVoucherEvent.returnValues.voucherID );

const voucherJSON = {

Check warning on line 244 in src/modules/smart-contract/payment.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/smart-contract/payment.controller.ts#L244

Added line #L244 was not covered by tests
id: _.get(voucherData, 'events.mintVoucherEvent.returnValues.0'),
amount: _.get(
voucherData,
'events.mintVoucherEvent.returnValues.1.[0]',
),
currency: _.get(
voucherData,
'events.mintVoucherEvent.returnValues.1.[1]',
),
ownerId: _.get(
voucherData,
'events.mintVoucherEvent.returnValues.1.[2]',
),
hospitalId: _.get(
voucherData,
'events.mintVoucherEvent.returnValues.1.[3]',
),
patientId: _.get(
voucherData,
'events.mintVoucherEvent.returnValues.1.[4]',
),
status: _.get(
voucherData,
'events.mintVoucherEvent.returnValues.1.[5]',
)
};

const transactionHash = _.get(

Check warning on line 272 in src/modules/smart-contract/payment.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/smart-contract/payment.controller.ts#L272

Added line #L272 was not covered by tests
voucherData,
'events.mintVoucherEvent.transactionHash',
);

const transactionToSave = this.transactionRepository.create({
senderAmount,
senderCurrency: senderCurrency.toUpperCase(),
amount: Math.round(currencyPatientAmount),
currency: currencyPatient,
conversionRate: currencyRate,
senderId,
ownerId: patientId,
stripePaymentId: "admin",
voucher: voucherJSON,
status: TransactionStatus.PENDING,
});
const savedTransaction = await this.transactionRepository.save(
transactionToSave,
);
const shortenHash = transactionHash.slice(0, 8);

Check warning on line 277 in src/modules/smart-contract/payment.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/smart-contract/payment.controller.ts#L277

Added line #L277 was not covered by tests

const transactionToSave = this.transactionRepository.create({

Check warning on line 279 in src/modules/smart-contract/payment.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/smart-contract/payment.controller.ts#L279

Added line #L279 was not covered by tests
senderAmount,
senderCurrency: senderCurrency.toUpperCase(),
amount: Math.round(currencyPatientAmount),
currency: currencyPatient,
conversionRate: currencyRate,
senderId,
ownerId: patientId,
stripePaymentId: randomstring.generate(12),
voucher: voucherJSON,
status: TransactionStatus.PENDING,
});
const savedTransaction = await this.transactionRepository.save(

Check warning on line 291 in src/modules/smart-contract/payment.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/smart-contract/payment.controller.ts#L291

Added line #L291 was not covered by tests
transactionToSave,
);

// update this
const voucherToSave = this.voucherRepository.create({
vid: voucherJSON.id,
voucherHash: transactionHash,
shortenHash: shortenHash,
value: Math.round(currencyPatientAmount),
senderId: senderId,
senderType: SenderType.PAYER,
receiverId: patientId,
receiverType: ReceiverType.PATIENT,
status: VoucherStatus.UNCLAIMED,
transaction: transactionToSave,
});
await this.voucherRepository.save(voucherToSave);
// update this
const voucherToSave = this.voucherRepository.create({

Check warning on line 296 in src/modules/smart-contract/payment.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/smart-contract/payment.controller.ts#L296

Added line #L296 was not covered by tests
vid: voucherJSON.id,
voucherHash: transactionHash,
shortenHash: shortenHash,
value: Math.round(currencyPatientAmount),
senderId: senderId,
senderType: SenderType.WIIQARE_MANAGER,
receiverId: patientId,
receiverType: ReceiverType.PATIENT,
status: VoucherStatus.UNCLAIMED,
transaction: transactionToSave,
});
await this.voucherRepository.save(voucherToSave);

Check warning on line 308 in src/modules/smart-contract/payment.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/smart-contract/payment.controller.ts#L308

Added line #L308 was not covered by tests

return savedTransaction

Check warning on line 310 in src/modules/smart-contract/payment.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/smart-contract/payment.controller.ts#L310

Added line #L310 was not covered by tests

break;
case 'payment_intent.payment_failed':
// Handle the failure in some way
break;
default:
logInfo(`Unhandled Stripe event type: `);
}
} catch (err) {
logError(`Error processing webhook event: ${err}`);
return { error: 'Failed to process payment event' };

Check warning on line 314 in src/modules/smart-contract/payment.controller.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/smart-contract/payment.controller.ts#L313-L314

Added lines #L313 - L314 were not covered by tests
Expand Down
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7590,13 +7590,30 @@ quick-lru@^5.1.1:
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-5.1.1.tgz#366493e6b3e42a3a6885e2e99d18f80fb7a8c932"
integrity sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA==

[email protected]:
version "2.0.3"
resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.0.3.tgz#674c99760901c3c4112771a31e521dc349cc09ec"
integrity sha512-lDVjxQQFoCG1jcrP06LNo2lbWp4QTShEXnhActFBwYuHprllQV6VUpwreApsYqCgD+N1mHoqJ/BI/4eV4R2GYg==

randombytes@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a"
integrity sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==
dependencies:
safe-buffer "^5.1.0"

randomized-string@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/randomized-string/-/randomized-string-2.0.0.tgz#38c54decadca38f39d77ef29283ffb8ec4a604a1"
integrity sha512-nQnPDFxRbaI2YSyV4C0tQufAvBXA9qnbisYL9z1aXXLG06BJMpYT0dlIuwyvN76hKdkto/C0L7x+4GcZDDEfDQ==

randomstring@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/randomstring/-/randomstring-1.3.0.tgz#1bf9d730066899e70aee3285573f84708278683d"
integrity sha512-gY7aQ4i1BgwZ8I1Op4YseITAyiDiajeZOPQUbIq9TPGPhUm5FX59izIaOpmKbME1nmnEiABf28d9K2VSii6BBg==
dependencies:
randombytes "2.0.3"

range-parser@~1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.1.tgz#3cf37023d199e1c24d1a55b84800c2f3e6468031"
Expand Down

0 comments on commit 87431cf

Please sign in to comment.