Skip to content

Commit

Permalink
feat: ORV2-2070 - Bulk Issuance - Iteration 1 (#1318)
Browse files Browse the repository at this point in the history
  • Loading branch information
praju-aot authored Apr 8, 2024
1 parent d72c1df commit 4a1f0dc
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 108 deletions.
59 changes: 33 additions & 26 deletions vehicles/src/modules/common/dops.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export class DopsService {
currentUser: IUserJWT,
dopsGeneratedDocument: DopsGeneratedDocument,
companyId?: number,
ignoreErrors?: boolean,
): Promise<ReadFileDto> {
// Construct the URL for the request
const url = process.env.DOPS_URL + `/dgen/template/render`;
Expand All @@ -126,14 +127,15 @@ export class DopsService {
responseType: 'json',
};

// Calls the DOPS service, which converts the the template document into a pdf
const dopsResponse = await lastValueFrom(
this.httpService.post(url, dopsGeneratedDocument, reqConfig),
)
.then((response) => {
return response;
})
.catch((error: AxiosError) => {
try {
// Calls the DOPS service, which converts the the template document into a pdf
const dopsResponse = await lastValueFrom(
this.httpService.post(url, dopsGeneratedDocument, reqConfig),
);
return dopsResponse.data as ReadFileDto;
} catch (error: unknown) {
if (error instanceof AxiosError) {
// Log and throw error if the request fails
if (error.response) {
const errorData = error.response.data as ExceptionDto;
this.logger.error(
Expand All @@ -142,12 +144,14 @@ export class DopsService {
} else {
this.logger.error(error?.message, error?.stack);
}
}
// Handle other types of errors
if (!ignoreErrors) {
throw new InternalServerErrorException(
'Error generating document via CDOGS',
);
});

return dopsResponse.data as ReadFileDto;
}
}
}

/**
Expand Down Expand Up @@ -252,16 +256,18 @@ export class DopsService {
}

/**
* Sends an notification with documents fetched from S3 using DOPS service.
* Sends a notification with documents using DOPS service, allowing optional error ignoring.
* @param currentUser - The current authenticated user's JWT details.
* @param notificationWithDocuments - The details of the notification and documents to be sent.
* @param ignoreErrors - Optional parameter to determine whether to ignore errors.
* @returns A Promise resolving to the response from DOPS service containing
* the message and the transaction ID of the notification sent.
*/
@LogAsyncMethodExecution()
async notificationWithDocumentsFromDops(
currentUser: IUserJWT,
notificationWithDocuments: INotificationDocument,
ignoreErrors?: boolean,
) {
// Construct the request URL by appending endpoint to the DOPS base URL
const url = process.env.DOPS_URL + `/notification/document`;
Expand All @@ -276,29 +282,30 @@ export class DopsService {
responseType: 'json', // Expecting a JSON response
};

// Send POST request to the DOPS service and handle the response or error
const dopsResponse = await lastValueFrom(
this.httpService.post(url, notificationWithDocuments, reqConfig),
)
.then((response) => {
// Return the Axios response directly on success
return response;
})
.catch((error: AxiosError) => {
try {
// Send POST request to the DOPS service and handle the response or error
const dopsResponse = await lastValueFrom(
this.httpService.post(url, notificationWithDocuments, reqConfig),
);
return dopsResponse.data as ReadNotificationDto;
} catch (error: unknown) {
if (error instanceof AxiosError) {
// Log and throw error if the request fails
if (error.response) {
const errorData = error.response.data as ExceptionDto;
this.logger.error(
`Error response from DOPS: ${error.response.status} ${error.response.statusText} `,
`Error response from DOPS: ${JSON.stringify(errorData, null, 2)}`,
);
} else {
this.logger.error(error?.message, error?.stack);
}
}
// Handle other types of errors
if (!ignoreErrors) {
throw new InternalServerErrorException(
'Error while sending notification',
);
});

// Return the response data after casting it to the expected type
return dopsResponse.data as ReadNotificationDto;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ import {
} from '../../../common/helper/permit-application.helper';
import { INotificationDocument } from '../../../common/interface/notification-document.interface';
import { PaymentService } from '../payment/payment.service';
import { CacheKey } from '../../../common/enum/cache-key.enum';
import { getFromCache } from '../../../common/helper/cache.helper';

@Injectable()
export class ApplicationService {
Expand Down Expand Up @@ -893,8 +895,9 @@ export class ApplicationService {
try {
const permit = permits?.at(0);
const company = permit?.company;
const receipt =
permit?.permitTransactions?.at(0)?.transaction?.receipt;
const permitTransactions = permit?.permitTransactions;
const transaction = permitTransactions?.at(0)?.transaction;
const receipt = transaction?.receipt;
if (receipt.receiptDocumentId) {
throw new HttpException('Document already exists', 409);
}
Expand All @@ -906,32 +909,33 @@ export class ApplicationService {
permit,
);

const permitDataForTemplate = formatTemplateData(
permit,
fullNames,
company,
const { companyName, companyAlternateName, permitData } =
formatTemplateData(permit, fullNames, company);
const permitDetails = await Promise.all(
permits?.map(async (permit) => {
return {
permitName: await getFromCache(
this.cacheManager,
CacheKey.PERMIT_TYPE,
permit?.permitType,
),
permitNumber: permit?.permitNumber,
transactionAmount: formatAmount(
transaction?.transactionTypeId,
permit?.permitTransactions?.at(0)?.transactionAmount,
),
};
}),
);

const dopsRequestData = {
templateName: TemplateName.PAYMENT_RECEIPT,
generatedDocumentFileName: `Receipt_No_${receiptNumber}`,
templateData: {
...permitDataForTemplate,
// transaction details still needs to be reworked to support multiple permits
pgTransactionId:
permit?.permitTransactions[0].transaction.pgTransactionId,
transactionOrderNumber:
permit?.permitTransactions[0].transaction
.transactionOrderNumber,
transactionAmount: formatAmount(
permit?.permitTransactions[0].transaction.transactionTypeId,
permit?.permitTransactions[0].transactionAmount,
),
totalTransactionAmount: formatAmount(
permit?.permitTransactions[0].transaction.transactionTypeId,
permit?.permitTransactions[0].transaction
.totalTransactionAmount,
),
receiptNo: receiptNumber,
companyName: companyName,
companyAlternateName: companyAlternateName,
permitData: permitData,
//Payer Name should be persisted in transacation Table so that it can be used for DocRegen
payerName:
currentUser.orbcUserDirectory === Directory.IDIR
Expand All @@ -943,21 +947,26 @@ export class ApplicationService {
currentUser.orbcUserDirectory === Directory.IDIR
? constants.PPC_FULL_TEXT
: constants.SELF_ISSUED,
totalTransactionAmount: formatAmount(
transaction?.transactionTypeId,
transaction?.totalTransactionAmount,
),
permitDetails: permitDetails,
//Transaction Details
pgTransactionId: transaction?.pgTransactionId,
transactionOrderNumber: transaction?.transactionOrderNumber,
consolidatedPaymentMethod: (
await getPaymentCodeFromCache(
this.cacheManager,
permit?.permitTransactions[0].transaction
.paymentMethodTypeCode,
permit?.permitTransactions[0].transaction
.paymentCardTypeCode,
transaction?.paymentMethodTypeCode,
transaction?.paymentCardTypeCode,
)
).consolidatedPaymentMethod,
transactionDate: convertUtcToPt(
permit?.permitTransactions[0].transaction
.transactionSubmitDate,
permit?.permitTransactions?.at(0)?.transaction
?.transactionSubmitDate,
'MMM. D, YYYY, hh:mm a Z',
),
receiptNo: receiptNumber,
},
};

Expand All @@ -975,9 +984,8 @@ export class ApplicationService {

try {
const emailList = [
permitDataForTemplate.permitData?.contactDetails?.email,
permitDataForTemplate.permitData?.contactDetails
?.additionalEmail,
permitData?.contactDetails?.email,
permitData?.contactDetails?.additionalEmail,
company?.email,
];
const subject = `onRouteBC Permit Receipt - ${receiptNumber}`;
Expand Down Expand Up @@ -1045,6 +1053,7 @@ export class ApplicationService {
void this.dopsService.notificationWithDocumentsFromDops(
currentUser,
notificationDocument,
true,
);
}

Expand Down
Loading

0 comments on commit 4a1f0dc

Please sign in to comment.