Skip to content

Commit 4e23e99

Browse files
committed
feat: risorsa api per la gestione dei token fcm per l'invio di notifiche all'app
1 parent 7c84c37 commit 4e23e99

2 files changed

Lines changed: 115 additions & 1 deletion

File tree

src/API/App/v1/FcmTokens.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/*
4+
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
5+
* Copyright (C) DevCode s.r.l.
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
namespace API\App\v1;
22+
23+
use API\Interfaces\CreateInterface;
24+
use API\Interfaces\UpdateInterface;
25+
use API\Resource;
26+
use API\Response;
27+
28+
/**
29+
* Risorsa API per la gestione dei token FCM (Firebase Cloud Messaging) dei dispositivi.
30+
* Permette all'app di salvare e aggiornare i token FCM per l'invio di notifiche push.
31+
*/
32+
class FcmTokens extends Resource implements CreateInterface, UpdateInterface
33+
{
34+
35+
public function create($request)
36+
{
37+
$database = database();
38+
$user = $this->getUser();
39+
40+
$fcm_token = $request['token'];
41+
$platform = $request['platform'] ?? null;
42+
$device_info = $request['device_info'] ?? null;
43+
$user_id = $user['id'];
44+
45+
try {
46+
// Verifica se esiste già un token per questo utente
47+
$existing_token = $database->fetchOne('SELECT * FROM `zz_app_tokens` WHERE `id_user` = :user_id', [
48+
':user_id' => $user_id,
49+
]);
50+
51+
if ($existing_token) {
52+
// Aggiorna il token esistente
53+
$database->update('zz_app_tokens', [
54+
'token' => $fcm_token,
55+
'platform' => $platform,
56+
'device_info' => $device_info ? json_encode($device_info) : null,
57+
], ['id' => $existing_token['id']]);
58+
59+
$token_id = $existing_token['id'];
60+
} else {
61+
// Crea un nuovo record
62+
$database->insert('zz_app_tokens', [
63+
'id_user' => $user_id,
64+
'token' => $fcm_token,
65+
'platform' => $platform,
66+
'device_info' => $device_info ? json_encode($device_info) : null,
67+
]);
68+
69+
$token_id = $database->lastInsertedID();
70+
}
71+
72+
return [
73+
'id' => $token_id
74+
];
75+
76+
} catch (\Exception $e) {
77+
return [
78+
'status' => Response::getStatus()['internal_error']['code'],
79+
'message' => 'Errore durante il salvataggio del token',
80+
];
81+
}
82+
}
83+
84+
/**
85+
* Aggiorna un token FCM esistente.
86+
*
87+
* @param array $request Dati della richiesta
88+
*
89+
* @return array Risposta dell'operazione
90+
*/
91+
public function update($request)
92+
{
93+
// Per i token FCM, l'operazione di update è identica a create
94+
// poiché gestiamo automaticamente la creazione o l'aggiornamento
95+
return $this->create($request);
96+
}
97+
}

update/2_10.sql

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ INSERT INTO `zz_settings_lang` (`id_lang`, `id_record`, `title`, `help`) VALUES
7474
(2, @id_setting, 'Allow attachment insertion in completed activities', '');
7575

7676
-- Risorsa api per la verifica modifica record
77+
INSERT INTO `zz_api_resources` (`id`, `version`, `type`, `resource`, `class`, `enabled`)
78+
VALUES (NULL, 'app-v1', 'retrieve', 'verifica-aggiornamenti', 'API\\App\\v1\\VerificaAggiornamenti', '1');
79+
80+
-- Tabella per la gestione dei token per l'invio di notifiche all'app
81+
CREATE TABLE `zz_app_tokens` (`id` INT NOT NULL AUTO_INCREMENT , `token` VARCHAR(500) NOT NULL , `id_user` INT NOT NULL , `created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP , `updated_at` TIMESTAMP on update CURRENT_TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP , PRIMARY KEY (`id`)) ENGINE = InnoDB;
82+
83+
-- Aggiunta colonne specifiche per FCM
84+
ALTER TABLE `zz_app_tokens`
85+
ADD COLUMN `platform` VARCHAR(50) NULL AFTER `token`,
86+
ADD COLUMN `device_info` TEXT NULL AFTER `platform`;
87+
88+
-- Aggiunta indice per migliorare le performance
89+
ALTER TABLE `zz_app_tokens` ADD INDEX `idx_user_fcm` (`id_user`, `token`);
90+
91+
-- Registrazione della risorsa API per la gestione dei token FCM
92+
INSERT INTO `zz_api_resources` (`id`, `version`, `type`, `resource`, `class`, `enabled`)
93+
VALUES (NULL, 'app-v1', 'create', 'fcm-tokens', 'API\\App\\v1\\FcmTokens', '1');
7794

7895
INSERT INTO `zz_api_resources` (`id`, `version`, `type`, `resource`, `class`, `enabled`)
79-
VALUES (NULL, 'app-v1', 'retrieve', 'verifica-aggiornamenti', 'API\\App\\v1\\VerificaAggiornamenti', '1');
96+
VALUES (NULL, 'app-v1', 'update', 'fcm-tokens', 'API\\App\\v1\\FcmTokens', '1');

0 commit comments

Comments
 (0)