Ejemplo n.º 1
0
        public string SaveMedical(MemberMedical data)
        {
            if (data == null || data.Member == null || data.Member.Id == Guid.Empty)
            {
                ThrowSubmitErrors(new[] { new Web.Model.SubmitError {
                                              Error = "No user specified"
                                          } });
            }

            if (!(Permissions.IsAdmin || Permissions.IsMembershipForPerson(data.Member.Id) || Permissions.IsSelf(data.Member.Id)))
            {
                ThrowAuthError();
            }


            Model.Member        member  = db.Members.Include("MedicalInfo", "EmergencyContacts").Single(f => f.Id == data.Member.Id);
            Model.MemberMedical medical = member.MedicalInfo;
            if (medical == null)
            {
                medical            = new Model.MemberMedical();
                member.MedicalInfo = medical;
            }

            medical.EncryptedAllergies   = string.IsNullOrWhiteSpace(data.Allergies) ? null : EncryptionService.Protect(EncryptionService.MEMBER_SENSITIVE, data.Allergies);
            medical.EncryptedMedications = string.IsNullOrWhiteSpace(data.Medications) ? null : EncryptionService.Protect(EncryptionService.MEMBER_SENSITIVE, data.Medications);
            medical.EncryptedDisclosures = string.IsNullOrWhiteSpace(data.Disclosure) ? null : EncryptionService.Protect(EncryptionService.MEMBER_SENSITIVE, data.Disclosure);

            var existingContacts = db.Members.Where(f => f.Id == data.Member.Id).SelectMany(f => f.EmergencyContacts).ToDictionary(f => f.Id, f => f);

            List <EmergencyContact> desiredContacts = new List <EmergencyContact>(data.Contacts);

            foreach (var contact in desiredContacts)
            {
                var cData = new Model.EmergencyContactData
                {
                    Name     = contact.Name,
                    Relation = contact.Relation,
                    Type     = contact.Type,
                    Number   = contact.Number
                };

                Model.MemberEmergencyContact memberContact;
                if (existingContacts.TryGetValue(contact.Id, out memberContact))
                {
                    existingContacts.Remove(contact.Id);
                }

                if (string.IsNullOrWhiteSpace(contact.Name))
                {
                    // If there's no name, delete it.
                    if (memberContact != null)
                    {
                        member.EmergencyContacts.Remove(memberContact);
                    }
                    continue;
                }

                if (string.IsNullOrWhiteSpace(contact.Number))
                {
                    return(string.Format("{0}'s number is blank", contact.Name));
                }

                if (memberContact == null)
                {
                    memberContact = new Model.MemberEmergencyContact();
                    member.EmergencyContacts.Add(memberContact);
                }

                memberContact.EncryptedData = EncryptionService.Protect(EncryptionService.MEMBER_SENSITIVE, JsonConvert.SerializeObject(cData));
            }

            foreach (var leftover in existingContacts.Values)
            {
                member.EmergencyContacts.Remove(leftover);
            }
            db.SaveChanges();

            return("OK");
        }
Ejemplo n.º 2
0
        public string SaveMedical(MemberMedical data)
        {
            if (data == null || data.Member == null || data.Member.Id == Guid.Empty)
            ThrowSubmitErrors(new[] { new Web.Model.SubmitError { Error = "No user specified" } });

              if (!(Permissions.IsAdmin || Permissions.IsMembershipForPerson(data.Member.Id) || Permissions.IsSelf(data.Member.Id)))
            ThrowAuthError();

              Model.Member member = db.Members.Include("MedicalInfo", "EmergencyContacts").Single(f => f.Id == data.Member.Id);
              Model.MemberMedical medical = member.MedicalInfo;
              if (medical == null)
              {
            medical = new Model.MemberMedical();
            member.MedicalInfo = medical;
              }

              medical.EncryptedAllergies = string.IsNullOrWhiteSpace(data.Allergies) ? null : EncryptionService.Protect(EncryptionService.MEMBER_SENSITIVE, data.Allergies);
              medical.EncryptedMedications = string.IsNullOrWhiteSpace(data.Medications) ? null : EncryptionService.Protect(EncryptionService.MEMBER_SENSITIVE, data.Medications);
              medical.EncryptedDisclosures = string.IsNullOrWhiteSpace(data.Disclosure) ? null : EncryptionService.Protect(EncryptionService.MEMBER_SENSITIVE, data.Disclosure);

              var existingContacts = db.Members.Where(f => f.Id == data.Member.Id).SelectMany(f => f.EmergencyContacts).ToDictionary(f => f.Id, f => f);

              List<EmergencyContact> desiredContacts = new List<EmergencyContact>(data.Contacts);
              foreach (var contact in desiredContacts)
              {
            var cData = new Model.EmergencyContactData
            {
              Name = contact.Name,
              Relation = contact.Relation,
              Type = contact.Type,
              Number = contact.Number
            };

            Model.MemberEmergencyContact memberContact;
            if (existingContacts.TryGetValue(contact.Id, out memberContact))
            {
              existingContacts.Remove(contact.Id);
            }

            if (string.IsNullOrWhiteSpace(contact.Name))
            {
              // If there's no name, delete it.
              if (memberContact != null)
              {
            member.EmergencyContacts.Remove(memberContact);
              }
              continue;
            }

            if (string.IsNullOrWhiteSpace(contact.Number))
              return string.Format("{0}'s number is blank", contact.Name);

            if (memberContact == null)
            {
              memberContact = new Model.MemberEmergencyContact();
              member.EmergencyContacts.Add(memberContact);
            }

            memberContact.EncryptedData = EncryptionService.Protect(EncryptionService.MEMBER_SENSITIVE, JsonConvert.SerializeObject(cData));
              }

              foreach (var leftover in existingContacts.Values)
              {
            member.EmergencyContacts.Remove(leftover);
              }
              db.SaveChanges();

              return "OK";
        }