Skip to content

Commit

Permalink
Use company registry api to get company name on custom delegations.
Browse files Browse the repository at this point in the history
  • Loading branch information
saevarma committed Jun 11, 2024
1 parent c1b6e41 commit 13d2445
Showing 1 changed file with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
IndividualDto,
NationalRegistryClientService,
} from '@island.is/clients/national-registry-v2'
import {
CompanyExtendedInfo,
CompanyRegistryClientService,
} from '@island.is/clients/rsk/company-registry'
import { LOGGER_PROVIDER } from '@island.is/logging'
import { AuditService } from '@island.is/nest/audit'
import { isDefined } from '@island.is/shared/utils'
Expand All @@ -31,6 +35,11 @@ type FindAllValidIncomingOptions = {
domainName?: string
}

type FromNameInfo = {
nationalId: string
name: string
}

/**
* Service class for incoming delegations.
* This class supports domain based delegations.
Expand All @@ -43,6 +52,7 @@ export class DelegationsIncomingCustomService {
@InjectModel(ApiScopeUserAccess)
private apiScopeUserAccessModel: typeof ApiScopeUserAccess,
private nationalRegistryClient: NationalRegistryClientService,
private companyRegistryClient: CompanyRegistryClientService,
@Inject(LOGGER_PROVIDER)
private logger: Logger,
private auditService: AuditService,
Expand Down Expand Up @@ -147,7 +157,7 @@ export class DelegationsIncomingCustomService {
validity: DelegationValidity
},
useMaster = false,
): Promise<{ delegations: Delegation[]; fromNameInfo: IndividualDto[] }> {
): Promise<{ delegations: Delegation[]; fromNameInfo: FromNameInfo[] }> {
let whereOptions = getScopeValidityWhereClause(validity)
if (domainName) whereOptions = { ...whereOptions, domainName: domainName }

Expand Down Expand Up @@ -242,7 +252,7 @@ export class DelegationsIncomingCustomService {
): Promise<{
aliveDelegations: Delegation[]
deceasedDelegations: Delegation[]
fromNameInfo: IndividualDto[]
fromNameInfo: FromNameInfo[]
}> {
if (delegations.length === 0) {
return {
Expand All @@ -254,18 +264,27 @@ export class DelegationsIncomingCustomService {

const delegationsPromises = delegations.map(({ fromNationalId }) =>
kennitala.isCompany(fromNationalId)
? null
? this.companyRegistryClient
.getCompany(fromNationalId)
.catch(this.handlerGetError)
: this.nationalRegistryClient
.getIndividual(fromNationalId)
.catch(this.handlerGetIndividualError),
.catch(this.handlerGetError),
)

try {
// Check if delegations is linked to a person, i.e. not deceased
const persons = await Promise.all(delegationsPromises)
const personsValuesNoError = persons
const identities = await Promise.all(delegationsPromises)
const identitiesValuesNoError = identities
.filter(this.isNotError)
.filter(isDefined)
.map((identity) => ({
nationalId: identity.nationalId,
name:
(kennitala.isCompany(identity.nationalId)
? (identity as CompanyExtendedInfo).name
: (identity as IndividualDto).fullName) ?? UNKNOWN_NAME,
}))

// Divide delegations into alive or deceased delegations.
const [aliveDelegations, deceasedDelegations] = partitionWithIndex(
Expand All @@ -274,15 +293,15 @@ export class DelegationsIncomingCustomService {
// All companies will be divided into aliveDelegations
kennitala.isCompany(fromNationalId) ||
// Pass through altough Þjóðskrá API throws an error since it is not required to view the delegation.
persons[index] instanceof Error ||
identities[index] instanceof Error ||
// Make sure we can match the person to the delegation, i.e. not deceased
(persons[index] as IndividualDto)?.nationalId === fromNationalId,
(identities[index] as IndividualDto)?.nationalId === fromNationalId,
)

return {
aliveDelegations,
deceasedDelegations,
fromNameInfo: personsValuesNoError,
fromNameInfo: identitiesValuesNoError,
}
} catch (error) {
this.logger.error(
Expand All @@ -302,7 +321,7 @@ export class DelegationsIncomingCustomService {
}
}

private handlerGetIndividualError(error: null | Error) {
private handlerGetError(error: null | Error) {
return error
}

Expand All @@ -317,10 +336,10 @@ export class DelegationsIncomingCustomService {
* Finds person by nationalId.
*/
private getPersonByNationalId(
persons: Array<IndividualDto | null>,
identities: Array<FromNameInfo | null>,
nationalId: string,
) {
return persons.find((person) => person?.nationalId === nationalId)
return identities.find((identity) => identity?.nationalId === nationalId)
}

private async findAccessControlList(
Expand Down

0 comments on commit 13d2445

Please sign in to comment.