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(license-service): check app version #16855

Merged
merged 23 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
bfa8519
fix: check app version
thorkellmani Nov 13, 2024
cc9e488
fix: add app ver constant
thorkellmani Nov 14, 2024
af486e9
chore: add comment
thorkellmani Nov 14, 2024
94e27d2
Merge branch 'main' into fix/firearm-app-version-mode
thorkellmani Nov 14, 2024
3881ffb
fix: import type
thorkellmani Nov 14, 2024
4c819ed
chore: update version
thorkellmani Nov 15, 2024
3f3b946
chore: add MOCKS
thorkellmani Nov 15, 2024
695a7bb
chore: nullcheck app
thorkellmani Nov 15, 2024
6d83098
fix: reverse flag
thorkellmani Nov 18, 2024
1d61483
Merge branch 'main' into fix/firearm-app-version-mode
thorkellmani Nov 21, 2024
bd5e40d
Merge branch 'main' into fix/firearm-app-version-mode
thorkellmani Nov 21, 2024
31e0253
fix: wrong resoler method
thorkellmani Nov 22, 2024
865912c
Merge branch 'main' into fix/firearm-app-version-mode
thorkellmani Nov 25, 2024
206e122
fix: add table
thorkellmani Nov 25, 2024
ce00467
fix: wrong resolver method
thorkellmani Nov 27, 2024
37ac338
fix: add logger
thorkellmani Nov 27, 2024
425125e
Update firearmLicenseMapper.ts
thorkellmani Nov 27, 2024
ff8eb99
fix: conditional return
thorkellmani Nov 28, 2024
38cf9cb
Merge branch 'main' into fix/firearm-app-version-mode
thorkellmani Nov 28, 2024
ff81c57
Update firearmLicenseMapper.ts
thorkellmani Nov 28, 2024
fa78c37
fix: erase mocks
thorkellmani Nov 28, 2024
a4eee21
Merge remote-tracking branch 'refs/remotes/origin/fix/firearm-app-ver…
thorkellmani Nov 28, 2024
61d3265
Merge branch 'main' into fix/firearm-app-version-mode
kodiakhq[bot] Dec 3, 2024
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 @@ -2,6 +2,7 @@ import { Locale } from '@island.is/shared/types'
import { GenericLicenseError } from './dto/GenericLicenseError.dto'
import { Payload } from './dto/Payload.dto'
import { GenericUserLicense as GenericUserLicenseModel } from './dto/GenericUserLicense.dto'
import { UserAgent } from '@island.is/nest/core'

export interface GenericLicenseMappedPayloadResponse {
licenseName: string
Expand Down Expand Up @@ -145,5 +146,6 @@ export interface GenericLicenseMapper {
parsePayload: (
payload: Array<unknown>,
locale: Locale,
userAgent?: UserAgent,
) => Promise<Array<GenericLicenseMappedPayloadResponse>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
BarcodeService,
TOKEN_EXPIRED_ERROR,
} from '@island.is/services/license'
import { UserAgent } from '@island.is/nest/core'

const LOG_CATEGORY = 'license-service'

Expand Down Expand Up @@ -96,6 +97,7 @@ export class LicenseService {
user: User,
locale: Locale,
{ includedTypes, excludedTypes, onlyList }: GetGenericLicenseOptions = {},
userAgent?: UserAgent,
): Promise<LicenseCollection> {
const fetchPromises = AVAILABLE_LICENSES.map(async (license) => {
if (excludedTypes && excludedTypes.indexOf(license.type) >= 0) {
Expand All @@ -107,7 +109,7 @@ export class LicenseService {
}

if (!onlyList) {
return this.getLicensesOfType(user, locale, license.type)
return this.getLicensesOfType(user, locale, license.type, userAgent)
}

return null
Expand Down Expand Up @@ -140,6 +142,7 @@ export class LicenseService {
user: User,
locale: Locale,
licenseType: GenericLicenseType,
agent?: UserAgent,
): Promise<LicenseTypeFetchResponse | null> {
const licenseTypeDefinition = AVAILABLE_LICENSES.find(
(i) => i.type === licenseType,
Expand Down Expand Up @@ -187,6 +190,7 @@ export class LicenseService {
const licensesPayload = await mapper.parsePayload(
licensesFetchResponse.data,
locale,
agent,
)

const mappedLicenses: Array<GenericUserLicense> = await Promise.all(
Expand Down Expand Up @@ -246,11 +250,13 @@ export class LicenseService {
locale: Locale,
licenseType: GenericLicenseType,
licenseId?: string,
agent?: UserAgent,
): Promise<GenericUserLicense | LicenseError | null> {
const licensesOfType = await this.getLicensesOfType(
user,
locale,
licenseType,
agent,
)

if (!licensesOfType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,34 @@ import {
GenericUserLicenseMetaLinksType,
} from '../licenceService.type'
import { FirearmLicenseDto } from '@island.is/clients/license-client'
import { Injectable } from '@nestjs/common'
import { Inject, Injectable } from '@nestjs/common'
import { isDefined } from '@island.is/shared/utils'
import { FormatMessage, IntlService } from '@island.is/cms-translations'
import { m } from '../messages'
import { expiryTag, formatDate } from '../utils'
import { GenericLicenseDataField } from '../dto/GenericLicenseDataField.dto'
import { UserAgent } from '@island.is/nest/core'
import { enableAppCompatibilityMode } from '../utils/appCompatibilityMode'
import { LOGGER_PROVIDER, type Logger } from '@island.is/logging'

const APP_VERSION_CUTOFF = '1.4.7'
thorkellmani marked this conversation as resolved.
Show resolved Hide resolved

@Injectable()
export class FirearmLicensePayloadMapper implements GenericLicenseMapper {
constructor(private readonly intlService: IntlService) {}
async parsePayload(
payload: Array<unknown>,
locale: Locale = 'is',
userAgent?: UserAgent,
): Promise<Array<GenericLicenseMappedPayloadResponse>> {
if (!payload) return Promise.resolve([])

//App version before 1.4.8 doesn't know how to handle table
const enableAppCompatibility = enableAppCompatibilityMode(
userAgent?.app?.version,
APP_VERSION_CUTOFF,
)

const typedPayload = payload as Array<FirearmLicenseDto>

const { formatMessage } = await this.intlService.useIntl(
Expand Down Expand Up @@ -99,21 +111,9 @@ export class FirearmLicensePayloadMapper implements GenericLicenseMapper {
: null,
properties
? {
type: GenericLicenseDataFieldType.Group,
hideFromServicePortal: true,
label: formatMessage(m.firearmProperties),
fields: (properties.properties ?? []).map((property) => ({
type: GenericLicenseDataFieldType.Category,
fields: this.parseProperties(
property,
formatMessage,
)?.filter(isDefined),
})),
}
: null,
properties
? {
type: GenericLicenseDataFieldType.Table,
type: enableAppCompatibility
? GenericLicenseDataFieldType.Group
: GenericLicenseDataFieldType.Table,
label: formatMessage(m.firearmProperties),
fields: (properties.properties ?? []).map((property) => ({
type: GenericLicenseDataFieldType.Category,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,32 @@ import { ApiScope } from '@island.is/auth/scopes'
import { Audit } from '@island.is/nest/audit'

import type { Locale } from '@island.is/shared/types'
import { UseGuards } from '@nestjs/common'
import { Inject, UseGuards } from '@nestjs/common'
import { Args, Query, Resolver } from '@nestjs/graphql'
import { GetGenericLicensesInput } from '../dto/GetGenericLicenses.input'
import { LicenseService } from '../licenseService.service'
import { LicenseCollection } from '../dto/GenericLicenseCollection.dto'
import { GenericUserLicense } from '../dto/GenericUserLicense.dto'
import { LOGGER_PROVIDER, type Logger } from '@island.is/logging'
import { ParsedUserAgent, type UserAgent } from '@island.is/nest/core'

@UseGuards(IdsUserGuard, ScopesGuard)
@Scopes(ApiScope.internal, ApiScope.licenses)
@Resolver(() => LicenseCollection)
@Audit({ namespace: '@island.is/api/license-service' })
export class LicenseCollectionResolver {
constructor(private readonly licenseServiceService: LicenseService) {}
constructor(
private readonly licenseServiceService: LicenseService,
@Inject(LOGGER_PROVIDER) private readonly logger: Logger,
) {}

@Query(() => [GenericUserLicense], {
deprecationReason: 'Use genericLicenseCollection instead',
})
@Audit()
async genericLicenses(
@CurrentUser() user: User,
@ParsedUserAgent() agent: UserAgent,
@Args('locale', { type: () => String, nullable: true })
locale: Locale = 'is',
@Args('input', { nullable: true }) input?: GetGenericLicensesInput,
Expand All @@ -43,6 +49,7 @@ export class LicenseCollectionResolver {
force: input?.force,
onlyList: input?.onlyList,
},
agent,
)

return collection.licenses
Expand All @@ -54,18 +61,24 @@ export class LicenseCollectionResolver {
@Audit()
async genericLicenseCollection(
@CurrentUser() user: User,
@ParsedUserAgent() agent: UserAgent,
@Args('locale', { type: () => String, nullable: true })
locale: Locale = 'is',
@Args('input') input: GetGenericLicensesInput,
) {
const licenseCollection =
await this.licenseServiceService.getLicenseCollection(user, locale, {
...input,
includedTypes: input?.includedTypes ?? ['DriversLicense'],
excludedTypes: input?.excludedTypes,
force: input?.force,
onlyList: input?.onlyList,
})
await this.licenseServiceService.getLicenseCollection(
user,
locale,
{
...input,
includedTypes: input?.includedTypes ?? ['DriversLicense'],
excludedTypes: input?.excludedTypes,
force: input?.force,
onlyList: input?.onlyList,
},
agent,
)
return licenseCollection
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { GenericUserLicense } from '../dto/GenericUserLicense.dto'
import { GetGenericLicenseInput } from '../dto/GetGenericLicense.input'
import { LicenseService } from '../licenseService.service'
import { GenericLicenseError } from '../dto/GenericLicenseError.dto'
import { logger } from '@island.is/logging'
import { ParsedUserAgent, type UserAgent } from '@island.is/nest/core'

@UseGuards(IdsUserGuard, ScopesGuard)
@Scopes(ApiScope.internal, ApiScope.licenses)
Expand All @@ -29,6 +31,8 @@ export class UserLicenseResolver {
@Audit()
async genericLicense(
@CurrentUser() user: User,
@ParsedUserAgent()
agent: UserAgent,
@Args('locale', { type: () => String, nullable: true })
locale: Locale = 'is',
@Args('input') input: GetGenericLicenseInput,
Expand All @@ -38,6 +42,7 @@ export class UserLicenseResolver {
locale,
input.licenseType,
input.licenseId,
agent,
)

if (license instanceof GenericLicenseError) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const enableAppCompatibilityMode = (
version?: string,
versionToCompare?: string,
): boolean => {
thorkellmani marked this conversation as resolved.
Show resolved Hide resolved
if (!version || !versionToCompare) {
return false
}

const versionComparison = version.localeCompare(versionToCompare, undefined, {
numeric: true,
sensitivity: 'base',
})

return versionComparison <= 0
}
Loading