TD-932: Fix shops routing rules link from global shops search (#378)

This commit is contained in:
Rinat Arsaev 2024-07-24 16:46:46 +05:00 committed by GitHub
parent 44820f94e3
commit 335100b3ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 129 additions and 68 deletions

View File

@ -6,7 +6,8 @@ import {
RoutingDelegate,
RoutingRulesObject,
} from '@vality/domain-proto/domain';
import { combineLatest, Observable } from 'rxjs';
import isNil from 'lodash-es/isNil';
import { combineLatest, Observable, of } from 'rxjs';
import { map, startWith, switchMap } from 'rxjs/operators';
import { DomainStoreService } from '@cc/app/api/domain-config';
@ -15,6 +16,12 @@ import { RoutingRulesService } from '../services/routing-rules';
import { RoutingRulesType } from '../types/routing-rules-type';
import { getPoliciesIdByType } from '../utils/get-policies-id-by-type';
export interface DelegateWithPaymentInstitution {
partyDelegate: RoutingDelegate;
paymentInstitution: PaymentInstitutionObject;
mainRoutingRule: RoutingRulesObject;
}
@Injectable()
export class PartyDelegateRulesetsService {
private partyID$ = this.route.params.pipe(
@ -32,10 +39,13 @@ export class PartyDelegateRulesetsService {
private routingRulesService: RoutingRulesService,
) {}
getDelegatesWithPaymentInstitution(type?: RoutingRulesType) {
getDelegatesWithPaymentInstitution(
type?: RoutingRulesType,
partyId?: PartyID,
): Observable<DelegateWithPaymentInstitution[]> {
return combineLatest([
this.getPaymentInstitutionsWithRoutingRule(type),
this.partyID$,
isNil(partyId) ? this.partyID$ : of(partyId),
]).pipe(
map(([paymentInstitutionsWithRoutingRule, partyID]) =>
paymentInstitutionsWithRoutingRule

View File

@ -1,7 +1,7 @@
<v-table
[(sort)]="sort"
[columns]="columns$ | async"
[data]="shops"
[data]="shops()"
[externalFilter]="filterChange.observed"
[progress]="progress"
[size]="100"

View File

@ -1,9 +1,18 @@
import { CommonModule } from '@angular/common';
import { Component, Output, EventEmitter, Input, booleanAttribute, OnChanges } from '@angular/core';
import {
Component,
Output,
EventEmitter,
Input,
booleanAttribute,
OnChanges,
input,
} from '@angular/core';
import { toObservable } from '@angular/core/rxjs-interop';
import { MatCardModule } from '@angular/material/card';
import { Sort } from '@angular/material/sort';
import { Router } from '@angular/router';
import { Shop, Party } from '@vality/domain-proto/domain';
import { Shop, Party, PartyID, RoutingRulesetRef } from '@vality/domain-proto/domain';
import {
InputFieldModule,
TableModule,
@ -16,14 +25,18 @@ import {
ComponentChanges,
} from '@vality/ng-core';
import { getUnionKey } from '@vality/ng-thrift';
import isNil from 'lodash-es/isNil';
import startCase from 'lodash-es/startCase';
import { map, switchMap, Subject, defer, combineLatest } from 'rxjs';
import { map, switchMap, Subject, defer, combineLatest, of } from 'rxjs';
import { filter, shareReplay, startWith, take, first } from 'rxjs/operators';
import { MemoizeExpiring } from 'typescript-memoize';
import { DomainStoreService } from '../../../api/domain-config';
import { PartyManagementService } from '../../../api/payment-processing';
import { PartyDelegateRulesetsService } from '../../../sections/routing-rules/party-delegate-rulesets';
import {
PartyDelegateRulesetsService,
DelegateWithPaymentInstitution,
} from '../../../sections/routing-rules/party-delegate-rulesets';
import { RoutingRulesType } from '../../../sections/routing-rules/types/routing-rules-type';
import { ShopCardComponent } from '../shop-card/shop-card.component';
import { ShopContractCardComponent } from '../shop-contract-card/shop-contract-card.component';
@ -37,7 +50,7 @@ import {
export interface ShopParty {
shop: Shop;
party: {
id: Party['id'];
id: PartyID;
email: Party['contact_info']['registration_email'];
};
}
@ -56,7 +69,7 @@ export interface ShopParty {
providers: [PartyDelegateRulesetsService],
})
export class ShopsTableComponent implements OnChanges {
@Input() shops!: ShopParty[];
shops = input<ShopParty[]>([]);
@Input({ transform: booleanAttribute }) changed!: boolean;
@Input() progress: number | boolean = false;
@Output() update = new EventEmitter<void>();
@ -66,12 +79,36 @@ export class ShopsTableComponent implements OnChanges {
@Input({ transform: booleanAttribute }) noPartyColumn: boolean = false;
columns$ = combineLatest([
this.partyDelegateRulesetsService
.getDelegatesWithPaymentInstitution(RoutingRulesType.Payment)
.pipe(startWith([])),
toObservable(this.shops).pipe(
startWith(null),
map((shops) =>
shops?.length ? Array.from(new Set(shops.map((s) => s.party.id))) : [],
),
switchMap((parties) =>
parties?.length
? combineLatest(
parties.map((id) =>
this.partyDelegateRulesetsService.getDelegatesWithPaymentInstitution(
RoutingRulesType.Payment,
id,
),
),
).pipe(map((rules) => new Map(rules.map((r, idx) => [parties[idx], r]))))
: of(new Map<string, DelegateWithPaymentInstitution[]>()),
),
map((delegatesWithPaymentInstitutionByParty) => ({
delegatesWithPaymentInstitutionByParty,
rulesetIds: Array.from(
Array.from(delegatesWithPaymentInstitutionByParty.values()).reduce((acc, d) => {
d?.map((v) => v?.partyDelegate?.ruleset?.id).forEach((v) => acc.add(v));
return acc;
}, new Set<number>([])),
),
})),
),
defer(() => this.updateColumns$).pipe(startWith(null)),
]).pipe(
map(([delegatesWithPaymentInstitution]): Column<ShopParty>[] => [
map(([delegatesByParty]): Column<ShopParty>[] => [
{
field: 'shop.id',
sortable: !this.noSort,
@ -157,60 +194,25 @@ export class ShopsTableComponent implements OnChanges {
},
},
},
createOperationColumn([
...delegatesWithPaymentInstitution.map((rule) => ({
label:
'Routing rules' +
(delegatesWithPaymentInstitution.length > 1
? ` #${rule.partyDelegate?.ruleset?.id}`
: ''),
click: ({ shop, party }) => {
this.domainStoreService
.getObjects('routing_rules')
.pipe(
take(1),
map((rules) =>
rules.find((r) => r.ref.id === rule.partyDelegate.ruleset.id),
),
)
.subscribe((ruleset) => {
const delegates =
ruleset.data?.decisions?.delegates?.filter?.(
(delegate) =>
delegate?.allowed?.condition?.party?.id === party.id &&
delegate?.allowed?.condition?.party?.definition
?.shop_is === shop.id,
) || [];
const paymentRulesCommands = [
'/party',
party.id,
'routing-rules',
'payment',
rule.partyDelegate?.ruleset?.id,
];
if (delegates.length === 1) {
void this.router.navigate([
...paymentRulesCommands,
'delegate',
delegates[0].ruleset.id,
]);
return;
}
this.log.success(
delegates.length === 0
? 'No routing rules'
: `${delegates.length} routing rules`,
);
void this.router.navigate(paymentRulesCommands, {
queryParams: {
routingRulesList: JSON.stringify({
filter: shop.id,
exact: true,
}),
},
});
});
},
createOperationColumn<ShopParty>([
...delegatesByParty.rulesetIds.map((id) => ({
label: `Routing rules #${id}`,
click: ({ shop, party }) =>
this.openRoutingRules(
delegatesByParty.delegatesWithPaymentInstitutionByParty
.get(party.id)
.find((d) => d?.partyDelegate?.ruleset?.id === id)?.partyDelegate
?.ruleset?.id,
shop.id,
party.id,
),
disabled: ({ party }) =>
isNil(
delegatesByParty.delegatesWithPaymentInstitutionByParty
.get(party.id)
.find((d) => d?.partyDelegate?.ruleset?.id === id)?.partyDelegate
?.ruleset?.id,
),
})),
{
label: ({ shop }) =>
@ -318,4 +320,53 @@ export class ShopsTableComponent implements OnChanges {
shareReplay({ refCount: true, bufferSize: 1 }),
);
}
private openRoutingRules(
partyDelegateRulesetId: RoutingRulesetRef['id'],
shopId: string,
partyId: string,
) {
this.domainStoreService
.getObjects('routing_rules')
.pipe(
take(1),
map((rules) => rules.find((r) => r.ref.id === partyDelegateRulesetId)),
)
.subscribe((ruleset) => {
const delegates =
ruleset.data?.decisions?.delegates?.filter?.(
(delegate) =>
delegate?.allowed?.condition?.party?.id === partyId &&
delegate?.allowed?.condition?.party?.definition?.shop_is === shopId,
) || [];
const paymentRulesCommands = [
'/party',
partyId,
'routing-rules',
'payment',
partyDelegateRulesetId,
];
if (delegates.length === 1) {
void this.router.navigate([
...paymentRulesCommands,
'delegate',
delegates[0].ruleset.id,
]);
return;
}
this.log.success(
delegates.length === 0
? 'No routing rules'
: `${delegates.length} routing rules`,
);
void this.router.navigate(paymentRulesCommands, {
queryParams: {
routingRulesList: JSON.stringify({
filter: shopId,
exact: true,
}),
},
});
});
}
}