@@ -126,7 +126,6 @@ export const ProcessDetail: React.FC<PropsProceso> = ({detallesRows, loadingDeta
126126 const uploadingRef = React . useRef ( false ) ;
127127
128128 const handleUploadAndComplete = async ( detalle : DetallesPasos , paso : any ) => {
129- // ✅ (1) Lock real anti doble ejecución
130129 if ( uploadingRef . current ) return ;
131130 uploadingRef . current = true ;
132131
@@ -158,15 +157,29 @@ export const ProcessDetail: React.FC<PropsProceso> = ({detallesRows, loadingDeta
158157 const nombreBaseRaw = `${ normalizeSpaces ( vm ?. numeroDoc ?? "" ) } - ${ normalizeSpaces ( evidenciaRaw ) } ` ;
159158 const baseSafe = sanitizeFileName ( nombreBaseRaw ) ;
160159
161- const look = `Colaboradores Activos/${ ( await servicioColaboradores . findFolderByDocNumber ( vm . numeroDoc ) ) . name } `
162- const carpeta = `Colaboradores Activos/${ normalizeSpaces ( vm ?. numeroDoc ?? "" ) } - ${ normalizeSpaces ( vm ?. nombre ?? "" ) } ` ;
160+ const carpeta = `Colaboradores Activos/${ normalizeSpaces ( vm ?. numeroDoc ?? "" ) } - ${ normalizeSpaces ( vm ?. nombre ?? "" ) } ` ;
163161
164162 setUploading ( true ) ;
165163
166164 try {
167- console . log ( "[UPLOAD] carpeta:" , carpeta ) ;
165+ // ✅ Resolver carpeta objetivo de forma segura
166+ let look : string | null = null ;
167+
168+ try {
169+ const found = await servicioColaboradores . findFolderByDocNumber ( vm . numeroDoc ) ;
170+ const name = normalizeSpaces ( found ?. name ?? "" ) ;
171+ if ( name ) look = `Colaboradores Activos/${ name } ` ;
172+ } catch {
173+ // No existe o no se pudo buscar: caemos a "carpeta"
174+ look = null ;
175+ }
176+
177+ const targetFolder = look ?? carpeta ;
178+ console . log ( "[UPLOAD] targetFolder:" , targetFolder ) ;
179+
180+ // (Opcional) si tu servicio soporta crear carpeta:
181+ // await servicioColaboradores.ensureFolder(targetFolder);
168182
169- // ✅ (3) Si hay 409, renombramos automáticamente (evita “sube y luego error” por duplicado)
170183 let uploadedName : string | null = null ;
171184 let lastErr : any = null ;
172185
@@ -176,7 +189,7 @@ export const ProcessDetail: React.FC<PropsProceso> = ({detallesRows, loadingDeta
176189
177190 try {
178191 console . log ( "[UPLOAD] intentando:" , candidate ) ;
179- await servicioColaboradores . uploadFile ( look ? look : carpeta , renamedFile ) ;
192+ await servicioColaboradores . uploadFile ( targetFolder , renamedFile ) ;
180193 uploadedName = candidate ;
181194 lastErr = null ;
182195 console . log ( "[UPLOAD] OK:" , candidate ) ;
@@ -187,26 +200,18 @@ export const ProcessDetail: React.FC<PropsProceso> = ({detallesRows, loadingDeta
187200
188201 if ( status === 409 || msg . includes ( "incompatible with a similar name" ) || msg . includes ( "409" ) ) {
189202 lastErr = e ;
190- continue ; // probamos otro nombre
203+ continue ;
191204 }
192205
193- // otro error real
194206 throw e ;
195207 }
196208 }
197209
198- if ( ! uploadedName ) {
199- // no encontramos nombre libre
200- throw lastErr ;
201- }
210+ if ( ! uploadedName ) throw lastErr ;
202211
203- // ✅ (4) Completar paso: si falla, NO decimos “falló la subida”
204212 try {
205- console . log ( "[STEP] completando paso..." ) ;
206213 await handleSubmit ( detalle , "Completado" ) ;
207- console . log ( "[STEP] OK" ) ;
208214 } catch ( e : any ) {
209- console . error ( "[STEP] Subió OK pero falló completar:" , e ) ;
210215 alert (
211216 `El archivo se subió (${ uploadedName } ), pero falló completar el paso. ` +
212217 `Reintenta completar el paso (no necesitas volver a subir).`
0 commit comments