|
| 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 | +} |
0 commit comments