diff --git a/libs/clients/license-client/src/lib/clients/machine-license-client/machineLicenseClient.service.ts b/libs/clients/license-client/src/lib/clients/machine-license-client/machineLicenseClient.service.ts index f49c370c883b..c38496f17533 100644 --- a/libs/clients/license-client/src/lib/clients/machine-license-client/machineLicenseClient.service.ts +++ b/libs/clients/license-client/src/lib/clients/machine-license-client/machineLicenseClient.service.ts @@ -25,6 +25,7 @@ import { Result, VerifyPkPassResult, } from '../../licenseClient.type' +import compareAsc from 'date-fns/compareAsc' /** Category to attach each log message to */ const LOG_CATEGORY = 'machinelicense-service' @@ -45,13 +46,23 @@ export class MachineLicenseClient private checkLicenseValidityForPkPass( licenseInfo: VinnuvelaDto, ): LicensePkPassAvailability { - if (!licenseInfo) { + const expirationDate = licenseInfo + ? findLatestExpirationDate(licenseInfo) + : null + + if (!licenseInfo || !expirationDate) { return LicensePkPassAvailability.Unknown } - //Nothing to check as of yet + const comparison = compareAsc(new Date(expirationDate), new Date()) + + if (Number.isNaN(comparison) || comparison < 0) { + return LicensePkPassAvailability.NotAvailable + } + return LicensePkPassAvailability.Available } + private async fetchLicense(user: User): Promise> { try { const licenseInfo = await this.machineApi diff --git a/libs/clients/license-client/src/lib/clients/machine-license-client/machineLicenseClient.spec.ts b/libs/clients/license-client/src/lib/clients/machine-license-client/machineLicenseClient.spec.ts index 8dd15d0a7a68..2768b94e7848 100644 --- a/libs/clients/license-client/src/lib/clients/machine-license-client/machineLicenseClient.spec.ts +++ b/libs/clients/license-client/src/lib/clients/machine-license-client/machineLicenseClient.spec.ts @@ -1,34 +1,21 @@ import * as mapper from './machineLicenseMapper' -import ExpiredMachineLicense from '../__mock_data/expiredMachineLicense.json' import ValidMachineLicense from '../__mock_data/validMachineLicense.json' describe('license-service/client/machine-license', () => { describe('check if expired', () => { - it('should return null if no license info', async () => { - const result = mapper.checkLicenseExpirationDate({}) - expect(result).toBeNull() - }) - it('should return true if expired', async () => { - const result = mapper.checkLicenseExpirationDate(ExpiredMachineLicense) - expect(result).toBeTruthy() - }) - it('should return false if valid and not expired', async () => { - const result = mapper.checkLicenseExpirationDate(ValidMachineLicense) - expect(result).toBeFalsy() - }) - }) - describe('get latest license qualification expiration date', () => { - it('should return the latest license right date', async () => { - const result = mapper.findLatestExpirationDate(ValidMachineLicense) + describe('get latest license qualification expiration date', () => { + it('should return the latest license right date', async () => { + const result = mapper.findLatestExpirationDate(ValidMachineLicense) - const expectedDate = new Date('2080-01-01T00:00:00Z').toISOString() + const expectedDate = new Date('2080-01-01T00:00:00Z').toISOString() - expect(result).toMatch(expectedDate) - }) - it('should return undefined if no license info', async () => { - const result = mapper.findLatestExpirationDate({}) + expect(result).toMatch(expectedDate) + }) + it('should return undefined if no license info', async () => { + const result = mapper.findLatestExpirationDate({}) - expect(result).toBeNull + expect(result).toBeNull + }) }) }) }) diff --git a/libs/clients/license-client/src/lib/clients/machine-license-client/machineLicenseMapper.ts b/libs/clients/license-client/src/lib/clients/machine-license-client/machineLicenseMapper.ts index 378a4d60955f..70f0493686e7 100644 --- a/libs/clients/license-client/src/lib/clients/machine-license-client/machineLicenseMapper.ts +++ b/libs/clients/license-client/src/lib/clients/machine-license-client/machineLicenseMapper.ts @@ -4,42 +4,30 @@ import { } from '@island.is/clients/adr-and-machine-license' import format from 'date-fns/format' -import isAfter from 'date-fns/isAfter' import { Locale } from '@island.is/shared/types' import is from 'date-fns/locale/is' import enGB from 'date-fns/locale/en-GB' import { format as formatSsn } from 'kennitala' -export const checkLicenseExpirationDate = (license: VinnuvelaDto) => { - return license.vinnuvelaRettindi - ? license.vinnuvelaRettindi - .filter((field) => field.kenna || field.stjorna) - .every( - (field: VinnuvelaRettindiDto) => - field.kenna && - !isAfter(new Date(field.kenna), new Date()) && - field.stjorna && - !isAfter(new Date(field.stjorna), new Date()), - ) - : null -} +const compareDates = (newDate: Date, latestDate?: Date) => + !latestDate || newDate > latestDate ? newDate : latestDate export const findLatestExpirationDate = (license: VinnuvelaDto) => { if (!license.vinnuvelaRettindi) { return null } - let maxDate = new Date() + let latestDate: Date | undefined for (const right of license.vinnuvelaRettindi) { - if (right.stjorna && new Date(right.stjorna) > maxDate) { - maxDate = new Date(right.stjorna) + if (right.stjorna) { + latestDate = compareDates(new Date(right.stjorna), latestDate) } - if (right.kenna && new Date(right.kenna) > maxDate) { - maxDate = new Date(right.kenna) + if (right.kenna) { + latestDate = compareDates(new Date(right.kenna), latestDate) } } - return maxDate.toISOString() + return latestDate ? latestDate.toISOString() : null } const formatDateString = (