Skip to content

Commit fbeea9f

Browse files
committed
fix: pulizia allegati app
1 parent 09f261b commit fbeea9f

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

src/API/App/v1/AllegatiInterventi.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function getCleanupData($last_sync_at)
3636
// Elenco allegati degli interventi da rimuovere
3737
$da_interventi = [];
3838
if (!empty($interventi)) {
39-
$query = 'SELECT `zz_files`.`id` FROM `zz_files` WHERE `id_module` = (SELECT `id_record` FROM `zz_modules_lang` WHERE `title` = "Interventi" AND `id_lang` = '.prepare(\Models\Locale::getDefault()->id).') AND `id_record` IN ('.implode(',', $interventi).')';
39+
$query = 'SELECT `zz_files`.`id` FROM `zz_files` WHERE `id_module` = (SELECT `id` FROM `zz_modules` WHERE `name` = "Interventi") AND `id_record` IN ('.implode(',', $interventi).')';
4040
$allegati_interventi = database()->fetchArray($query);
4141
$da_interventi = array_column($allegati_interventi, 'id');
4242
}
@@ -58,7 +58,7 @@ public function getModifiedRecords($last_sync_at)
5858
}
5959

6060
$id_interventi = array_keys($interventi);
61-
$query = 'SELECT `zz_files`.`id`, `zz_files`.`updated_at` FROM `zz_files` WHERE `id_module` = (SELECT `id_record` FROM `zz_modules_lang` WHERE `title` = "Interventi" AND `id_lang` = '.prepare(\Models\Locale::getDefault()->id).') AND `id_record` IN ('.implode(',', $id_interventi).')';
61+
$query = 'SELECT `zz_files`.`id`, `zz_files`.`updated_at` FROM `zz_files` WHERE `id_module` = (SELECT `id` FROM `zz_modules` WHERE `name` = "Interventi") AND `id_record` IN ('.implode(',', $id_interventi).')';
6262

6363
// Filtro per data
6464
if ($last_sync_at) {
@@ -90,21 +90,33 @@ public function retrieveRecord($id)
9090

9191
public function createRecord($data)
9292
{
93-
$module = Module::where('name', 'Interventi')->first()->id;
93+
$module_record = Module::where('name', 'Interventi')->first();
94+
if (!$module_record) {
95+
throw new InternalError('Modulo Interventi non trovato');
96+
}
97+
$module = $module_record->id;
98+
99+
// Validazione dati richiesti
100+
if (empty($data['contenuto']) || empty($data['nome']) || empty($data['id_intervento'])) {
101+
throw new InternalError('Dati mancanti: contenuto, nome o id_intervento');
102+
}
94103

95104
// Creazione del file temporaneo
96105
$content = explode(',', (string) $data['contenuto']);
97-
if (count($content) < 1) {
98-
throw new InternalError();
106+
if (count($content) < 2) {
107+
throw new InternalError('Formato contenuto non valido');
99108
}
100109

101110
$file = base64_decode($content[1]);
111+
if ($file === false) {
112+
throw new InternalError('Errore nella decodifica base64');
113+
}
102114

103115
// Salvataggio del file come allegato
104116
$upload = Upload::build($file, [
105117
'id_module' => $module,
106118
'id_record' => $data['id_intervento'],
107-
], $data['nome'], $data['categoria']);
119+
], $data['nome'], $data['categoria'] ?? '');
108120

109121
return [
110122
'id' => $upload->id,

0 commit comments

Comments
 (0)