Beispiel #1
0
        private EntityResult CreateOccurrence(TaskInstance instance)
        {
            Data.Model.Occurrence occurrence = new Occurrence()
            {
                AsEarlyAsDate = instance.AsEarlyAsDate,
                AssetId       = instance.AssetId,
                Date          = instance.Date,
                FormModel     = instance.FormModel,
                ScheduleId    = instance.ScheduleId,
                TaskId        = instance.TaskId,
                UserId        = instance.UserId,
                TimeSpent     = 0
            };

            occurrence.Logs.Add(new OccurrenceLog()
            {
                Type = "Created"
            });
            _context.Occurrences.Add(occurrence);
            var result = _context.SaveChanges();

            if (!result.Succeeded)
            {
                _context.Entry(occurrence).Reference(o => o.Asset).Load();
                _context.Entry(occurrence).Reference(o => o.User).Load();
                SignalRHub.NotifyOccurrenceCreate(null, occurrence.ToDto());
                return(EntityResult.Failed(result.Errors.ToArray()));
            }

            return(EntityResult.Succeded(1));
        }
Beispiel #2
0
        internal override async Task <EntityResult> AfterCreateAsync(Data.Model.TaskAlert entity, bool notifyAll = false)
        {
            foreach (var user in entity.Users)
            {
                await Context.Entry <Data.Model.TaskAlertUser>(user).Reference(tau => tau.User).LoadAsync();
            }

            return(EntityResult.Succeded(0));
        }
Beispiel #3
0
        private async Task <EntityResult <TaskInstance> > AddSingleOccurrence(Data.Model.Task task, Data.Model.Schedule scheduleEntity, int?assetId, int?userId, DateTime date)
        {
            date = date.LessSeconds();
            Context.Entry(task).Reference(t => t.Form).Load();

            var newOccurrence = new Altask.Data.Model.Occurrence()
            {
                AsEarlyAsDate = date,
                Date          = date,
                FormModel     = task.Form.PublishedModel,
                TaskId        = task.Id,
                TimeSpent     = 0,
                ScheduleId    = scheduleEntity.Id,
            };

            var assetEntity = await Context.Assets.FindAsync(assetId);

            if (assetEntity != null)
            {
                newOccurrence.AssetId = assetEntity.Id;
            }

            var userEntity = await Context.Users.FindAsync(userId);

            if (userEntity != null)
            {
                newOccurrence.UserId = userEntity.Id;
            }

            if (assetEntity == null && userEntity == null)
            {
                return(EntityResult <TaskInstance> .Failed(ErrorDescriber.DefaultError("An occurrence must have either and asset or user associated to it.")));
            }

            newOccurrence.Logs.Add(new Data.Model.OccurrenceLog()
            {
                Type = "Created"
            });
            Context.Occurrences.Add(newOccurrence);
            BeforeCreate(newOccurrence, newOccurrence.ToDto());
            var result = await Context.SaveChangesAsync();

            if (result.Succeeded)
            {
                await AfterCreateAsync(newOccurrence, true);

                var instance = TaskInstance.FromSchedule(task, newOccurrence.Date, scheduleEntity).MergeOccurrence(newOccurrence);
                SignalRHub.NotifyOccurrenceCreate(null, instance, newOccurrence.ToDto());
                return(EntityResult <TaskInstance> .Succeded(instance));
            }
            else
            {
                return(EntityResult <TaskInstance> .Failed(result.Errors.ToArray()));
            }
        }
        internal override async Task <EntityResult> AfterUpdateAsync(Data.Model.AssetAlertLog entity, bool notifyAll = false)
        {
            await Context.Entry(entity).Reference(e => e.User).LoadAsync();

            await Context.Entry(entity).Reference(e => e.AssetLog).LoadAsync();

            if (!string.IsNullOrEmpty(HttpContext.Request.Headers["X-Altask-Client-Id"]))
            {
                SignalRHub.NotifyAssetAlertLogUpdate(Guid.Parse(HttpContext.Request.Headers["X-Altask-Client-Id"]), entity.ToDto());
            }

            return(EntityResult.Succeded(0));
        }
Beispiel #5
0
        internal override async Task <EntityResult> AfterUpdateAsync(Data.Model.Schedule entity, bool notifyAll = false)
        {
            foreach (var user in entity.Users)
            {
                await Context.Entry <Data.Model.ScheduleUser>(user).Reference(su => su.User).LoadAsync();
            }

            foreach (var asset in entity.Assets)
            {
                await Context.Entry <Data.Model.ScheduleAsset>(asset).Reference(sa => sa.Asset).LoadAsync();
            }

            return(EntityResult.Succeded(0));
        }
Beispiel #6
0
        internal override async Task <EntityResult> AfterUpdateAsync(Data.Model.Occurrence entity, bool notifyAll = false)
        {
            await Context.Entry(entity).Reference(e => e.Asset).LoadAsync();

            await Context.Entry(entity).Reference(e => e.User).LoadAsync();

            await Context.Entry(entity).Reference(e => e.Task).LoadAsync();

            await Context.Entry(entity).Reference(e => e.Schedule).LoadAsync();

            var instance = TaskInstance.FromSchedule(entity.Task, entity.Date, entity.Schedule).MergeOccurrence(entity);

            SignalRHub.NotifyOccurrenceUpdate(ClientId, instance, entity.ToDto());
            return(EntityResult.Succeded(0));
        }
Beispiel #7
0
        public new async Task <EntityResult> SaveChangesAsync()
        {
            var result = 0;

            try {
                SetModifiedDates();
                result = await base.SaveChangesAsync();
            } catch (DbUpdateConcurrencyException) {
                return(EntityResult.Failed(_errorDescriber.ConcurrencyFailure()));
            } catch (Exception ex) {
                return(EntityResult.Failed(ex, _errorDescriber.DefaultError(ex.Message)));
            }

            return(EntityResult.Succeded(result));
        }
Beispiel #8
0
        public EntityResult Send(string from, string to, string subject, string body)
        {
            try {
                var client = new SmtpClient {
                    UseDefaultCredentials = false,
                    Credentials           = new System.Net.NetworkCredential(_userName.ToLower(), _password),
                    Host    = _smtpAddress,
                    Port    = _smtpPort,
                    Timeout = 10000,
                };

                var message = new MailMessage(from, to, subject, body);
                client.Send(message);
                return(EntityResult.Succeded(0));
            }
            catch (Exception e) {
                return(EntityResult.Failed(e, _errorDescriber.DefaultError(e.Message)));
            }
        }
Beispiel #9
0
        internal override async Task <EntityResult> AfterCreateAsync(AssetLog entity, bool notifyAll = false)
        {
            var assetAlerts = await Context.AssetAlerts.Where(aa => aa.AssetId == entity.AssetId && aa.AssetLogTypeId == entity.AssetLogTypeId).ToListAsync();

            var userEntity = await Context.Users.FirstOrDefaultAsync(u => u.UserName == entity.CreatedBy);

            var assetEntity = await Context.Assets.FirstOrDefaultAsync(a => a.Id == entity.AssetId);

            var assetLogTypeEntity = await Context.AssetLogTypes.FirstOrDefaultAsync(alt => alt.Id == entity.AssetLogTypeId);

            var userName  = userEntity.FullName ?? userEntity.UserName;
            var assetName = assetEntity.Name + (assetEntity.CustomId != null ? string.Format(" - {0}", assetEntity.CustomId) : "");

            foreach (var assetAlert in assetAlerts)
            {
                var message = string.Format("{0} reported {1} for asset {2} on {3}.", userName, assetLogTypeEntity.Name, assetName, entity.CreatedOn.ToLocalTime().ToString("MM/dd/yyyy hh:mm tt"));

                if (!string.IsNullOrEmpty(entity.Comment))
                {
                    message += string.Format(" Comment(s): {0}", entity.Comment);
                }

                foreach (var user in assetAlert.Users)
                {
                    var date = DateTime.Now;
                    var log  = new AssetAlertLog()
                    {
                        AlertDate    = date,
                        AssetLogId   = entity.Id,
                        AssetAlertId = assetAlert.Id,
                        Description  = message,
                        Type         = AssetAlertLogType.System.ToString(),
                        UserId       = user.UserId
                    };

                    Context.AssetAlertLogs.Add(log);

                    if (Context.SaveChanges().Succeeded)
                    {
                        if (!string.IsNullOrEmpty(HttpContext.Request.Headers["X-Altask-Client-Id"]))
                        {
                            SignalRHub.NotifyAssetAlertLogCreate(Guid.Parse(HttpContext.Request.Headers["X-Altask-Client-Id"]), log.ToDto());
                        }
                    }

                    if (user.User.ReceiveEmail)
                    {
                        var result = await UserManager.EmailService.SendAsync(Settings.AlertEmailFrom, user.User.EmailAddress, Settings.AlertEmailSubject, message.ToString());

                        Context.AssetAlertLogs.Add(new AssetAlertLog()
                        {
                            AlertDate    = date,
                            AssetLogId   = entity.Id,
                            AssetAlertId = assetAlert.Id,
                            Description  = message,
                            Type         = (result.Succeeded ? AssetAlertLogType.SendEmailSuccess : AssetAlertLogType.SendEmailFailure).ToString(),
                            UserId       = user.UserId
                        });

                        Context.SaveChanges();
                    }

                    if (user.User.ReceiveText)
                    {
                        var result = await UserManager.EmailService.SendAsync(Settings.AlertEmailFrom, user.User.SmsAddress, Settings.AlertEmailSubject, message.ToString());

                        Context.AssetAlertLogs.Add(new AssetAlertLog()
                        {
                            AlertDate    = date,
                            AssetLogId   = entity.Id,
                            AssetAlertId = assetAlert.Id,
                            Description  = message,
                            Type         = (result.Succeeded ? AssetAlertLogType.SendSmsSuccess : AssetAlertLogType.SendSmsFailure).ToString(),
                            UserId       = user.UserId
                        });

                        Context.SaveChanges();
                    }
                }
            }

            return(EntityResult.Succeded(1));
        }
Beispiel #10
0
 internal virtual async System.Threading.Tasks.Task <EntityResult> AfterUpdateAsync(TModel entity, bool notifyAll = false)
 {
     return(await System.Threading.Tasks.Task.FromResult(EntityResult.Succeded(0)));
 }
Beispiel #11
0
 internal async Task <EntityResult> AfterUpdateAsync(Altask.www.Models.TaskInstance instance, Altask.Data.Dto.Occurrence occurrence, bool notifyAll = false)
 {
     SignalRHub.NotifyOccurrenceUpdate(ClientId, instance, occurrence);
     return(await System.Threading.Tasks.Task.FromResult(EntityResult.Succeded(0)));
 }
Beispiel #12
0
 public async System.Threading.Tasks.Task <EntityResult> AddUserLogAsync(User user, UserLog log)
 {
     await((UserStore)Store).AddUserLogAsync(user, log);
     return(await System.Threading.Tasks.Task.FromResult(EntityResult.Succeded(0)));
 }