KanbanStore.cs 8.0 KB

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