Skip to content

Instantly share code, notes, and snippets.

@oops-wrong
Created January 1, 2019 07:30
Show Gist options
  • Save oops-wrong/325079368ee5298386c9caf0201eda20 to your computer and use it in GitHub Desktop.
Save oops-wrong/325079368ee5298386c9caf0201eda20 to your computer and use it in GitHub Desktop.
updateDispatcher
import { Injectable } from '@angular/core';
import { select, Store } from '@ngrx/store';
import { Observable, throwError } from 'rxjs';
import { catchError, filter, map, take } from 'rxjs/operators';
import { CroppingAPIService } from '~/core/cropping/cropping-api.service';
import { DeadcartManagerService } from '~/core/deadcart/manager/deadcart-manager.service';
import { MatcherAPIService } from '~/core/matcher/matcher-api.service';
import { ScapeLambAPIService } from '~/core/scape-lamb/scape-lamb-api.service';
import { selectDispatcherEntities, State as CoreState } from '~/core/store/reducers';
import {
ResetDispatcherEntities,
SetDispatcherEntities,
} from '~/core/store/workload-priorities/workload-priorities.actions';
import { BulkCompaniesInfo, DeadcartCompanyInfo } from '~/shared/models/deadcart.model';
import {
DispatcherEntity,
WorkEntity,
ListType,
WorkEntitiesApiService,
WorkEntitiesResponse,
} from '~/shared/models/work-dispatcher.model';
import {
ResetListType,
ShowWorkDispatcherMessage,
} from '../store/dispatcher-workspace/dispatcher-workspace.actions';
import { selectListType, State } from '../store/reducers';
import { AppState } from '~/store/reducers';
@Injectable()
export class WorkDispatcherManagerService {
private readonly dispatcherListTypes = [ListType.Matcher, ListType.Cropping];
private readonly workloadListTypes = [ListType.Expedite, ListType.P1, ListType.P2, ListType.P3];
constructor(
private croppingApiService: CroppingAPIService,
private deadcartManagerService: DeadcartManagerService,
private matcherApiService: MatcherAPIService,
private scapeLambApiSvc: ScapeLambAPIService,
private store: Store<AppState & CoreState & State>,
) {
this.watchForListType();
}
public watchForListType() {
this.store
.pipe(
select(selectListType),
filter(listType => Boolean(listType)),
)
.subscribe((listType: ListType) => {
switch (true) {
case this.isDispatcherListType(listType):
this.notifyEntityLevelListChange(listType);
break;
case this.isWorkloadListType(listType):
break;
default:
this.onWorkloadsGettingError(`No such work dispatcher service '${listType}'`);
}
});
}
public onUpdate(): void {
this.store
.pipe(
select(selectListType),
take(1),
)
.subscribe((listType: ListType) => {
switch (true) {
case this.isDispatcherListType(listType):
this.updateDispatcher(listType);
break;
case this.isWorkloadListType(listType):
break;
default:
this.showMessage('ERROR', `Unknown queue update request '${listType}'`);
}
});
}
private isDispatcherListType(listType: ListType): boolean {
return this.dispatcherListTypes.indexOf(listType) !== -1;
}
private isWorkloadListType(listType: ListType): boolean {
return this.workloadListTypes.indexOf(listType) !== -1;
}
private updateDispatcher(listType: ListType): void {
this.store
.pipe(
select(selectDispatcherEntities),
take(1),
)
.subscribe(dispatcherEntities => {
this.notifyUpdateResult(
this.getEntityService(listType)
.updateWorkPriority(dispatcherEntities.map(entity => entity.entityId))
.pipe(catchError(() => throwError('Failed to update service api'))),
listType,
);
});
}
private getEntityService(listType: ListType): WorkEntitiesApiService {
switch (listType) {
case ListType.Cropping:
return this.croppingApiService;
case ListType.Matcher:
return this.matcherApiService;
default:
const message = `No such entity service '${listType}'`;
this.onWorkloadsGettingError(message);
throw new TypeError(message);
}
}
private onWorkloadsGettingError(error: string): void {
this.showMessage('ERROR', error);
}
private notifyEntityLevelListChange(listType: ListType): void {
this.getEntityService(listType)
.getWorkingEntities()
.pipe(map((response: WorkEntitiesResponse) => response.entities))
.subscribe(
// todo change to store service select
(entities: WorkEntity[]) => {
const {
companiesIds,
entitiesMap,
}: {
companiesIds: string[];
entitiesMap: { [companyId: string]: WorkEntity };
} = entities.reduce(
(accumulator, companyItem: WorkEntity) => {
if (accumulator.companiesIds.includes(companyItem.companyId)) {
return accumulator;
}
return {
companiesIds: [...accumulator.companiesIds, companyItem.companyId],
entitiesMap: { ...accumulator.entitiesMap, [companyItem.companyId]: companyItem },
};
},
{ companiesIds: [], entitiesMap: {} },
);
this.deadcartManagerService
.getBulkCompanyInfo(companiesIds)
.subscribe((companiesInfo: BulkCompaniesInfo) => {
// todo change to select
if (companiesInfo.failures.length > 0) {
console.error('Some Data was incorrect');
}
const companiesInfoMap: {
[id: string]: DeadcartCompanyInfo;
} = companiesInfo.companiesInfo.reduce(
(accumulator, companyInfo: DeadcartCompanyInfo) => ({
...accumulator,
[companyInfo.companyId]: companyInfo,
}),
{},
);
const dispatcherEntities: DispatcherEntity[] = entities
.filter(({ companyId }: WorkEntity) => Boolean(companiesInfoMap[companyId]))
.map(({ companyId }: WorkEntity) => ({
companyName: companiesInfoMap[companyId].name,
entityId: companiesInfoMap[companyId].companyId,
inProgress: entitiesMap[companyId].inProgress,
accountType: companiesInfoMap[companyId].accountType,
isCompanyActive: companiesInfoMap[companyId].active,
companyType: companiesInfoMap[companyId].companyType,
isCompanyDomestic: companiesInfoMap[companyId].domestic,
stage: companiesInfoMap[companyId].stage,
companyCountry: companiesInfoMap[companyId].country,
}));
this.store.dispatch(new SetDispatcherEntities({ dispatcherEntities }));
});
},
error => {
this.showMessage('ERROR', error);
},
);
}
private notifyUpdateResult(obs: Observable<any>, listType: ListType): void {
obs.subscribe(
() => {
this.showMessage(`SUCCESS`, `${ListType[listType]} priorities changed successfully!`);
},
() => {
this.showMessage(`FAILURE`, `${ListType[listType]} priorities failed to update!`);
},
() => {
if (this.isWorkloadListType(listType)) {
} else {
this.store.dispatch(new ResetDispatcherEntities());
}
this.store.dispatch(new ResetListType());
},
);
}
private showMessage(title: string, content: string): void {
this.store.dispatch(new ShowWorkDispatcherMessage({ title, content }));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment