KanbanStore.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.Database;
  9. using System;
  10. namespace Comal.Stores
  11. {
  12. public class ServerKanbanSubscriberSet : BaseKanbanSubscriberSet
  13. {
  14. public ServerKanbanSubscriberSet(IEnumerable<Guid> kanbanids) : base(kanbanids)
  15. {
  16. }
  17. protected override void DoSave(IEnumerable<KanbanSubscriber> updates)
  18. => DbFactory.NewProvider(Logger.Main).Save(updates);
  19. protected override void DoDelete(KanbanSubscriber[] deletes)
  20. => DbFactory.NewProvider(Logger.Main).Delete(deletes,"");
  21. protected override CoreTable DoQuery(Filter<KanbanSubscriber> filter, Columns<KanbanSubscriber> columns)
  22. => DbFactory.NewProvider(Logger.Main).Query(filter, columns);
  23. }
  24. internal class KanbanStore : ScheduleActionStore<Kanban>
  25. {
  26. private void CheckNotifications(Kanban entity)
  27. {
  28. if (entity.HasOriginalValue(x => x.Completed) && !entity.Completed.IsEmpty())
  29. {
  30. // Find all notifications linked to this task
  31. var notifications = Provider.Query(
  32. new Filter<Notification>(x => x.EntityType).IsEqualTo(typeof(Kanban).EntityName()).And(x => x.EntityID).IsEqualTo(entity.ID)
  33. .And(x => x.Closed).IsEqualTo(DateTime.MinValue),
  34. Columns.None<Notification>().Add(x => x.ID)
  35. ).Rows.Select(x => x.ToObject<Notification>()).ToArray();
  36. foreach (var notification in notifications)
  37. notification.Closed = entity.Completed;
  38. if (notifications.Any())
  39. FindSubStore<Notification>().Save(notifications, "");
  40. }
  41. }
  42. private void NotifyUsers(Kanban entity)
  43. {
  44. var Subject = $"Updated Task: {entity.Title} (Due: {entity.DueDate:dd MMM yy})";
  45. var emps = Provider.Query(
  46. new Filter<KanbanSubscriber>(x => x.Kanban.ID).IsEqualTo(entity.ID),
  47. Columns.None<KanbanSubscriber>().Add(x => x.Employee.ID, x => x.Employee.UserLink.ID));
  48. var senderrow = emps.Rows.FirstOrDefault(r => r.Get<KanbanSubscriber, Guid>(c => c.Employee.UserLink.ID).Equals(UserGuid));
  49. var senderid = senderrow?.Get<KanbanSubscriber, Guid>(c => c.Employee.ID) ?? Guid.Empty;
  50. var notifications = new List<Notification>();
  51. foreach (var row in emps.Rows)
  52. {
  53. var userid = row.Get<KanbanSubscriber, Guid>(c => c.Employee.UserLink.ID);
  54. if (userid != UserGuid)
  55. {
  56. var notification = new Notification();
  57. notification.Employee.ID = row.Get<KanbanSubscriber, Guid>(c => c.Employee.ID);
  58. notification.Sender.ID = senderid;
  59. notification.Title = Subject;
  60. notification.EntityType = typeof(Kanban).EntityName();
  61. notification.EntityID = entity.ID;
  62. notification.Description = BuildDescription(entity);
  63. notifications.Add(notification);
  64. }
  65. }
  66. if (emps.Rows.Count == 0)
  67. {
  68. var userID = Provider.Query(
  69. new Filter<Employee>(x => x.ID).IsEqualTo(entity.EmployeeLink.ID),
  70. Columns.None<Employee>().Add(x => x.UserLink.ID)
  71. ).Rows.FirstOrDefault()?.Get<Employee, Guid>(x => x.UserLink.ID);
  72. if(userID != UserGuid)
  73. {
  74. Notification notification = new Notification();
  75. notification.Employee.ID = entity.EmployeeLink.ID;
  76. notification.Sender.ID = entity.ManagerLink.ID;
  77. notification.Title = Subject;
  78. notification.EntityType = typeof(Kanban).EntityName();
  79. notification.EntityID = entity.ID;
  80. notification.Description = BuildDescription(entity);
  81. notifications.Add(notification);
  82. }
  83. }
  84. if (notifications.Any())
  85. FindSubStore<Notification>().Save(notifications, "");
  86. }
  87. private string BuildDescription(Kanban entity)
  88. {
  89. var notes = string.Join("\r\n", entity.Notes)
  90. .Split(new[] { "===================================\r\n" }, StringSplitOptions.RemoveEmptyEntries);
  91. var summary = notes.LastOrDefault();
  92. if (string.IsNullOrEmpty(summary))
  93. summary = entity.Summary;
  94. if (string.IsNullOrEmpty(summary))
  95. summary = "";
  96. summary = Regex.Replace(summary.Replace("\r", "\n"), @"( |\r?\n|\r|\n)\1+", "$1");
  97. var sb = new StringBuilder();
  98. if (entity.EmployeeLink.HasOriginalValue(x => x.ID))
  99. sb.AppendLine("The above task has been assigned to you. Please check the relevant details, and action as required.");
  100. else
  101. sb.AppendLine("The above task has been changed. Please check the relevant details, and action as required.");
  102. if (entity.HasOriginalValue(x => x.Status))
  103. {
  104. sb.AppendLine();
  105. sb.AppendLine(string.Format("- The task has progressed from {0} to {1}.", entity.GetOriginalValue(x => x.Status),
  106. entity.Status));
  107. }
  108. if (entity.HasOriginalValue(x => x.DueDate) && entity.GetOriginalValue(x => x.DueDate) != DateTime.MinValue)
  109. {
  110. sb.AppendLine();
  111. sb.AppendLine(string.Format("- The due date for the task has been changed from {0} to {1}.",
  112. entity.OriginalValues["DueDate"],
  113. entity.DueDate));
  114. }
  115. if (entity.HasOriginalValue(x => x.Notes) && entity.Notes.Count() > 1)
  116. {
  117. sb.AppendLine("- The notes for the task have been updated as follows:");
  118. foreach (var line in summary.Split('\n'))
  119. sb.AppendLine(" " + line);
  120. }
  121. return sb.ToString();
  122. }
  123. private void CheckNotificationRequired(Kanban entity)
  124. {
  125. if (entity.EmployeeLink.HasOriginalValue(x => x.ID) || entity.HasOriginalValue(x => x.Notes) ||
  126. entity.HasOriginalValue(x => x.Status) ||
  127. entity.HasOriginalValue(x => x.Summary) || entity.HasOriginalValue(x => x.DueDate))
  128. {
  129. NotifyUsers(entity);
  130. }
  131. else if (entity.ID != Guid.Empty && entity.HasOriginalValue(x => x.ID))
  132. {
  133. if (entity.GetOriginalValue<Kanban, Guid>("ID") == Guid.Empty)
  134. {
  135. NotifyUsers(entity);
  136. }
  137. }
  138. }
  139. protected override void BeforeSave(Kanban entity)
  140. {
  141. base.BeforeSave(entity);
  142. if (entity.Completed.IsEmpty())
  143. entity.Closed = DateTime.MinValue;
  144. }
  145. private void CheckKanbanTypeForms(Kanban kanban)
  146. {
  147. if(!kanban.Type.HasOriginalValue(x => x.ID))
  148. {
  149. return;
  150. }
  151. var kanbanForms = Provider.Query(
  152. new Filter<KanbanForm>(x => x.Parent.ID).IsEqualTo(kanban.ID)).Rows.Select(x => x.ToObject<KanbanForm>()).ToArray();
  153. var toDelete = kanbanForms.Where(x => string.IsNullOrWhiteSpace(x.FormData)).ToArray();
  154. Provider.Delete(toDelete, UserID);
  155. var kanbanTypeForms = Provider.Query(
  156. new Filter<KanbanTypeForm>(x => x.Type.ID).IsEqualTo(kanban.Type.ID)
  157. .And(x => x.Form.AppliesTo).IsEqualTo(nameof(Kanban)));
  158. var newForms = new List<KanbanForm>();
  159. foreach(var row in kanbanTypeForms.Rows)
  160. {
  161. var formID = row.Get<KanbanTypeForm, Guid>(x => x.Form.ID);
  162. if (!(kanbanForms.Any(x => x.Form.ID == formID) && toDelete.All(x => x.Form.ID != formID)))
  163. {
  164. var kanbanForm = new KanbanForm();
  165. kanbanForm.Form.ID = formID;
  166. kanbanForm.Parent.ID = kanban.ID;
  167. newForms.Add(kanbanForm);
  168. }
  169. }
  170. Provider.Save(newForms);
  171. }
  172. protected override void AfterSave(Kanban entity)
  173. {
  174. base.AfterSave(entity);
  175. CheckNotifications(entity);
  176. CheckNotificationRequired(entity);
  177. CheckKanbanTypeForms(entity);
  178. //CheckSubscribers(entity);
  179. }
  180. // private void CheckSubscribers(Kanban entity)
  181. // {
  182. // if (entity.EmployeeLink.HasOriginalValue(x => x.ID) || (entity.ManagerLink.HasOriginalValue(x => x.ID)))
  183. // {
  184. // var set = new ServerKanbanSubscriberSet(new Guid[] { entity.ID });
  185. // if (entity.EmployeeLink.HasOriginalValue(x => x.ID))
  186. // set.EnsureAssignee(entity.ID,entity.EmployeeLink.ID);
  187. // if (entity.ManagerLink.HasOriginalValue(x => x.ID))
  188. // set.EnsureManager(entity.ID,entity.ManagerLink.ID);
  189. // set.Save();
  190. //
  191. // }
  192. // }
  193. }
  194. }