Skip to content

Commit

Permalink
feat(auth-admin): Check if delegation already exists. (#16420)
Browse files Browse the repository at this point in the history
* handle delegation already exists

* fix error messages

* minor tweaks

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
GunnlaugurG and kodiakhq[bot] authored Oct 17, 2024
1 parent 17e1369 commit d1c22d8
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { getModelToken } from '@nestjs/sequelize'
import { faker } from '@island.is/shared/mocking'
import { TicketStatus, ZendeskService } from '@island.is/clients/zendesk'
import { NationalRegistryClientService } from '@island.is/clients/national-registry-v2'
import { ErrorCodes } from '@island.is/shared/utils'

const currentUser = createCurrentUser({
scope: [DelegationAdminScopes.read, DelegationAdminScopes.admin],
Expand Down Expand Up @@ -339,6 +340,85 @@ describe('DelegationAdmin - With authentication', () => {
expect(res.status).toEqual(400)
})

it('POST /delegation-admin should not create delegation since it already exists', async () => {
// Arrange
const { toNationalId, fromNationalId } = {
toNationalId: '0101302399',
fromNationalId: '0101307789',
}

mockZendeskService(toNationalId, fromNationalId)

const existingDelegation = await factory.createCustomDelegation({
fromNationalId,
toNationalId,
domainName: null,
scopes: [{ scopeName: 's1' }],
referenceId: 'ref1',
})

const delegation: CreatePaperDelegationDto = {
toNationalId: existingDelegation.toNationalId,
fromNationalId: existingDelegation.fromNationalId,
referenceId: 'ref2',
}

// Act
const res = await getRequestMethod(
server,
'POST',
)('/delegation-admin').send(delegation)

// Assert
expect(res.status).toEqual(400)
expect(res.body).toMatchObject({
status: 400,
type: 'https://httpstatuses.org/400',
title: ErrorCodes.COULD_NOT_CREATE_DELEGATION,
detail: 'Could not create delegation',
})
})

it('POST /delegation-admin should not create delegation since the delegation id already exists', async () => {
// Arrange
const { toNationalId, fromNationalId } = {
toNationalId: '0101302399',
fromNationalId: '0101307789',
}

mockZendeskService(fromNationalId, toNationalId)

const existingDelegation = await factory.createCustomDelegation({
fromNationalId,
toNationalId,
domainName: null,
scopes: [{ scopeName: 's1' }],
referenceId: 'ref1',
})

// Send in opposite national ids so they will not exist in db
const delegation: CreatePaperDelegationDto = {
toNationalId: existingDelegation.fromNationalId,
fromNationalId: existingDelegation.toNationalId,
referenceId: 'ref1',
}

// Act
const res = await getRequestMethod(
server,
'POST',
)('/delegation-admin').send(delegation)

// Assert
expect(res.status).toEqual(400)
expect(res.body).toMatchObject({
status: 400,
type: 'https://httpstatuses.org/400',
title: ErrorCodes.REFERENCE_ID_ALREADY_EXISTS,
detail: 'Delegation with the same reference id already exists',
})
})

it('POST /delegation-admin should not create delegation with incorrect zendesk ticket status', async () => {
// Arrange
mockZendeskService(toNationalId, fromNationalId, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { DelegationDelegationType } from '../models/delegation-delegation-type.m
import { DelegationsIncomingCustomService } from '../delegations-incoming-custom.service'
import { DelegationValidity } from '../types/delegationValidity'
import { ErrorCodes } from '@island.is/shared/utils'
import { Op } from 'sequelize'

@Injectable()
export class DelegationAdminCustomService {
Expand Down Expand Up @@ -303,6 +304,36 @@ export class DelegationAdminCustomService {
this.namesService.getPersonName(delegation.toNationalId),
])

const existingDelegation = await this.delegationModel.findOne({
where: {
[Op.or]: [
{
fromNationalId: delegation.fromNationalId,
toNationalId: delegation.toNationalId,
},
{
referenceId: delegation.referenceId,
},
],
},
})

if (existingDelegation) {
// Throw error if reference ID already exists in db.
if (existingDelegation.referenceId === delegation.referenceId) {
throw new BadRequestException({
message: 'Delegation with the same reference id already exists',
error: ErrorCodes.REFERENCE_ID_ALREADY_EXISTS,
})
} else {
// Throw generic error if delegation already exists.
throw new BadRequestException({
message: 'Could not create delegation',
error: ErrorCodes.COULD_NOT_CREATE_DELEGATION,
})
}
}

return this.sequelize.transaction(async (transaction) => {
return this.delegationModel.create(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class Delegation extends Model<
allowNull: true,
})
@ForeignKey(() => Domain)
domainName?: string
domainName?: string | null

/**
* ReferenceId is a field for storing a reference to the zendesk ticket id
Expand Down
2 changes: 2 additions & 0 deletions libs/portals/admin/delegation-admin/src/constants/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ export const FORM_ERRORS: Record<ErrorCodes, MessageDescriptor> = {
[ErrorCodes.INPUT_VALIDATION_SAME_NATIONAL_ID]: m.sameNationalIdError,
[ErrorCodes.INPUT_VALIDATION_INVALID_PERSON]: m.validPersonError,
[ErrorCodes.INVALID_DATE_FORMAT]: m.invalidDateFormatError,
[ErrorCodes.COULD_NOT_CREATE_DELEGATION]: m.couldNotCreateDelegationError,
[ErrorCodes.REFERENCE_ID_ALREADY_EXISTS]: m.referenceIdAlreadyExistsError,
}
8 changes: 8 additions & 0 deletions libs/portals/admin/delegation-admin/src/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,12 @@ export const m = defineMessages({
id: 'admin.delegationAdmin:invalidDateFormatError',
defaultMessage: 'Dagsetning er ekki á réttu sniði',
},
couldNotCreateDelegationError: {
id: 'admin.delegationAdmin:couldNotCreateDelegationError',
defaultMessage: 'Ekki tókst að skrá umboð',
},
referenceIdAlreadyExistsError: {
id: 'admin.delegationAdmin:referenceIdAlreadyExistsError',
defaultMessage: 'Númer máls í Zendesk er þegar til',
},
})
9 changes: 6 additions & 3 deletions libs/services/auth/testing/src/fixtures/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ export type CreateCustomDelegationScope = Optional<
export type CreateCustomDelegation = Optional<
Pick<
DelegationDTO,
'toNationalId' | 'fromNationalId' | 'fromName' | 'referenceId'
| 'toNationalId'
| 'fromNationalId'
| 'fromName'
| 'referenceId'
| 'domainName'
>,
'toNationalId' | 'fromNationalId' | 'fromName' | 'referenceId'
'toNationalId' | 'fromNationalId' | 'fromName' | 'referenceId' | 'domainName'
> & {
domainName: string
scopes?: CreateCustomDelegationScope[]
}

Expand Down
2 changes: 2 additions & 0 deletions libs/shared/utils/src/lib/errorCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export enum ErrorCodes {
INPUT_VALIDATION_SAME_NATIONAL_ID = 'INPUT_VALIDATION_SAME_NATIONAL_ID',
INPUT_VALIDATION_INVALID_PERSON = 'INPUT_VALIDATION_INVALID_PERSON',
INVALID_DATE_FORMAT = 'INVALID_DATE_FORMAT',
COULD_NOT_CREATE_DELEGATION = 'COULD_NOT_CREATE_DELEGATION',
REFERENCE_ID_ALREADY_EXISTS = 'REFERENCE_ID_ALREADY_EXISTS',
}

0 comments on commit d1c22d8

Please sign in to comment.