Skip to content

Commit

Permalink
feat(fe-piattaforma): aggiornamento gestione scd rfd per caricamento …
Browse files Browse the repository at this point in the history
…massivo 20240925
  • Loading branch information
federico-tocci-dxc authored and sgravinadxc committed Sep 25, 2024
1 parent f566905 commit b895af2
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 70 deletions.
6 changes: 5 additions & 1 deletion fe-piattaforma/src/components/CSVUploader/CSVUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { ProjectContext } from '../../contexts/ProjectContext';
import { useParams } from 'react-router-dom';
import { searchActivityReport } from '../../services/activityReportService';
import { hideLoader, showLoader } from '../../redux/features/app/appSlice';
import { selectProjects } from '../../redux/features/administrativeArea/administrativeAreaSlice';
import { policy } from '../../pages/administrator/AdministrativeArea/Entities/utils';

function showErrorFormatCSV() {
dispatchNotify({
Expand Down Expand Up @@ -65,6 +67,8 @@ export default function CSVUploader({
const projectContext = useContext(ProjectContext);
const [selectedFile, setSelectedFile] = useState<File[] | null>(null);
const { projectId, enteId } = useParams();
const projectDetail = useAppSelector(selectProjects).detail?.dettagliInfoProgetto;
const fileInputConsentito = projectDetail?.policy === policy.RFD ? 'CSV' : 'EXCEL';

const handleFileInput = useCallback(
(filesToUpload: File[]) => {
Expand Down Expand Up @@ -236,7 +240,7 @@ export default function CSVUploader({
</>
) : (
<>
<h5>Trascina il file dati (CSV)</h5>
<h5>Trascina il file dati ({fileInputConsentito})</h5>
<p>
oppure{' '}
<input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function CSVFileHandler() {
<div className={file ? 'd-block' : 'd-none'}>
<div className='my-2 row'>
<div className='col-12 col-md-6 d-flex flex-column justify-content-around my-2 px-4'>
<ProcessFile file={file} />
<ProcessFile file={file} removeFile={removeFile} />
</div>
<div className='col-12 col-md-6 d-flex flex-column justify-content-around my-2 px-4'>
<SubmitFileCsv clearFile={removeFile} />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React from 'react';
import uploadIcon from '../../../public/assets/img/it-upload-primary.png';
import { selectProjects } from '../../redux/features/administrativeArea/administrativeAreaSlice';
import { useAppSelector } from '../../redux/hooks';
import { policy } from '../../pages/administrator/AdministrativeArea/Entities/utils';

export default function CsvInstructions({ urlGuida, attachmentGuida }: { urlGuida: string | "" , attachmentGuida: any}) {
const projectDetail = useAppSelector(selectProjects).detail?.dettagliInfoProgetto;
const fileInputConsentito = projectDetail?.policy === policy.RFD ? 'CSV' : 'EXCEL';
return (
<div className='d-flex flex-column'>
<h1 className='h5 text-black font-weight-semibold'>
Expand All @@ -11,7 +16,7 @@ export default function CsvInstructions({ urlGuida, attachmentGuida }: { urlGuid
<li>
<p>
<strong>
Trascina o seleziona il file dei dati in formato CSV
Trascina o seleziona il file dei dati in formato {fileInputConsentito}
</strong>{' '}
per inserirlo nello strumento di caricamento massivo.
</p>
Expand Down
5 changes: 3 additions & 2 deletions fe-piattaforma/src/components/FileHandling/ProcessFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import checkImg from './../../../public/assets/img/icon-check-no-circle.png';

type CSVProcessorProps = {
file: File | undefined;
removeFile: () => void;
};

function showError(error: Error) {
Expand All @@ -19,8 +20,8 @@ function showError(error: Error) {
});
}

export default function ProcessFile({ file }: CSVProcessorProps) {
const { isProcessing, processFile } = useFileProcessor(file);
export default function ProcessFile({ file, removeFile }: CSVProcessorProps) {
const { isProcessing, processFile } = useFileProcessor(file, removeFile);
const dataUploadContext = useContext(DataUploadContext);

const handleProcessFile = useCallback(() => {
Expand Down
141 changes: 76 additions & 65 deletions fe-piattaforma/src/hooks/useFileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const headersCSV = [
'ES6',
];

export function useFileProcessor(file: File | undefined) {
export function useFileProcessor(file: File | undefined, removeFile: () => void) {
const { isValidFiscalCode } = useFiscalCodeValidation();
const [isProcessing, setIsProcessing] = useState(false);
const projectDetail = useAppSelector(selectProjects).detail?.dettagliInfoProgetto;
Expand Down Expand Up @@ -246,7 +246,11 @@ export function useFileProcessor(file: File | undefined) {
},
});
} else {
// Operazioni SCD
setIsProcessing(false);
removeFile();
rejectWithMessage(
'Tipo file non supportato.'
);
}
/*else {
reject({
Expand All @@ -255,74 +259,81 @@ export function useFileProcessor(file: File | undefined) {
});
}*/
} else if (fileExtension === 'xlsx' || fileExtension === 'xls') {
try {
const excelData = await handleExcelFileUpload(file);

const serviziValidati: ServiziElaboratiDto[] = [];
const serviziScartati: ServiziElaboratiDto[] = [];

if (excelData.length > 0) {
excelData.forEach((record: CSVRecord, index: number) => {
sanitizeFields(record);
const { AN14: _AN14, AN17: _AN17, ...filteredRecord } = record;
filteredRecord.AN3 = filteredRecord.AN3.trim();
filteredRecord.SE1 = excelSerialDateToJSDate(Number(filteredRecord.SE1));
filteredRecord.SE2 = excelSerialTimeToHHMM(Number(filteredRecord.SE2));
const { rejectedTypes } = generateServiceName(filteredRecord.SE3);
const errors = validateFields(
filteredRecord,
isValidFiscalCode
);
checkMapValues(record, errors);
checkMapSpaces(record, errors);
if (
rejectedTypes.length > 0 &&
filteredRecord.SE3 &&
filteredRecord.SE3.trim() !== ''
) {
errors.push(
`Servizio non riconosciuto nel campo SE3: ${rejectedTypes.join(
', '
)}. Assicurati che i tipi di servizio inseriti siano corretti`
);
}

const cfData: IPersonalInfo =
CodiceFiscaleUtils.Parser.cfDecode(filteredRecord.AN3);

if (!getAgeGroupCodeByYear(cfData.date)) {
errors.push('Il cittadino deve essere maggiorenne.');
}

const isValidFields = errors.length === 0;
const servizioElaborato: ServiziElaboratiDto =
mappingDatiElaborati(
if (projectDetail?.policy == policy.SCD) {
try {
const excelData = await handleExcelFileUpload(file);

const serviziValidati: ServiziElaboratiDto[] = [];
const serviziScartati: ServiziElaboratiDto[] = [];

if (excelData.length > 0) {
excelData.forEach((record: CSVRecord, index: number) => {
sanitizeFields(record);
const { AN14: _AN14, AN17: _AN17, ...filteredRecord } = record;
filteredRecord.AN3 = filteredRecord.AN3.trim();
filteredRecord.SE1 = excelSerialDateToJSDate(Number(filteredRecord.SE1));
filteredRecord.SE2 = excelSerialTimeToHHMM(Number(filteredRecord.SE2));
const { rejectedTypes } = generateServiceName(filteredRecord.SE3);
const errors = validateFields(
filteredRecord,
errors,
index + 1,
cfData
isValidFiscalCode
);
if (isValidFields && rejectedTypes.length === 0) {
serviziValidati.push(servizioElaborato);
} else {
serviziScartati.push(servizioElaborato);
}
});
const serviziElaborati: ElaboratoCsvRequest = {
serviziValidati: serviziValidati,
serviziScartati: serviziScartati,
estensioneInput: fileExtension
};
resolve(serviziElaborati);
checkMapValues(record, errors);
checkMapSpaces(record, errors);
if (
rejectedTypes.length > 0 &&
filteredRecord.SE3 &&
filteredRecord.SE3.trim() !== ''
) {
errors.push(
`Servizio non riconosciuto nel campo SE3: ${rejectedTypes.join(
', '
)}. Assicurati che i tipi di servizio inseriti siano corretti`
);
}

const cfData: IPersonalInfo =
CodiceFiscaleUtils.Parser.cfDecode(filteredRecord.AN3);

if (!getAgeGroupCodeByYear(cfData.date)) {
errors.push('Il cittadino deve essere maggiorenne.');
}

const isValidFields = errors.length === 0;
const servizioElaborato: ServiziElaboratiDto =
mappingDatiElaborati(
filteredRecord,
errors,
index + 1,
cfData
);
if (isValidFields && rejectedTypes.length === 0) {
serviziValidati.push(servizioElaborato);
} else {
serviziScartati.push(servizioElaborato);
}
});
const serviziElaborati: ElaboratoCsvRequest = {
serviziValidati: serviziValidati,
serviziScartati: serviziScartati,
estensioneInput: fileExtension
};
resolve(serviziElaborati);
setIsProcessing(false);
} else {
rejectWithMessage(
'Il file inserito non è conforme ai criteri di elaborazione, assicurati che siano presenti dei dati da elaborare.'
);
}
} catch (error) {
setIsProcessing(false);
} else {
rejectWithMessage(
'Il file inserito non è conforme ai criteri di elaborazione, assicurati che siano presenti dei dati da elaborare.'
);
reject(error);
}
} catch (error) {
}else{
setIsProcessing(false);
reject(error);
rejectWithMessage(
'Tipo file non supportato.'
);
}
} else {
rejectWithMessage(
Expand Down

0 comments on commit b895af2

Please sign in to comment.