123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.Database;
- using System;
- namespace Comal.Stores
- {
- public class ServerKanbanSubscriberSet : BaseKanbanSubscriberSet
- {
- public ServerKanbanSubscriberSet(IEnumerable<Guid> kanbanids) : base(kanbanids)
- {
-
- }
-
- protected override void DoSave(IEnumerable<KanbanSubscriber> updates)
- => DbFactory.NewProvider(Logger.Main).Save(updates);
- protected override void DoDelete(KanbanSubscriber[] deletes)
- => DbFactory.NewProvider(Logger.Main).Delete(deletes,"");
- protected override CoreTable DoQuery(Filter<KanbanSubscriber> filter, Columns<KanbanSubscriber> columns)
- => DbFactory.NewProvider(Logger.Main).Query(filter, columns);
-
- }
-
- internal class KanbanStore : ScheduleActionStore<Kanban>
- {
- private void CheckNotifications(Kanban entity)
- {
- if (entity.HasOriginalValue(x => x.Completed) && !entity.Completed.IsEmpty())
- {
- // Find all notifications linked to this task
- var notifications = Provider.Query(
- new Filter<Notification>(x => x.EntityType).IsEqualTo(typeof(Kanban).EntityName()).And(x => x.EntityID).IsEqualTo(entity.ID)
- .And(x => x.Closed).IsEqualTo(DateTime.MinValue),
- Columns.None<Notification>().Add(x => x.ID)
- ).Rows.Select(x => x.ToObject<Notification>()).ToArray();
- foreach (var notification in notifications)
- notification.Closed = entity.Completed;
- if (notifications.Any())
- FindSubStore<Notification>().Save(notifications, "");
- }
- }
- private void NotifyUsers(Kanban entity)
- {
- var Subject = $"Updated Task: {entity.Title} (Due: {entity.DueDate:dd MMM yy})";
- var emps = Provider.Query(
- new Filter<KanbanSubscriber>(x => x.Kanban.ID).IsEqualTo(entity.ID),
- Columns.None<KanbanSubscriber>().Add(x => x.Employee.ID, x => x.Employee.UserLink.ID));
- var senderrow = emps.Rows.FirstOrDefault(r => r.Get<KanbanSubscriber, Guid>(c => c.Employee.UserLink.ID).Equals(UserGuid));
- var senderid = senderrow?.Get<KanbanSubscriber, Guid>(c => c.Employee.ID) ?? Guid.Empty;
- var notifications = new List<Notification>();
- foreach (var row in emps.Rows)
- {
- var userid = row.Get<KanbanSubscriber, Guid>(c => c.Employee.UserLink.ID);
- if (userid != UserGuid)
- {
- var notification = new Notification();
- notification.Employee.ID = row.Get<KanbanSubscriber, Guid>(c => c.Employee.ID);
- notification.Sender.ID = senderid;
- notification.Title = Subject;
- notification.EntityType = typeof(Kanban).EntityName();
- notification.EntityID = entity.ID;
- notification.Description = BuildDescription(entity);
- notifications.Add(notification);
- }
- }
- if (emps.Rows.Count == 0)
- {
- var userID = Provider.Query(
- new Filter<Employee>(x => x.ID).IsEqualTo(entity.EmployeeLink.ID),
- Columns.None<Employee>().Add(x => x.UserLink.ID)
- ).Rows.FirstOrDefault()?.Get<Employee, Guid>(x => x.UserLink.ID);
- if(userID != UserGuid)
- {
- Notification notification = new Notification();
- notification.Employee.ID = entity.EmployeeLink.ID;
- notification.Sender.ID = entity.ManagerLink.ID;
- notification.Title = Subject;
- notification.EntityType = typeof(Kanban).EntityName();
- notification.EntityID = entity.ID;
- notification.Description = BuildDescription(entity);
- notifications.Add(notification);
- }
- }
- if (notifications.Any())
- FindSubStore<Notification>().Save(notifications, "");
- }
- private string BuildDescription(Kanban entity)
- {
- var notes = string.Join("\r\n", entity.Notes)
- .Split(new[] { "===================================\r\n" }, StringSplitOptions.RemoveEmptyEntries);
- var summary = notes.LastOrDefault();
- if (string.IsNullOrEmpty(summary))
- summary = entity.Summary;
- if (string.IsNullOrEmpty(summary))
- summary = "";
- summary = Regex.Replace(summary.Replace("\r", "\n"), @"( |\r?\n|\r|\n)\1+", "$1");
- var sb = new StringBuilder();
- if (entity.EmployeeLink.HasOriginalValue(x => x.ID))
- sb.AppendLine("The above task has been assigned to you. Please check the relevant details, and action as required.");
- else
- sb.AppendLine("The above task has been changed. Please check the relevant details, and action as required.");
- if (entity.HasOriginalValue(x => x.Status))
- {
- sb.AppendLine();
- sb.AppendLine(string.Format("- The task has progressed from {0} to {1}.", entity.GetOriginalValue(x => x.Status),
- entity.Status));
- }
- if (entity.HasOriginalValue(x => x.DueDate) && entity.GetOriginalValue(x => x.DueDate) != DateTime.MinValue)
- {
- sb.AppendLine();
- sb.AppendLine(string.Format("- The due date for the task has been changed from {0} to {1}.",
- entity.OriginalValues["DueDate"],
- entity.DueDate));
- }
- if (entity.HasOriginalValue(x => x.Notes) && entity.Notes.Count() > 1)
- {
- sb.AppendLine("- The notes for the task have been updated as follows:");
- foreach (var line in summary.Split('\n'))
- sb.AppendLine(" " + line);
- }
- return sb.ToString();
- }
- private void CheckNotificationRequired(Kanban entity)
- {
- if (entity.EmployeeLink.HasOriginalValue(x => x.ID) || entity.HasOriginalValue(x => x.Notes) ||
- entity.HasOriginalValue(x => x.Status) ||
- entity.HasOriginalValue(x => x.Summary) || entity.HasOriginalValue(x => x.DueDate))
- {
- NotifyUsers(entity);
- }
- else if (entity.ID != Guid.Empty && entity.HasOriginalValue(x => x.ID))
- {
- if (entity.GetOriginalValue<Kanban, Guid>("ID") == Guid.Empty)
- {
- NotifyUsers(entity);
- }
- }
- }
- protected override void BeforeSave(Kanban entity)
- {
- base.BeforeSave(entity);
- if (entity.Completed.IsEmpty())
- entity.Closed = DateTime.MinValue;
- }
- private void CheckKanbanTypeForms(Kanban kanban)
- {
- if(!kanban.Type.HasOriginalValue(x => x.ID))
- {
- return;
- }
- var kanbanForms = Provider.Query(
- new Filter<KanbanForm>(x => x.Parent.ID).IsEqualTo(kanban.ID)).Rows.Select(x => x.ToObject<KanbanForm>()).ToArray();
- var toDelete = kanbanForms.Where(x => string.IsNullOrWhiteSpace(x.FormData)).ToArray();
- Provider.Delete(toDelete, UserID);
- var kanbanTypeForms = Provider.Query(
- new Filter<KanbanTypeForm>(x => x.Type.ID).IsEqualTo(kanban.Type.ID)
- .And(x => x.Form.AppliesTo).IsEqualTo(nameof(Kanban)));
- var newForms = new List<KanbanForm>();
- foreach(var row in kanbanTypeForms.Rows)
- {
- var formID = row.Get<KanbanTypeForm, Guid>(x => x.Form.ID);
- if (!(kanbanForms.Any(x => x.Form.ID == formID) && toDelete.All(x => x.Form.ID != formID)))
- {
- var kanbanForm = new KanbanForm();
- kanbanForm.Form.ID = formID;
- kanbanForm.Parent.ID = kanban.ID;
- newForms.Add(kanbanForm);
- }
- }
- Provider.Save(newForms);
- }
- protected override void AfterSave(Kanban entity)
- {
- base.AfterSave(entity);
- CheckNotifications(entity);
- CheckNotificationRequired(entity);
- CheckKanbanTypeForms(entity);
- //CheckSubscribers(entity);
- }
- // private void CheckSubscribers(Kanban entity)
- // {
- // if (entity.EmployeeLink.HasOriginalValue(x => x.ID) || (entity.ManagerLink.HasOriginalValue(x => x.ID)))
- // {
- // var set = new ServerKanbanSubscriberSet(new Guid[] { entity.ID });
- // if (entity.EmployeeLink.HasOriginalValue(x => x.ID))
- // set.EnsureAssignee(entity.ID,entity.EmployeeLink.ID);
- // if (entity.ManagerLink.HasOriginalValue(x => x.ID))
- // set.EnsureManager(entity.ID,entity.ManagerLink.ID);
- // set.Save();
- //
- // }
- // }
-
- }
- }
|