Skip to content

Commit b0a4cab

Browse files
committed
chore: refactor
1 parent 25a219d commit b0a4cab

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

apps/backoffice-v2/src/pages/Entity/components/Case/hooks/useDocuments/helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { isCsv } from '@/common/utils/is-csv/is-csv';
22
import { convertCsvToPdfBase64String } from '../../../../../../common/utils/convert-csv-to-pdf-base64-string/convert-csv-to-pdf-base64-string';
33
import { IDocumentsProps } from '../../interfaces';
4+
import { isBase64 } from '@/common/utils/is-base64/is-base64';
45

56
export const convertCsvDocumentsToPdf = (documents: IDocumentsProps['documents']) => {
67
return documents?.map(document => {
7-
if (isCsv(document) && document.base64) {
8+
if (isCsv(document) && isBase64(document.base64 || '')) {
89
return { ...document, imageUrl: convertCsvToPdfBase64String(document.base64) };
910
}
1011

services/workflows-service/src/document/document.service.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import {
4949
import { PrismaService } from '@/prisma/prisma.service';
5050
import { assertIsValidProjectIds } from '@/project/project-scope.service';
5151
import { HttpService } from '@nestjs/axios';
52+
import { isCsvMimetype } from './utils/is-csv-mimetype';
53+
import { fetchCsvFromUrlAndConvertToBase64 } from './utils/fetch-csv-from-url-and-convert-to-base-64';
5254

5355
@Injectable()
5456
export class DocumentService {
@@ -1184,9 +1186,9 @@ export class DocumentService {
11841186
files.map(async ({ imageUrl, file, ...fileData }) => {
11851187
let base64: string | undefined;
11861188

1187-
if (this.isCsv(fileData) && imageUrl) {
1189+
if (isCsvMimetype(fileData.mimeType) && imageUrl) {
11881190
try {
1189-
base64 = await this.fetchCsvFromUrlAndCovertToBase64(imageUrl);
1191+
base64 = await fetchCsvFromUrlAndConvertToBase64(imageUrl);
11901192
} catch (error) {
11911193
console.error(`Failed to fetch CSV and convert to base64 file ${file.id}:`, error);
11921194
}
@@ -1212,23 +1214,6 @@ export class DocumentService {
12121214
return Promise.all(formatPromises);
12131215
}
12141216

1215-
private isCsv(file: { mimeType: string | null }) {
1216-
return file.mimeType === 'text/csv' || file.mimeType === 'application/csv';
1217-
}
1218-
1219-
private async fetchCsvFromUrlAndCovertToBase64(csvUrl: string) {
1220-
const response = await this.httpService.axiosRef.get(csvUrl, {
1221-
responseType: 'arraybuffer',
1222-
});
1223-
const buffer = response.data;
1224-
const base64 = Buffer.from(buffer).toString('base64');
1225-
const contentType = response.headers['content-type'];
1226-
1227-
const base64Result = `data:${contentType};base64,${base64}`;
1228-
1229-
return base64Result;
1230-
}
1231-
12321217
getLatestDocumentVersions(documents: Document[]) {
12331218
const documentsByType = documents.reduce((acc, document) => {
12341219
const documentId = document.businessId
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import axios from 'axios';
2+
3+
export const fetchCsvFromUrlAndConvertToBase64 = async (csvUrl: string) => {
4+
const response = await axios.get(csvUrl, {
5+
responseType: 'arraybuffer',
6+
});
7+
const buffer = response.data;
8+
const base64 = Buffer.from(buffer).toString('base64');
9+
const contentType = response.headers['content-type'];
10+
11+
const base64Result = `data:${contentType};base64,${base64}`;
12+
13+
return base64Result;
14+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const isCsvMimetype = (mimeType: string | null) =>
2+
mimeType === 'text/csv' || mimeType === 'application/csv';

0 commit comments

Comments
 (0)