public bool Save(PhysicianRecordRequestSignatureModel model, long orgRoleUserId)
        {
            var eventCustomer = _eventCustomerRepository.GetById(model.EventCustomerId);

            if (eventCustomer == null)
            {
                return(false);
            }

            var signatureLocation = _mediaRepository.GetPhysicianRecordRequestSignatureLocation(eventCustomer.EventId, eventCustomer.CustomerId);

            var version = _physicianRecordRequestSignatureRepository.GetLatestVersion(model.EventCustomerId);

            var physicianRecordRequestSignature = new PhysicianRecordRequestSignature
            {
                EventCustomerId = model.EventCustomerId,
                Version         = version,
                IsActive        = true,
                CreatedBy       = orgRoleUserId,
                DateCreated     = DateTime.Now
            };

            if (!string.IsNullOrWhiteSpace(model.SignatureBytes))
            {
                var fileName = "Signature_" + Guid.NewGuid() + ".jpg";

                var patientImageFile = SaveSignatureImage(model, orgRoleUserId, signatureLocation, fileName);

                physicianRecordRequestSignature.SignatureFileId   = patientImageFile.Id;
                physicianRecordRequestSignature.ConsentSignedDate = DateTime.Now;
            }

            _physicianRecordRequestSignatureRepository.Save(physicianRecordRequestSignature);

            if (physicianRecordRequestSignature.SignatureFileId > 0)
            {
                eventCustomer.PcpConsentStatus = RegulatoryState.Signed;
                eventCustomer.IsPhysicianRecordRequestSigned = true;

                _eventCustomerRepository.Save(eventCustomer);
            }
            else
            {
                eventCustomer.IsPhysicianRecordRequestSigned = false;
                _eventCustomerRepository.Save(eventCustomer);
            }

            return(true);
        }
        public MobileResponseModel Save([FromBody] PhysicianRecordRequestSignatureModel model)
        {
            try
            {
                _logger.Info("Physician Record Request (Save) EventCustomerId : " + model.EventCustomerId);

                var isSuccess = _physicianRecordRequestService.Save(model, _sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId);

                if (isSuccess)
                {
                    return(new MobileResponseModel
                    {
                        IsSuccess = true,
                        Message = "Physician Record Request saved successfully."
                    });
                }
                else
                {
                    return(new MobileResponseModel
                    {
                        IsSuccess = false,
                        Message = "Unable to save Physician Record Request."
                    });
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Error while saving Physician Record Request.");
                _logger.Error("Message : " + ex.Message);
                _logger.Error("Stack Trace : " + ex.StackTrace);

                return(new MobileResponseModel
                {
                    IsSuccess = false,
                    Message = string.Format("Error while saving Physician Record Request. Message : {0}", ex.Message)
                });
            }
        }
        private Core.Application.Domain.File SaveSignatureImage(PhysicianRecordRequestSignatureModel model, long orgRoleUserId, MediaLocation signatureLocation, string fileName)
        {
            var filePath = Path.Combine(signatureLocation.PhysicalPath, fileName);

            var imageBytes = Convert.FromBase64String(model.SignatureBytes);

            File.WriteAllBytes(filePath, imageBytes);

            var fileInfo = new FileInfo(filePath);

            var file = new Core.Application.Domain.File
            {
                Path       = fileName,
                FileSize   = fileInfo.Length,
                Type       = FileType.Image,
                UploadedBy = new OrganizationRoleUser(orgRoleUserId),
                UploadedOn = DateTime.Now
            };

            file = _fileRepository.Save(file);

            return(file);
        }
        public PhysicianRecordRequestSignatureModel GetPhysicianRecordRequestSignature(long eventCustomerId)
        {
            var physicianRecordRequest = _physicianRecordRequestSignatureRepository.GetByEventCustomerId(eventCustomerId);

            var file = physicianRecordRequest != null?_fileRepository.GetById(physicianRecordRequest.SignatureFileId) : null;

            if (file == null)
            {
                return(new PhysicianRecordRequestSignatureModel
                {
                    EventCustomerId = eventCustomerId,
                    SignatureBytes = null
                });
            }

            var eventCustomer     = _eventCustomerRepository.GetById(eventCustomerId);
            var filename          = file.Path;
            var signatureLocation = _mediaRepository.GetPhysicianRecordRequestSignatureLocation(eventCustomer.EventId, eventCustomer.CustomerId);
            var filePath          = Path.Combine(signatureLocation.PhysicalPath, filename);

            var physicianRecordRequestSignatureModel = new PhysicianRecordRequestSignatureModel
            {
                EventCustomerId = eventCustomerId
            };

            var signatureBytes = string.Empty;

            if (File.Exists(filePath))
            {
                var bytes = File.ReadAllBytes(filePath);
                signatureBytes = Convert.ToBase64String(bytes);
            }

            physicianRecordRequestSignatureModel.SignatureBytes = signatureBytes;

            return(physicianRecordRequestSignatureModel);
        }