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

feat(auth-admin): Delete delegation UI #16073

Merged
merged 6 commits into from
Sep 20, 2024
Merged
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 @@ -7,6 +7,7 @@ import {
Post,
UseGuards,
Delete,
Query,
} from '@nestjs/common'
import { ApiSecurity, ApiTags } from '@nestjs/swagger'

Expand Down Expand Up @@ -74,7 +75,7 @@ export class MeClientsController {
@CurrentUser() user: User,
@Param('tenantId') tenantId: string,
@Param('clientId') clientId: string,
@Param('includeArchived') includeArchived?: boolean,
@Query('includeArchived') includeArchived?: boolean,
): Promise<AdminClientDto> {
return this.clientsService.findByTenantIdAndClientId(
tenantId,
Expand Down
20 changes: 20 additions & 0 deletions apps/services/auth/admin-api/src/openApi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import { DocumentBuilder } from '@nestjs/swagger'
import { environment } from './environments'
import { AuthScope } from '@island.is/auth/scopes'

export const openApi = new DocumentBuilder()
.setTitle('IdentityServer Admin api')
.setDescription('Api for administration.')
.setVersion('2.0')
.addTag('auth-admin-api')
.addOAuth2(
{
type: 'oauth2',
description:
'Authentication and authorization using island.is authentication service (IAS).',
flows: {
authorizationCode: {
authorizationUrl: `${environment.auth.issuer}/connect/authorize`,
tokenUrl: `${environment.auth.issuer}/connect/token`,
scopes: {
openid: 'Default openid scope',
[AuthScope.delegations]: 'Access to manage delegations.',
},
},
},
},
'ias',
)
.build()
3 changes: 3 additions & 0 deletions libs/api/domains/auth/src/lib/models/delegation.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export abstract class Delegation {

@Field(() => AuthDelegationProvider)
provider!: AuthDelegationProvider

@Field(() => String, { nullable: true })
referenceId?: string
}

@ObjectType('AuthLegalGuardianDelegation', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import { Sequelize } from 'sequelize-typescript'
import { CreatePaperDelegationDto } from '../dto/create-paper-delegation.dto'
import { DelegationDTO } from '../dto/delegation.dto'
import { NamesService } from '../names.service'
import { TicketStatus, ZendeskService } from '@island.is/clients/zendesk'
import {
Ticket,
TicketStatus,
ZendeskService,
} from '@island.is/clients/zendesk'
import { DELEGATION_TAG, ZENDESK_CUSTOM_FIELDS } from '../constants/zendesk'

@Injectable()
export class DelegationAdminCustomService {
Expand All @@ -31,13 +36,24 @@ export class DelegationAdminCustomService {
private sequelize: Sequelize,
) {}

private async getZendeskStatus(id: string): Promise<TicketStatus | string> {
try {
const zenDeskCase = await this.zendeskService.getTicket(id)
private getNationalIdsFromZendeskTicket(ticket: Ticket): {
fromReferenceId: string
toReferenceId: string
} {
const fromReferenceId = ticket.custom_fields.find(
(field) => field.id === ZENDESK_CUSTOM_FIELDS.DelegationFromReferenceId,
)
const toReferenceId = ticket.custom_fields.find(
(field) => field.id === ZENDESK_CUSTOM_FIELDS.DelegationToReferenceId,
)

if (!fromReferenceId || !toReferenceId) {
throw new Error('Zendesk ticket is missing required custom fields')
}

return zenDeskCase.status
} catch (error) {
throw new Error('Error checking zendesk status')
return {
fromReferenceId: fromReferenceId.value,
toReferenceId: toReferenceId.value,
}
}

Expand Down Expand Up @@ -120,12 +136,30 @@ export class DelegationAdminCustomService {
if (delegation.fromNationalId === delegation.toNationalId)
throw new Error('Cannot create a delegation between the same nationalId.')

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

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

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

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

if (
fromReferenceId !== delegation.fromNationalId ||
toReferenceId !== delegation.toNationalId
) {
throw new Error(
'Zendesk ticket nationalIds does not match delegation nationalIds',
)
}

const [fromDisplayName, toName] = await Promise.all([
this.namesService.getPersonName(delegation.fromNationalId),
this.namesService.getPersonName(delegation.toNationalId),
Expand Down
5 changes: 5 additions & 0 deletions libs/auth-api-lib/src/lib/delegations/constants/zendesk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const DELEGATION_TAG = 'umsokn_um_umboð_a_mínum_síðum'
export const ZENDESK_CUSTOM_FIELDS = {
DelegationFromReferenceId: 21401464004498,
DelegationToReferenceId: 21401435545234,
}
2 changes: 2 additions & 0 deletions libs/clients/zendesk/src/lib/zendesk.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export type User = {
export type Ticket = {
id: number
status: TicketStatus | string
custom_fields: Array<{ id: number; value: string }>
tags: Array<string>
}

export interface ZendeskServiceOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
import { AuthCustomDelegation } from '@island.is/api/schema'
import { Box, Stack } from '@island.is/island-ui/core'
import { AccessCard } from '@island.is/portals/shared-modules/delegations'
import { useDeleteCustomDelegationAdminMutation } from '../screens/DelegationAdminDetails/DelegationAdmin.generated'
import { useRevalidator } from 'react-router-dom'

interface DelegationProps {
direction: 'incoming' | 'outgoing'
delegationsList: AuthCustomDelegation[]
}

const DelegationList = ({ delegationsList, direction }: DelegationProps) => {
const [deleteCustomDelegationAdminMutation] =
useDeleteCustomDelegationAdminMutation()
const { revalidate } = useRevalidator()

return (
<Box marginTop={2}>
<Stack space={3}>
{delegationsList.map((delegation) => (
<AccessCard
canModify={false}
canModify={direction === 'outgoing' && !!delegation.referenceId} // only allow deletion of paper delegations
direction={direction}
key={delegation.id}
delegation={delegation}
onDelete={() => {
console.warn('Delete delegation')
onDelete={async () => {
const { data } = await deleteCustomDelegationAdminMutation({
variables: {
id: delegation.id as string,
},
})
if (data) {
revalidate()
}
}}
/>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ query getCustomDelegationsAdmin($nationalId: String!) {
incoming {
id
validTo
referenceId
domain {
name
organisationLogoKey
Expand All @@ -31,6 +32,7 @@ query getCustomDelegationsAdmin($nationalId: String!) {
outgoing {
id
validTo
referenceId
domain {
name
organisationLogoKey
Expand All @@ -56,3 +58,7 @@ query getCustomDelegationsAdmin($nationalId: String!) {
}
}
}

mutation deleteCustomDelegationAdmin($id: String!) {
authDeleteAdminDelegation(id: $id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { m as coreMessages } from '@island.is/portals/core'
import uniqBy from 'lodash/uniqBy'
import sortBy from 'lodash/sortBy'
import { m } from '../../lib/messages'
import { DelegationPaths } from '../../lib/paths'
import { AuthApiScope, AuthDelegationType } from '@island.is/api/schema'
import {
AuthCustomDelegation,
Expand Down Expand Up @@ -57,6 +56,7 @@ interface AccessCardProps {
direction?: 'incoming' | 'outgoing'

canModify?: boolean
href?: string
}

export const AccessCard = ({
Expand All @@ -66,6 +66,7 @@ export const AccessCard = ({
variant = 'outgoing',
direction = 'outgoing',
canModify = true,
href,
}: AccessCardProps) => {
const { formatMessage } = useLocale()
const navigate = useNavigate()
Expand All @@ -74,7 +75,6 @@ export const AccessCard = ({

const hasTags = tags.length > 0
const isOutgoing = variant === 'outgoing'
const href = `${DelegationPaths.Delegations}/${delegation.id}`

const isExpired = useMemo(() => {
if (delegation.validTo) {
Expand Down Expand Up @@ -282,7 +282,7 @@ export const AccessCard = ({
>
{formatMessage(coreMessages.view)}
</Button>
) : !isExpired ? (
) : !isExpired && href ? (
<Button
icon="pencil"
iconType="outline"
Expand All @@ -292,7 +292,7 @@ export const AccessCard = ({
>
{formatMessage(coreMessages.buttonEdit)}
</Button>
) : (
) : href ? (
<Button
icon="reload"
iconType="outline"
Expand All @@ -302,7 +302,7 @@ export const AccessCard = ({
>
{formatMessage(coreMessages.buttonRenew)}
</Button>
)}
) : null}
</Box>
</Box>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { DelegationsEmptyState } from '../DelegationsEmptyState'
import { DelegationIncomingModal } from './DelegationIncomingModal/DelegationIncomingModal'
import { useAuthDelegationsIncomingQuery } from './DelegationIncomingModal/DelegationIncomingModal.generated'
import { AuthCustomDelegationIncoming } from '../../../types/customDelegation'
import { DelegationPaths } from '../../../lib/paths'

export const DelegationsIncoming = () => {
const { formatMessage, lang = 'is' } = useLocale()
Expand Down Expand Up @@ -78,6 +79,7 @@ export const DelegationsIncoming = () => {
}}
direction="incoming"
variant="incoming"
href={`${DelegationPaths.Delegations}/${delegation.id}`}
/>
),
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useAuthDelegationsOutgoingQuery } from './DelegationsOutgoing.generated
import { AuthCustomDelegationOutgoing } from '../../../types/customDelegation'
import { ALL_DOMAINS } from '../../../constants/domain'
import { m } from '../../../lib/messages'
import { DelegationPaths } from '../../../lib/paths'

const prepareDomainName = (domainName: string | null) =>
domainName === ALL_DOMAINS ? null : domainName
Expand Down Expand Up @@ -114,6 +115,7 @@ export const DelegationsOutgoing = () => {
)
}}
variant="outgoing"
href={`${DelegationPaths.Delegations}/${delegation.id}`}
/>
),
)}
Expand Down
Loading