Aggiunta API per sincronizzazione rapida intervento da applicazione

This commit is contained in:
Dasc3er 2021-02-24 16:27:04 +01:00
parent a56166da22
commit dc2952c6b1
3 changed files with 102 additions and 2 deletions

View File

@ -200,7 +200,7 @@ abstract class AppResource extends Resource implements RetrieveInterface, Create
*/
protected function mapModifiedRecords($records)
{
if ($records instanceof Collection){
if ($records instanceof Collection) {
return $records->mapToGroups(function ($item, $key) {
return [$item['id'] => $item];
})->toArray();
@ -210,7 +210,7 @@ abstract class AppResource extends Resource implements RetrieveInterface, Create
$accumulator[$item['id']] = $item;
return $accumulator;
});
}) ?: [];
}
/**

View File

@ -0,0 +1,96 @@
<?php
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace API\App\v1\Flash;
use API\App\v1\AllegatiInterventi;
use API\App\v1\Clienti;
use API\App\v1\Interventi;
use API\App\v1\RigheInterventi;
use API\App\v1\SessioniInterventi;
use API\Interfaces\UpdateInterface;
use API\Resource;
use Carbon\Carbon;
class Intervento extends Resource implements UpdateInterface
{
public function update($request)
{
// Elenco risorse API
$risorse = [
'cliente' => new Clienti(),
'intervento' => new Interventi(),
'righe' => new RigheInterventi(),
'sessioni' => new SessioniInterventi(),
'allegati' => new AllegatiInterventi(),
];
$sezioni = array_keys($risorse);
// Generazione record semplificati
$records = [];
foreach ($sezioni as $sezione) {
if (isset($request[$sezione][0])) {
foreach ($request[$sezione] as $record) {
$records[] = [$record, $risorse[$sezione]];
}
} else {
$records[] = [$request[$sezione], $risorse[$sezione]];
}
}
// Controlli sui conflitti
$conflict = false;
foreach ($records as $key => [$record, $risorsa]) {
$record['updated_at'] = new Carbon($record['updated_at']);
$record['last_sync_at'] = new Carbon($record['last_sync_at']);
if (!$record['updated_at']->greaterThan($record['last_sync_at'])) {
unset($records[$key]);
continue;
}
$modifiche = $risorsa->getModifiedRecords($record['last_sync_at']);
$modifiche = array_keys($modifiche);
if (in_array($record['id'], $modifiche)) {
$conflict = true;
break;
}
}
// Messaggio di conflitto in caso di problematica riscontrata
if ($conflict) {
return [
'status' => 200,
'message' => 'CONFLICT',
];
}
// Salvataggio delle modifiche
foreach ($records as [$record, $risorsa]) {
if (!empty($record['remote_id'])) {
$risorsa->updateRecord($record);
} else {
$risorsa->createRecord($record);
}
}
return [];
}
}

View File

@ -189,3 +189,7 @@ ALTER TABLE `or_ordini` ADD `codice_commessa` VARCHAR(100) NULL AFTER `updated_a
-- Copiato in or_ordini id_documento_fe in numero_cliente dove è presente
UPDATE `or_ordini` SET `numero_cliente`= `id_documento_fe` WHERE `id_documento_fe`!='' AND `id_documento_fe` IS NOT NULL;
-- Risorsa API per sincronizzazione rapida di un singolo intervento
INSERT INTO `zz_api_resources` (`id`, `version`, `type`, `resource`, `class`, `enabled`) VALUES
(NULL, 'app-v1', 'update', 'intervento-flash', 'API\\App\\v1\\Flash\\Intervento', '1');