Skip to content

Commit

Permalink
on delete
Browse files Browse the repository at this point in the history
  • Loading branch information
obmagnusson committed Oct 29, 2024
1 parent f799ad2 commit 99090a5
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,7 @@ export class ApplicationController {
async delete(
@Param('id', new ParseUUIDPipe()) id: string,
@CurrentUser() user: User,
@CurrentLocale() locale: Locale,
) {
const { nationalId } = user
const existingApplication =
Expand All @@ -1097,19 +1098,75 @@ export class ApplicationController {
)
}

// delete charge in FJS
await this.applicationChargeService.deleteCharge(existingApplication)
const template = await getApplicationTemplateByTypeId(
existingApplication.typeId,
)
if (template === null) {
throw new BadRequestException(
`No application template exists for type: ${existingApplication.typeId}`,
)
}

let onDeleteActions = new ApplicationTemplateHelper(
existingApplication,
template,
).getOnDeleteStateAPIAction()
if (onDeleteActions) {
const namespaces = await getApplicationTranslationNamespaces(
existingApplication,
)
if (!Array.isArray(onDeleteActions)) {
onDeleteActions = [onDeleteActions]
}

const intl = await this.intlService.useIntl(namespaces, locale)
const deletingApplication = await this.templateApiActionRunner.run(
existingApplication,
onDeleteActions,
user,
locale,
intl.formatMessage,
)

for (const api of onDeleteActions) {
const result =
deletingApplication.externalData[api.externalDataId || api.action]

this.logger.debug(
`Performing action ${api.action} on ${JSON.stringify(
template.name,
)} ended with ${result.status}`,
)

// delete the entry in Payment table to prevent FK error
await this.paymentService.delete(existingApplication.id, user)
if (result.status === 'failure' && api.throwOnError) {
const reason = result.reason ?? 'Unknown error'
throw new TemplateApiError(reason, 500)
}
}

await this.fileService.deleteAttachmentsForApplication(existingApplication)
// delete charge in FJS
await this.applicationChargeService.deleteCharge(existingApplication)

// delete history for application
await this.historyService.deleteHistoryByApplicationId(
existingApplication.id,
)
// delete the entry in Payment table to prevent FK error
await this.paymentService.delete(existingApplication.id, user)

await this.applicationService.delete(existingApplication.id)
await this.fileService.deleteAttachmentsForApplication(
existingApplication,
)

// delete history for application
await this.historyService.deleteHistoryByApplicationId(
existingApplication.id,
)

await this.applicationService.delete(existingApplication.id)

this.auditService.audit({
auth: user,
action: 'delete',
resources: existingApplication.id,
meta: { type: existingApplication.typeId },
})
}
}
}
9 changes: 9 additions & 0 deletions libs/application/core/src/lib/ApplicationTemplateHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ export class ApplicationTemplateHelper<
return this.getTemplateAPIAction(action)
}

getOnDeleteStateAPIAction(
stateKey: string = this.application.state,
): TemplateApi | TemplateApi[] | null {
const action =
this.template.stateMachineConfig.states[stateKey]?.meta?.onDelete ?? null

return this.getTemplateAPIAction(action)
}

getApplicationStateInformation(
stateKey: string = this.application.state,
): ApplicationStateMeta<TEvents> | undefined {
Expand Down
1 change: 1 addition & 0 deletions libs/application/types/src/lib/StateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export interface ApplicationStateMeta<
roles?: RoleInState<T>[]
onExit?: TemplateApi<R>[] | TemplateApi<R>
onEntry?: TemplateApi<R>[] | TemplateApi<R>
onDelete?: TemplateApi<R>[] | TemplateApi<R>
}

export interface ApplicationStateSchema<T extends EventObject = AnyEventObject>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import React, { useCallback, useState } from 'react'
import { Box, Pagination, Stack } from '@island.is/island-ui/core'
import {
Box,
Pagination,
Stack,
ToastContainer,
} from '@island.is/island-ui/core'
import {
Application,
ApplicationTypes,
Expand Down Expand Up @@ -95,6 +100,7 @@ const ApplicationList = ({
/>
</Box>
) : null}
<ToastContainer hideProgressBar closeButton useKeyframeStyles={false} />
</>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { useMutation } from '@apollo/client'
import { DELETE_APPLICATION } from '@island.is/application/graphql'
import { handleServerError } from '../utilities/handleServerError'
import { useLocale } from '@island.is/localization'

export const useDeleteApplication = (refetch?: (() => void) | undefined) => {
const { formatMessage } = useLocale()
const [deleteApplicationMutation, { error, loading }] = useMutation(
DELETE_APPLICATION,
{
onCompleted: () => {
refetch?.()
},
onError: (error) => {
handleServerError(error, formatMessage)
},
},
)

Expand Down

0 comments on commit 99090a5

Please sign in to comment.