示例#1
0
        /// <summary>This method grabs all unread webmails, and creates/modifies/deletes alerts for the providers and linked users the webmails are addressed to.</summary>
        public static void CreateAlertsForNewWebmail(Logger.IWriteLine log)
        {
            //This method first collect all unread webmails, and counts how many each provider has.
            //It then fetches all WebMailRecieved alerts, and will create/modify alerts for each provider who was counted before.
            //Finally, it will sync the alerts in the database with the ones we created.
            //If the user has unread webmail and an existing alert, it is modified.
            //If the user has unread webmail and no existing alert, an alert is created.
            //If the user has no unread webmail and an existing alert, it will be deleted.
            //Key: ProvNum, Value: Number of unread webmails
            Dictionary <long, long> dictRecievedCount = EmailMessages.GetProvUnreadWebMailCount();

            log.WriteLine("Collected Webmails for the following providers (ProvNum: # Webmails): "
                          + String.Join(", ", dictRecievedCount.Select(x => POut.Long(x.Key) + ":" + POut.Long(x.Value))), LogLevel.Verbose);
            //This list contains every single WebMailRecieved alert and is synced with listAlerts later.
            List <AlertItem> listOldAlerts = AlertItems.RefreshForType(AlertType.WebMailRecieved);

            log.WriteLine("Fetched current alerts for users: " + String.Join(", ", listOldAlerts.Select(x => POut.Long(x.UserNum))), LogLevel.Verbose);
            //If a user doesn't have any unread webmail, they won't be placed on this list, and any alert they have in listOldAlerts will be deleted.
            List <AlertItem> listAlerts = new List <AlertItem>();
            List <long>      listChangedAlertItemNums = new List <long>();

            //Go through each provider value, and create/update alerts for each patnum under that provider.
            //There will only be a value if they have atleast 1 unread webmail.
            foreach (KeyValuePair <long, long> kvp in dictRecievedCount)
            {
                List <Userod> listUsers = Providers.GetAttachedUsers(kvp.Key);
                //Go through each usernum and create/update their alert item.
                foreach (long usernum in listUsers.Select(x => x.UserNum))
                {
                    AlertItem alertForUser = listOldAlerts.FirstOrDefault(x => x.UserNum == usernum);
                    //If an alert doesn't exist for the user, we'll create it.
                    if (alertForUser == null)
                    {
                        alertForUser             = new AlertItem();
                        alertForUser.Type        = AlertType.WebMailRecieved;
                        alertForUser.FormToOpen  = FormType.FormEmailInbox;
                        alertForUser.Actions     = ActionType.MarkAsRead | ActionType.OpenForm; //Removed delete because the alert will just be re-added next time it checks.
                        alertForUser.Severity    = SeverityType.Normal;
                        alertForUser.ClinicNum   = -1;                                          //The alert is user dependent, not clinic dependent.
                        alertForUser.UserNum     = usernum;
                        alertForUser.Description = POut.Long(kvp.Value);
                        listAlerts.Add(alertForUser);
                        log.WriteLine("Created webmail alert for user " + POut.Long(usernum), LogLevel.Verbose);
                    }
                    else
                    {
                        //If the alert already exists, we'll be updating it and usually mark it as unread.
                        AlertItem selectedAlert = alertForUser.Copy();
                        long      previousValue = PIn.Long(selectedAlert.Description);
                        //We only need to modify the alert if the amount of unread webmails changed.
                        if (previousValue != kvp.Value)
                        {
                            selectedAlert.Description = POut.Long(kvp.Value);
                            //If the new value is greater, the user has recieved more webmails so we want to mark the alert as "Unread".
                            if (previousValue < kvp.Value)
                            {
                                listChangedAlertItemNums.Add(selectedAlert.AlertItemNum);
                            }
                        }
                        listAlerts.Add(selectedAlert);
                        log.WriteLine("Modified webmail alert for user " + POut.Long(usernum), LogLevel.Verbose);
                    }
                }
            }
            //Push our changes to the database.
            AlertItems.Sync(listAlerts, listOldAlerts);
            List <AlertItem> listDeletedAlerts = listOldAlerts.Where(x => !listAlerts.Any(y => y.AlertItemNum == x.AlertItemNum)).ToList();

            log.WriteLine("Deleted webmail alerts for users: " + String.Join(", ", listDeletedAlerts.Select(x => POut.Long(x.UserNum))), LogLevel.Verbose);
            //Make sure to mark alerts that were deleted, modified (not created) and increased as unread.
            listChangedAlertItemNums.AddRange(listDeletedAlerts.Select(x => x.AlertItemNum));
            AlertReads.DeleteForAlertItems(listChangedAlertItemNums);
        }