Skip to content

Commit

Permalink
chore: small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
illfixit committed Jun 18, 2024
1 parent 2383ace commit bf1a9ed
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions src/app/core/services/contract-negotiation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,43 @@ export class ContractNegotiationService {
.afterClosed()
.subscribe((result) => {
if (result) {
this.initiateNegotiation(initiateRequest)
.pipe(
tap(() => this.onStarted(contractOfferId)),
switchMap((negotiation) =>
interval(1000).pipe(
switchMap(() =>
this.fetchNegotiation(
negotiation.contractNegotiationId,
).pipe(catchError(() => EMPTY)),
),
),
),
filter(
(negotiation) =>
negotiation.state.simplifiedState != 'IN_PROGRESS',
),
first(),
)
.subscribe({
next: (negotiation) => {
if (negotiation.state.simplifiedState === 'AGREED') {
this.onSuccess(contractOfferId);
} else {
this.onFailureNegotiating(contractOfferId);
}
},
error: () => this.onFailureStarting(),
});
this.startNegotiation(initiateRequest);
}
});
}

private startNegotiation(initiateRequest: ContractNegotiationRequest) {
const contractOfferId = initiateRequest.contractOfferId;

this.initiateNegotiation(initiateRequest)
.pipe(
tap(() => this.onStarted(contractOfferId)),
switchMap((negotiation) =>
interval(1000).pipe(
switchMap(() =>
this.fetchNegotiation(negotiation.contractNegotiationId).pipe(
catchError(() => EMPTY),
),
),
),
),
filter(
(negotiation) => negotiation.state.simplifiedState != 'IN_PROGRESS',
),
first(),
)
.subscribe({
next: (negotiation) => {
if (negotiation.state.simplifiedState === 'AGREED') {
this.onSuccess(contractOfferId);
} else {
this.onFailureNegotiating(contractOfferId);
}
},
error: () => this.onFailureStarting(),
});
}

private onFailureStarting() {
this.notificationService.showError('Failure starting negotiation.');
}
Expand Down

0 comments on commit bf1a9ed

Please sign in to comment.