Improving document upload and retrieval processes in a microservice architecture
Tags: microservices, java
Published: 2024-07-01 12:24:29

## Introduction

I was  recently tasked with streamlining the process by which documents are downloaded on an application with a microservice architecture. There were 3 services involved; a **journal** service where a journal can be created for note taking purposes, a **document** service which handles all documents being processed in the application, and an **enrollment** service. All three services are linked in the application. The journal and enrollment services are supposed call the document service when a document needs to be uploaded or downloaded.

## Problem

The present state of the application was such that the enrollment service made a call to the document service for document processing while the journal service processed the documents internally. This was an issue because it meant the journal service would not be in sync with the enrollment service. Documents might then be duplicated and could cause issues down the line.

## Acceptance Criteria

Design the architecture such that all calls pertaining to documents are made to the document service to separate concerns in the system. This ensures that the application is modular and and all requests regarding documents come from one service.

## Solution
I then injected the document service into the journal service and made the service implementation to make a call to the document service whenever a document is needed. With this, I also removed the stored table in the journal database which housed documents as it was no longer necessary.

#### Sample Code

```
@Override
    public Attachment addAttachmentToStatement(InputStream fileInputStream, String fileName, String description, String type, String journalId, String partyId, String statementId) throws NotFoundException {
        log.debug("An Attachment is about to be added to a Statement");
        statementId = Base64Converter.getUUID(statementId);
        Document document = documentService.uploadDocument(fileInputStream, fileName, description, Service.NOTE.getName(), statementId, type).getDocument();

        AttachmentEntity attachmentEntity = new AttachmentEntity();
        attachmentEntity.setAttachmentId(UUID.randomUUID().toString());
        LocalDateTime dateCreated = LocalDateTime.now();
        attachmentEntity.setDateCreated(dateCreated);
        attachmentEntity.setCreatedBy(securityContext.getSubject().getGlobalPrincipal().get().getName());
        attachmentEntity.setStatementId(statementEntity.get());
        attachmentEntity.setDocumentId(document.getDocumentId());
        attachmentEntity.setJournalId(journalId);
        attachmentEntity = attachmentRepository.save(attachmentEntity);
    }
```

## Summary
This problem was solvable with the right domain knowledge and problems can and do get tougher in the software development field. That is why I have decided to enroll into the [HNG Internship] (https://hng.tech/internship) to sharpen my skills and improve my learning. I hope to gain valuable knowledge that would propel me forward in my career. Thanks to [HNG Internship] (https://hng.tech/hire) for giving the push to publish an article.