Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(licenses): pkpass status on machine license #16679

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
Result,
VerifyPkPassResult,
} from '../../licenseClient.type'
import compareAsc from 'date-fns/compareAsc'
thorkellmani marked this conversation as resolved.
Show resolved Hide resolved

/** Category to attach each log message to */
const LOG_CATEGORY = 'machinelicense-service'
Expand All @@ -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
}
thorkellmani marked this conversation as resolved.
Show resolved Hide resolved

return LicensePkPassAvailability.Available
}

private async fetchLicense(user: User): Promise<Result<VinnuvelaDto | null>> {
try {
const licenseInfo = await this.machineApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
thorkellmani marked this conversation as resolved.
Show resolved Hide resolved
}
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 = (
Expand Down
Loading