Beispiel #1
0
        public async Task SignaturesReceived(User trainer, Models.Patrol patrol, User trainee, Assignment assignment, Plan plan, List <Tuple <Section, Skill, Level> > signatures, DateTime when)
        {
            var localWhen = when.UtcToPatrolLocal(patrol);

            string plainText = $"You've got new signatures on {plan.Name} from {trainer.GetFullName()}.\n{localWhen.ToShortDateString()} {localWhen.ToShortTimeString()}\n";
            string html      = $"You've got new signatures on {plan.Name} from {trainer.GetFullName()}.<br/>{localWhen.ToShortDateString()} {localWhen.ToShortTimeString()}<br/><table>";

            foreach (var sig in signatures)
            {
                plainText = plainText + $"\t{sig.Item1.Name}\t{sig.Item2.Name}\t{sig.Item3.Name}\n";
                html      = html + $"<tr><td>{sig.Item1.Name}</td><td>{sig.Item2.Name}</td><td>{sig.Item3.Name}</td></tr>";
            }
            plainText = plainText + "\nNice work!";
            html      = html + "</table><br/><strong>Nice work!</strong>";

            var msg = new SendGridMessage()
            {
                Subject          = $"New Signatures on {plan.Name} from {trainer.GetFullName()}",
                PlainTextContent = plainText,
                HtmlContent      = html,
            };

            this.AddOnBehalfOf(msg, patrol);

            var response = await Send(msg, patrol, trainee);
        }
Beispiel #2
0
        private void AddOnBehalfOf(SendGridMessage message, Models.Patrol patrol)
        {
            message.PlainTextContent += $"\n\nOn behalf of {patrol.Name}";
            message.HtmlContent      += $"<br/><br/><em>On behalf of {patrol.Name}</em>";

            AddToUnsubscribe(message);
        }
Beispiel #3
0
        public async Task SendShiftRejected(User assigned, Models.Patrol patrol, User claimed, User rejected, ScheduledShift shift)
        {
            var localStart = shift.StartsAt.UtcToPatrolLocal(patrol);
            var localEnd   = shift.EndsAt.UtcToPatrolLocal(patrol);

            if (assigned == null)
            {
                var msg = new SendGridMessage()
                {
                    Subject          = $"Shift claim on {localStart.ToShortDateString()} to {claimed.GetFullName()} Rejected",
                    PlainTextContent = $"Your shift claim on {localStart.ToShortDateString()} to {claimed.GetFullName()} has been rejected",
                    HtmlContent      = $"Your shift Claim on {localStart.ToShortDateString()} to {claimed.GetFullName()} has been rejected",
                };

                this.AddOnBehalfOf(msg, patrol);

                var response = await Send(msg, patrol, claimed);
            }
            else
            {
                var msg = new SendGridMessage()
                {
                    Subject          = $"Shift swap on {localStart.ToShortDateString()} from {assigned.GetFullName()} to {claimed.GetFullName()} Rejected",
                    PlainTextContent = $"The shift swap on {localStart.ToShortDateString()} from {assigned.GetFullName()} to {claimed.GetFullName()} has been rejected",
                    HtmlContent      = $"The shift swap on {localStart.ToShortDateString()} from {assigned.GetFullName()} to {claimed.GetFullName()} has been rejected",
                };

                this.AddOnBehalfOf(msg, patrol);

                var response = await Send(msg, patrol, assigned, claimed);
            }
        }
Beispiel #4
0
 public static DateTime?UtcFromPatrolLocal(this DateTime?local, Models.Patrol patrol)
 {
     if (local.HasValue)
     {
         return(local.Value.UtcFromPatrolLocal(patrol));
     }
     else
     {
         return(null);
     }
 }
Beispiel #5
0
 public static DateTime?UtcToPatrolLocal(this DateTime?utc, Models.Patrol patrol)
 {
     if (utc.HasValue)
     {
         return(utc.Value.UtcToPatrolLocal(patrol));
     }
     else
     {
         return(null);
     }
 }
Beispiel #6
0
        public async Task <IActionResult> CreateEmptyPatrol(Models.Patrol patrolSetup)
        {
            //TODO: update to also update jwt
            var patrol = await _patrolCreationService.CreateNewPatrol(User.UserId(), patrolSetup);

            //TODO: we don't need this once the UI uses the JWT
            var patrols = await _patrolRepository.GetPatrolsForUser(User.UserId());

            //refresh the users jwt to match the above change
            Response.SendNewToken(await _authenticationService.IssueJwtToUser(User.UserId(), User.TokenGuid()));

            return(Ok(patrols));
        }
Beispiel #7
0
        public static TimeZoneInfo LocalTimeZone(this Models.Patrol patrol)
        {
            TimeZoneInfo timeZone;

            if (!string.IsNullOrEmpty(patrol.TimeZone))
            {
                timeZone = TZConvert.GetTimeZoneInfo(patrol.TimeZone);
            }
            else
            {
                timeZone = TZConvert.GetTimeZoneInfo("Eastern Standard Time");
            }
            return(timeZone);
        }
Beispiel #8
0
        public async Task AssignmentCancelled(User trainee, Models.Patrol patrol, Assignment assignment, Plan plan)
        {
            var localEnd = assignment.DueAt.UtcToPatrolLocal(patrol);

            var msg = new SendGridMessage()
            {
                Subject          = $"{plan.Name} Assignment Cancelled",
                PlainTextContent = $"Your training assignment {plan.Name} has been cancelled.",
                HtmlContent      = $"Your training assignment {plan.Name} has been cancelled.",
            };

            this.AddOnBehalfOf(msg, patrol);

            var response = await Send(msg, patrol, trainee);
        }
Beispiel #9
0
        //training
        public async Task AssignmentCreated(User assignedTo, Models.Patrol patrol, Assignment assignment, Plan plan)
        {
            var localEnd = assignment.DueAt.UtcToPatrolLocal(patrol);

            var msg = new SendGridMessage()
            {
                Subject          = $"{plan.Name} Assignment",
                PlainTextContent = $"You have been assigned {plan.Name} {(assignment.DueAt.HasValue ? ("Due "+assignment.DueAt.UtcToPatrolLocal(patrol)?.ToShortDateString()): "")}",
                HtmlContent      = $"You have been assigned {plan.Name} {(assignment.DueAt.HasValue ? ("Due " + assignment.DueAt.UtcToPatrolLocal(patrol)?.ToShortDateString()) : "")}",
            };

            this.AddOnBehalfOf(msg, patrol);

            var response = await Send(msg, patrol, assignedTo);
        }
Beispiel #10
0
        public async Task SendShiftAdded(List <User> assigned, Models.Patrol patrol, ScheduledShift shift)
        {
            var localStart = shift.StartsAt.UtcToPatrolLocal(patrol);
            var localEnd   = shift.EndsAt.UtcToPatrolLocal(patrol);

            var msg = new SendGridMessage()
            {
                Subject          = $"Shift created on {localStart.ToShortDateString()}",
                PlainTextContent = $"You have a new shift on {localStart.ToShortDateString()}, {localStart.ToShortTimeString()} - {localEnd.ToShortTimeString()}.",
                HtmlContent      = $"You have a new shift on {localStart.ToShortDateString()}, {localStart.ToShortTimeString()} - {localEnd.ToShortTimeString()}.",
            };

            this.AddOnBehalfOf(msg, patrol);

            var response = await Send(msg, patrol, assigned.ToArray());
        }
Beispiel #11
0
        public async Task SendShiftRemoved(User assigned, Models.Patrol patrol, ScheduledShift shift)
        {
            var localStart = shift.StartsAt.UtcToPatrolLocal(patrol);
            var localEnd   = shift.EndsAt.UtcToPatrolLocal(patrol);

            var msg = new SendGridMessage()
            {
                Subject          = $"Shift on {localStart.ToShortDateString()} removed.",
                PlainTextContent = $"Your shift on {localStart.ToShortDateString()} has been removed.",
                HtmlContent      = $"Your shift on {localStart.ToShortDateString()} has been removed.",
            };

            this.AddOnBehalfOf(msg, patrol);

            var response = await Send(msg, patrol, assigned);
        }
Beispiel #12
0
        public async Task SendEventEmail(User fromUser, List <User> users, Models.Patrol patrol, string name, string location, string textMessage, string htmlMessage, DateTime from, DateTime to)
        {
            var localFrom = from.UtcToPatrolLocal(patrol);
            var localTo   = to.UtcToPatrolLocal(patrol);

            var msg = new SendGridMessage()
            {
                Subject          = $"New Event: {name} {location} {localFrom.ToShortDateString()}",
                PlainTextContent = $"{fromUser.GetFullName()} has created a new calendar event.\n\nWhat: {name}\nWhere: {location}\nWhen:{localFrom.ToShortDateString()} {localFrom.ToShortTimeString()} - {localTo.ToShortDateString()} {localTo.ToShortTimeString()}\n\n{textMessage}",
                HtmlContent      = $"{fromUser.GetFullName()} has created a new calendar event.<br/><br/>What: {name}<br/>Where: {location}<br/>When: {localFrom.ToShortDateString()} {localFrom.ToShortTimeString()} - {localTo.ToShortDateString()} {localTo.ToShortTimeString()}<br/><br/>{htmlMessage}",
            };

            this.AddOnBehalfOf(msg, patrol);

            var response = await Send(msg, patrol, users.ToArray());
        }
Beispiel #13
0
        //scheduling
        public async Task SendShiftClaimed(User assigned, Models.Patrol patrol, User claimed, ScheduledShift shift)
        {
            var localStart = shift.StartsAt.UtcToPatrolLocal(patrol);
            var localEnd   = shift.EndsAt.UtcToPatrolLocal(patrol);

            var msg = new SendGridMessage()
            {
                Subject          = $"Shift on {localStart.ToShortDateString()} Claimed by {claimed.GetFullName()}",
                PlainTextContent = $"The shift you released on {localStart.ToShortDateString()} has been claimed by {claimed.GetFullName()}.\nIt is now pending approval.",
                HtmlContent      = $"The shift you released on {localStart.ToShortDateString()} has been claimed by {claimed.GetFullName()}.<br/>It is now pending approval.",
            };

            this.AddOnBehalfOf(msg, patrol);

            var response = await Send(msg, patrol, assigned);
        }
Beispiel #14
0
        public async Task SendTraineeCancel(User trainerUser, User traineeUser, Models.Patrol patrol, ScheduledShift shift)
        {
            var localStart = shift.StartsAt.UtcToPatrolLocal(patrol);
            var localEnd   = shift.EndsAt.UtcToPatrolLocal(patrol);

            var msg = new SendGridMessage()
            {
                Subject          = $"{traineeUser.GetFullName()} Cancelled for {localStart.ToShortDateString()}",
                PlainTextContent = $"{traineeUser.GetFullName()} has cancelled their request to train with you during your shift on {localStart.ToShortDateString()} {localStart.ToShortTimeString()} - {localEnd.ToShortTimeString()}",
                HtmlContent      = $"{traineeUser.GetFullName()} has cancelled their request to train with you during your shift on {localStart.ToShortDateString()} {localStart.ToShortTimeString()} - {localEnd.ToShortTimeString()}",
            };

            this.AddOnBehalfOf(msg, patrol);

            var response = await Send(msg, patrol, trainerUser);
        }
Beispiel #15
0
        private async Task <Response> Send(SendGridMessage message, Models.Patrol patrol, params User[] to)
        {
            SendGridClient client = new SendGridClient(new SendGridClientOptions()
            {
                ApiKey = _apiKey
            });

            message.From = new EmailAddress(_fromEmail, patrol != null ? patrol.Name + " @ " + _fromName : _fromName);

            //remove anybody that doesn't want notifications,dedup the to list
            to = to.Where(x => x.AllowEmailNotifications).GroupBy(x => x.Email).Select(x => x.First()).ToArray();

            bool hasTo = false;

            if (!string.IsNullOrEmpty(_overrideTo))
            {
                if (to.Any(x => x.AllowEmailNotifications))
                {
                    if (_overrideTo != "nobody")
                    {
                        message.AddTo(new EmailAddress(_overrideTo, "Test User"));
                        hasTo = true;
                    }
                }
            }
            else
            {
                var filteredTo = new List <EmailAddress>();
                foreach (var email in to)
                {
                    if (email.Email.Contains("@"))
                    {
                        var domain = email.Email.Substring(email.Email.IndexOf("@") + 1);
                        if (!_urlRoot.Contains(domain))
                        {
                            filteredTo.Add(email.ToEmailAddress());
                        }
                    }
                }

                if (filteredTo.Count > 0)
                {
                    hasTo = true;
                    //remove any that have the same host as this app, since these are test users
                    if (filteredTo.Count > 1)
                    {
                        message.AddBccs(filteredTo.ToList());
                    }
                    else
                    {
                        message.AddTos(filteredTo.ToList());
                    }
                }
            }

            if (hasTo)
            {
                AttachEmbeddedImages(message);

                var response = await client.SendEmailAsync(message);

                if (response.StatusCode != System.Net.HttpStatusCode.Accepted)
                {
                    throw new SendGridEmailException()
                          {
                              Body       = await response.Body.ReadAsStringAsync(),
                              StatusCode = response.StatusCode
                          };
                }

                return(response);
            }
            else
            {
                return(null);
            }
        }
Beispiel #16
0
 public static DateTime UtcFromPatrolLocal(this DateTime local, Models.Patrol patrol)
 {
     return(TimeZoneInfo.ConvertTimeToUtc(local, patrol.LocalTimeZone()));
 }
Beispiel #17
0
 public static DateTime UtcToPatrolLocal(this DateTime utc, Models.Patrol patrol)
 {
     return(TimeZoneInfo.ConvertTimeFromUtc(utc, patrol.LocalTimeZone()));
 }
Beispiel #18
0
        public async Task PopulateShiftWorkItemOccurences(RecurringWorkItemDto recurringWorkItem, DateTime now, int userId, Models.Patrol patrol, bool populateWorkitemAssignments = true)
        {
            var shifts = await _workItemRepository.GetShiftRecurringWorkItems(recurringWorkItem.Id);

            var workItems = await _workItemRepository.GetWorkItems(recurringWorkItem.Id, now);

            var allScheduledShiftAssignments = new List <ScheduledShiftAssignmentDto>();

            foreach (var shift in shifts)
            {
                var scheduledShiftAssignments = await _shiftRepository.GetScheduledShiftAssignments(recurringWorkItem.PatrolId, from : now, shiftId : shift.ShiftId);

                allScheduledShiftAssignments.AddRange(scheduledShiftAssignments);

                var scheduledShifts = scheduledShiftAssignments.GroupBy(x => x.ScheduledShiftId);

                foreach (var scheduledShift in scheduledShifts)
                {
                    var shiftWorkItems  = workItems.Where(x => x.ScheduledShiftId == scheduledShift.Key);
                    var shiftStartLocal = scheduledShift.First().StartsAt.UtcToPatrolLocal(patrol);

                    if (!shiftWorkItems.Any())
                    {
                        //create the work item
                        var workItem = new WorkItem()
                        {
                            AdminGroupId      = recurringWorkItem.AdminGroupId,
                            CreatedAt         = now,
                            CreatedByUserId   = userId,
                            CompletionMode    = recurringWorkItem.CompletionMode,
                            DescriptionMarkup = recurringWorkItem.DescriptionMarkup,
                            Location          = recurringWorkItem.Location,
                            Name                = recurringWorkItem.Name,
                            PatrolId            = recurringWorkItem.PatrolId,
                            RecurringWorkItemId = recurringWorkItem.Id,
                            ScheduledShiftId    = scheduledShift.Key,
                            ScheduledAt         = new DateTime(shiftStartLocal.Year, shiftStartLocal.Month, shiftStartLocal.Day, shift.ScheduledAtHour, shift.ScheduledAtMinute, 0).UtcFromPatrolLocal(patrol)
                        };
                        await _workItemRepository.InsertWorkItem(workItem);
                    }
                    else
                    {
                        //realistically should only ever be 1
                        foreach (var workItem in shiftWorkItems)
                        {
                            if (!workItem.CompletedAt.HasValue && !workItem.CanceledAt.HasValue)
                            {
                                workItem.CompletionMode    = recurringWorkItem.CompletionMode;
                                workItem.DescriptionMarkup = recurringWorkItem.DescriptionMarkup;
                                workItem.Location          = recurringWorkItem.Location;
                                workItem.Name         = recurringWorkItem.Name;
                                workItem.ScheduledAt  = new DateTime(shiftStartLocal.Year, shiftStartLocal.Month, shiftStartLocal.Day, shift.ScheduledAtHour, shift.ScheduledAtMinute, 0).UtcFromPatrolLocal(patrol);
                                workItem.AdminGroupId = recurringWorkItem.AdminGroupId;
                                await _workItemRepository.UpdateWorkItem(workItem);
                            }
                        }
                    }
                }
            }

            if (populateWorkitemAssignments)
            {
                await RecalculateShiftWorkItemAssignments(allScheduledShiftAssignments);
            }
        }
Beispiel #19
0
        public async Task SendTrainerShiftRemoved(User trainerUser, List <User> traineeUsers, Models.Patrol patrol, ScheduledShift shift)
        {
            var localStart = shift.StartsAt.UtcToPatrolLocal(patrol);
            var localEnd   = shift.EndsAt.UtcToPatrolLocal(patrol);

            var msg = new SendGridMessage()
            {
                Subject          = $"Trainer Shift Removed {localStart.ToShortDateString()}",
                PlainTextContent = $"{trainerUser.GetFullName()}'s shift in which you wanted to train with them on {localStart.ToShortDateString()} {localStart.ToShortTimeString()} - {localEnd.ToShortTimeString()} has been removed.\n  Please reschedule your training.",
                HtmlContent      = $"{trainerUser.GetFullName()}'s shift in which you wanted to train with them on {localStart.ToShortDateString()} {localStart.ToShortTimeString()} - {localEnd.ToShortTimeString()} has been removed.<br/>  Please reschedule your training.",
            };

            this.AddOnBehalfOf(msg, patrol);

            var response = await Send(msg, patrol, traineeUsers.ToArray());
        }