@@ -18,40 +18,36 @@ import {
1818} from '@opencrvs/commons/types'
1919import { CertifyInput , IssueInput } from './records/validations'
2020
21- export async function uploadFileToMinio (
22- fileData : string ,
23- authHeader : IAuthHeader
24- ) : Promise < string > {
25- const suffix = '/upload'
26- const request = {
27- method : 'POST' ,
21+ const fetchDocuments = async < T = any > (
22+ suffix : string ,
23+ authHeader : IAuthHeader ,
24+ method = 'GET' ,
25+ body : string | undefined = undefined
26+ ) : Promise < T > => {
27+ const result = await fetch ( `${ DOCUMENTS_URL } ${ suffix } ` , {
28+ method,
2829 headers : {
2930 ...authHeader ,
3031 'Content-Type' : 'application/json'
3132 } ,
32- body : JSON . stringify ( { fileData : fileData } )
33- }
34- const result = await fetch ( `${ DOCUMENTS_URL } ${ suffix } ` , request )
33+ body
34+ } )
3535 const res = await result . json ( )
36- return res . refUrl
36+ return res
3737}
3838
39- async function uploadSVGToMinio (
39+ export async function uploadBase64ToMinio (
4040 fileData : string ,
4141 authHeader : IAuthHeader
4242) : Promise < string > {
43- const suffix = '/upload-svg'
44- const request = {
45- method : 'POST' ,
46- headers : {
47- ...authHeader ,
48- 'Content-Type' : 'image/svg+xml'
49- } ,
50- body : fileData
51- }
52- const result = await fetch ( `${ DOCUMENTS_URL } ${ suffix } ` , request )
53- const res = await result . json ( )
54- return res . refUrl
43+ const docUploadResponse = await fetchDocuments (
44+ '/upload' ,
45+ authHeader ,
46+ 'POST' ,
47+ JSON . stringify ( { fileData : fileData } )
48+ )
49+
50+ return docUploadResponse . refUrl
5551}
5652
5753export async function uploadCertificateAttachmentsToDocumentsStore <
@@ -62,11 +58,11 @@ export async function uploadCertificateAttachmentsToDocumentsStore<
6258 certificateDetails . collector . affidavit
6359 ) {
6460 for ( const affidavit of certificateDetails . collector . affidavit ) {
65- affidavit . data = await uploadFileToMinio ( affidavit . data , authHeader )
61+ affidavit . data = await uploadBase64ToMinio ( affidavit . data , authHeader )
6662 }
6763 }
6864 if ( 'data' in certificateDetails ) {
69- certificateDetails . data = await uploadSVGToMinio (
65+ certificateDetails . data = await uploadBase64ToMinio (
7066 certificateDetails . data ,
7167 authHeader
7268 )
@@ -90,7 +86,7 @@ function uploadOrNormaliseSignatureData(
9086 authHeader : IAuthHeader
9187) {
9288 if ( isBase64FileString ( signature ) ) {
93- return uploadFileToMinio ( signature , authHeader )
89+ return uploadBase64ToMinio ( signature , authHeader )
9490 }
9591
9692 if ( isPresignedUrl ( signature ) ) {
@@ -142,7 +138,7 @@ export async function uploadBase64AttachmentsToDocumentsStore(
142138 if ( record . registration ?. attachments ) {
143139 for ( const attachment of record . registration . attachments ) {
144140 if ( attachment . data && isBase64FileString ( attachment . data ) ) {
145- const fileUri = await uploadFileToMinio ( attachment . data , authHeader )
141+ const fileUri = await uploadBase64ToMinio ( attachment . data , authHeader )
146142 attachment . data = fileUri
147143 }
148144 }
@@ -155,15 +151,18 @@ export async function uploadBase64AttachmentsToDocumentsStore(
155151 if ( certificate . collector . affidavit ) {
156152 for ( const affidavit of certificate . collector . affidavit ) {
157153 if ( affidavit . data && isBase64FileString ( affidavit . data ) ) {
158- const fileUri = await uploadFileToMinio ( affidavit . data , authHeader )
154+ const fileUri = await uploadBase64ToMinio (
155+ affidavit . data ,
156+ authHeader
157+ )
159158 affidavit . data = fileUri
160159 }
161160 }
162161 }
163162 if ( certificate . collector . photo ) {
164163 for ( const photo of certificate . collector . photo ) {
165164 if ( photo . data && isBase64FileString ( photo . data ) ) {
166- const fileUri = await uploadFileToMinio ( photo . data , authHeader )
165+ const fileUri = await uploadBase64ToMinio ( photo . data , authHeader )
167166 photo . data = fileUri
168167 }
169168 }
@@ -173,13 +172,13 @@ export async function uploadBase64AttachmentsToDocumentsStore(
173172 if ( record . registration ?. correction ?. attachments ) {
174173 for ( const attachment of record . registration . correction . attachments ) {
175174 if ( attachment . data && isBase64FileString ( attachment . data ) ) {
176- const fileUri = await uploadFileToMinio ( attachment . data , authHeader )
175+ const fileUri = await uploadBase64ToMinio ( attachment . data , authHeader )
177176 attachment . data = fileUri
178177 }
179178 }
180179 }
181180 if ( record . registration ?. correction ?. payment ?. attachmentData ) {
182- const fileUri = await uploadFileToMinio (
181+ const fileUri = await uploadBase64ToMinio (
183182 record . registration . correction . payment . attachmentData ,
184183 authHeader
185184 )
0 commit comments