using CoviDok.Api; using CoviDok.Api.Request; using CoviDok.BLL.Sessions; using CoviDok.BLL.Storage; using CoviDok.BLL.User.Handlers; using CoviDok.Data.Model; using CoviDok.Data.MySQL; using CoviDok.Data.StorageProviders; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; namespace CoviDok.BLL.User.Managers { public class CaseManager { private readonly ICaseHandler handler = new MySqlCaseHandler(); private readonly StorageHandler storage = new StorageHandler(new MinioProvider()); public async Task> FilterCases(Session s, CaseFilter filter) { // TODO: Jogosultságkezelés return await handler.Filter(filter); } public async Task> GetUpdatesForCase(Session s, int id) { Case c = await handler.GetCase(id); if (c == null) throw new KeyNotFoundException(); if (handler.IsAuthorized(s.Id, c)) { return handler.GetUpdatesForCase(id); } else throw new UnauthorizedAccessException(); } public async Task SetCertified(Session s, int id) { Case c = await handler.GetCase(id); if (c == null) throw new KeyNotFoundException(); if (handler.IsAuthorized(s.Id, c) && s.Type == Api.Role.Ast) { if (c.CaseStatus == CaseStatus.Cured) { Update update = new Update { CaseId = c.Id, SenderId = s.Id, SenderRole = s.Type, CreatedDate = DateTime.Now, Content = "Case set to 'Certified'" }; await handler.SetCase(id, CaseStatus.Certified, update); } else throw new InvalidOperationException(); } else { throw new UnauthorizedAccessException(); } } public async Task SetCured(Session s, int id) { Case c = await handler.GetCase(id); if (c == null) throw new KeyNotFoundException(); if (s.Id == c.DoctorId) { if (c.CaseStatus == CaseStatus.InProgress) { Update update = new Update { CaseId = c.Id, SenderId = s.Id, SenderRole = s.Type, CreatedDate = DateTime.Now, Content = "Case set to 'Cured'" }; await handler.SetCase(id, CaseStatus.Cured, update); } else throw new InvalidOperationException(); } else { throw new UnauthorizedAccessException(); } } public async Task GetUpdate(Session s, int id) { // Parent, Doctor, and doctors assistants can access a case Update u = handler.GetUpadte(id); Case c = await handler.GetCase(u.CaseId); if (c == null) throw new KeyNotFoundException(); if (handler.IsAuthorized(s.Id, c)) { return u; } else { throw new UnauthorizedAccessException(); } } public async Task GetCase(Session s, int id) { // Parent, Doctor, and doctors assistants can access a case Case c = await handler.GetCase(id); if (c == null) throw new KeyNotFoundException(); if (handler.IsAuthorized(s.Id, c)) { return c; } else { throw new UnauthorizedAccessException(); } } public async Task SetPriority(Session s, int id, Priority priority) { Case c = await handler.GetCase(id); if (c == null) throw new KeyNotFoundException(); if (handler.IsAuthorized(s.Id, c)) { await handler.SetPriority(id, priority); } else { throw new UnauthorizedAccessException(); } } public async Task CreateCase(Session s, int DoctorId, int ChildId, string Title, DateTime startDate, Api.Priority priority) { // TODO szülő csak saját gyereket jelenthet Case c = new Case { StartDate = startDate, ChildId = ChildId, ParentId = s.Id, DoctorId = DoctorId, Title = Title, CreatedDate = DateTime.Now, LastModificationDate = DateTime.Now, CaseStatus = CaseStatus.InProgress, Assignee = s.Id, Priority = priority }; return await handler.AddCase(c); } public async Task UpdateCase(Session s, int id, string updateMsg, List Images) { Case c = await handler.GetCase(id); if (c == null) throw new KeyNotFoundException("Case Id not found: " + id); if (handler.IsAuthorized(s.Id, c)) { foreach (string ImageId in Images) { if (!await storage.ImageExists("test1", ImageId)) throw new InvalidOperationException(ImageId + " not found in storage!"); } if (c.CaseStatus == CaseStatus.Certified) throw new ArgumentException("Can't modify closed Case!"); if (s.Id == c.ParentId) c.Assignee = c.DoctorId; // Ha szülő updatel, az assignee az orvos lesz // TODO Ha a doki VAGY asszisztense frissít if (s.Id == c.DoctorId) c.Assignee = c.ParentId; // Ha doki frissít, a szülőhöz kerül c.LastModificationDate = DateTime.Now; Update update = new Update { CaseId = c.Id, SenderId = s.Id, SenderRole = s.Type, Content = updateMsg, CreatedDate = DateTime.Now, Images = Images }; await handler.UpdateCase(id, c, update); } else { throw new UnauthorizedAccessException(); } } } }