123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Comal.Classes;
- using InABox.Core;
- namespace Comal.Stores
- {
- public class FormSubscription
- {
- public Guid EmployeeID { get; set; }
- public Guid FormID { get; set; }
- }
- public static class FormSubscriptionCache
- {
- static FormSubscriptionCache()
- {
- Subscriptions = null;
- }
- public static FormSubscription[] Subscriptions { get; set; }
- }
- public abstract class EntityFormStore<TEntity, TParent, TParentLink> : BaseStore<TEntity>
- where TEntity : EntityForm<TParent, TParentLink, TEntity>, new()
- where TParent : Entity, new()
- where TParentLink : IEntityLink<TParent>, new()
- {
- private void CheckFormSubscriptionCache()
- {
- if (FormSubscriptionCache.Subscriptions == null)
- {
- Logger.Send(LogType.Information, UserID, "Refreshing Form Subscription Cache");
- var subs = Provider.Query(
- null,
- new Columns<EmployeeFormSubscription>(x => x.Employee.ID, x => x.Form.ID)
- );
- FormSubscriptionCache.Subscriptions = subs.Rows.Select(r =>
- new FormSubscription
- {
- EmployeeID = r.Get<EmployeeFormSubscription, Guid>(c => c.Employee.ID),
- FormID = r.Get<EmployeeFormSubscription, Guid>(c => c.Form.ID)
- }
- ).ToArray();
- }
- }
- private void CheckFormSubscriptions(TEntity entity)
- {
- if (entity is IDigitalFormInstance instance
- && entity.HasOriginalValue(x=>x.FormCompleted)
- && entity.GetOriginalValue(x=>x.FormCompleted) == DateTime.MinValue
- )
- {
- var updates = new List<Notification>();
- CheckFormSubscriptionCache();
- var formid = entity.Form.ID; //(Guid)CoreUtils.GetPropertyValue(entity, "Form.ID");
- var formname = entity.Form.Description; //(string)CoreUtils.GetPropertyValue(entity, "Form.Description");
- var userid = entity.FormCompletedBy.ID; //CoreUtils.GetPropertyValue(entity, "FormCompletedBy.ID");
- if (userid == Guid.Empty)
- userid = UserGuid;
- var emprow = userid != Guid.Empty
- ? Provider.Query(
- new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(userid),
- new Columns<Employee>(x => x.ID, x => x.Name)
- ).Rows.FirstOrDefault()
- : null;
- var empid = emprow != null ? emprow.Get<Employee, Guid>(x => x.ID) : Guid.Empty;
- var empname = emprow != null ? emprow.Get<Employee, string>(x => x.Name) : UserID;
- var subscriptions = FormSubscriptionCache.Subscriptions.Where(x => x.FormID == formid).ToArray();
- foreach (var subscription in subscriptions)
- {
- var notification = new Notification();
- notification.Employee.ID = subscription.EmployeeID;
- notification.Sender.ID = empid;
- notification.Title = "Form Completed: " + formname;
- notification.Description = string.Format("<html><body>{0} has completed a <u><b>{1}</b></u> digital form.</body></html>", empname,
- formname);
- notification.EntityType = entity.GetType().EntityName();
- notification.EntityID = entity.ID;
- updates.Add(notification);
- }
- if (updates.Any())
- FindSubStore<Notification>().Save(updates, "");
- }
- }
- protected override void AfterSave(TEntity entity)
- {
- base.AfterSave(entity);
- CheckFormSubscriptions(entity);
- CheckParentStatus(entity);
- }
- private void CheckParentStatus(TEntity entity)
- {
- if (entity.HasOriginalValue(x=>x.FormCompleted) || entity.HasOriginalValue(x=>x.FormCancelled))
- {
- bool isFinal = false;
- TParent? parent = null;
- Task[] tasks = new Task[]
- {
- Task.Run(() =>
- {
- isFinal = Provider.Query(
- new Filter<DigitalForm>(x => x.ID).IsEqualTo(entity.Form.ID),
- new Columns<DigitalForm>(x => x.Final)
- ).Rows.FirstOrDefault()?.Get<DigitalForm, bool>(x => x.Final) ?? false;
- }),
- Task.Run(() =>
- {
- parent = Provider.Query(
- new Filter<TParent>(x=>x.ID).IsEqualTo(entity.ParentID()),
- LookupFactory.RequiredColumns<TParent>()
- ).Rows
- .FirstOrDefault()?
- .ToObject<TParent>();
- })
- };
- Task.WaitAll(tasks);
- if (isFinal && (parent != null))
- {
- UpdateParentStatus(entity, parent);
- if (parent.IsChanged())
- FindSubStore<TParent>().Save(parent, $"{typeof(TParent).EntityName().Split('.').Last()} status updated by {entity.Form.Description}");
- }
-
- }
- }
- protected virtual void UpdateParentStatus(IDigitalFormInstance form, TParent parent)
- {
-
- }
- }
- public class AssignmentFormStore : EntityFormStore<AssignmentForm, Assignment, AssignmentLink>
- {
- protected override void UpdateParentStatus(IDigitalFormInstance instance, Assignment parent)
- {
- if (instance.FormCompleted.IsEmpty() != parent.Completed.IsEmpty())
- parent.Completed = instance.FormCompleted;
- }
- }
- public class LeaveRequestFormStore : EntityFormStore<LeaveRequestForm, LeaveRequest, LeaveRequestLink>
- {
- protected override void UpdateParentStatus(IDigitalFormInstance form, LeaveRequest parent)
- {
- base.UpdateParentStatus(form, parent);
- if (!form.FormCancelled.IsEmpty())
- {
- parent.Status = LeaveRequestStatus.Rejected;
- parent.StatusNotes = $"{parent.StatusNotes}\nLeave Form cancelled by user".Trim();
- }
- else if (!form.FormCompleted.IsEmpty())
- {
- if (parent.Status == LeaveRequestStatus.NotSubmitted)
- {
- parent.Status = LeaveRequestStatus.InProgress;
- parent.StatusNotes = $"{parent.StatusNotes}\nLeave Form submitted by user".Trim();
- }
- }
- else
- {
- parent.Status = LeaveRequestStatus.NotSubmitted;
- parent.StatusNotes = $"{parent.StatusNotes}\nLeave Form re-opened by user".Trim();
- }
- }
- }
- public class JobITPFormStore : EntityFormStore<JobITPForm, JobITP, JobITPLink>
- {
- }
- public class JobFormStore : EntityFormStore<JobForm, Job, JobLink>
- {
- }
- public class EmployeeFormStore : EntityFormStore<EmployeeForm, Employee, EmployeeLink>
- {
- }
- public class TimeSheetFormStore : EntityFormStore<TimeSheetForm, TimeSheet, TimeSheetLink>
- {
- }
- public class KanbanFormStore : EntityFormStore<KanbanForm, Kanban, KanbanLink>
- {
- protected override void UpdateParentStatus(IDigitalFormInstance instance, Kanban parent)
- {
- if (instance.FormCompleted.IsEmpty() != parent.Completed.IsEmpty())
- parent.Completed = instance.FormCompleted;
- }
- }
- }
|