Example #1
0
        public async Task <IActionResult> CreateHazardRemote([FromForm] SaveHazardResource hazardResource)
        {
            try
            {
                var loggedInUserId = _userRepo.GetLoggedInUserId();
                var user           = await _userRepo.GetUser(loggedInUserId);

                bool triggerDeptNotification = false;
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                //business rule validation
                var hazardCode = !string.IsNullOrEmpty(hazardResource.Description) ? hazardResource.Description + "/"
                                 + ((hazardResource.ReportedLatitude != null) ? hazardResource.ReportedLatitude : DateTime.Now.Minute) + "/"
                                 + ((hazardResource.ReportedLongitude != null) ? hazardResource.ReportedLongitude : DateTime.Now.Minute)
                   : hazardResource.ReportedLatitude + "/" + hazardResource.ReportedLongitude;
                if (repository.CheckHazardCode(hazardCode))
                {
                    ModelState.AddModelError("Error", "The incidence with this Code already exists.");
                    return(BadRequest(ModelState));
                }

                var hazard = mapper.Map <SaveHazardResource, Hazard>(hazardResource);
                hazard.Deleted         = false;
                hazard.CreatedByUserId = loggedInUserId;
                // hazard.Protected = true;
                hazard.DateCreated = DateTime.UtcNow;
                hazard.Code        = hazardCode;

                if (user != null)
                {
                    var reporterFullName = string.IsNullOrEmpty(user.MiddleName) ? user.FirstName + " " + user.LastName
                        : user.FirstName + " " + user.MiddleName + " " + user.LastName;
                    hazard.ReporterName         = reporterFullName ?? "";
                    hazard.ReporterDepartmentId = user.DepartmentId;
                    hazard.ReporterEmail        = user.Email1;
                }

                hazard.OrganizationId = user.UserName != "Admin" ? user.OrganizationId : null;
                var organization = await _orgRepo.GetOrganization(hazard?.OrganizationId);

                hazard.AssignedOrganizationId = organization?.Id;
                hazard.AssignedDepartmentId   = organization?.HazardDefaultDepartmentId;

                hazard.IncidenceStatusId = hazardResource.IncidenceStatusId ?? GlobalFields.NewIncidenceStatus;
                if (hazard.IncidenceStatusId == GlobalFields.NewIncidenceStatus && hazard.AssignedDepartmentId != null)
                {
                    triggerDeptNotification = true;
                }

                repository.Add(hazard);
                await unitOfWork.CompleteAsync();

                hazard = await repository.GetHazard(hazard.Id);

                if (hazard == null)
                {
                    return(NotFound());
                }

                if (hazardResource.file != null && hazardResource.file.Length != 0)
                {
                    var deptSettings = await _settingsRepo.GetDepartmentSettings(hazard.AssignedDepartmentId);

                    var orgSettings = await _settingsRepo.GetOrganizationSettings(hazard?.OrganizationId);

                    var generalSettings = await _settingsRepo.GetGeneralSettings();

                    if (!_settingsRepo.ValidateFileType(deptSettings, orgSettings, generalSettings, hazardResource.file))
                    {
                        return(BadRequest("Invalid file type"));
                    }

                    if (!_settingsRepo.ValidateFileSize(deptSettings, orgSettings, generalSettings, hazardResource.file))
                    {
                        return(BadRequest("Maximun file size exceeded"));
                    }

                    var isFilePhotoNotVideo = _settingsRepo.IsImageFileNotVideo(deptSettings, orgSettings, generalSettings, hazardResource.file);

                    var uploadsFolderPath = Path.Combine(_host.WebRootPath, "uploads");
                    var photo             = await _photoService.UploadHazardPhoto(hazard, hazardResource.file, uploadsFolderPath, FileUploadChannels.incidencesReportedOnMobile, isFilePhotoNotVideo);

                    // return Ok(mapper.Map<Media, MediaResource>(photo));
                }

                //Next, fetch notification settings from the IncidenceType's-mapped department or the mapped department's organization if dept settings is empty. Dept receives notification here
                //IncidenceType's mapped department's settings is used for sending notification, otherwise if empty, use organization or system settings
                await _notificationRepo.SendIncidenceNotification(hazardResource, hazard.AssignedDepartmentId, null, null, hazard?.OrganizationId);

                if (triggerDeptNotification)
                {
                    //pass the reporter details into incidenceResource as it only exists in incidence and it is needed in email notification
                    hazardResource.ReporterName  = hazard.ReporterName;
                    hazardResource.ReporterEmail = hazard.ReporterEmail;
                    hazard.ReporterDepartmentId  = hazard.ReporterDepartmentId;
                    await _notificationRepo.SendHazardEmailNotificationToOrgDeptUsers(hazardResource, hazard.AssignedDepartmentId, hazard.AssignedOrganizationId, hazard.ReporterDepartmentId); // use SendEmailNotificationToOrgDeptUsers method in notificationRepo
                }

                hazard = await repository.GetHazard(hazard.Id);

                var result = mapper.Map <Hazard, HazardResource>(hazard);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Error", "An error occured while performing this operation.");
                return(BadRequest(ModelState));
            }

            //var result = mapper.Map<Incidence, IncidenceResource>(incidence);

            //return Ok(result);
        }
Example #2
0
        public async Task <IActionResult> PutHazard([FromRoute] Guid id, [FromBody] SaveHazardResource hazardResource)
        {
            try
            {
                bool triggerNotification = false;
                //input validation
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                var hazard = await repository.GetHazard(id);

                if (hazard == null)
                {
                    return(NotFound());
                }

                hazardResource.EditedByUserId  = _userRepo.GetLoggedInUserId();
                hazardResource.DateCreated     = hazard.DateCreated;
                hazardResource.CreatedByUserId = hazard.CreatedByUserId;

                hazardResource.Description                  = hazard.Description;
                hazardResource.ReportedLatitude             = hazard.ReportedLatitude ?? 0;
                hazardResource.ReportedLongitude            = hazard.ReportedLongitude ?? 0;
                hazardResource.ReporterName                 = hazard.ReporterName;
                hazardResource.ReporterEmail                = hazard.ReporterEmail;
                hazardResource.ReporterFirstResponderAction = hazard.ReporterFirstResponderAction;
                hazardResource.ReporterFeedbackRating       = hazard.ReporterFeedbackRating;
                //hazardResource.ManagerFeedbackRating = hazard.ManagerFeedbackRating;
                hazardResource.Protected = hazard.Protected;
                hazardResource.Deleted   = hazard.Deleted;

                hazardResource.Code       = hazard.Code;
                hazardResource.DateEdited = DateTime.UtcNow;
                hazardResource.Deleted    = hazard.Deleted;
                //incidenceResource.Protected = incidence.Protected;
                //hazardResource.AssignerId = hazard.AssignerId;
                hazardResource.AssignedOrganizationId = hazard.AssignedOrganizationId;
                //hazardResource.AssignedDepartmentId = hazard.AssignedDepartmentId;
                //hazardResource.ResolutionDate = hazard.ResolutionDate;

                // trigger alert when incidence status is open or re-opened
                if (hazardResource.IncidenceStatusId != hazard.IncidenceStatusId && (hazardResource.IncidenceStatusId == GlobalFields.OpenIncidenceStatus || hazardResource.IncidenceStatusId == GlobalFields.ReOpenedIncidenceStatus))
                {
                    hazardResource.AssignerId = _userRepo.GetLoggedInUserId();
                    triggerNotification       = true;
                }
                else if (hazardResource.IncidenceStatusId != hazard.IncidenceStatusId && hazardResource.IncidenceStatusId == GlobalFields.ClosedIncidenceStatus)
                {
                    // set resolution date when incidence is closed
                    hazardResource.ResolutionDate = DateTime.Now;
                    triggerNotification           = true;
                }
                else if (hazardResource.IncidenceStatusId != hazard.IncidenceStatusId && (hazardResource.IncidenceStatusId == GlobalFields.ResolvedIncidenceStatus || hazardResource.IncidenceStatusId == GlobalFields.UnderReviewIncidenceStatus))
                {
                    triggerNotification = true;
                }

                mapper.Map <SaveHazardResource, Hazard>(hazardResource, hazard);

                await unitOfWork.CompleteAsync();

                //send email and sms to notify assignee upon incidence opening and assignment
                //IncidenceType's mapped department's settings is used for sending notification, otherwise if empty, use organization or system settings
                if (triggerNotification)
                {
                    await _notificationRepo.SendIncidenceNotification(hazardResource, hazardResource.AssignedDepartmentId, hazardResource.AssignerId, hazardResource.AssigneeId, hazardResource.AssignedOrganizationId);
                }

                hazard = await repository.GetHazard(id);

                var result = mapper.Map <Hazard, HazardResource>(hazard);
                return(Ok(result));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Error", "An error occured while performing this operation.");
                return(BadRequest(ModelState));
            }
        }
Example #3
0
        public async Task <IActionResult> CreateHazard([FromBody] SaveHazardResource hazardResource)
        {
            try
            {
                var loggedInUserId = _userRepo.GetLoggedInUserId();
                var user           = await _userRepo.GetUser(loggedInUserId);

                bool triggerNotification     = false;
                bool triggerDeptNotification = false;
                //assumes the organization creates this incidence front front-end, notification applies only when its assigned, resolved or closed
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                //business rule validation
                var hazardCode = !string.IsNullOrEmpty(hazardResource.Description) ? hazardResource.Description + "/"
                                 + ((hazardResource.ReportedLatitude != null) ? hazardResource.ReportedLatitude : DateTime.Now.Minute) + "/"
                                 + ((hazardResource.ReportedLongitude != null) ? hazardResource.ReportedLongitude : DateTime.Now.Minute)
                    : hazardResource.ReportedLatitude + "/" + hazardResource.ReportedLongitude;
                if (repository.CheckHazardCode(hazardCode))
                {
                    ModelState.AddModelError("Error", "The incidence with this Code already exists.");
                    return(BadRequest(ModelState));
                }

                var hazard = mapper.Map <SaveHazardResource, Hazard>(hazardResource);
                hazard.Deleted           = false;
                hazard.Protected         = true;
                hazard.CreatedByUserId   = _userRepo.GetLoggedInUserId();
                hazard.DateCreated       = DateTime.Now;
                hazard.IncidenceStatusId = hazardResource.IncidenceStatusId ?? GlobalFields.NewIncidenceStatus;
                hazard.Code = hazardCode;

                if (user != null)
                {
                    var reporterFullName = string.IsNullOrEmpty(user.MiddleName) ? user.FirstName + " " + user.LastName
                        : user.FirstName + " " + user.MiddleName + " " + user.LastName;
                    hazard.ReporterName         = reporterFullName ?? "";
                    hazard.ReporterDepartmentId = user.DepartmentId;
                    hazard.ReporterEmail        = user.Email1;
                }

                hazard.OrganizationId = user.UserName != "Admin" ? user.OrganizationId : null;
                var organization = await _orgRepo.GetOrganization(hazard?.OrganizationId);

                hazard.AssignedOrganizationId = organization?.Id;
                hazard.AssignedDepartmentId   = organization?.HazardDefaultDepartmentId;

                //if (organization.HazardDefaultDepartmentId != null)
                //    hazard.AssignedDepartmentId = organization.HazardDefaultDepartmentId;

                // trigger alert when incidence status is open or re-opened provided as Assignee and Assigner were sent
                if (hazardResource.AssigneeId != null && (hazardResource.IncidenceStatusId == GlobalFields.OpenIncidenceStatus || hazardResource.IncidenceStatusId == GlobalFields.ReOpenedIncidenceStatus))
                {
                    hazard.AssignerId   = loggedInUserId;
                    triggerNotification = true;
                }
                else if (hazardResource.IncidenceStatusId != hazard.IncidenceStatusId && hazardResource.IncidenceStatusId == GlobalFields.ClosedIncidenceStatus)
                {
                    // set resolution date when incidence is closed. No notification here as it incidence was already closed before creation. No Assigner or Assignee before now
                    hazardResource.ResolutionDate = DateTime.Now;
                }

                hazard.IncidenceStatusId = hazardResource.IncidenceStatusId ?? GlobalFields.NewIncidenceStatus;
                if (hazard.IncidenceStatusId == GlobalFields.NewIncidenceStatus && hazard.AssignedDepartmentId != null)
                {
                    triggerDeptNotification = true;
                }

                //incidence.LastUpdate = DateTime.Now;
                repository.Add(hazard);
                await unitOfWork.CompleteAsync();

                if (triggerDeptNotification)
                {
                    //pass the reporter details into incidenceResource as it only exists in incidence and it is needed in email notification
                    hazardResource.ReporterName  = hazard.ReporterName;
                    hazardResource.ReporterEmail = hazard.ReporterEmail;
                    hazard.ReporterDepartmentId  = hazard.ReporterDepartmentId;
                    await _notificationRepo.SendHazardEmailNotificationToOrgDeptUsers(hazardResource, hazard.AssignedDepartmentId, hazard.AssignedOrganizationId, hazard.ReporterDepartmentId); // use SendEmailNotificationToOrgDeptUsers method in notificationRepo
                }

                hazard = await repository.GetHazard(hazard.Id);

                var result = mapper.Map <Hazard, HazardResource>(hazard);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Error", "An error occured while performing this operation.");
                return(BadRequest(ModelState));
            }
        }