diff --git a/src/app/api-codegen/capi/capi.module.ts b/src/app/api-codegen/capi/capi.module.ts index e8a94644..c7f9ed8e 100644 --- a/src/app/api-codegen/capi/capi.module.ts +++ b/src/app/api-codegen/capi/capi.module.ts @@ -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 {} diff --git a/src/app/api/capi/capi-parties.service.ts b/src/app/api/capi/capi-parties.service.ts new file mode 100644 index 00000000..83fc48b9 --- /dev/null +++ b/src/app/api/capi/capi-parties.service.ts @@ -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 { + return this.partiesService.getMyParty(genXRequestID()); + } +} diff --git a/src/app/api/capi/capi.module.ts b/src/app/api/capi/capi.module.ts index 80a4112b..df8811a8 100644 --- a/src/app/api/capi/capi.module.ts +++ b/src/app/api/capi/capi.module.ts @@ -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 {} diff --git a/src/app/api/capi/index.ts b/src/app/api/capi/index.ts index e01b6248..4a80a26a 100644 --- a/src/app/api/capi/index.ts +++ b/src/app/api/capi/index.ts @@ -1,4 +1,5 @@ export * from './capi.module'; export * from './capi-claims.service'; +export * from './capi-parties.service'; export * from './utils'; export * from './models'; diff --git a/src/app/app.component.html b/src/app/app.component.html index 84b9fa9c..f57a0e33 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,5 +1,5 @@ - + diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 5132d99c..6b08c1be 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -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')); } } diff --git a/src/app/bootstrap.service.ts b/src/app/bootstrap.service.ts new file mode 100644 index 00000000..92819d80 --- /dev/null +++ b/src/app/bootstrap.service.ts @@ -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; + + 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 { + return this.capiClaimsService.createClaim(createTestShopClaimChangeset()).pipe(map(() => true)); + } +} diff --git a/src/app/test-shop.service.ts b/src/app/test-shop.service.ts deleted file mode 100644 index b2e660dc..00000000 --- a/src/app/test-shop.service.ts +++ /dev/null @@ -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(); - } -} diff --git a/src/assets/i18n/ru.json b/src/assets/i18n/ru.json index 230dc187..ee9c71a6 100644 --- a/src/assets/i18n/ru.json +++ b/src/assets/i18n/ru.json @@ -138,7 +138,8 @@ "saveConversationsFailed": "Не удалось сохранить комментарий", "revokeClaimByIDFailed": "Не удалось отозвать заявку", "requestReviewClaimByIDFailed": "Не удалось отправить заявку на рассмотрение", - "updateClaimByIDFailed": " Не удалось обновить заявку" + "updateClaimByIDFailed": " Не удалось обновить заявку", + "bootstrapAppFailed": "Ошибка инициализации" }, "yes": "Да", "no": "Нет",