Merged PR 1518: #3957 Filter Current Branch if Role is Store

#3957 Filter Current Branch if Role is Store
This commit is contained in:
Nino Righi
2023-04-05 09:55:41 +00:00
committed by Lorenz Hilpert
parent 4952c090ef
commit bbe9326954
2 changed files with 43 additions and 25 deletions

View File

@@ -194,21 +194,21 @@ describe('BranchSelectorStore', () => {
branchType = 1;
});
it('should call _filterBranches({ branches: response, branchType })', () => {
const filterBranchesSpy = spyOn<any>(spectator.service, '_filterBranches');
it('should call _filter({ branches: response, branchType })', () => {
const filterBranchesSpy = spyOn<any>(spectator.service, '_filter');
spectator.service.loadBranchesResponseFn({ response: branches, branchType });
expect(filterBranchesSpy).toHaveBeenCalledWith({ branches, branchType });
});
it('should call setBranches() with branches after filtering', () => {
spyOn<any>(spectator.service, '_filterBranches').and.returnValue([{ id: 1 }]);
spyOn<any>(spectator.service, '_filter').and.returnValue([{ id: 1 }]);
const setBranchesSpy = spyOn(spectator.service, 'setBranches');
spectator.service.loadBranchesResponseFn({ response: branches, branchType });
expect(setBranchesSpy).toHaveBeenCalledWith([{ id: 1 }]);
});
it('should call setBranches() with default [] if getBranches() return undefined', () => {
spyOn<any>(spectator.service, '_filterBranches').and.returnValue([]);
spyOn<any>(spectator.service, '_filter').and.returnValue([]);
const setBranchesSpy = spyOn(spectator.service, 'setBranches');
spectator.service.loadBranchesResponseFn({ response: branches, branchType });
expect(setBranchesSpy).toHaveBeenCalledWith([]);
@@ -324,7 +324,7 @@ describe('BranchSelectorStore', () => {
});
});
describe('_filterBranches()', () => {
describe('_filter()', () => {
it('should return branches based on filter criterias', () => {
const branches = [
{
@@ -341,7 +341,7 @@ describe('BranchSelectorStore', () => {
];
const branchType: BranchType = 1;
expect(spectator.service['_filterBranches']({ branches, branchType })).toEqual([branches[1]]);
expect(spectator.service['_filter']({ branches, branchType })).toEqual([branches[1]]);
});
});
});

View File

@@ -138,8 +138,7 @@ export class BranchSelectorStore extends ComponentStore<BranchSelectorState> {
branchType?: BranchType;
currentBranch?: BranchDTO;
}) => {
const branches = this._filterBranches({ branches: response, branchType });
this.sortBranches({ branches, currentBranch });
const branches = this._filterAndOrderByBranches({ branches: response, branchType, currentBranch });
this.setBranches(branches ?? []);
if (selectedBranch) {
this.setSelectedBranch(selectedBranch);
@@ -157,20 +156,6 @@ export class BranchSelectorStore extends ComponentStore<BranchSelectorState> {
});
};
sortBranches({ branches, currentBranch }: { branches: BranchDTO[]; currentBranch: BranchDTO }): void {
if (this._auth.hasRole('CallCenter')) {
branches?.sort((branchA, branchB) => branchA?.name?.localeCompare(branchB?.name));
} else if (this._auth.hasRole('Store')) {
branches?.sort((a: BranchDTO, b: BranchDTO) =>
this._branchSorterFn(
a,
b,
branches?.find((b) => b?.id === currentBranch?.id)
)
);
}
}
setBranches(branches: BranchDTO[]) {
this.patchState({ branches });
}
@@ -225,17 +210,50 @@ export class BranchSelectorStore extends ComponentStore<BranchSelectorState> {
);
}
private _filterAndOrderByBranches({
branches,
branchType,
currentBranch,
}: {
branches: BranchDTO[];
branchType: BranchType;
currentBranch: BranchDTO;
}): BranchDTO[] {
let filteredBranches = this._filter({ branches, branchType });
filteredBranches = this._configureBranchesByRole({ filteredBranches, currentBranch });
return filteredBranches;
}
// Frontend-Seitige Filterung der Branches vom Backend (Filterkriterien wie bei PDP - Weitere Verfügbarkeiten Modal)
private _filterBranches({ branches, branchType }: { branches: BranchDTO[]; branchType: BranchType }): BranchDTO[] {
private _filter({ branches, branchType }: { branches: BranchDTO[]; branchType: BranchType }): BranchDTO[] {
return branches?.filter((branch) => {
if (!!branchType) {
return branch && branch?.isOnline && branch?.isShippingEnabled && branch?.isOrderingEnabled && branch?.branchType === branchType;
} else {
return branch && branch?.isOnline && branch?.isShippingEnabled && branch?.isOrderingEnabled;
}
return branch && branch?.isOnline && branch?.isShippingEnabled && branch?.isOrderingEnabled;
});
}
_configureBranchesByRole({ filteredBranches, currentBranch }: { filteredBranches: BranchDTO[]; currentBranch: BranchDTO }): BranchDTO[] {
if (this._auth.hasRole('CallCenter')) {
return filteredBranches?.sort((branchA, branchB) => branchA?.name?.localeCompare(branchB?.name));
} else if (this._auth.hasRole('Store')) {
filteredBranches?.sort((a: BranchDTO, b: BranchDTO) =>
this._branchSorterFn(
a,
b,
filteredBranches?.find((b) => b?.id === currentBranch?.id)
)
);
return this._filterCurrentBranch({ branches: filteredBranches, currentBranch });
}
return filteredBranches;
}
private _filterCurrentBranch({ branches, currentBranch }: { branches: BranchDTO[]; currentBranch: BranchDTO }): BranchDTO[] {
return branches.filter((branch) => branch?.id !== currentBranch?.id);
}
getBranchById(id: number): BranchDTO {
return this.branches.find((branch) => branch.id === id);
}