lot of work in message sending thing
This commit is contained in:
parent
adc8deffa9
commit
f4a6a0ceb0
@ -34,7 +34,8 @@ Future<void> syncAll(BuildContext context) {
|
||||
print("INFO Syncing all");
|
||||
|
||||
UserProvider user = Provider.of<UserProvider>(context, listen: false);
|
||||
StatusProvider statusProvider = Provider.of<StatusProvider>(context, listen: false);
|
||||
StatusProvider statusProvider =
|
||||
Provider.of<StatusProvider>(context, listen: false);
|
||||
|
||||
List<Future<void>> tasks = [];
|
||||
int taski = 0;
|
||||
@ -47,10 +48,14 @@ Future<void> syncAll(BuildContext context) {
|
||||
|
||||
tasks = [
|
||||
syncStatus(Provider.of<GradeProvider>(context, listen: false).fetch()),
|
||||
syncStatus(Provider.of<TimetableProvider>(context, listen: false).fetch(week: Week.current())),
|
||||
syncStatus(Provider.of<TimetableProvider>(context, listen: false)
|
||||
.fetch(week: Week.current())),
|
||||
syncStatus(Provider.of<ExamProvider>(context, listen: false).fetch()),
|
||||
syncStatus(Provider.of<HomeworkProvider>(context, listen: false).fetch(from: DateTime.now().subtract(const Duration(days: 30)))),
|
||||
syncStatus(Provider.of<HomeworkProvider>(context, listen: false)
|
||||
.fetch(from: DateTime.now().subtract(const Duration(days: 30)))),
|
||||
syncStatus(Provider.of<MessageProvider>(context, listen: false).fetchAll()),
|
||||
syncStatus(
|
||||
Provider.of<MessageProvider>(context, listen: false).fetchRecipients()),
|
||||
syncStatus(Provider.of<NoteProvider>(context, listen: false).fetch()),
|
||||
syncStatus(Provider.of<EventProvider>(context, listen: false).fetch()),
|
||||
syncStatus(Provider.of<AbsenceProvider>(context, listen: false).fetch()),
|
||||
@ -58,14 +63,17 @@ Future<void> syncAll(BuildContext context) {
|
||||
// Sync student
|
||||
syncStatus(() async {
|
||||
if (user.user == null) return;
|
||||
Map? studentJson = await Provider.of<KretaClient>(context, listen: false).getAPI(KretaAPI.student(user.instituteCode!));
|
||||
Map? studentJson = await Provider.of<KretaClient>(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<DatabaseProvider>(context, listen: false).store.storeUser(user.user!);
|
||||
await Provider.of<DatabaseProvider>(context, listen: false)
|
||||
.store
|
||||
.storeUser(user.user!);
|
||||
}()),
|
||||
];
|
||||
|
||||
|
@ -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";
|
||||
|
@ -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((Map recipient) => Recipient.fromJson(recipient)).toList(),
|
||||
attachments: (message["csatolmanyok"] as List).cast<Map>().map((Map attachment) => Attachment.fromJson(attachment)).toList(),
|
||||
recipients: (message["cimzettLista"] as List)
|
||||
.cast<Map>()
|
||||
.map((Map recipient) => Recipient.fromJson(recipient))
|
||||
.toList(),
|
||||
attachments: (message["csatolmanyok"] as List)
|
||||
.cast<Map>()
|
||||
.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 }
|
||||
|
@ -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<void> fetchRecipients() async {
|
||||
Map<AddresseeType, SendRecipientType> addressable = {};
|
||||
|
||||
// check user
|
||||
User? user = Provider.of<UserProvider>(_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<KretaClient>(_context, listen: false)
|
||||
.getAPI(KretaAPI.recipientsTeacher);
|
||||
if (recipientsJson == null) {
|
||||
.getAPI(KretaAPI.recipientCategories);
|
||||
|
||||
// print(availableCategoriesJson);
|
||||
|
||||
// get recipients
|
||||
List? recipientTeachersJson =
|
||||
await Provider.of<KretaClient>(_context, listen: false)
|
||||
.getAPI(KretaAPI.recipientTeachers);
|
||||
List? recipientDirectorateJson =
|
||||
await Provider.of<KretaClient>(_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<SendRecipient> 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user