fix: check if contract already exists
With this check we can avoid exceptions in the sign-up flow because we check if the contract already exists before trying to create a new one
MODIFY
src/app/components/marketplace-signup-stepper/marketplace-signup-stepper.component.html
src/app/components/marketplace-signup-stepper/marketplace-signup-stepper.component.html
<div id="center">
<mat-stepper #stepper linear>
- <mat-step
- label="{{ 'WIZARD.FIRST_STEP_LABEL' | translate }}"
- [completed]="selectedClients.size >= 1">
+ <mat-step label="{{ 'WIZARD.FIRST_STEP_LABEL' | translate }}">
<mat-card>
<mat-card-header>
<mat-card-title>{{
<button
mat-raised-button
color="primary"
- matStepperNext
- (click)="nextAfterClientSelect()">
+ (click)="nextAfterClientSelect(stepper)">
{{ 'COMMON.NEXT' | translate }}
</button>
</mat-card-actions>

MODIFY
src/app/components/marketplace-signup-stepper/marketplace-signup-stepper.component.ts
src/app/components/marketplace-signup-stepper/marketplace-signup-stepper.component.ts
@@ -20,6 +20,7 @@ import { HeroService } from '../../services/hero/hero.service';
import { TranslateService } from '@ngx-translate/core';
import {
IClientSummary,
+ IContractSummary,
INewContract,
IPolicy
} from '../../interfaces/ICommunication';
@@ -87,31 +88,56 @@ export class MarketplaceSignupStepperComponent implements OnInit {
this.agreedTermsAndPrivacy = $event;
}
- nextAfterClientSelect(): void {
+ nextAfterClientSelect(stepper: MatStepper): void {
if (this.selectedClients.size == 0) {
- this.snackbar.showErrorSnackBar(
- this.translator.instant('WIZARD.APPLICATION_ERROR') as string
+ this.printUserError('WIZARD.APPLICATION_ERROR');
+ } else {
+ this.checkIfContractAlreadyExists(stepper);
+ }
+ }
+
+ private checkIfContractAlreadyExists(stepper: MatStepper) {
+ const client: IClientSummary = this.selectedClients.values().next()
+ .value as IClientSummary;
+ this.backend
+ .getContracts(client.organizationId, client.id, '1.0')
+ .subscribe(
+ (contractSummaries: IContractSummary[]) => {
+ if (
+ contractSummaries.some((summary: IContractSummary) => {
+ return (
+ summary.apiId === this.newContractDetails.apiVersion.api.id &&
+ summary.apiOrganizationId ===
+ this.newContractDetails.apiVersion.api.organization.id &&
+ summary.apiVersion ===
+ this.newContractDetails.apiVersion.version
);
+ })
+ ) {
+ this.printUserError('WIZARD.CONTRACT_EXISTS');
+ } else {
+ stepper.next();
+ }
+ },
+ (error: HttpErrorResponse) => {
+ this.snackbar.showErrorSnackBar(error.message, error);
}
+ );
}
nextAfterTermsAgreed(): void {
- if (!this.agreedTermsAndPrivacy)
- this.snackbar.showErrorSnackBar(
- this.translator.instant('WIZARD.TERMS_ERROR') as string
- );
+ if (!this.agreedTermsAndPrivacy) {
+ this.printUserError('WIZARD.TERMS_ERROR');
+ }
}
private checkNavigationAllowed(): void {
if (
!this.newContractDetails ||
- !this.newContractDetails.organizationId ||
!this.newContractDetails.apiVersion ||
!this.newContractDetails.plan
) {
- this.snackbar.showErrorSnackBar(
- this.translator.instant('WIZARD.REDIRECT') as string
- );
+ this.printUserError('WIZARD.REDIRECT');
void this.router.navigate(['home']);
}
}
@@ -121,7 +147,7 @@ export class MarketplaceSignupStepperComponent implements OnInit {
.value as IClientSummary;
const contract: INewContract = {
- apiOrgId: this.newContractDetails.organizationId,
+ apiOrgId: this.newContractDetails.apiVersion.api.organization.id,
apiId: this.newContractDetails.apiVersion.api.id,
apiVersion: this.newContractDetails.apiVersion.version,
planId: this.newContractDetails.plan.planId
@@ -149,9 +175,7 @@ export class MarketplaceSignupStepperComponent implements OnInit {
)
.subscribe(
(contract: IContractExt) => {
- this.snackbar.showPrimarySnackBar(
- this.translator.instant('WIZARD.SUCCESS') as string
- );
+ this.printUserError('WIZARD.SUCCESS');
this.contract = contract;
if ('AwaitingApproval' === this.contract.client.status) {
void this.router.navigate(['approval']);
@@ -184,4 +208,8 @@ export class MarketplaceSignupStepperComponent implements OnInit {
fragment: this.tocService.formatClientId(this.contract)
});
}
+
+ private printUserError(key: string): void {
+ this.snackbar.showErrorSnackBar(this.translator.instant(key) as string);
+ }
}

MODIFY
src/app/components/my-apps-use-api/my-apps-use-api.component.ts
src/app/components/my-apps-use-api/my-apps-use-api.component.ts
@@ -82,7 +82,7 @@ export class MyAppsUseApiComponent implements OnInit, OnChanges {
this.endpoint = this.previewText.endpoint;
this.oAuthServerUrl = this.previewText.oAuthServerUrl;
this.oAuthClientSecret = this.previewText.oAuthClientSecret;
- this.orgId = this.newContractDetails.organizationId;
+ this.orgId = this.newContractDetails.apiVersion.api.organization.id;
this.apiId = this.newContractDetails.apiVersion.api.id;
this.apiVersion = this.newContractDetails.apiVersion.version;
this.docsAvailable = this.newContractDetails.docsAvailable;

MODIFY
src/app/interfaces/ISignUpInfo.ts
src/app/interfaces/ISignUpInfo.ts
@@ -21,6 +21,5 @@ export interface ISignUpInfo {
apiVersion: IApiVersion;
plan: IApiPlanSummary;
policies: IPolicyExt[];
- organizationId: string;
docsAvailable: boolean;
}

MODIFY
src/assets/i18n/en.json
src/assets/i18n/en.json
"API_KEY_PREVIEW": "You will be provided an API key after sign-up",
"API_ENDPOINT_PREVIEW": "You will be provided an endpoint after sign-up",
"LICENSE_TYPE": "Licence Type",
- "POLICIES": "Policies"
+ "POLICIES": "Policies",
+ "CONTRACT_EXISTS": "There is already an contract for this API with the selected application"
},
"POLICIES": {
"RateLimitingPolicy": {

fix: check if contract already exists

With this check we can avoid exceptions in the sign-up flow because we check if the contract already exists before trying to create a new one