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(application-system): Fixing error messages for certain types of machines #17486

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -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,9 +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)
.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 @@ -16,6 +16,11 @@ export const MustInspectBeforeRegistrationApi = defineTemplateApi({
externalDataId: 'typesMustInspectBeforeRegistration',
})

export const GetAvailableRegistrationTypes = defineTemplateApi({
action: 'getAvailableRegistrationTypes',
externalDataId: 'availableRegistrationTypes',
})

export const VinnueftirlitidPaymentCatalogApi = PaymentCatalogApi.configure({
params: {
organizationId: InstitutionNationalIds.SAMGONGUSTOFA,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,36 @@ import {
getValueViaPath,
} from '@island.is/application/core'
import { information } from '../../../lib/messages'
import { MachinesWithTotalCount } from '@island.is/clients/work-machines'
import { mustInspectBeforeStreetRegistration } from '../../../utils/getSelectedMachine'
import {
MachineDto,
MachinesWithTotalCount,
} from '@island.is/clients/work-machines'
import {
isInvalidRegistrationType,
isMachineDisabled,
mustInspectBeforeStreetRegistration,
} from '../../../utils/getSelectedMachine'
import { useLocale } from '@island.is/localization'
import { Application } from '@island.is/application/types'
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 All @@ -31,14 +57,11 @@ export const pickMachineSubSection = buildSubSection({
) as MachinesWithTotalCount
return machines.totalCount <= 5
},
defaultValue: (application: Application) => {
const machineList = application?.externalData.machinesList
.data as MachinesWithTotalCount
return machineList?.machines[0].id ?? ''
},

options: (application) => {
const machineList = application?.externalData.machinesList
.data as MachinesWithTotalCount
const externalData = application.externalData

return machineList.machines.map((machine) => {
return {
Expand All @@ -47,25 +70,21 @@ export const pickMachineSubSection = buildSubSection({
subLabel: `${machine.category}: ${machine.type} - ${machine.subType}`,
disabled: machine?.disabled
? true
: mustInspectBeforeStreetRegistration(
application?.externalData,
machine?.regNumber || '',
) || false,
tag: machine?.disabled
? {
label: mustInspectBeforeStreetRegistration(
application?.externalData,
machine?.regNumber || '',
)
? useLocale().formatMessage(
information.labels.pickMachine
.inspectBeforeRegistration,
)
: machine?.status || '',
variant: 'red',
outlined: true,
}
: undefined,
: isMachineDisabled(externalData, machine?.regNumber || '') ||
false,
tag:
machine?.disabled ||
isMachineDisabled(externalData, machine?.regNumber || '')
? {
label: getTagLabel(
externalData,
machine,
useLocale().formatMessage,
),
variant: 'red',
outlined: true,
}
: undefined,
}
})
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
MachinesApi,
MustInspectBeforeRegistrationApi,
VinnueftirlitidPaymentCatalogApi,
GetAvailableRegistrationTypes,
} from '../../dataProviders'
import { DefaultEvents } from '@island.is/application/types'

Expand Down Expand Up @@ -58,6 +59,11 @@ export const prerequisitesSection = buildSection({
title: '',
subTitle: '',
}),
buildDataProviderItem({
provider: GetAvailableRegistrationTypes,
title: '',
subTitle: '',
}),
buildDataProviderItem({
provider: VinnueftirlitidPaymentCatalogApi,
title: '',
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 @@ -30,6 +30,7 @@ import {
MachinesApi,
MustInspectBeforeRegistrationApi,
VinnueftirlitidPaymentCatalogApi,
GetAvailableRegistrationTypes,
} from '../dataProviders'
import { ApiScope } from '@island.is/auth/scopes'
import { getChargeItems } from '../utils'
Expand Down Expand Up @@ -111,6 +112,7 @@ const template: ApplicationTemplate<
UserProfileApi,
MachinesApi,
MustInspectBeforeRegistrationApi,
GetAvailableRegistrationTypes,
VinnueftirlitidPaymentCatalogApi,
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ const otherAddressSchema = z.object({
})

export const MachineAnswersSchema = z.object({
machine: z.object({
id: z.string().optional(),
date: z.string().optional(),
type: z.string().optional(),
plate: z.string().optional(),
subType: z.string().optional(),
category: z.string().optional(),
regNumber: z.string().optional(),
ownerNumber: z.string().optional(),
}),
machine: z
.object({
id: z.string().optional(),
date: z.string().optional(),
type: z.string().optional(),
plate: z.string().optional(),
subType: z.string().optional(),
category: z.string().optional(),
regNumber: z.string().optional(),
ownerNumber: z.string().optional(),
})
.refine((obj) => Object.keys(obj).length > 0),
contact: z.object({
name: z.string().min(1),
email: z.string().email().min(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ export const applicationCheck = {
defaultMessage: 'Hafið samband við Vinnueftirlitið til að götuskrá',
description: 'Pick machine inspect before registration',
},
unavailableTypeForRegistration: {
id: 'aosh.sr.application:applicationCheck.validation.unavailableTypeForRegistration',
defaultMessage: 'Ekki er hægt að götuskrá tæki í þessum flokk',
description: 'Pick machine has type that is not valid for registration',
},
}),
}
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 @@ -82,6 +82,11 @@ export const information = {
defaultMessage: 'Hafið samband við Vinnueftirlitið til að götuskrá',
description: 'Pick machine inspect before registration',
},
invalidRegistrationType: {
id: 'aosh.sr.application:information.labels.pickMachine.invalidRegistrationType',
defaultMessage: 'Ekki er hægt að götuskrá tæki í þessum flokk',
description: 'Pick machine invalid registration type tag',
},
}),
machine: defineMessages({
sectionTitle: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const mustInspectBeforeStreetRegistration = (
'typesMustInspectBeforeRegistration.data',
[],
) as string[]

return inspectBeforeTypes?.includes(regNumber.substring(0, 2)) || false
}

Expand All @@ -47,3 +48,41 @@ export const getExtraData = (application: ApplicationType): ExtraData[] => {
{ name: 'date', value: streetAnswers.machine?.date || '' },
]
}

export const isInvalidRegistrationType = (
externalData: ExternalData,
regNumber: string,
) => {
const validTypes = getValueViaPath<string[]>(
externalData,
'availableRegistrationTypes.data',
[],
)
const inspectBeforeTypes = getValueViaPath<string[]>(
externalData,
'typesMustInspectBeforeRegistration.data',
[],
)

const regType = regNumber.substring(0, 2)

return (
!validTypes?.includes(regType) && !inspectBeforeTypes?.includes(regType)
)
}

export const isMachineDisabled = (
externalData: ExternalData,
regNumber: string,
): boolean => {
const validTypes = getValueViaPath<string[]>(
externalData,
'availableRegistrationTypes.data',
[],
)

const regType = regNumber.substring(0, 2)

// Checks the machine type against the available registration types
return !validTypes?.includes(regType)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import format from 'date-fns/format'
import { formatCurrency } from '@island.is/application/ui-components'
import {
energyFundsLabel,
isInvalidRegistrationType,
mustInspectBeforeStreetRegistration,
} from './FindVehicleFormField.util'

Expand Down Expand Up @@ -159,8 +160,6 @@ export const FindVehicleFormField: FC<React.PropsWithChildren<Props>> = ({
isMachine,
isEnergyFunds,
energyFundsMessages,
marginTop,
marginBottom,
clearOnChange,
} = field

Expand Down Expand Up @@ -268,21 +267,30 @@ export const FindVehicleFormField: FC<React.PropsWithChildren<Props>> = ({
const machineDisabled = machineDetails.disabled

if (application.typeId === 'StreetRegistration') {
const isUnavailableTypeForRegistration = isInvalidRegistrationType(
application?.externalData,
machineDetails.regNumber || '',
)
const mustInspect = mustInspectBeforeStreetRegistration(
application?.externalData,
machineDetails.regNumber || '',
)
if (mustInspect && !machineDisabled) {
const statusKey = mustInspect
? 'inspectBeforeRegistration'
: isUnavailableTypeForRegistration
? 'unavailableTypeForRegistration'
: null

if (
statusKey === 'inspectBeforeRegistration' ||
statusKey === 'unavailableTypeForRegistration'
) {
machineDetails = {
...machineDetails,
disabled: true,
status:
validationErrors &&
formatText(
validationErrors.inspectBeforeRegistration,
application,
formatMessage,
),
formatText(validationErrors[statusKey], application, formatMessage),
}
}
}
Expand Down
Loading
Loading