Skip to content

Commit

Permalink
fix pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
GunnlaugurG committed Sep 24, 2024
1 parent 838df23 commit 7911fc9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
CreatePaperDelegationDto,
Delegation,
DELEGATION_TAG,
DelegationDelegationType,
DelegationsIndexService,
SequelizeConfigService,
ZENDESK_CUSTOM_FIELDS,
Expand Down Expand Up @@ -172,10 +173,19 @@ describe('DelegationAdmin - With authentication', () => {
describe('POST /delegation-admin', () => {
const toNationalId = '0101302399'
const fromNationalId = '0101307789'

let zendeskServiceApiSpy: jest.SpyInstance
let nationalRegistryApiSpy: jest.SpyInstance

let delegationModel: typeof Delegation
let delegationDelegationTypeModel: typeof DelegationDelegationType

beforeEach(async () => {
delegationModel = await app.get(getModelToken(Delegation))
delegationDelegationTypeModel = await app.get(
getModelToken(DelegationDelegationType),
)

await factory.createDomain({
name: 'd1',
apiScopes: [
Expand Down Expand Up @@ -287,6 +297,18 @@ describe('DelegationAdmin - With authentication', () => {
expect(res.body.toNationalId).toEqual(toNationalId)
expect(res.body.referenceId).toEqual(delegation.referenceId)
expect(res.body).not.toHaveProperty('validTo')

// Assert db
const createdDelegation = await delegationModel.findByPk(res.body.id)
const createdDelegationDelegationType =
await delegationDelegationTypeModel.findOne({
where: {
delegationId: res.body.id,
},
})

expect(createdDelegation).not.toBeNull()
expect(createdDelegationDelegationType).not.toBeNull()
})

it('POST /delegation-admin should not create delegation with company national id', async () => {
Expand Down
1 change: 0 additions & 1 deletion apps/services/auth/admin-api/src/openApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const openApi = new DocumentBuilder()
tokenUrl: `${environment.auth.issuer}/connect/token`,
scopes: {
openid: 'Default openid scope',
[AuthScope.delegations]: 'Access to manage delegations.',
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions libs/auth-api-lib/sequelize.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-env node */
module.exports = {
development: {
username: process.env.DB_USER ?? 'dev_db',
password: process.env.DB_PASS ?? 'dev_db',
database: process.env.DB_NAME ?? 'dev_db',
username: process.env.DB_USER_AUTH_DB ?? 'dev_db',
password: process.env.DB_PASS_AUTH_DB ?? 'dev_db',
database: process.env.DB_USER_AUTH_DB ?? 'dev_db',
host: 'localhost',
dialect: 'postgres',
port: process.env.DB_PORT ?? 5433,
port: process.env.DB_PORT_AUTH_DB ?? 5433,
seederStorage: 'sequelize',
},
test: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,19 @@ export class DelegationAdminCustomService {
async getAllDelegationsByNationalId(
nationalId: string,
): Promise<DelegationAdminCustomDto> {
const incomingDelegationPromises = []

incomingDelegationPromises.push(
const [
incomingDelegations,
generalIncomingDelegations,
outgoingDelegations,
generalOutgoingDelegations,
] = await Promise.all([
this.delegationsIncomingCustomService.findAllValidIncoming({
nationalId: nationalId,
validity: DelegationValidity.ALL,
}),
)

incomingDelegationPromises.push(
this.delegationsIncomingCustomService.findAllValidGeneralMandate({
nationalId: nationalId,
}),
)

const [
incomingDelegationSets,
outgoingDelegations,
generalOutgoingDelegations,
] = await Promise.all([
await Promise.all(incomingDelegationPromises),
this.delegationModel.findAll({
where: {
fromNationalId: nationalId,
Expand Down Expand Up @@ -137,13 +129,13 @@ export class DelegationAdminCustomService {
])

return {
incoming: ([] as DelegationDTO[]).concat(...incomingDelegationSets),
incoming: [...incomingDelegations, ...generalIncomingDelegations],
outgoing: [
generalOutgoingDelegations.map((d) =>
...generalOutgoingDelegations.map((d) =>
d.toDTO(AuthDelegationType.GeneralMandate),
),
outgoingDelegations.map((delegation) => delegation.toDTO()),
].flat(),
...outgoingDelegations.map((delegation) => delegation.toDTO()),
],
}
}

Expand All @@ -156,26 +148,26 @@ export class DelegationAdminCustomService {
delegation.fromNationalId,
)

const zenDeskCase = await this.zendeskService.getTicket(
const zendeskCase = await this.zendeskService.getTicket(
delegation.referenceId,
)

if (!zenDeskCase.tags.includes(DELEGATION_TAG)) {
throw new Error('Zendesk ticket is missing required tag')
if (!zendeskCase.tags.includes(DELEGATION_TAG)) {
throw new BadRequestException('Zendesk ticket is missing required tag')
}

if (zenDeskCase.status !== TicketStatus.Solved) {
throw new Error('Zendesk case is not solved')
if (zendeskCase.status !== TicketStatus.Solved) {
throw new BadRequestException('Zendesk case is not solved')
}

const { fromReferenceId, toReferenceId } =
this.getNationalIdsFromZendeskTicket(zenDeskCase)
this.getNationalIdsFromZendeskTicket(zendeskCase)

if (
fromReferenceId !== delegation.fromNationalId ||
toReferenceId !== delegation.toNationalId
) {
throw new Error(
throw new BadRequestException(
'Zendesk ticket nationalIds does not match delegation nationalIds',
)
}
Expand All @@ -185,42 +177,33 @@ export class DelegationAdminCustomService {
this.namesService.getPersonName(delegation.toNationalId),
])

return this.sequelize.transaction(async (transaction) => {
const newDelegation = await this.delegationModel.create(
{
id: uuid(),
toNationalId: delegation.toNationalId,
fromNationalId: delegation.fromNationalId,
createdByNationalId: user.actor?.nationalId ?? user.nationalId,
referenceId: delegation.referenceId,
toName,
fromDisplayName,
},
{
transaction,
},
)

const ddt = await this.delegationDelegationTypeModel.create(
{
delegationId: newDelegation.id,
delegationTypeId: AuthDelegationType.GeneralMandate,
validTo: delegation.validTo,
},
{
transaction,
},
)

newDelegation.delegationDelegationTypes = [ddt]
const newDelegation = await this.delegationModel.create(
{
id: uuid(),
toNationalId: delegation.toNationalId,
fromNationalId: delegation.fromNationalId,
createdByNationalId: user.actor?.nationalId ?? user.nationalId,
referenceId: delegation.referenceId,
toName,
fromDisplayName,
delegationDelegationTypes: [
{
delegationTypeId: AuthDelegationType.GeneralMandate,
validTo: delegation.validTo,
},
] as DelegationDelegationType[],
},
{
include: [this.delegationDelegationTypeModel],
},
)

// Index custom delegations for the toNationalId
void this.delegationIndexService.indexCustomDelegations(
delegation.toNationalId,
)
// Index custom delegations for the toNationalId
void this.delegationIndexService.indexCustomDelegations(
delegation.toNationalId,
)

return newDelegation.toDTO(AuthDelegationType.GeneralMandate)
})
return newDelegation.toDTO(AuthDelegationType.GeneralMandate)
}

async deleteDelegation(user: User, delegationId: string): Promise<void> {
Expand Down Expand Up @@ -260,13 +243,6 @@ export class DelegationAdminCustomService {
})
}

await this.delegationDelegationTypeModel.destroy({
transaction,
where: {
delegationId,
},
})

// Index custom delegations for the toNationalId
void this.delegationIndexService.indexCustomDelegations(
delegation.toNationalId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
AuthDelegationType,
} from '@island.is/shared/types'
import { DelegationDelegationType } from './delegation-delegation-type.model'
import { isDefined } from '@island.is/shared/utils'

@Table({
tableName: 'delegation',
Expand Down Expand Up @@ -102,7 +103,7 @@ export class Delegation extends Model<
) {
const dates = this.delegationDelegationTypes
.map((x) => x.validTo)
.filter((x) => x !== null) as Array<Date>
.filter((x) => isDefined(x)) as Array<Date>
return max(dates)
}

Expand All @@ -114,7 +115,7 @@ export class Delegation extends Model<

// 2. Find items with value in the array
const dates = (this.delegationScopes
?.filter((x) => x.validTo !== null && x.validTo !== undefined)
?.filter((x) => isDefined(x.validTo))
.map((x) => x.validTo) || []) as Array<Date>

// Return the max value
Expand Down

0 comments on commit 7911fc9

Please sign in to comment.