From 519b81d1fd339d53fe4d4302132d34c35822029c Mon Sep 17 00:00:00 2001 From: Mark van der Wal Date: Thu, 27 Jan 2022 17:07:24 +0100 Subject: [PATCH] AW-3441 fix package validation --- package.json | 2 +- .../has-package/has-package.directive.ts | 72 +++++++++---------- .../src/fm/reducers/app-common.reducer.ts | 5 +- .../common/src/fm/services/package.service.ts | 41 ++++++----- 4 files changed, 60 insertions(+), 60 deletions(-) diff --git a/package.json b/package.json index 1565053..7681903 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "farmmaps-lib-app", - "version": "1.1.7", + "version": "1.1.8", "scripts": { "ng": "ng", "start": "ng serve", diff --git a/projects/common/src/fm/components/has-package/has-package.directive.ts b/projects/common/src/fm/components/has-package/has-package.directive.ts index daec258..86fc207 100644 --- a/projects/common/src/fm/components/has-package/has-package.directive.ts +++ b/projects/common/src/fm/components/has-package/has-package.directive.ts @@ -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) { - this.thenTemplate$ = thenTemplate; - this.updateView(); + set fmHasPackageThen(thenTemplate: TemplateRef) { + this.thenTemplate = thenTemplate; + this.updateView(); } @Input() - set fmHasPackageElse(thenTemplate:TemplateRef) { - this.elseTemplate$ = thenTemplate; - this.updateView(); + set fmHasPackageElse(thenTemplate: TemplateRef) { + this.elseTemplate = thenTemplate; + this.updateView(); } - private packageIdentifier$:string; - private thenTemplate$:TemplateRef; - private elseTemplate$:TemplateRef; - private packages$:any = {}; - private packagesObservable$:Observable = this.store$.select(appCommonReducer.SelectGetUserPackages); - private packSub:Subscription; + private packageIdentifier: string; + private thenTemplate: TemplateRef; + private elseTemplate: TemplateRef; + private packSub: Subscription; - constructor(private templateRef$: TemplateRef,private viewContainerRef$: ViewContainerRef,private store$: Store) { - this.thenTemplate$=templateRef$; - this.packSub = this.store$.select(appCommonReducer.SelectGetUserPackages).subscribe((packages) => { - this.packages$ = packages; - this.updateView(); - }); - this.updateView(); + constructor(private hostTemplateRef: TemplateRef, 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(); + } } -} \ No newline at end of file +} diff --git a/projects/common/src/fm/reducers/app-common.reducer.ts b/projects/common/src/fm/reducers/app-common.reducer.ts index 2b8f19d..7ed3a20 100644 --- a/projects/common/src/fm/reducers/app-common.reducer.ts +++ b/projects/common/src/fm/reducers/app-common.reducer.ts @@ -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) => { diff --git a/projects/common/src/fm/services/package.service.ts b/projects/common/src/fm/services/package.service.ts index b5b9bad..cf2a427 100644 --- a/projects/common/src/fm/services/package.service.ts +++ b/projects/common/src/fm/services/package.service.ts @@ -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, public httpClient: HttpClient, public appConfig: AppConfig) { - store$.select(appCommonReducer.SelectGetValidUserPackages).subscribe((packages) => { - this.packages = packages; - }); + constructor(private store$: Store, 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 { - return this.httpClient.post(`${this.ApiEndpoint()}/api/v1/items/${item.code}/packagetasks`, task); - } + postItemPackageTask(item: IItem, task: IItemTask): Observable { + return this.httpClient.post(`${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); }