diff --git a/filcnaplo/lib/api/providers/sync.dart b/filcnaplo/lib/api/providers/sync.dart index 42f6277..23089f9 100644 --- a/filcnaplo/lib/api/providers/sync.dart +++ b/filcnaplo/lib/api/providers/sync.dart @@ -34,7 +34,8 @@ Future syncAll(BuildContext context) { print("INFO Syncing all"); UserProvider user = Provider.of(context, listen: false); - StatusProvider statusProvider = Provider.of(context, listen: false); + StatusProvider statusProvider = + Provider.of(context, listen: false); List> tasks = []; int taski = 0; @@ -47,10 +48,14 @@ Future syncAll(BuildContext context) { tasks = [ syncStatus(Provider.of(context, listen: false).fetch()), - syncStatus(Provider.of(context, listen: false).fetch(week: Week.current())), + syncStatus(Provider.of(context, listen: false) + .fetch(week: Week.current())), syncStatus(Provider.of(context, listen: false).fetch()), - syncStatus(Provider.of(context, listen: false).fetch(from: DateTime.now().subtract(const Duration(days: 30)))), + syncStatus(Provider.of(context, listen: false) + .fetch(from: DateTime.now().subtract(const Duration(days: 30)))), syncStatus(Provider.of(context, listen: false).fetchAll()), + syncStatus( + Provider.of(context, listen: false).fetchRecipients()), syncStatus(Provider.of(context, listen: false).fetch()), syncStatus(Provider.of(context, listen: false).fetch()), syncStatus(Provider.of(context, listen: false).fetch()), @@ -58,14 +63,17 @@ Future syncAll(BuildContext context) { // Sync student syncStatus(() async { if (user.user == null) return; - Map? studentJson = await Provider.of(context, listen: false).getAPI(KretaAPI.student(user.instituteCode!)); + Map? studentJson = await Provider.of(context, listen: false) + .getAPI(KretaAPI.student(user.instituteCode!)); if (studentJson == null) return; Student student = Student.fromJson(studentJson); user.user?.name = student.name; // Store user - await Provider.of(context, listen: false).store.storeUser(user.user!); + await Provider.of(context, listen: false) + .store + .storeUser(user.user!); }()), ]; diff --git a/filcnaplo_kreta_api/lib/client/api.dart b/filcnaplo_kreta_api/lib/client/api.dart index 5060149..fb1e624 100644 --- a/filcnaplo_kreta_api/lib/client/api.dart +++ b/filcnaplo_kreta_api/lib/client/api.dart @@ -79,8 +79,10 @@ class KretaAPI { BaseKreta.kretaAdmin + KretaAdminEndpoints.recipientCategories; static const availableCategories = BaseKreta.kretaAdmin + KretaAdminEndpoints.availableCategories; - static const recipientsTeacher = - BaseKreta.kretaAdmin + KretaAdminEndpoints.recipientsTeacher; + static const recipientTeachers = + BaseKreta.kretaAdmin + KretaAdminEndpoints.recipientTeachers; + static const recipientDirectorate = + BaseKreta.kretaAdmin + KretaAdminEndpoints.recipientDirectorate; static const uploadAttachment = BaseKreta.kretaAdmin + KretaAdminEndpoints.uploadAttachment; static String downloadAttachment(String id) => @@ -131,7 +133,8 @@ class KretaAdminEndpoints { "/api/v1/kommunikacio/postaladaelemek/$id"; static const recipientCategories = "/api/v1/adatszotarak/cimzetttipusok"; static const availableCategories = "/api/v1/kommunikacio/cimezhetotipusok"; - static const recipientsTeacher = "/api/v1/kreta/alkalmazottak/tanar"; + static const recipientTeachers = "/api/v1/kreta/alkalmazottak/tanar"; + static const recipientDirectorate = "/api/v1/kreta/alkalmazottak/igazgatosag"; static const uploadAttachment = "/ideiglenesfajlok"; static String downloadAttachment(String id) => "/api/v1/dokumentumok/uzenetek/$id"; diff --git a/filcnaplo_kreta_api/lib/models/message.dart b/filcnaplo_kreta_api/lib/models/message.dart index 97cd5f3..519cbd3 100644 --- a/filcnaplo_kreta_api/lib/models/message.dart +++ b/filcnaplo_kreta_api/lib/models/message.dart @@ -68,13 +68,21 @@ class Message { messageId: message["azonosito"], seen: json["isElolvasva"] ?? false, deleted: json["isToroltElem"] ?? false, - date: message["kuldesDatum"] != null ? DateTime.parse(message["kuldesDatum"]).toLocal() : DateTime(0), + date: message["kuldesDatum"] != null + ? DateTime.parse(message["kuldesDatum"]).toLocal() + : DateTime(0), author: (message["feladoNev"] ?? "").trim(), content: message["szoveg"].replaceAll("\r", "") ?? "", subject: message["targy"] ?? "", type: type, - recipients: (message["cimzettLista"] as List).cast().map((Map recipient) => Recipient.fromJson(recipient)).toList(), - attachments: (message["csatolmanyok"] as List).cast().map((Map attachment) => Attachment.fromJson(attachment)).toList(), + recipients: (message["cimzettLista"] as List) + .cast() + .map((Map recipient) => Recipient.fromJson(recipient)) + .toList(), + attachments: (message["csatolmanyok"] as List) + .cast() + .map((Map attachment) => Attachment.fromJson(attachment)) + .toList(), replyId: message["elozoUzenetAzonosito"], conversationId: message["beszelgetesAzonosito"], json: json, @@ -105,3 +113,89 @@ class Conversation { Message get newest => _messages.first; } + +// sendable message object and it's things +class SendMessage { + Map? json; + int id; + int lastMessageId; + String? subject; + String text; + + SendMessage({ + required this.id, + required this.lastMessageId, + this.subject, + required this.text, + }); +} + +class SendRecipient { + Map? json; + int? id; + int? kretaId; + String? name; + SendRecipientType type; + + SendRecipient({ + required this.id, + required this.kretaId, + required this.name, + required this.type, + this.json, + }); + + factory SendRecipient.fromJson(Map json, SendRecipientType type) { + return SendRecipient( + id: json['azonosito'], + kretaId: json['kretaAzonosito'], + name: json['name'], + type: type, + json: json, + ); + } + + Object get kretaJson => { + 'azonosito': id, + 'kretaAzonosito': kretaId, + 'nev': name, + 'tipus': { + 'azonosito': type.id, + 'kod': type.code, + 'leiras': type.description, + 'nev': type.name, + 'rovidNev': type.shortName, + } + }; +} + +class SendRecipientType { + Map? json; + int id; + String code; + String description; + String name; + String shortName; + + SendRecipientType({ + required this.id, + required this.code, + required this.description, + required this.name, + required this.shortName, + this.json, + }); + + factory SendRecipientType.fromJson(Map json) { + return SendRecipientType( + id: json['azonosito'], + code: json['kod'], + description: json['leiras'], + name: json['nev'], + shortName: json['rovidNev'], + json: json, + ); + } +} + +enum AddresseeType { teachers, directorate } diff --git a/filcnaplo_kreta_api/lib/providers/message_provider.dart b/filcnaplo_kreta_api/lib/providers/message_provider.dart index 5e15574..8bd1fcd 100644 --- a/filcnaplo_kreta_api/lib/providers/message_provider.dart +++ b/filcnaplo_kreta_api/lib/providers/message_provider.dart @@ -1,3 +1,5 @@ +// ignore_for_file: use_build_context_synchronously + import 'package:filcnaplo/api/providers/user_provider.dart'; import 'package:filcnaplo/api/providers/database_provider.dart'; import 'package:filcnaplo/models/user.dart'; @@ -92,19 +94,64 @@ class MessageProvider with ChangeNotifier { // fetch recipients Future fetchRecipients() async { + Map addressable = {}; + // check user User? user = Provider.of(_context, listen: false).user; if (user == null) throw "Cannot fetch Messages for User null"; - // get recipients - List? recipientsJson = + // get categories + List? availableCategoriesJson = await Provider.of(_context, listen: false) - .getAPI(KretaAPI.recipientsTeacher); - if (recipientsJson == null) { + .getAPI(KretaAPI.recipientCategories); + + // print(availableCategoriesJson); + + // get recipients + List? recipientTeachersJson = + await Provider.of(_context, listen: false) + .getAPI(KretaAPI.recipientTeachers); + List? recipientDirectorateJson = + await Provider.of(_context, listen: false) + .getAPI(KretaAPI.recipientDirectorate); + + if (availableCategoriesJson == null || + recipientTeachersJson == null || + recipientDirectorateJson == null) { throw "Cannot fetch Recipients for User ${user.id}"; } - print(recipientsJson); + for (var e in availableCategoriesJson) { + // print(e); + switch (e['kod']) { + case 'TANAR': + addressable + .addAll({AddresseeType.teachers: SendRecipientType.fromJson(e)}); + break; + case 'IGAZGATOSAG': + addressable.addAll( + {AddresseeType.directorate: SendRecipientType.fromJson(e)}); + break; + default: + break; + } + } + + // parse recipients + List recipients = []; + + if (addressable.containsKey(AddresseeType.teachers)) { + recipients.addAll(recipientTeachersJson.map((e) => + SendRecipient.fromJson(e, addressable[AddresseeType.teachers]!))); + } + if (addressable.containsKey(AddresseeType.directorate)) { + recipients.addAll(recipientDirectorateJson.map((e) => + SendRecipient.fromJson(e, addressable[AddresseeType.directorate]!))); + } + + print(addressable); + print(recipients); + print(recipients.first.json); } // send message