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(transport-authority): Get correct requiresMileageRegistration value #16639

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
45 changes: 25 additions & 20 deletions libs/api/domains/energy-funds/src/lib/energyFunds.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,43 @@ export class EnergyFundsService {
}

async getVehicleDetailsWithGrant(auth: User, permno: string) {
const vehicle = await this.vehiclesApiWithAuth(
// Get current vehicle information where you are (main) owner
const result = await this.vehiclesApiWithAuth(
auth,
).currentvehicleswithmileageandinspGet({
permno: permno,
showOwned: true,
showCoowned: false,
showOperated: false,
})

const basicVehicle = await this.vehiclesApiWithAuth(
auth,
).basicVehicleInformationGet({ permno: permno })
if (!vehicle) {
throw Error(
'Did not find the vehicle with for that permno, or you are neither owner nor co-owner of the vehicle',
)
}
if (
vehicle.owners &&
!vehicle.owners.some((owner) => owner.persidno === auth.nationalId)
) {

if (!result || !result.data || result.data.length === 0 || !basicVehicle) {
throw Error(
'Did not find the vehicle with for that permno, or you are neither owner nor co-owner of the vehicle',
'Did not find the vehicle with that permno, or you are not owner of the vehicle',
)
}

const vehicle = result.data[0]

const vehicleGrantItem =
await this.energyFundsClientService.getCatalogValueForVehicle(auth, {
vehicleRegistrationCode: vehicle.euGroup,
firstRegistrationDate: vehicle.firstregdate,
newRegistrationDate: vehicle.newregdate,
vehicleRegistrationCode: basicVehicle.euGroup,
firstRegistrationDate: basicVehicle.firstregdate,
newRegistrationDate: basicVehicle.newregdate,
johannaagma marked this conversation as resolved.
Show resolved Hide resolved
permno: vehicle.permno,
})

if (!vehicleGrantItem)
throw new Error('Could not get available grants for this vehicle')

const hasReceivedSubsidy = vehicle.vin
const hasReceivedSubsidy = basicVehicle.vin
? await this.energyFundsClientService.checkVehicleSubsidyAvilability(
auth,
vehicle.vin,
basicVehicle.vin,
)
: false

Expand All @@ -96,11 +101,11 @@ export class EnergyFundsService {
hasReceivedSubsidy,
permno: vehicle.permno,
make: vehicle.make,
color: vehicle.color,
color: vehicle.colorName,
requireMileage: vehicle.requiresMileageRegistration,
newRegistrationDate: vehicle.newregdate,
firstRegistrationDate: vehicle.firstregdate,
vin: vehicle.vin,
newRegistrationDate: basicVehicle.newregdate,
firstRegistrationDate: basicVehicle.firstregdate,
vin: basicVehicle.vin,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from './graphql/models'
import { ApolloError } from 'apollo-server-express'
import { CoOwnerChangeAnswers } from './graphql/dto/coOwnerChangeAnswers.input'
import { MileageReadingApi } from '@island.is/clients/vehicles-mileage'

@Injectable()
export class TransportAuthorityApi {
Expand All @@ -36,12 +37,17 @@ export class TransportAuthorityApi {
private readonly vehiclePlateRenewalClient: VehiclePlateRenewalClient,
private readonly vehicleServiceFjsV1Client: VehicleServiceFjsV1Client,
private readonly vehiclesApi: VehicleSearchApi,
private readonly mileageReadingApi: MileageReadingApi,
) {}

private vehiclesApiWithAuth(auth: Auth) {
return this.vehiclesApi.withMiddleware(new AuthMiddleware(auth))
}

private mileageReadingApiWithAuth(auth: Auth) {
return this.mileageReadingApi.withMiddleware(new AuthMiddleware(auth))
}

async checkTachoNet(
user: User,
input: CheckTachoNetInput,
Expand All @@ -55,25 +61,29 @@ export class TransportAuthorityApi {
return { exists: hasActiveCard }
}

private isOwnerOrCoOwner(
vehicle: BasicVehicleInformationDto,
auth: Auth,
): boolean {
if (vehicle.owners) {
for (const owner of vehicle.owners) {
if (owner.persidno === auth.nationalId) {
return true
}
if (owner.coOwners) {
for (const coOwner of owner.coOwners) {
if (coOwner.persidno === auth.nationalId) {
return true
}
}
}
}
private async fetchVehicleDataForOwnerCoOwner(auth: User, permno: string) {
const result = await this.vehiclesApiWithAuth(
auth,
).currentvehicleswithmileageandinspGet({
permno: permno,
showOwned: true,
showCoowned: true,
showOperated: false,
})
if (!result || !result.data || result.data.length === 0) {
throw Error(
'Did not find the vehicle with that permno, or you are neither owner nor co-owner of the vehicle',
)
}
return false

const vehicle = result.data[0]

// Get mileage reading
const mileageReadings = await this.mileageReadingApiWithAuth(
auth,
).getMileageReading({ permno: permno })

return { vehicle, mileageReadings }
}

async getVehicleOwnerchangeChecksByPermno(
Expand All @@ -82,19 +92,8 @@ export class TransportAuthorityApi {
): Promise<VehicleOwnerchangeChecksByPermno | null | ApolloError> {
// Make sure user is only fetching information for vehicles where he is either owner or co-owner
// (mainly debt status info that is sensitive)
const vehicle = await this.vehiclesApiWithAuth(
auth,
).basicVehicleInformationGet({ permno: permno })
if (!vehicle) {
throw Error(
'Did not find the vehicle with for that permno, or you are neither owner nor co-owner of the vehicle',
)
}
if (!this.isOwnerOrCoOwner(vehicle, auth)) {
throw Error(
'Did not find the vehicle with for that permno, or you are neither owner nor co-owner of the vehicle',
)
}
const { vehicle, mileageReadings } =
await this.fetchVehicleDataForOwnerCoOwner(auth, permno)

// Get debt status
const debtStatus =
Expand All @@ -106,18 +105,19 @@ export class TransportAuthorityApi {
auth,
permno,
)

return {
isDebtLess: debtStatus.isDebtLess,
validationErrorMessages: ownerChangeValidation?.hasError
? ownerChangeValidation.errorMessages
: null,
basicVehicleInformation: {
permno: vehicle.permno,
make: `${vehicle.make} ${vehicle.vehcom}`,
color: vehicle.color,
make: vehicle.make,
color: vehicle.colorName,
requireMileage: vehicle.requiresMileageRegistration,
mileageReading: vehicle?.mileageReadings?.[0]?.mileage || '',
mileageReading: (mileageReadings?.[0]?.mileage ?? '').toString(),
johannaagma marked this conversation as resolved.
Show resolved Hide resolved
},
isDebtLess: debtStatus.isDebtLess,
validationErrorMessages: ownerChangeValidation?.hasError
? ownerChangeValidation.errorMessages
: null,
}
}

Expand Down Expand Up @@ -258,19 +258,8 @@ export class TransportAuthorityApi {
): Promise<VehicleOperatorChangeChecksByPermno | null | ApolloError> {
// Make sure user is only fetching information for vehicles where he is either owner or co-owner
// (mainly debt status info that is sensitive)
const vehicle = await this.vehiclesApiWithAuth(
auth,
).basicVehicleInformationGet({ permno: permno })
if (!vehicle) {
throw Error(
'Did not find the vehicle with for that permno, or you are neither owner nor co-owner of the vehicle',
)
}
if (!this.isOwnerOrCoOwner(vehicle, auth)) {
throw Error(
'Did not find the vehicle with for that permno, or you are neither owner nor co-owner of the vehicle',
)
}
const { vehicle, mileageReadings } =
await this.fetchVehicleDataForOwnerCoOwner(auth, permno)

// Get debt status
const debtStatus =
Expand All @@ -289,11 +278,11 @@ export class TransportAuthorityApi {
? operatorChangeValidation.errorMessages
: null,
basicVehicleInformation: {
color: vehicle.color,
make: `${vehicle.make} ${vehicle.vehcom}`,
color: vehicle.colorName,
make: vehicle.make,
permno: vehicle.permno,
requireMileage: vehicle.requiresMileageRegistration,
mileageReading: vehicle?.mileageReadings?.[0]?.mileage || '',
mileageReading: (mileageReadings?.[0]?.mileage ?? '').toString(),
},
}
}
Expand Down Expand Up @@ -373,8 +362,6 @@ export class TransportAuthorityApi {
color: vehicleInfo.color,
make: `${vehicleInfo.make} ${vehicleInfo.vehcom}`,
permno: vehicleInfo.permno,
requireMileage: vehicleInfo.requiresMileageRegistration,
mileageReading: vehicleInfo?.mileageReadings?.[0]?.mileage || '',
},
}
}
Expand Down
Loading
Loading