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", "name": "farmmaps-lib-app",
"version": "1.1.7", "version": "1.1.8",
"scripts": { "scripts": {
"ng": "ng", "ng": "ng",
"start": "ng serve", "start": "ng serve",

View File

@ -1,61 +1,53 @@
import { Directive, ViewContainerRef,TemplateRef,OnInit,Input,OnDestroy } from '@angular/core'; import {Directive, Input, OnDestroy, TemplateRef, ViewContainerRef} from '@angular/core';
import { Store} from '@ngrx/store'; import {Subscription} from 'rxjs';
import * as appCommonReducer from '../../reducers/app-common.reducer' import {PackageService} from '../../services/package.service';
import { IPackages } from '../../models/package';
import { Observable, Subscription } from 'rxjs';
@Directive({ @Directive({
selector: '[fmHasPackage]', selector: '[fmHasPackage]',
}) })
export class HasPackageDirective implements OnDestroy{ export class HasPackageDirective implements OnDestroy {
@Input() @Input()
set fmHasPackage(packageIdentifier:string) { set fmHasPackage(packageIdentifier: string) {
this.packageIdentifier$ = packageIdentifier; this.packageIdentifier = packageIdentifier;
this.updateView(); this.updateView();
} }
@Input() @Input()
set fmHasPackageThen(thenTemplate:TemplateRef<any>) { set fmHasPackageThen(thenTemplate: TemplateRef<any>) {
this.thenTemplate$ = thenTemplate; this.thenTemplate = thenTemplate;
this.updateView(); this.updateView();
} }
@Input() @Input()
set fmHasPackageElse(thenTemplate:TemplateRef<any>) { set fmHasPackageElse(thenTemplate: TemplateRef<any>) {
this.elseTemplate$ = thenTemplate; this.elseTemplate = thenTemplate;
this.updateView(); this.updateView();
} }
private packageIdentifier$:string; private packageIdentifier: string;
private thenTemplate$:TemplateRef<any>; private thenTemplate: TemplateRef<any>;
private elseTemplate$:TemplateRef<any>; private elseTemplate: TemplateRef<any>;
private packages$:any = {}; private packSub: Subscription;
private packagesObservable$:Observable<IPackages> = this.store$.select(appCommonReducer.SelectGetUserPackages);
private packSub:Subscription;
constructor(private templateRef$: TemplateRef<any>,private viewContainerRef$: ViewContainerRef,private store$: Store<appCommonReducer.State>) { constructor(private hostTemplateRef: TemplateRef<any>, private viewContainerRef: ViewContainerRef,
this.thenTemplate$=templateRef$; private packageService: PackageService) {
this.packSub = this.store$.select(appCommonReducer.SelectGetUserPackages).subscribe((packages) => { this.thenTemplate = hostTemplateRef;
this.packages$ = packages; this.updateView();
this.updateView();
});
this.updateView();
} }
updateView() { updateView() {
this.viewContainerRef$.clear(); this.viewContainerRef.clear();
const today = new Date(new Date(Date.now()).toUTCString()).setHours(0, 0, 0, 0); if (this.packageService.hasPackage(this.packageIdentifier)) {
if (this.packages$[this.packageIdentifier$] && this.viewContainerRef.createEmbeddedView(this.thenTemplate);
(this.packages$[this.packageIdentifier$].dataDate && new Date(this.packages$[this.packageIdentifier$].dataDate).setHours(0, 0, 0, 0) <= today) && } else if (this.elseTemplate) {
(this.packages$[this.packageIdentifier$].dataEndDate == null || new Date(this.packages$[this.packageIdentifier$].dataEndDate).setHours(0, 0, 0, 0) >= today)) { this.viewContainerRef.createEmbeddedView(this.elseTemplate);
this.viewContainerRef$.createEmbeddedView(this.thenTemplate$); }
} else if (this.elseTemplate$) {
this.viewContainerRef$.createEmbeddedView(this.elseTemplate$);
}
} }
ngOnDestroy() { 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 { MODULE_NAME } from '../module-name';
import { IItem } from '../models/item'; import { IItem } from '../models/item';
import {isValidPackage} from '../services/package.service';
export interface State { export interface State {
openedModalName: string, openedModalName: string,
@ -207,12 +208,10 @@ export const SelectGetMenuVisible = createSelector(selectAppCommonState,getMenuV
export const SelectGetUser = createSelector(selectAppCommonState,getUser); export const SelectGetUser = createSelector(selectAppCommonState,getUser);
export const SelectGetUserPackages = createSelector(selectAppCommonState,getUserPackages); export const SelectGetUserPackages = createSelector(selectAppCommonState,getUserPackages);
export const SelectGetValidUserPackages = createSelector(SelectGetUserPackages, (packageMap) => { export const SelectGetValidUserPackages = createSelector(SelectGetUserPackages, (packageMap) => {
const nowYear = new Date(Date.now()).getUTCFullYear();
const keys = Object.keys(packageMap); const keys = Object.keys(packageMap);
return keys.filter(k => { return keys.filter(k => {
const packages = packageMap[k] const packages = packageMap[k]
.filter((p) => p.dataEndDate.getUTCFullYear() >= nowYear); .filter((p) => isValidPackage(p));
return packages.length > 0; return packages.length > 0;
}).reduce((map, key) => { }).reduce((map, key) => {

View File

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