From f46610314dfffd9bcc2a553262eeb1a50d307470 Mon Sep 17 00:00:00 2001 From: Kima Date: Sat, 23 Dec 2023 00:05:12 +0100 Subject: [PATCH] fixed msg sending and added error handler --- filcnaplo_kreta_api/lib/client/client.dart | 64 +++++++++++++++++++ filcnaplo_kreta_api/lib/models/message.dart | 5 +- .../lib/providers/message_provider.dart | 30 ++++++--- .../messages/send_message/send_message.dart | 16 ++++- .../send_message/send_message.i18n.dart | 3 + 5 files changed, 107 insertions(+), 11 deletions(-) diff --git a/filcnaplo_kreta_api/lib/client/client.dart b/filcnaplo_kreta_api/lib/client/client.dart index 97aef58..f9a4589 100644 --- a/filcnaplo_kreta_api/lib/client/client.dart +++ b/filcnaplo_kreta_api/lib/client/client.dart @@ -156,6 +156,7 @@ class KretaClient { if (res == null) throw "Login error"; if (json) { + print(jsonDecode(res.body)); return jsonDecode(res.body); } else { return res.body; @@ -168,6 +169,69 @@ class KretaClient { } } + Future sendFilesAPI( + String url, { + Map? headers, + bool autoHeader = true, + Map? body, + }) async { + Map headerMap; + + if (headers != null) { + headerMap = headers; + } else { + headerMap = {}; + } + + try { + http.StreamedResponse? res; + + for (int i = 0; i < 3; i++) { + if (autoHeader) { + if (!headerMap.containsKey("authorization") && accessToken != null) { + headerMap["authorization"] = "Bearer $accessToken"; + } + if (!headerMap.containsKey("user-agent") && userAgent != null) { + headerMap["user-agent"] = "$userAgent"; + } + if (!headerMap.containsKey("content-type")) { + headerMap["content-type"] = "multipart/form-data"; + } + if (url.contains('kommunikacio/uzenetek')) { + headerMap["X-Uzenet-Lokalizacio"] = "hu-HU"; + } + } + + var request = http.MultipartRequest("POST", Uri.parse(url)); + + // request.files.add(value) + + request.fields.addAll(body ?? {}); + request.headers.addAll(headers ?? {}); + + res = await request.send(); + + if (res.statusCode == 401) { + await refreshLogin(); + headerMap.remove("authorization"); + } else { + break; + } + } + + if (res == null) throw "Login error"; + + print(res.statusCode); + + return res.statusCode; + } on http.ClientException catch (error) { + print( + "ERROR: KretaClient.postAPI ($url) ClientException: ${error.message}"); + } catch (error) { + print("ERROR: KretaClient.postAPI ($url) ${error.runtimeType}: $error"); + } + } + Future refreshLogin() async { if (_loginRefreshing) return; _loginRefreshing = true; diff --git a/filcnaplo_kreta_api/lib/models/message.dart b/filcnaplo_kreta_api/lib/models/message.dart index 711f7a9..e938ff5 100644 --- a/filcnaplo_kreta_api/lib/models/message.dart +++ b/filcnaplo_kreta_api/lib/models/message.dart @@ -156,8 +156,9 @@ class SendRecipient { }); factory SendRecipient.fromJson(Map json, SendRecipientType type) { + print(json); return SendRecipient( - id: int.parse(json['oktatasiAzonosito'] ?? '0'), + id: int.parse(json['kretaAzonosito'] ?? '0'), kretaId: json['kretaAzonosito'], name: json['nev'], type: type, @@ -166,7 +167,7 @@ class SendRecipient { } Object get kretaJson => { - 'azonosito': id ?? 0, + 'azonosito': kretaId ?? 0, 'kretaAzonosito': kretaId ?? 0, 'nev': name ?? 'Teszt Lajos', 'tipus': { diff --git a/filcnaplo_kreta_api/lib/providers/message_provider.dart b/filcnaplo_kreta_api/lib/providers/message_provider.dart index f704d72..a04e1a1 100644 --- a/filcnaplo_kreta_api/lib/providers/message_provider.dart +++ b/filcnaplo_kreta_api/lib/providers/message_provider.dart @@ -1,5 +1,5 @@ // ignore_for_file: use_build_context_synchronously - +import 'dart:convert'; import 'dart:math'; import 'package:filcnaplo/api/providers/user_provider.dart'; @@ -209,7 +209,7 @@ class MessageProvider with ChangeNotifier { } // send message - Future sendMessage({ + Future sendMessage({ required List recipients, String subject = "Nincs tárgy", required String messageText, @@ -235,20 +235,34 @@ class MessageProvider with ChangeNotifier { // } recipientList.addAll(recipients.map((e) => e.kretaJson)); - Object body = { + Map body = { "cimzettLista": recipientList, "csatolmanyok": [], - "azonosito": Random().nextInt(10000) + 10000, + "azonosito": (Random().nextInt(10000) + 10000), "feladoNev": user.name, "feladoTitulus": user.role == Role.parent ? "Szülő" : "Diák", "kuldesDatum": DateTime.now().toIso8601String(), "targy": subject, "szoveg": messageText, - "elozoUzenetAzonosito": 0, + // "elozoUzenetAzonosito": 0, }; - // send the message - await Provider.of(_context, listen: false) - .postAPI(KretaAPI.sendMessage, autoHeader: true, body: body); + Map headers = { + "content-type": "application/json", + }; + + var res = await Provider.of(_context, listen: false).postAPI( + KretaAPI.sendMessage, + autoHeader: true, + json: true, + body: json.encode(body), + headers: headers, + ); + + if (res!['hibakod'] == 'UzenetKuldesEngedelyRule') { + return 'send_permission_error'; + } + + return 'successfully_sent'; } } diff --git a/filcnaplo_mobile_ui/lib/pages/messages/send_message/send_message.dart b/filcnaplo_mobile_ui/lib/pages/messages/send_message/send_message.dart index 79c520e..c10031f 100644 --- a/filcnaplo_mobile_ui/lib/pages/messages/send_message/send_message.dart +++ b/filcnaplo_mobile_ui/lib/pages/messages/send_message/send_message.dart @@ -1,7 +1,10 @@ +// ignore_for_file: use_build_context_synchronously + import 'package:dropdown_button2/dropdown_button2.dart'; import 'package:filcnaplo/theme/colors/colors.dart'; import 'package:filcnaplo_kreta_api/models/message.dart'; import 'package:filcnaplo_kreta_api/providers/message_provider.dart'; +import 'package:filcnaplo_mobile_ui/common/custom_snack_bar.dart'; // import 'package:filcnaplo_mobile_ui/common/custom_snack_bar.dart'; import 'package:filcnaplo_mobile_ui/common/material_action_button.dart'; import 'package:filcnaplo_mobile_ui/pages/messages/send_message/send_message.i18n.dart'; @@ -190,11 +193,22 @@ class SendMessageSheetState extends State { ? _subjectController.text : 'Nincs tárgy'; - messageProvider.sendMessage( + var res = await messageProvider.sendMessage( recipients: selectedRecipients, subject: subjectText, messageText: _messageController.text, ); + + // do after send + if (res == 'send_permission_error') { + ScaffoldMessenger.of(context).showSnackBar(CustomSnackBar( + content: Text('cant_send'.i18n), context: context)); + } + + if (res == 'successfully_sent') { + ScaffoldMessenger.of(context).showSnackBar(CustomSnackBar( + content: Text('sent'.i18n), context: context)); + } }, ), ), diff --git a/filcnaplo_mobile_ui/lib/pages/messages/send_message/send_message.i18n.dart b/filcnaplo_mobile_ui/lib/pages/messages/send_message/send_message.i18n.dart index e07fe2a..c78c64b 100644 --- a/filcnaplo_mobile_ui/lib/pages/messages/send_message/send_message.i18n.dart +++ b/filcnaplo_mobile_ui/lib/pages/messages/send_message/send_message.i18n.dart @@ -11,6 +11,7 @@ extension Localization on String { "message_subject": "Subject...", "message_text": "Message text...", "select_recipient": "Add Recipient", + "cant_send": "You can't send a message to one of the recipients!", }, "hu_hu": { "recipients": "Címzettek", @@ -20,6 +21,7 @@ extension Localization on String { "message_subject": "Tárgy...", "message_text": "Üzenet szövege...", "select_recipient": "Címzett hozzáadása", + "cant_send": "Az egyik címzettnek nem küldhetsz üzenetet!", }, "de_de": { "recipients": "Empfänger", @@ -29,6 +31,7 @@ extension Localization on String { "message_subject": "Betreff...", "message_text": "Nachrichtentext...", "select_recipient": "Empfänger hinzufügen", + "cant_send": "Neki nem küldhetsz üzenetet!", }, };