AW-3441 fix package validation
All checks were successful
FarmMaps/FarmMapsLib/pipeline/head This commit looks good

This commit is contained in:
Mark van der Wal 2022-01-27 17:07:24 +01:00
parent b7c80dfdd8
commit 519b81d1fd
4 changed files with 60 additions and 60 deletions

View File

@ -1,6 +1,6 @@
{
"name": "farmmaps-lib-app",
"version": "1.1.7",
"version": "1.1.8",
"scripts": {
"ng": "ng",
"start": "ng serve",

View File

@ -1,61 +1,53 @@
import { Directive, ViewContainerRef,TemplateRef,OnInit,Input,OnDestroy } from '@angular/core';
import { Store} from '@ngrx/store';
import * as appCommonReducer from '../../reducers/app-common.reducer'
import { IPackages } from '../../models/package';
import { Observable, Subscription } from 'rxjs';
import {Directive, Input, OnDestroy, TemplateRef, ViewContainerRef} from '@angular/core';
import {Subscription} from 'rxjs';
import {PackageService} from '../../services/package.service';
@Directive({
selector: '[fmHasPackage]',
})
export class HasPackageDirective implements OnDestroy{
export class HasPackageDirective implements OnDestroy {
@Input()
set fmHasPackage(packageIdentifier:string) {
this.packageIdentifier$ = packageIdentifier;
this.updateView();
set fmHasPackage(packageIdentifier: string) {
this.packageIdentifier = packageIdentifier;
this.updateView();
}
@Input()
set fmHasPackageThen(thenTemplate:TemplateRef<any>) {
this.thenTemplate$ = thenTemplate;
this.updateView();
set fmHasPackageThen(thenTemplate: TemplateRef<any>) {
this.thenTemplate = thenTemplate;
this.updateView();
}
@Input()
set fmHasPackageElse(thenTemplate:TemplateRef<any>) {
this.elseTemplate$ = thenTemplate;
this.updateView();
set fmHasPackageElse(thenTemplate: TemplateRef<any>) {
this.elseTemplate = thenTemplate;
this.updateView();
}
private packageIdentifier$:string;
private thenTemplate$:TemplateRef<any>;
private elseTemplate$:TemplateRef<any>;
private packages$:any = {};
private packagesObservable$:Observable<IPackages> = this.store$.select(appCommonReducer.SelectGetUserPackages);
private packSub:Subscription;
private packageIdentifier: string;
private thenTemplate: TemplateRef<any>;
private elseTemplate: TemplateRef<any>;
private packSub: Subscription;
constructor(private templateRef$: TemplateRef<any>,private viewContainerRef$: ViewContainerRef,private store$: Store<appCommonReducer.State>) {
this.thenTemplate$=templateRef$;
this.packSub = this.store$.select(appCommonReducer.SelectGetUserPackages).subscribe((packages) => {
this.packages$ = packages;
this.updateView();
});
this.updateView();
constructor(private hostTemplateRef: TemplateRef<any>, private viewContainerRef: ViewContainerRef,
private packageService: PackageService) {
this.thenTemplate = hostTemplateRef;
this.updateView();
}
updateView() {
this.viewContainerRef$.clear();
const today = new Date(new Date(Date.now()).toUTCString()).setHours(0, 0, 0, 0);
if (this.packages$[this.packageIdentifier$] &&
(this.packages$[this.packageIdentifier$].dataDate && new Date(this.packages$[this.packageIdentifier$].dataDate).setHours(0, 0, 0, 0) <= today) &&
(this.packages$[this.packageIdentifier$].dataEndDate == null || new Date(this.packages$[this.packageIdentifier$].dataEndDate).setHours(0, 0, 0, 0) >= today)) {
this.viewContainerRef$.createEmbeddedView(this.thenTemplate$);
} else if (this.elseTemplate$) {
this.viewContainerRef$.createEmbeddedView(this.elseTemplate$);
}
this.viewContainerRef.clear();
if (this.packageService.hasPackage(this.packageIdentifier)) {
this.viewContainerRef.createEmbeddedView(this.thenTemplate);
} else if (this.elseTemplate) {
this.viewContainerRef.createEmbeddedView(this.elseTemplate);
}
}
ngOnDestroy() {
if(this.packSub) this.packSub.unsubscribe();
if (this.packSub) {
this.packSub.unsubscribe();
}
}
}
}

View File

@ -8,6 +8,7 @@ import { createSelector, createFeatureSelector, ActionReducerMap } from '@ngrx/s
import { MODULE_NAME } from '../module-name';
import { IItem } from '../models/item';
import {isValidPackage} from '../services/package.service';
export interface State {
openedModalName: string,
@ -207,12 +208,10 @@ export const SelectGetMenuVisible = createSelector(selectAppCommonState,getMenuV
export const SelectGetUser = createSelector(selectAppCommonState,getUser);
export const SelectGetUserPackages = createSelector(selectAppCommonState,getUserPackages);
export const SelectGetValidUserPackages = createSelector(SelectGetUserPackages, (packageMap) => {
const nowYear = new Date(Date.now()).getUTCFullYear();
const keys = Object.keys(packageMap);
return keys.filter(k => {
const packages = packageMap[k]
.filter((p) => p.dataEndDate.getUTCFullYear() >= nowYear);
.filter((p) => isValidPackage(p));
return packages.length > 0;
}).reduce((map, key) => {

View File

@ -14,24 +14,33 @@ import {Observable} from 'rxjs';
})
export class PackageService {
private packages: {[key: string]: IPackage} = {};
private packages: { [key: string]: IPackage } = {};
constructor(private store$: Store<appCommonReducer.State>, public httpClient: HttpClient, public appConfig: AppConfig) {
store$.select(appCommonReducer.SelectGetValidUserPackages).subscribe((packages) => {
this.packages = packages;
});
constructor(private store$: Store<appCommonReducer.State>, public httpClient: HttpClient, public appConfig: AppConfig) {
store$.select(appCommonReducer.SelectGetValidUserPackages).subscribe((packages) => {
this.packages = packages;
});
}
ApiEndpoint() {
return this.appConfig.getConfig('apiEndPoint');
}
hasPackage(id: string): boolean {
if (!this.packages[id]) {
return false;
}
ApiEndpoint() {
return this.appConfig.getConfig("apiEndPoint");
}
return this.packages[id].enabled;
}
hasPackage(id:string):boolean {
if(!this.packages[id]) return false;
return this.packages[id].enabled ? this.packages[id].enabled === true : true;
}
postItemPackageTask(item: IItem, task: IItemTask): Observable<IItemTask> {
return this.httpClient.post<IItemTask>(`${this.ApiEndpoint()}/api/v1/items/${item.code}/packagetasks`, task);
}
postItemPackageTask(item: IItem, task: IItemTask): Observable<IItemTask> {
return this.httpClient.post<IItemTask>(`${this.ApiEndpoint()}/api/v1/items/${item.code}/packagetasks`, task);
}
}
export function isValidPackage(pack: IPackage): boolean {
const today = new Date(new Date(Date.now()).toUTCString()).setHours(0, 0, 0, 0);
return pack !== null && pack.dataDate.getTime() <= today
&& (!pack.dataEndDate || pack.dataEndDate.getTime() >= today);
}