예제 #1
0
        public async Task <ActionResult> updateIncidentStatus(UpdateIncidentStatusRequest updateIncidentStatusRequest)
        {
            Incident incident = appDbContex.Incidents.Where(a => a.Id == updateIncidentStatusRequest.Id).FirstOrDefault();

            if (incident != null)
            {
                if (incident.EmergencyAccountId == null || incident.EmergencyAccountId == updateIncidentStatusRequest.EmergencyAccountId)
                {
                    incident.EmergencyAccountId = updateIncidentStatusRequest.EmergencyAccountId;
                    incident.Status             = updateIncidentStatusRequest.Status;
                    incident.UpdatedAt          = DateTime.Now;
                    await appDbContex.SaveChangesAsync();

                    List <ApplicationUser> usersToNotify = new List <ApplicationUser>();
                    FindUsers findUsers = new FindUsers(appDbContex, userManager);
                    usersToNotify = findUsers.FindUsersToNotifybyCoordinateXY(incident.CoordinateX, incident.CoordinateY, incident.IncidentTypeId);

                    SendSms sendsms          = new SendSms();
                    string  contactNumber    = string.Empty;
                    var     incidenttypeName = appDbContex.IncidentTypes.Where(a => a.Id == incident.IncidentTypeId).FirstOrDefault();
                    foreach (var user in usersToNotify)
                    {
                        NotifiedUser notifiedUser = appDbContex.NotifiedUsers.Where(a => a.IncidentId == updateIncidentStatusRequest.Id && a.UserId == user.Id).FirstOrDefault();

                        if (notifiedUser == null)
                        {
                            NotifiedUser addNotifiedUser = new NotifiedUser
                            {
                                Id         = Guid.NewGuid().ToString(),
                                IncidentId = updateIncidentStatusRequest.Id,
                                UserId     = user.Id,
                                CreatedAt  = DateTime.Now
                            };

                            appDbContex.NotifiedUsers.Add(addNotifiedUser);
                            await appDbContex.SaveChangesAsync();
                        }
                    }

                    List <NotifiedUser>    lstnotifiedUsers = appDbContex.NotifiedUsers.Where(a => a.IncidentId == updateIncidentStatusRequest.Id).ToList();
                    List <ApplicationUser> usersToNotify1   = new List <ApplicationUser>();
                    foreach (var user in lstnotifiedUsers)
                    {
                        ApplicationUser appUser = userManager.Users.Where(a => a.Id == user.UserId).FirstOrDefault();
                        if (appUser != null)
                        {
                            usersToNotify1.Add(appUser);
                        }
                    }
                    string notificationMessage = string.Empty;
                    string location            = "http://maps.google.com/?q=" + incident.CoordinateX + "," + incident.CoordinateY + "";
                    foreach (var user in usersToNotify1)
                    {
                        if (user.EmergencyContactNo != null)
                        {
                            //Notification to Emergency Contact
                            contactNumber       = user.InternationalPrefix.ToString() + user.PhoneNumber.ToString();
                            notificationMessage = "Here is the " + incidenttypeName.Name + " incident happen... please click here " + location;
                            sendsms.SendTextSms(notificationMessage, contactNumber);
                        }
                        else
                        {
                            //Notification to User
                            contactNumber       = user.InternationalPrefix.ToString() + user.PhoneNumber.ToString();
                            notificationMessage = "Here is the " + incidenttypeName.Name + " incident happen... please click here " + location;
                            sendsms.SendTextSms(notificationMessage, contactNumber);
                        }



                        IncidentUserMessage incidentUserMessage = new IncidentUserMessage
                        {
                            Id            = Guid.NewGuid().ToString(),
                            IncidentId    = updateIncidentStatusRequest.Id,
                            UserId        = user.Id,
                            Status        = updateIncidentStatusRequest.Status,
                            StatusMessage = notificationMessage,
                            CreatedAt     = DateTime.Now
                        };

                        appDbContex.IncidentUserMessages.Add(incidentUserMessage);
                        await appDbContex.SaveChangesAsync();
                    }
                }

                return(Ok(new { Message = "Incident Updated successfully !" }));
            }
            return(BadRequest(new { Message = "Incident Not Found !" }));
        }