Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const useWorkflowDocumentsAdapter = ({
country: issuingCountry,
},
details:
document?.files?.map(({ mimeType, fileName, variant, fileId, imageUrl }) => {
document?.files?.map(({ mimeType, fileName, variant, fileId, imageUrl, base64 }) => {
const title = generateDocumentTitle({
category: document?.category ?? '',
type: document?.type ?? '',
Expand All @@ -64,6 +64,7 @@ export const useWorkflowDocumentsAdapter = ({
fileType: mimeType,
fileName,
imageUrl,
base64,
};
}) ?? [],
})) ?? [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React, { FunctionComponent } from 'react';
import { FunctionComponent, useMemo } from 'react';
import { IMultiDocumentsProps } from './interfaces';
import { Case } from '@/pages/Entity/components/Case/Case';

export const MultiDocuments: FunctionComponent<IMultiDocumentsProps> = ({ value }) => {
const documents = value?.data?.filter(({ imageUrl }) => !!imageUrl);
const documents = useMemo(
() => value?.data?.filter(({ imageUrl, base64 }) => imageUrl || base64),
[value?.data],
);

return (
<div className={`m-2 rounded p-1`}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface IMultiDocumentsProps {
isDocumentEditable: boolean;
data: Array<{
imageUrl: string;
base64?: string;
title: string;
fileType: string;
}>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { isCsv } from '@/common/utils/is-csv/is-csv';
import { convertCsvToPdfBase64String } from '../../../../../../common/utils/convert-csv-to-pdf-base64-string/convert-csv-to-pdf-base64-string';
import { IDocumentsProps } from '../../interfaces';
import { isBase64 } from '@/common/utils/is-base64/is-base64';

export const convertCsvDocumentsToPdf = (documents: IDocumentsProps['documents']) => {
return documents?.map(document => {
if (isCsv(document)) {
return { ...document, imageUrl: convertCsvToPdfBase64String(document.imageUrl) };
if (isCsv(document) && isBase64(document.base64 || '')) {
return { ...document, imageUrl: convertCsvToPdfBase64String(document.base64) };
}

return document;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ComponentProps } from 'react';

import { TStateTags } from '@ballerine/common';
import { TAssignee } from '../../../../common/components/atoms/AssignDropdown/AssignDropdown';
import { Actions } from './Case.Actions';
Expand Down Expand Up @@ -40,6 +38,7 @@ export interface IDocumentsProps {
documents: Array<{
id: string;
imageUrl: string;
base64?: string;
fileType: string;
fileName: string;
title: string;
Expand Down
46 changes: 38 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion services/workflows-service/prisma/data-migrations
2 changes: 2 additions & 0 deletions services/workflows-service/src/document/document.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { WorkflowModule } from '@/workflow/workflow.module';
import { UiDefinitionModule } from '@/ui-definition/ui-definition.module';
import { WorkflowDefinitionModule } from '@/workflow-defintion/workflow-definition.module';
import { ProjectScopeService } from '@/project/project-scope.service';
import { HttpModule } from '@nestjs/axios';

@Module({
imports: [
Expand All @@ -19,6 +20,7 @@ import { ProjectScopeService } from '@/project/project-scope.service';
forwardRef(() => WorkflowModule),
UiDefinitionModule,
WorkflowDefinitionModule,
HttpModule,
],
controllers: [DocumentControllerExternal],
providers: [DocumentService, DocumentRepository, ProjectScopeService],
Expand Down
39 changes: 32 additions & 7 deletions services/workflows-service/src/document/document.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ import {
EntitySchema,
TParsedDocuments,
} from './types';
import { defaultPrismaTransactionOptions } from '@/prisma/prisma.util';
import { beginTransactionIfNotExistCurry } from '@/prisma/prisma.util';
import {
defaultPrismaTransactionOptions,
beginTransactionIfNotExistCurry,
} from '@/prisma/prisma.util';
import { PrismaService } from '@/prisma/prisma.service';
import { assertIsValidProjectIds } from '@/project/project-scope.service';
import { HttpService } from '@nestjs/axios';
import { isCsvMimetype } from './utils/is-csv-mimetype';
import { fetchCsvFromUrlAndConvertToBase64 } from './utils/fetch-csv-from-url-and-convert-to-base-64';

@Injectable()
export class DocumentService {
Expand All @@ -58,6 +63,7 @@ export class DocumentService {
protected readonly uiDefinitionService: UiDefinitionService,
protected readonly workflowDefinitionService: WorkflowDefinitionService,
protected readonly prismaService: PrismaService,
protected readonly httpService: HttpService,
) {}

async create(
Expand Down Expand Up @@ -1165,7 +1171,7 @@ export class DocumentService {
}
>;

return typedDocuments.map(({ files, ...document }) => {
const formatPromises = typedDocuments.map(async ({ files, ...document }) => {
const documentWithPropertiesSchema = addPropertiesSchemaToDocument(
// @ts-expect-error -- the function expects properties not used by the function.
{
Expand All @@ -1176,17 +1182,36 @@ export class DocumentService {
},
documentSchema,
);
const filesWithBase64 = await Promise.all(
files.map(async ({ imageUrl, file, ...fileData }) => {
let base64: string | undefined;

if (isCsvMimetype(fileData.mimeType) && imageUrl) {
try {
base64 = await fetchCsvFromUrlAndConvertToBase64(imageUrl);
} catch (error) {
console.error(`Failed to fetch CSV and convert to base64 file ${file.id}:`, error);
}
}

return {
...fileData,
imageUrl,
fileName: file.fileName,
base64,
};
}),
);

return {
...document,
decision: document.decision,
files: files.map(({ file, ...fileData }) => ({
...fileData,
fileName: file.fileName,
})),
files: filesWithBase64,
propertiesSchema: documentWithPropertiesSchema.propertiesSchema,
};
});

return Promise.all(formatPromises);
}

getLatestDocumentVersions(documents: Document[]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import axios from 'axios';

export const fetchCsvFromUrlAndConvertToBase64 = async (csvUrl: string) => {
const response = await axios.get(csvUrl, {
responseType: 'arraybuffer',
});
const buffer = response.data;
const base64 = Buffer.from(buffer).toString('base64');
const contentType = response.headers['content-type'];

const base64Result = `data:${contentType};base64,${base64}`;

return base64Result;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const isCsvMimetype = (mimeType: string | null) =>
mimeType === 'text/csv' || mimeType === 'application/csv';