Skip to content

Commit

Permalink
feat(transfer-of-machine-ownership): aosh ondelete (#16784)
Browse files Browse the repository at this point in the history
* adding delete functionality to ownerchange machine

* fix: init

* chore: charts update dirty files

* adding mock payment

* fix: add secret from aws paramter store

* removing console.log

* revome other pull request

* remove console.log

* adding delete async func

* error handling

---------

Co-authored-by: Þorkell Máni Þorkelsson <[email protected]>
Co-authored-by: andes-it <[email protected]>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Nov 8, 2024
1 parent 6c0c221 commit e485d7e
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Message } from '@island.is/email-service'
import { EmailTemplateGeneratorProps } from '../../../../../types'
import { EmailRecipient } from '../types'
import { pathToAsset } from '../transfer-of-machine-ownership.utils'
import { ApplicationConfigurations } from '@island.is/application/types'
import { TransferOfMachineOwnershipAnswers } from '@island.is/application/templates/aosh/transfer-of-machine-ownership'

export type ApplicationRejectedEmail = (
props: EmailTemplateGeneratorProps,
recipient: EmailRecipient,
rejectedBy: EmailRecipient | undefined,
) => Message

export const generateApplicationRejectedEmail: ApplicationRejectedEmail = (
props,
recipient,
rejectedBy,
): Message => {
const {
application,
options: { email, clientLocationOrigin },
} = props
const answers = application.answers as TransferOfMachineOwnershipAnswers
const regNumber = answers?.machine?.regNumber

if (!recipient.email) throw new Error('Recipient email was undefined')
if (!regNumber) throw new Error('Registration Number was undefined')
if (!rejectedBy?.ssn) throw new Error('Rejected by ssn was undefined')

const subject = 'Tilkynning um eigendaskipti - Umsókn afturkölluð'

return {
from: {
name: email.sender,
address: email.address,
},
to: [{ name: recipient.name, address: recipient.email }],
subject,
template: {
title: subject,
body: [
{
component: 'Image',
context: {
src: pathToAsset('logo.jpg'),
alt: 'Ísland.is logo',
},
},
{
component: 'Image',
context: {
src: pathToAsset('computerIllustration.jpg'),
alt: 'Kaffi við skjá myndskreyting',
},
},
{
component: 'Heading',
context: { copy: subject },
},
{
component: 'Copy',
context: {
copy:
`<span>Góðan dag,</span><br/><br/>` +
`<span>Beiðni um eigendaskipti á tækinu ${regNumber} hefur verið afturkölluð þar sem eigandi ökutækis eyddi umsókninni.</span><br/>` +
`<span>Til þess að skrá eigendaskiptin rafrænt verður að byrja ferlið að upp á nýtt á umsóknarvef island.is: island.is/umsoknir, ásamt því að allir aðilar þurfa að staðfesta rafrænt innan gefins tímafrests.</span><br/>` +
`<span>Vinsamlegast hafið samband við Vinnueftirlitið [email protected] ef nánari upplýsinga er þörf.</span>`,
},
},
{
component: 'Button',
context: {
copy: 'Skoða umsókn',
href: `${clientLocationOrigin}/${ApplicationConfigurations.TransferOfMachineOwnership.slug}/${application.id}`,
},
},
],
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
MachinesWithTotalCount,
WorkMachinesClientService,
} from '@island.is/clients/work-machines'
import { User } from '@island.is/auth-nest-tools'
@Injectable()
export class TransferOfMachineOwnershipTemplateService extends BaseTemplateApiService {
constructor(
Expand Down Expand Up @@ -270,6 +271,101 @@ export class TransferOfMachineOwnershipTemplateService extends BaseTemplateApiSe
return null
}

private async deleteOwnerChange(
auth: User,
applicationId: string,
): Promise<void> {
try {
const deleteChange = {
ownerchangeId: applicationId,
xCorrelationID: applicationId,
}
await this.workMachineClientService.deleteOwnerChange(auth, deleteChange)
} catch (error) {
this.logger.error(
`Failed to delete owner change for application ${applicationId}`,
error,
)
throw error
}
}

async deleteApplication({
application,
auth,
}: TemplateApiModuleActionProps): Promise<void> {
// 1. Delete charge so that the seller gets reimburshed
const chargeId = getPaymentIdFromExternalData(application)
try {
if (chargeId) {
await this.chargeFjsV2ClientService.deleteCharge(chargeId)
}
} catch (error) {
this.logger.error(
`Failed to delete charge ${chargeId} for application ${application.id}`,
error,
)
throw error
}

// 2. Delete owner change in work machines
await this.deleteOwnerChange(auth, application.id)

// 3. Notify everyone in the process that the application has been withdrawn

// 3a. Get list of users that need to be notified
const answers = application.answers as TransferOfMachineOwnershipAnswers
const recipientList = getRecipients(answers, [
EmailRole.seller,
EmailRole.buyer,
])

// 3b. Send email/sms individually to each recipient about success of withdrawing application
const deletedByRecipient = getRecipientBySsn(answers, auth.nationalId)
for (let i = 0; i < recipientList.length; i++) {
if (recipientList[i].email) {
await this.sharedTemplateAPIService
.sendEmail(
(props) =>
generateApplicationRejectedEmail(
props,
recipientList[i],
deletedByRecipient,
),
application,
)
.catch((e) => {
this.logger.error(
`Error sending email about deleteApplication in application: ID: ${application.id},
role: ${recipientList[i].role}`,
e,
)
})
}

if (recipientList[i].phone) {
await this.sharedTemplateAPIService
.sendSms(
() =>
generateApplicationRejectedSms(
application,
recipientList[i],
deletedByRecipient,
),
application,
)
.catch((e) => {
this.logger.error(
`Error sending sms about deleteApplication to
a phonenumber in application: ID: ${application.id},
role: ${recipientList[i].role}`,
e,
)
})
}
}
}

async rejectApplication({
application,
auth,
Expand All @@ -280,16 +376,19 @@ export class TransferOfMachineOwnershipTemplateService extends BaseTemplateApiSe
await this.chargeFjsV2ClientService.deleteCharge(chargeId)
}

// 2. Notify everyone in the process that the application has been withdrawn
// 2. Delete owner change in work machines
await this.deleteOwnerChange(auth, application.id)

// 3. Notify everyone in the process that the application has been withdrawn

// 2a. Get list of users that need to be notified
// 3a. Get list of users that need to be notified
const answers = application.answers as TransferOfMachineOwnershipAnswers
const recipientList = getRecipients(answers, [
EmailRole.seller,
EmailRole.buyer,
])

// 2b. Send email/sms individually to each recipient about success of withdrawing application
// 3b. Send email/sms individually to each recipient about success of withdrawing application
const rejectedByRecipient = getRecipientBySsn(answers, auth.nationalId)
for (let i = 0; i < recipientList.length; i++) {
if (recipientList[i].email) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
defineTemplateApi,
InstitutionNationalIds,
MockablePaymentCatalogApi,
PaymentCatalogApi,
} from '@island.is/application/types'

Expand All @@ -13,6 +14,14 @@ export const VinnueftirlitidPaymentCatalogApi = PaymentCatalogApi.configure({
externalDataId: 'payment',
})

export const MockableVinnueftirlitidPaymentCatalogApi =
MockablePaymentCatalogApi.configure({
params: {
organizationId: InstitutionNationalIds.VINNUEFTIRLITID,
},
externalDataId: 'payment',
})

export const MachinesApi = defineTemplateApi({
action: 'getMachines',
externalDataId: 'machinesList',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
UserProfileApi,
VinnueftirlitidPaymentCatalogApi,
MachinesApi,
MockableVinnueftirlitidPaymentCatalogApi,
} from '../../dataProviders'
import { DefaultEvents } from '@island.is/application/types'

Expand Down Expand Up @@ -56,6 +57,10 @@ export const prerequisitesSection = buildSection({
provider: VinnueftirlitidPaymentCatalogApi,
title: '',
}),
buildDataProviderItem({
provider: MockableVinnueftirlitidPaymentCatalogApi,
title: '',
}),
],
}),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
UserProfileApi,
VinnueftirlitidPaymentCatalogApi,
MachinesApi,
MockableVinnueftirlitidPaymentCatalogApi,
} from '../dataProviders'
import { getChargeItemCodes, hasReviewerApproved } from '../utils'
import { buildPaymentState } from '@island.is/application/utils'
Expand Down Expand Up @@ -138,6 +139,7 @@ const template: ApplicationTemplate<
api: [
IdentityApi,
UserProfileApi,
MockableVinnueftirlitidPaymentCatalogApi,
VinnueftirlitidPaymentCatalogApi,
MachinesApi,
],
Expand Down Expand Up @@ -214,6 +216,9 @@ const template: ApplicationTemplate<
meta: {
name: 'Tilkynning um eigendaskipti að ökutæki',
status: 'inprogress',
onDelete: defineTemplateApi({
action: ApiActions.deleteApplication,
}),
actionCard: {
tag: {
label: applicationMessage.actionCardDraft,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export enum ApiActions {
initReview = 'initReview',
rejectApplication = 'rejectApplication',
submitApplication = 'submitApplication',
deleteApplication = 'deleteApplication',
}
Loading

0 comments on commit e485d7e

Please sign in to comment.