Skip to content

Commit

Permalink
feat(university-application): file uploads added to service (#15056)
Browse files Browse the repository at this point in the history
* adding function to blob

* file upload test1

* small updates to application

* optimization

* optimization

* refactoring

* Update shared.service.ts

refactoring
  • Loading branch information
berglindoma13 authored Jun 4, 2024
1 parent 623be73 commit 1f60816
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,17 @@ class CreateApplicationFileDto {

@IsString()
@ApiProperty({
description: 'Base 64 for file',
description: 'File type',
example: 'profskirteini',
})
fileType!: string

@IsString()
@ApiProperty({
description: 'Blob for file',
example: '',
})
base64!: string
blob!: Blob
}

class CreateApplicationEducationDto {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ export class UniversityApplicationService {
)
}

const allAttachments = applicationDto.educationList
.map((x) => x.degreeAttachments)
.filter((y) => !!y)
.flat()

// Wrap answers in obj that can be sent to libs/clients for universities
const applicationObj: IApplication = {
id: applicationDto.applicationId,
Expand Down Expand Up @@ -116,6 +121,7 @@ export class UniversityApplicationService {
educationOption: applicationDto.educationOption,
workExperienceList: applicationDto.workExperienceList,
extraFieldList: applicationDto.extraFieldList,
attachments: allAttachments,
}

// Create application in our DB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,26 +242,40 @@ export class SharedTemplateApiService {
)
}

async getAttachmentContentAsBase64(
async getS3File(
application: ApplicationWithAttachments,
attachmentKey: string,
): Promise<string> {
) {
const fileName = (
application.attachments as {
[key: string]: string
}
)[attachmentKey]

const { bucket, key } = AmazonS3URI(fileName)

const uploadBucket = bucket
const file = await this.s3
.getObject({
Bucket: uploadBucket,
Key: key,
})
.promise()
const fileContent = file.Body as Buffer
return file.Body as Buffer
}

async getAttachmentContentAsBase64(
application: ApplicationWithAttachments,
attachmentKey: string,
): Promise<string> {
const fileContent = await this.getS3File(application, attachmentKey)
return fileContent?.toString('base64') || ''
}

async getAttachmentContentAsBlob(
application: ApplicationWithAttachments,
attachmentKey: string,
): Promise<Blob> {
const fileContent = await this.getS3File(application, attachmentKey)
const blob: Blob = new Blob([fileContent], { type: 'multipart/form-data' })
return blob
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,16 @@ export class UniversityService extends BaseTemplateApiService {
? {
degreeAttachments: await this.getFilesFromAttachment(
application,
answers.educationDetails.exemptionDetails?.degreeAttachments,
answers.educationDetails.exemptionDetails?.degreeAttachments?.map(
(x, i) => {
const type = this.mapFileTypes(i)
return {
name: x.name,
key: x.key,
type: type,
}
},
),
),
moreDetails: answers.educationDetails.exemptionDetails?.moreDetails,
}
Expand All @@ -160,7 +169,16 @@ export class UniversityService extends BaseTemplateApiService {
answers.educationDetails.thirdLevelDetails?.moreDetails,
degreeAttachments: await this.getFilesFromAttachment(
application,
answers.educationDetails.thirdLevelDetails?.degreeAttachments,
answers.educationDetails.thirdLevelDetails?.degreeAttachments?.map(
(x, i) => {
const type = this.mapFileTypes(i)
return {
name: x.name,
key: x.key,
type: type,
}
},
),
),
}
: undefined
Expand All @@ -171,7 +189,14 @@ export class UniversityService extends BaseTemplateApiService {
...item,
degreeAttachments: await this.getFilesFromAttachment(
application,
item.degreeAttachments,
item.degreeAttachments?.map((x, i) => {
const type = this.mapFileTypes(i)
return {
name: x.name,
key: x.key,
type: type,
}
}),
),
}
})) ||
Expand Down Expand Up @@ -233,20 +258,39 @@ export class UniversityService extends BaseTemplateApiService {

private async getFilesFromAttachment(
application: ApplicationWithAttachments,
attachments?: { name: string; key: string }[],
): Promise<{ fileName: string; base64: string }[]> {
attachments?: { name: string; key: string; type: string }[],
): Promise<{ fileName: string; fileType: string; blob: Blob }[]> {
return await Promise.all(
attachments?.map(async (file) => {
const base64 =
await this.sharedTemplateAPIService.getAttachmentContentAsBase64(
const blob =
await this.sharedTemplateAPIService.getAttachmentContentAsBlob(
application,
file.key,
)
return {
fileName: file.name,
base64,
fileType: file.type,
blob,
}
}) || [],
)
}

private mapFileTypes = (fileIndex: number): string => {
let type
switch (fileIndex) {
case 1:
type = 'profskirteini'
break
case 2:
type = 'profskirteini2'
break
case 3:
type = 'profskirteini3'
break
default:
type = ''
}
return type
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ const SummaryBlock = ({ children, editAction }: Props) => {
borderColor="blue300"
>
<Box width="full">{children}</Box>
<Button
{/* <Button
icon="pencil"
iconType="filled"
variant="utility"
onClick={editAction}
>
{formatMessage(review.labels.changeButtonText)}
</Button>
</Button> */}
</Box>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ export const DetailsRepeaterItem: FC<DetailsRepeaterItemProps> = ({

{!readOnly && (
<FileUploadController
id={degreeAttachmentsField}
id={degreeAttachmentsField} //profskirteini, profskirteini2, profskirteini3
application={application}
multiple
header={formatMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const ProgramReview: FC<Props> = ({
const answers = application.answers as UniversityApplication

return (
<Box paddingBottom={4}>
<Box paddingBottom={4} paddingTop={4}>
<Box></Box>
<GridRow>
<GridColumn span="1/2">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export const FormerEducationSection = buildSection({
NotFinishedEducationSubSection,
ThirdLevelEducationSubSection,
ExemptionSubSection,
OtherDocumentsSection,
// OtherDocumentsSection, //TODO replace again when there are some programs that request otherDocuments
],
})
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,40 @@ paths:
type: 'string'
required:
type: 'boolean'
extraApplicationTexts:
extraApplicationTextsIs:
type: 'object'
properties:
toptextIs:
type: 'string'
sociallifeIs:
type: 'string'
forYouIs:
type: 'string'
exchangeStudyIs:
type: 'string'
workIs:
type: 'string'
aboutIs:
type: 'string'
items:
type: 'object'
properties:
toptext:
type: 'string'
sociallife:
type: 'string'
forYou:
type: 'string'
exchangeStudy:
type: 'string'
work:
type: 'string'
about:
type: 'string'
extraApplicationTextsEn:
type: 'object'
items:
type: 'object'
properties:
toptext:
type: 'string'
sociallife:
type: 'string'
forYou:
type: 'string'
exchangeStudy:
type: 'string'
work:
type: 'string'
about:
type: 'string'
kjorsvid:
type: 'array'
items:
Expand Down Expand Up @@ -494,6 +513,45 @@ paths:
description: 'Application ID'
tags:
- 'Application'
/applications/attachments/{guid}:
post:
parameters:
- name: guid
required: true
in: path
allowEmptyValue: false
description: Application ID
schema:
type: string
format: uuid
summary: Submit an applications accompanying documents
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
attachment:
type: string
format: binary
attachmentKey:
$ref: '#/components/schemas/AttachmentKey'
responses:
'200':
description: Successful submission
content:
application/json:
schema:
type: object
properties:
id:
type: string
description: Attachment ID
'400':
description: Bad Request - No application with given ID
tags:
- Application
/openapi.yaml:
get:
operationId: 'InfraController_openapi'
Expand Down Expand Up @@ -590,6 +648,23 @@ components:
- 'universityId'
- 'programId'
- 'modeOfDelivery'
AttachmentKey:
type: string
enum:
- cv
- markmid
- rannsoknaraaetlun
- namsaaetlun
- rannsoknarverkefni
- profskirteini
- profskirteini2
- profskirteini3
- onnur_skjol
- kynningarbref
- ferilsskra
- leyfisbref
- portofolio
- sakavottord
UpdateApplicationDto:
type: 'object'
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { logger } from '@island.is/logging'
import { mapUglaPrograms } from './utils/mapUglaPrograms'
import { mapUglaCourses } from './utils/mapUglaCourses'
import { mapUglaApplication } from './utils/mapUglaApplication'
import { InlineResponse2004 } from '../../gen/fetch'
import { AttachmentKey, InlineResponse2004 } from '../../gen/fetch'

@Injectable()
export class UniversityOfIcelandApplicationClient {
Expand Down Expand Up @@ -73,6 +73,20 @@ export class UniversityOfIcelandApplicationClient {
const response = await this.applicationApi.applicationsPost(
mappedApplication,
)

application.attachments?.filter(Boolean).forEach(async (attachment) => {
const attachmentKey = attachment?.fileType
? AttachmentKey[attachment.fileType as keyof typeof AttachmentKey] ||
undefined
: undefined
const requestParams = {
guid: application.id,
attachment: attachment?.blob,
attachmentKey,
}
await this.applicationApi.applicationsAttachmentsGuidPost(requestParams)
})

return response
}

Expand Down
Loading

0 comments on commit 1f60816

Please sign in to comment.