When working on creating or updating records, I encounter a problem with the length and cleanliness of my code. The dealTypeValues object varies based on the dealDispositionType (buyout or sale), resulting in lengthy and messy code.
Does anyone have suggestions on how to streamline the code to minimize the payload or declaration of dealTtype values object? Or perhaps a cleaner implementation method for the code below, which contains multiple payload objects. Thank you.
#html code
<ng-template #editButtons>
<div class="flex" *ngIf="isEditing">
<app-page-section-cards-btn
[btnData]="pageSectionsOptions.btnData.cancel"
(btnClickEvent)="cancelEdit()"></app-page-section-cards-btn>
<app-page-section-cards-btn
[btnData]="pageSectionsOptions.btnData.save"
(btnClickEvent)="saveDeal()">
</app-page-section-cards-btn>
</div>
</ng-template>
#ts code
saveDeal(){
if(!this.isExistingDeal){
if(this.dealDispositionFormFields.dealDispositionType === 'Buyout') {
const dealTypeValues = {
"id": 0,
"name": this.dealDispositionFormFields.dealName,
"summary": this.dealDispositionFormFields.summary,
"terminationPayment": this.dealDispositionFormFields.terminationPayment,
"effectiveDate": AppUtils.convertDateStringToYYYYMMDD(this.dealDispositionFormFields.effectiveDate),
"totalBrokerCommission": this.dealDispositionFormFields.totalBrokerCommission,
"dealId:": 0,
}
const payload = {
"id": 0,
"name": this.dealDispositionFormFields.dealName,
"dealType": "Idle Buyout",
"annualRentProposed": null,
"annualRentCurrent": null,
"firmTermRemaining": null,
"firmTermAdded": null,
"maxAvailableTerm": null,
"status": null,
"capitalContribution": null,
"parentCloneId": null,
"accountId": this.currentAccount.accountId,
"transactionId": this.transactionData.id,
"dealTypeValues": JSON.stringify(dealTypeValues)
}
this.createDispositionDeal(payload);
}else if(this.dealDispositionFormFields.dealDispositionType === 'Sale') {
const dealTypeValues = {
"id": 0,
"name": this.dealDispositionFormFields.dealName,
&quo...
}
}
createDispositionDeal(payload:any) {
this._dealService.createDeal(payload)
.subscribe(
res=>{
this._notificationService.showSuccess('Deal was successfully created.');
this.gotoManageDealsEvent.emit('');
},
err=>{
console.log('Error creating deal')
}
)
}
updateDispositionDeal(payload:any) {
this._dealService.updateDeal(payload)
.subscribe(
res=>{
this._notificationService.showSuccess('Deal was successfully updated.');
this.gotoManageDealsEvent.emit('');
},
err=>{
console.log('Error creating deal')
}
)
}
}