Add bootstrap service with getMyParty call (#328)

This commit is contained in:
Ildar Galeev 2020-11-27 11:33:13 +03:00 committed by GitHub
parent db7b9dedb5
commit c96e2b0837
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 83 additions and 53 deletions

View File

@ -3,7 +3,7 @@ import { NgModule } from '@angular/core';
import { CAPIConfigService } from './capi-config.service';
import { InvoicesService } from './invoices.service';
import { ShopsService } from './shops.service';
import { ApiModule, ClaimsService, Configuration, PayoutsService } from './swagger-codegen';
import { ApiModule, ClaimsService, Configuration, PartiesService, PayoutsService } from './swagger-codegen';
@NgModule({
imports: [
@ -12,6 +12,6 @@ import { ApiModule, ClaimsService, Configuration, PayoutsService } from './swagg
providers: [{ provide: Configuration, useClass: CAPIConfigService }],
},
],
providers: [CAPIConfigService, ClaimsService, ShopsService, InvoicesService, PayoutsService],
providers: [CAPIConfigService, ClaimsService, ShopsService, InvoicesService, PayoutsService, PartiesService],
})
export class CAPIModule {}

View File

@ -0,0 +1,14 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { PartiesService, Party } from '../../api-codegen/capi';
import { genXRequestID } from '../utils';
@Injectable()
export class CAPIPartiesService {
constructor(private partiesService: PartiesService) {}
getMyParty(): Observable<Party> {
return this.partiesService.getMyParty(genXRequestID());
}
}

View File

@ -1,8 +1,9 @@
import { NgModule } from '@angular/core';
import { CAPIClaimsService } from './capi-claims.service';
import { CAPIPartiesService } from './capi-parties.service';
@NgModule({
providers: [CAPIClaimsService],
providers: [CAPIClaimsService, CAPIPartiesService],
})
export class CAPIModule {}

View File

@ -1,4 +1,5 @@
export * from './capi.module';
export * from './capi-claims.service';
export * from './capi-parties.service';
export * from './utils';
export * from './models';

View File

@ -1,5 +1,5 @@
<dsh-container>
<dsh-sections></dsh-sections>
<dsh-sections *ngIf="bootstrapSuccessful$ | async"></dsh-sections>
</dsh-container>
<dsh-yandex-metrika *ngIf="env.production"></dsh-yandex-metrika>

View File

@ -1,17 +1,30 @@
import { Component, Inject, OnInit } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { TranslocoService } from '@ngneat/transloco';
import { filter } from 'rxjs/operators';
import { ENV, Env } from '../environments';
import { TestShopService } from './test-shop.service';
import { BootstrapService } from './bootstrap.service';
@Component({
selector: 'dsh-root',
templateUrl: 'app.component.html',
providers: [TestShopService],
providers: [BootstrapService],
})
export class AppComponent implements OnInit {
constructor(private testShopService: TestShopService, @Inject(ENV) public env: Env) {}
bootstrapSuccessful$ = this.bootstrapService.bootstrapped$.pipe(filter((r) => !!r));
constructor(
private bootstrapService: BootstrapService,
@Inject(ENV) public env: Env,
private snackBar: MatSnackBar,
private transloco: TranslocoService
) {}
ngOnInit() {
this.testShopService.createTestShopWhenNoShops();
this.bootstrapService.bootstrap();
this.bootstrapService.bootstrapped$
.pipe(filter((r) => !r))
.subscribe(() => this.snackBar.open(this.transloco.translate('errors.bootstrapAppFailed'), 'OK'));
}
}

View File

@ -0,0 +1,44 @@
import { Injectable } from '@angular/core';
import { iif, Observable, of, ReplaySubject } from 'rxjs';
import { catchError, first, map, shareReplay, switchMap, switchMapTo, tap } from 'rxjs/operators';
import { ApiShopsService, CAPIClaimsService, CAPIPartiesService, createTestShopClaimChangeset } from './api';
@Injectable()
export class BootstrapService {
bootstrapped$: Observable<boolean>;
private bootstrap$ = new ReplaySubject(1);
constructor(
private partiesService: CAPIPartiesService,
private shopService: ApiShopsService,
private capiClaimsService: CAPIClaimsService
) {
this.bootstrapped$ = this.bootstrap$.pipe(
first(),
switchMapTo(this.partiesService.getMyParty()),
switchMapTo(this.shopService.shops$.pipe(first())),
switchMap((shops) =>
iif(
() => shops.length === 0,
this.createTestShop().pipe(tap(() => this.shopService.reloadShops())),
of(true)
)
),
catchError((err) => {
console.error(err);
return of(false);
}),
shareReplay(1)
);
}
bootstrap() {
this.bootstrap$.next();
}
private createTestShop(): Observable<boolean> {
return this.capiClaimsService.createClaim(createTestShopClaimChangeset()).pipe(map(() => true));
}
}

View File

@ -1,44 +0,0 @@
import { Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { TranslocoService } from '@ngneat/transloco';
import { Subject } from 'rxjs';
import { filter, first, switchMap, switchMapTo } from 'rxjs/operators';
import { ApiShopsService, CAPIClaimsService, createTestShopClaimChangeset } from './api';
@Injectable()
export class TestShopService {
private createTestShopWhenNoShops$ = new Subject();
constructor(
private capiClaimsService: CAPIClaimsService,
private shopService: ApiShopsService,
private snackBar: MatSnackBar,
private transloco: TranslocoService
) {
this.createTestShopWhenNoShops$
.pipe(
first(),
switchMapTo(this.shopService.shops$),
first(),
filter((shops) => shops.length === 0),
switchMap(() => this.createTestShop())
)
.subscribe(
() => {
this.shopService.reloadShops();
},
() => {
this.snackBar.open(this.transloco.translate('commonError'), 'OK');
}
);
}
private createTestShop() {
return this.capiClaimsService.createClaim(createTestShopClaimChangeset());
}
createTestShopWhenNoShops(): void {
this.createTestShopWhenNoShops$.next();
}
}

View File

@ -138,7 +138,8 @@
"saveConversationsFailed": "Не удалось сохранить комментарий",
"revokeClaimByIDFailed": "Не удалось отозвать заявку",
"requestReviewClaimByIDFailed": "Не удалось отправить заявку на рассмотрение",
"updateClaimByIDFailed": " Не удалось обновить заявку"
"updateClaimByIDFailed": " Не удалось обновить заявку",
"bootstrapAppFailed": "Ошибка инициализации"
},
"yes": "Да",
"no": "Нет",