Skip to content

Commit

Permalink
PR related improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
norda-gunni committed Nov 28, 2024
1 parent 846bd06 commit af739e4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,33 @@ export class ApplicationLifeCycleService {
const stateConfig = template.stateMachineConfig.states[application.state]
const lifeCycle = stateConfig.meta?.lifecycle
if (lifeCycle && lifeCycle.shouldBePruned && lifeCycle.pruneMessage) {
const pruneMessage =
typeof lifeCycle.pruneMessage === 'function'
? lifeCycle.pruneMessage(application as ApplicationWithAttachments)
: lifeCycle.pruneMessage
const notification = {
recipient: application.applicant,
templateId: pruneMessage.notificationTemplateId,
args: [
{
key: 'externalBody',
value: pruneMessage.externalBody || '',
},
{
key: 'internalBody',
value: pruneMessage.internalBody || '',
},
],
try {
const pruneMessage =
typeof lifeCycle.pruneMessage === 'function'
? lifeCycle.pruneMessage(application as ApplicationWithAttachments)
: lifeCycle.pruneMessage
const notification = {
recipient: application.applicant,
templateId: pruneMessage.notificationTemplateId,
args: [
{
key: 'externalBody',
value: pruneMessage.externalBody || '',
},
{
key: 'internalBody',
value: pruneMessage.internalBody || '',
},
],
}
return notification
} catch (error) {
this.logger.error(
`Failed to prepare pruning notification for application ${application.id}`,
error,
)
return null
}
return notification
}
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,5 +307,42 @@ describe('ApplicationLifeCycleService', () => {
`Failed to send pruning notification with error: ${mockError} for application ${mockApplicationId}`,
)
})

it('should handle malformed pruneMessage function', async () => {
const mockApplication = {
id: '123',
typeId: ApplicationTypes.EXAMPLE,
state: 'draft',
applicant: 'user123',
answers: {},
externalData: {},
attachments: [],
}

const mockTemplate = {
stateMachineConfig: {
states: {
draft: {
meta: {
lifecycle: {
shouldBePruned: true,
pruneMessage: () => {
throw new Error('Unexpected error')
},
},
},
},
},
},
}

;(getApplicationTemplateByTypeId as jest.Mock).mockResolvedValue(
mockTemplate,
)

const result = await service['preparePrunedNotification'](mockApplication)
expect(result).toBeNull()
expect(mockLogger.error).toHaveBeenCalled()
})
})
})
15 changes: 2 additions & 13 deletions libs/application/types/src/lib/StateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { Condition } from './Condition'
import { TestSupport } from '@island.is/island-ui/utils'
import { TemplateApi } from './template-api/TemplateApi'
import { PruningNotification } from './ApplicationLifecycle'
import { PruningApplication, PruningNotification } from './ApplicationLifecycle'

export type ApplicationRole = 'applicant' | 'assignee' | string

Expand Down Expand Up @@ -76,18 +76,7 @@ export type StateLifeCycle =
shouldDeleteChargeIfPaymentFulfilled?: boolean | null
pruneMessage?:
| PruningNotification
| ((
application: Pick<
ApplicationWithAttachments,
| 'id'
| 'typeId'
| 'state'
| 'applicant'
| 'attachments'
| 'answers'
| 'externalData'
>,
) => PruningNotification)
| ((application: PruningApplication) => PruningNotification)
}

export type PendingActionDisplayType = 'warning' | 'success' | 'info' | 'error'
Expand Down

0 comments on commit af739e4

Please sign in to comment.