Skip to content

Commit

Permalink
Code re-factor and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ballioli committed Jan 14, 2025
1 parent 889876f commit d922aa1
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SharedTemplateApiService } from '../../../shared'
import { TemplateApiModuleActionProps } from '../../../../types'
import { ApplicationTypes } from '@island.is/application/types'
import { BaseTemplateApiService } from '../../../base-template-api.service'

import { error as errorMsg } from '@island.is/application/templates/aosh/street-registration'
import { TemplateApiError } from '@island.is/nest/problem'
import { coreErrorMessages } from '@island.is/application/core'
import { StreetRegistrationAnswers } from '@island.is/application/templates/aosh/street-registration'
Expand Down Expand Up @@ -61,17 +61,41 @@ export class StreetRegistrationTemplateService extends BaseTemplateApiService {
async getTypesMustInspectBeforeRegistration({
auth,
}: TemplateApiModuleActionProps): Promise<string[]> {
return await this.workMachineClientService.mustInspectBeforeRegistration(
auth,
)
return await this.workMachineClientService
.mustInspectBeforeRegistration(auth)
.catch((error) => {
this.logger.warning(
'[street-registration-service]: Error fetching types requires for inspection',
error,
)
throw new TemplateApiError(
{
title: coreErrorMessages.defaultTemplateApiError,
summary: errorMsg.errorGetFromAOSH,
},
500,
)
})
}

async getAvailableRegistrationTypes({
auth,
}: TemplateApiModuleActionProps): Promise<string[]> {
return await this.workMachineClientService.getAvailableRegistrationTypes(
auth,
)
return await this.workMachineClientService
.getAvailableRegistrationTypes(auth)
.catch((error) => {
this.logger.warning(
'[street-registration-service]: Error fetching available types for registration',
error,
)
throw new TemplateApiError(
{
title: coreErrorMessages.defaultTemplateApiError,
summary: errorMsg.errorGetFromAOSH,
},
500,
)
})
}

async submitApplication({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import RegisterNewMachineTemplate from './lib/RegisterNewMachineTemplate'
export const getDataProviders = () => import('./dataProviders/')
export const getFields = () => import('./fields/')

export { NewMachineAnswers } from './lib/dataSchema'
export type { NewMachineAnswers } from './lib/dataSchema'

export * from './utils'
export * from './shared/types'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,36 @@ import {
getValueViaPath,
} from '@island.is/application/core'
import { information } from '../../../lib/messages'
import { MachinesWithTotalCount } from '@island.is/clients/work-machines'
import {
MachineDto,
MachinesWithTotalCount,
} from '@island.is/clients/work-machines'
import {
isInvalidRegistrationType,
isMachineDisabled,
mustInspectBeforeStreetRegistration,
} from '../../../utils/getSelectedMachine'
import { useLocale } from '@island.is/localization'
import { ExternalData } from '@island.is/application/types'
import { MessageDescriptor } from 'react-intl'

const getTagLabel = (
externalData: ExternalData,
machine: MachineDto,
formatMessage: (message: MessageDescriptor) => string,
): string => {
if (
mustInspectBeforeStreetRegistration(externalData, machine?.regNumber || '')
) {
return formatMessage(
information.labels.pickMachine.inspectBeforeRegistration,
)
}
if (isInvalidRegistrationType(externalData, machine?.regNumber || '')) {
return formatMessage(information.labels.pickMachine.invalidRegistrationType)
}
return machine?.status || ''
}

export const pickMachineSubSection = buildSubSection({
id: 'pickMachine',
Expand Down Expand Up @@ -53,23 +76,11 @@ export const pickMachineSubSection = buildSubSection({
machine?.disabled ||
isMachineDisabled(externalData, machine?.regNumber || '')
? {
label: mustInspectBeforeStreetRegistration(
application?.externalData,
machine?.regNumber || '',
)
? useLocale().formatMessage(
information.labels.pickMachine
.inspectBeforeRegistration,
)
: isInvalidRegistrationType(
externalData,
machine?.regNumber || '',
)
? useLocale().formatMessage(
information.labels.pickMachine
.invalidRegistrationType,
)
: machine?.status || '',
label: getTagLabel(
externalData,
machine,
useLocale().formatMessage,
),
variant: 'red',
outlined: true,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export type StreetRegistrationAnswers = MachineAnswers
export * from './utils'
export * from './lib/messages/externalData'
export * from './lib/messages/applicationCheck'
export * from './lib/messages/error'
export default template
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ export const error = defineMessages({
defaultMessage: 'Villa kom upp við að skila inn umsókn',
description: 'Failed to submit application',
},
errorGetFromAOSH: {
id: 'aosh.sr.application:error.errorGetFromAOSH',
defaultMessage: 'Ekki tókst að sækja gögn frá Vinnueftirlitinu',
description: 'Failed to fetch data from AOSH',
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
} from '@island.is/application/types'
import { MessageDescriptor } from 'react-intl'

const REGISTRATION_TYPE_LENGTH = 2

export const energyFundsLabel = function (
energyDetails: EnergyFundVehicleDetailsWithGrant | null,
energyFundsMessages: Record<string, FormText> | undefined,
Expand Down Expand Up @@ -49,13 +51,20 @@ export const mustInspectBeforeStreetRegistration = (
'typesMustInspectBeforeRegistration.data',
[],
)
return inspectBeforeTypes?.includes(regNumber.substring(0, 2)) || false
return (
inspectBeforeTypes?.includes(
regNumber.substring(0, REGISTRATION_TYPE_LENGTH),
) || false
)
}

export const isInvalidRegistrationType = (
externalData: ExternalData,
regNumber: string,
) => {
if (!regNumber || regNumber.length < REGISTRATION_TYPE_LENGTH) {
return true
}
const validTypes = getValueViaPath<string[]>(
externalData,
'availableRegistrationTypes.data',
Expand All @@ -67,7 +76,7 @@ export const isInvalidRegistrationType = (
[],
)

const regType = regNumber.substring(0, 2)
const regType = regNumber.substring(0, REGISTRATION_TYPE_LENGTH)

return (
!validTypes?.includes(regType) && !inspectBeforeTypes?.includes(regType)
Expand Down

0 comments on commit d922aa1

Please sign in to comment.