KanbanStore.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. Notification notification = new Notification();
  55. notification.Employee.ID = entity.EmployeeLink.ID;
  56. notification.Sender.ID = entity.ManagerLink.ID;
  57. notification.Title = Subject;
  58. notification.EntityType = typeof(Kanban).EntityName();
  59. notification.EntityID = entity.ID;
  60. notification.Description = BuildDescription(entity);
  61. notifications.Add(notification);
  62. }
  63. if (notifications.Any())
  64. FindSubStore<Notification>().Save(notifications, "");
  65. }
  66. private string BuildDescription(Kanban entity)
  67. {
  68. var notes = entity.Notes != null
  69. ? string.Join("\r\n", entity.Notes)
  70. .Split(new[] { "===================================\r\n" }, StringSplitOptions.RemoveEmptyEntries)
  71. : new string[] { };
  72. var summary = notes.LastOrDefault();
  73. if (string.IsNullOrEmpty(summary))
  74. summary = entity.Summary;
  75. if (string.IsNullOrEmpty(summary))
  76. summary = "";
  77. summary = Regex.Replace(summary.Replace("\r", "\n"), @"( |\r?\n|\r|\n)\1+", "$1");
  78. var sb = new StringBuilder();
  79. if (entity.EmployeeLink.HasOriginalValue(x => x.ID))
  80. sb.AppendLine("The above task has been assigned to you. Please check the relevant details, and action as required.");
  81. else
  82. sb.AppendLine("The above task has been changed. Please check the relevant details, and action as required.");
  83. if (entity.HasOriginalValue(x => x.Category) && !string.IsNullOrEmpty(entity.GetOriginalValue(x => x.Category)))
  84. {
  85. sb.AppendLine();
  86. sb.AppendLine(string.Format("- The task has progressed from {0} to {1}.", entity.GetOriginalValue(x => x.Category),
  87. entity.Category));
  88. }
  89. if (entity.HasOriginalValue(x => x.DueDate) && entity.GetOriginalValue(x => x.DueDate) != DateTime.MinValue)
  90. {
  91. sb.AppendLine();
  92. sb.AppendLine(string.Format("- The due date for the task has been changed from {0} to {1}.",
  93. entity.OriginalValues["DueDate"],
  94. entity.DueDate));
  95. }
  96. if (entity.HasOriginalValue(x => x.Notes) && entity.Notes.Count() > 1)
  97. {
  98. sb.AppendLine("- The notes for the task have been updated as follows:");
  99. foreach (var line in summary.Split('\n'))
  100. sb.AppendLine(" " + line);
  101. }
  102. return sb.ToString();
  103. }
  104. private void CheckNotificationRequired(Kanban entity)
  105. {
  106. if (entity.EmployeeLink.HasOriginalValue(x => x.ID) || entity.HasOriginalValue(x => x.Notes) ||
  107. entity.HasOriginalValue(x => x.Category) ||
  108. entity.HasOriginalValue(x => x.Summary) || entity.HasOriginalValue(x => x.DueDate))
  109. {
  110. NotifyUsers(entity);
  111. }
  112. else if (entity.ID != Guid.Empty && entity.HasOriginalValue(x => x.ID))
  113. {
  114. if (entity.GetOriginalValue<Kanban, Guid>("ID") == Guid.Empty)
  115. {
  116. NotifyUsers(entity);
  117. }
  118. }
  119. }
  120. protected override void BeforeSave(Kanban entity)
  121. {
  122. base.BeforeSave(entity);
  123. if (entity.Completed.IsEmpty())
  124. entity.Closed = DateTime.MinValue;
  125. }
  126. private void CheckKanbanTypeForms(Kanban kanban)
  127. {
  128. if(!kanban.Type.HasOriginalValue(x => x.ID))
  129. {
  130. return;
  131. }
  132. var kanbanForms = Provider.Query(
  133. new Filter<KanbanForm>(x => x.Parent.ID).IsEqualTo(kanban.ID)).Rows.Select(x => x.ToObject<KanbanForm>());
  134. var toDelete = kanbanForms.Where(x => string.IsNullOrWhiteSpace(x.FormData));
  135. Provider.Delete(toDelete, UserID);
  136. var kanbanTypeForms = Provider.Query(
  137. new Filter<KanbanTypeForm>(x => x.Type.ID).IsEqualTo(kanban.Type.ID)
  138. .And(x => x.Form.AppliesTo).IsEqualTo(nameof(Kanban)));
  139. var newForms = new List<KanbanForm>();
  140. foreach(var row in kanbanTypeForms.Rows)
  141. {
  142. var formID = row.Get<KanbanTypeForm, Guid>(x => x.Form.ID);
  143. if (!(kanbanForms.Any(x => x.Form.ID == formID) && !toDelete.Any(x => x.Form.ID == formID)))
  144. {
  145. var kanbanForm = new KanbanForm();
  146. kanbanForm.Form.ID = formID;
  147. kanbanForm.Parent.ID = kanban.ID;
  148. newForms.Add(kanbanForm);
  149. }
  150. }
  151. Provider.Save(newForms);
  152. }
  153. protected override void AfterSave(Kanban entity)
  154. {
  155. base.AfterSave(entity);
  156. CheckNotifications(entity);
  157. CheckNotificationRequired(entity);
  158. CheckKanbanTypeForms(entity);
  159. }
  160. }
  161. }