Skip to content

Commit

Permalink
feat: political party as a new application
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnigs committed Sep 19, 2024
1 parent 873c574 commit 545c4cf
Show file tree
Hide file tree
Showing 71 changed files with 3,789 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,42 @@ import {
CemeteryFinancialStatementValues,
FinancialStatementsInaoClientService,
ClientRoles,
Contact,
ContactType,
DigitalSignee,
} from '@island.is/clients/financial-statements-inao'
import {
ApplicationTypes,
ApplicationWithAttachments as Application,
PerformActionResult,
} from '@island.is/application/types'
import { getValueViaPath } from '@island.is/application/core'
import AmazonS3URI from 'amazon-s3-uri'
import { TemplateApiModuleActionProps } from '../../../types'
import * as kennitala from 'kennitala'
import {
DataResponse,
getCurrentUserType,
} from '../financial-statements-inao/financial-statements-inao.service'
import {
BoardMember,
FSIUSERTYPE,
} from '@island.is/application/templates/financial-statements-inao/types'
import {
mapValuesToCemeterytype,
getNeededCemeteryValues,
mapContactsAnswersToContacts,
mapDigitalSignee,
} from '../financial-statement-cemetery/mappers/mapValuesToUserType'
import { TemplateApiError } from '@island.is/nest/problem'
import { ApplicationApiAction } from '../../template-api.service'

export type AttachmentData = {
key: string
name: string
}

export interface DataResponse {
success: boolean
message?: string
}

export const getCurrentUserType = (answers: any, externalData: any) => {
const fakeUserType: any = getValueViaPath(answers, 'fakeData.options')

const currentUserType: any = getValueViaPath(
externalData,
'getUserType.data.value',
)
return fakeUserType ? fakeUserType : currentUserType
}

export class FinancialStatementCemeteryTemplateService extends BaseTemplateApiService {
s3: S3
constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getValueViaPath } from '@island.is/application/core'
import { BoardMember } from '@island.is/application/templates/financial-statements-inao/types'
import { FormValue } from '@island.is/application/types'
import {
Contact,
Expand All @@ -8,6 +7,12 @@ import {
DigitalSignee,
} from '@island.is/clients/financial-statements-inao'

type BoardMember = {
nationalId: string
name: string
role: string
}

export const mapValuesToCemeterytype = (answers: FormValue) => {
return {
careIncome: Number(getValueViaPath(answers, 'cemetryIncome.careIncome')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
PersonalElectionSubmitInput,
} from '@island.is/clients/financial-statements-inao'
import { mapValuesToIndividualtype } from './mapValuesToUserTypes'
import { ApplicationWithAttachments as Application } from '@island.is/application/types'

const LESS = 'less'
const GREATER = 'greater'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { DynamicModule } from '@nestjs/common'
import { BaseTemplateAPIModuleConfig } from '../../../types'
import { SharedTemplateAPIModule } from '../../shared'
import { ConfigModule } from '@nestjs/config'
import {
FinancialStatementsInaoClientConfig,
FinancialStatementsInaoClientModule,
} from '@island.is/clients/financial-statements-inao'
import { FinancialStatementPoliticalPartyTemplateService } from './financial-statement-political-party.service'

export class FinancialStatementPoliticalPartyTemplateModule {
static register(config: BaseTemplateAPIModuleConfig): DynamicModule {
return {
module: FinancialStatementPoliticalPartyTemplateModule,
imports: [
SharedTemplateAPIModule.register(config),
ConfigModule.forRoot({
load: [FinancialStatementsInaoClientConfig],
}),
FinancialStatementsInaoClientModule,
],
providers: [FinancialStatementPoliticalPartyTemplateService],
exports: [FinancialStatementPoliticalPartyTemplateService],
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import { Inject, Injectable } from '@nestjs/common'
import { BaseTemplateApiService } from '../../base-template-api.service'
import { S3 } from 'aws-sdk'
import { LOGGER_PROVIDER } from '@island.is/logging'
import {
ApplicationTypes,
ApplicationWithAttachments as Application,
} from '@island.is/application/types'
import {
Contact,
ContactType,
DigitalSignee,
FinancialStatementsInaoClientService,
PoliticalPartyFinancialStatementValues,
} from '@island.is/clients/financial-statements-inao'
import { getValueViaPath } from '@island.is/application/core'
import AmazonS3Uri from 'amazon-s3-uri'
import { TemplateApiModuleActionProps } from '../../../types'
import * as kennitala from 'kennitala'
import { mapValuesToPartyTypes } from './mappers/mapValuesToPartyTypes'

export interface AttachmentData {
key: string
name: string
}

export interface DataResponse {
success: boolean
message?: string
}

export const getCurrentUserType = (answers: any, externalData: any) => {
const fakeUserType: any = getValueViaPath(answers, 'fakeData.options')

const currentUserType: any = getValueViaPath(
externalData,
'getUserType.data.value',
)
return fakeUserType ? fakeUserType : currentUserType
}

const PARTY_USER_TYPE = 150000001

@Injectable()
export class FinancialStatementPoliticalPartyTemplateService extends BaseTemplateApiService {
s3: S3
constructor(
@Inject(LOGGER_PROVIDER) private logger: Logger,
private financialStatementPoliticalPartyService: FinancialStatementsInaoClientService,
) {
super(ApplicationTypes.FINANCIAL_STATEMENT_POLITICAL_PARTY)
this.s3 = new S3()
}

private async getAttachment(application: Application): Promise<string> {
const attachments: AttachmentData[] | undefined = getValueViaPath(
application.answers,
'attachments.files',
) as Array<{ key: string; name: string }>

const attachmentKey = attachments[0].key

const fileName = (
application.attachments as {
[key: string]: string
}
)[attachmentKey]

if (!fileName) {
return Promise.reject({})
}

const { bucket, key } = AmazonS3Uri(fileName)

const uploadBucket = bucket
try {
const file = await this.s3
.getObject({ Bucket: uploadBucket, Key: key })
.promise()
const fileContent = file.Body as Buffer
return fileContent?.toString('base64') || ''
} catch (error) {
throw new Error('Villa kom upp við að senda umsókn')
}
}

async getUserType({ auth }: TemplateApiModuleActionProps) {
const { nationalId } = auth
if (kennitala.isPerson(nationalId)) {
return this.financialStatementPoliticalPartyService.getClientType(
'Einstaklingur',
)
} else {
return (
this,
this.financialStatementPoliticalPartyService.getUserClientType(
nationalId,
)
)
}
}
async submitApplication({ application, auth }: TemplateApiModuleActionProps) {
const { nationalId, actor } = auth

if (!actor) {
return new Error('Enginn umboðsmaður fannst')
}

const answers = application.answers
const externalData = application.externalData
const currentUserType = getCurrentUserType(answers, externalData)

if (currentUserType !== PARTY_USER_TYPE) {
throw new Error(`Application submission failed`)
}

const values: PoliticalPartyFinancialStatementValues =
mapValuesToPartyTypes(answers)
const year = getValueViaPath(
answers,
'conditionalAbout.operatingYear',
) as string

const actorsName = getValueViaPath(
answers,
'about.powerOfAttorneyName',
) as string

const clientPhone = getValueViaPath(answers, 'about.phoneNumber') as string

const clientEmail = getValueViaPath(answers, 'about.email') as string

const fileName = await this.getAttachment(application)

const client = { nationalId }

const contacts: Array<Contact> = [
{
nationalId: actor.nationalId,
name: actorsName,
contactType: ContactType.Actor,
},
]

const digitalSignee: DigitalSignee = {
email: clientEmail,
phone: clientPhone,
}

const result: DataResponse =
await this.financialStatementPoliticalPartyService
.postFinancialStatementForPoliticalParty(
client,
contacts,
digitalSignee,
year,
'',
values,
fileName,
)
.then((data) => {
if (data === true) {
return { success: true }
} else {
return { success: false }
}
})
.catch((e) => {
return {
success: false,
message: e.message,
}
})

if (!result.success) {
throw new Error('Application submission failed')
}

return { success: result.success }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { getValueViaPath } from '@island.is/application/core'
import { FormValue } from '@island.is/application/types'

export const mapValuesToPartyTypes = (answers: FormValue) => {
return {
contributionsFromTheTreasury: Number(
getValueViaPath(answers, 'partyIncome.contributionsFromTheTreasury'),
),
parliamentaryPartySupport: Number(
getValueViaPath(answers, 'partyIncome.parliamentaryPartySupport'),
),
municipalContributions: Number(
getValueViaPath(answers, 'partyIncome.municipalContributions'),
),
contributionsFromLegalEntities: Number(
getValueViaPath(answers, 'partyIncome.contributionsFromLegalEntities'),
),
contributionsFromIndividuals: Number(
getValueViaPath(answers, 'partyIncome.contributionsFromIndividuals'),
),
generalMembershipFees: Number(
getValueViaPath(answers, 'partyIncome.generalMembershipFees'),
),
otherIncome: Number(getValueViaPath(answers, 'partyIncome.otherIncome')),
capitalIncome: Number(
getValueViaPath(answers, 'capitalNumbers.capitalIncome'),
),
officeOperations: Number(
getValueViaPath(answers, 'partyExpense.electionOffice'),
),
otherOperatingExpenses: Number(
getValueViaPath(answers, 'partyExpense.otherCost'),
),
financialExpenses: Number(
getValueViaPath(answers, 'capitalNumbers.capitalCost'),
),
fixedAssetsTotal: Number(
getValueViaPath(answers, 'asset.fixedAssetsTotal'),
),
currentAssets: Number(getValueViaPath(answers, 'asset.currentAssets')),
longTermLiabilitiesTotal: Number(
getValueViaPath(answers, 'liability.longTerm'),
),
shortTermLiabilitiesTotal: Number(
getValueViaPath(answers, 'liability.shortTerm'),
),
equityTotal: Number(getValueViaPath(answers, 'equity.totalEquity')),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type KeyValue = {
key: number
value: number
}
Loading

0 comments on commit 545c4cf

Please sign in to comment.