Пример #1
0
        private ResidentEntity ConvertToResidentEntity(ResidentRequest resident)
        {
            ResidentEntity residentEntity = new ResidentEntity()
            {
                CareHomeId       = resident.CareHomeId,
                LocalAuthorityId = resident.LocalAuthorityId,
                NhsNumber        = resident.NhsNumber,
                PoNumber         = resident.PoNumber,
                LaId             = resident.LaId,
                NymsId           = resident.NymsId,
                ForeName         = resident.ForeName,
                SurName          = resident.SurName,
                MiddleName       = resident.MiddleName,
                Dob                = resident.Dob ?? Convert.ToDateTime("1800-01-01"),
                Gender             = resident.Gender,
                MaritalStatus      = resident.MaritalStatus,
                CareCategoryId     = resident.CareCategoryId,
                CareNeed           = resident.CareNeed,
                StayType           = resident.StayType,
                RoomLocation       = resident.RoomLocation,
                RoomNumber         = resident.RoomNumber,
                AdmissionDate      = (DateTime)resident.AdmissionDate,
                Comments           = resident.Comments,
                UpdatedById        = resident.UpdatedBy,
                ExitDate           = Convert.ToDateTime("9999-12-31"),
                EnquiryReferenceId = resident.ReferenceId,
                Address            = null,
                NextOfKins         = null,
                EmailAddress       = resident.EmailAddress,
                PhoneNumber        = resident.PhoneNumber,
                CareHomeDivisionId = resident.CareHomeDivisionId
            };

            // fill in if resident address found?
            if (resident.Address != null && !string.IsNullOrEmpty(resident.Address.Street1))
            {
                residentEntity.Address = CreateAddress(
                    resident.Address,
                    resident.Address.AddrType ?? ADDRESS_TYPE.home.ToString());
            }

            // fill in if nok found?
            if (resident.NextOfKins != null && resident.NextOfKins.Any())
            {
                // safeguard. ensure atleast the 1st record has forename
                var fn = resident.NextOfKins.FirstOrDefault().ForeName;
                if (!string.IsNullOrEmpty(fn))
                {
                    residentEntity.NextOfKins = CreateNextOfKinList(resident);
                }
            }

            if (resident.SocialWorker != null && !string.IsNullOrEmpty(resident.SocialWorker.ForeName))
            {
                residentEntity.SocialWorker = resident.SocialWorker;
            }

            return(residentEntity);
        }
        public Response AddResident(string idNumber)
        {
            Response response = new Response()
            {
                Status = 0, Message = ""
            };

            if (string.IsNullOrEmpty(idNumber))
            {
                return(response);
            }

            IdentityHelper helper = new IdentityHelper(idNumber);

            if (helper.IsValid)
            {
                if (residentRepo.GetByID(idNumber) == null)
                {
                    ResidentEntity residentEntity = new ResidentEntity()
                    {
                        IDNumber = helper.IdentityNumber, DateOfBirth = helper.BirthDate, Gender = helper.Gender, Citizenship = helper.IsSouthAfrican
                    };
                    try
                    {
                        residentRepo.Add(residentEntity);
                        response.Status  = 1;
                        response.Message = helper.IdentityNumber + ": Inserted Successfully";
                        logger.LogInfo(helper.IdentityNumber + ": Inserted Successfully");
                    }
                    catch (Exception ex)
                    {
                        response.Status  = -1;
                        response.Message = "Error occoured while processing the request";
                        logger.LogError("Error occoured in ResidentService.AddResident");
                        logger.LogError(ex.Message);
                    }
                }
                else
                {
                    response.Status  = -2;
                    response.Message = helper.IdentityNumber + " South African ID Number already exist!!";
                    logger.LogWarn(helper.IdentityNumber + " South African ID Number already exist!!");
                }
            }
            else
            {
                response.Status  = -3;
                response.Message = helper.IdentityNumber + " Validation failed for the below reasons!!<hr>" + helper.ValidationMessage;
                logger.LogWarn(helper.IdentityNumber + " Validation failed for the below reasons!!");
                logger.LogWarn(helper.ValidationMessage);
            }
            return(response);
        }
        public void Add(ResidentEntity resident)
        {
            try
            {
                MockResidentDBContext.Residents.Add(mapper.MapToResidentData(resident));
            }
            catch (Exception)
            {
                logger.LogError("Error occoured in ResidentRepository.Add");
                throw;
            }

            //SaveChanges if context is used
        }
Пример #4
0
        public IActionResult Get(string idNumber)
        {
            ResidentEntity resident = null;

            try
            {
                resident = coreService.GetResidentByID(idNumber);
                if (resident == null)
                {
                    logger.LogInfo("No records found for " + idNumber + " ID number!!");
                    return(NotFound());
                }
                return(Ok(resident));
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return(Ok(false));
            }
            finally
            {
                resident = null;
            }
        }
Пример #5
0
        public Task <ResidentEntity> Update(ResidentEntity residentEntity)
        {
            if (residentEntity == null)
            {
                throw new ArgumentNullException("Resident and Enquriy entities required");
            }

            string sql = @"UPDATE [dbo].[residents] SET
                    [care_home_id] = @carehomeid
                   ,[local_authority_id] = @localauthorityid
                   ,[nhs_number] = @nhsnumber
                   ,[po_number] = @ponumber
                   ,[la_id] = @laid
                   ,[nyms_id] = @nymsid
                   ,[forename] = @forename
                   ,[surname] = @surname
                   ,[middle_name] = @middlename
                   ,[dob] = @dob
                   ,[gender] = @gender
                   ,[marital_status] = @maritalstatus
                   ,[care_category_id] = @carecategoryid
                   ,[care_need] = @careneed
                   ,[stay_type] = @staytype
                   ,[room_location] = @roomlocation
                   ,[room_number] = @roomnumber
                   ,[admission_date] = @admissiondate
                   ,[exit_date] = @exitdate
                   ,[comments] = @comments
                   ,[status] = @status
                   ,[updated_by_id] = @updatedbyid
                   ,[care_home_division_id] = @carehomedivisionid
                WHERE [id] = @id";

            string sqlUpdateAddress = @"UPDATE [dbo].[resident_addresses] SET
                                        [addr_type] = @addrtype
                                       ,[street1] = @street1
                                       ,[street2] = @street2
                                       ,[city] = @city
                                       ,[county] = @county
                                       ,[post_code] = @postcode
                                    WHERE [id] = @id"; // id of address table

            string sqlInsertAddress = @"INSERT INTO [dbo].[resident_addresses]
                                       ([resident_id]
                                       ,[addr_type]
                                       ,[street1]
                                       ,[street2]
                                       ,[city]
                                       ,[county]
                                       ,[post_code])
                                 VALUES
                                       (@residentid
                                       ,@addrtype
                                       ,@street1
                                       ,@street2
                                       ,@city
                                       ,@county
                                       ,@postcode)";

            string sqlUpdateResidentContact = @"UPDATE [dbo].[resident_contacts] SET
                                            [data] = @data
                                        WHERE [id] = @id"; // id of address table

            string sqlInsertResidentContact = @"INSERT INTO [dbo].[resident_contacts]
                                       ([resident_id]
                                       ,[contact_type]
                                       ,[data])
                                 VALUES
                                       (@residentid
                                       ,@contacttype
                                       ,@data)";

            // TODO LATER
            string sqlUpdateNok = @"UPDATE [dbo].[next_of_kin] SET
                                        [forename] = @forename
                                       ,[surname] = @surname
                                       ,[relationship] = @relationship
                                       ,[updated_by_id] = @updatedbyid";

            string sqlUpdateSocialWorker = @"UPDATE [dbo].[social_workers] SET
                                        [forename] = @forename
                                       ,[surname] = @surname
                                       ,[email_address] = @emailaddress
                                       ,[phone_number] = @phonenumber
                                    WHERE [id] = @id";

            string sqlInsertSocialWorker = @"INSERT INTO [dbo].[social_workers]
                                       ([resident_id]
                                       ,[forename]
                                       ,[surname]
                                       ,[email_address]
                                       ,[phone_number])
                                 VALUES
                                       (@residentid
                                       ,@forename
                                       ,@surname
                                       ,@emailaddress
                                       ,@phonenumber)";

            DynamicParameters dp = new DynamicParameters();

            dp.Add("carehomeid", residentEntity.CareHomeId, DbType.Int32, ParameterDirection.Input);
            dp.Add("localauthorityid", residentEntity.LocalAuthorityId, DbType.Int32, ParameterDirection.Input);
            dp.Add("nhsnumber", residentEntity.NhsNumber, DbType.String, ParameterDirection.Input, 20);
            dp.Add("ponumber", residentEntity.PoNumber, DbType.String, ParameterDirection.Input, 20);
            dp.Add("laid", residentEntity.LaId, DbType.String, ParameterDirection.Input, 20);
            dp.Add("nymsid", residentEntity.NymsId, DbType.String, ParameterDirection.Input, 20);
            dp.Add("forename", residentEntity.ForeName.Trim(), DbType.String, ParameterDirection.Input, 80);
            dp.Add("surname", residentEntity.SurName.Trim(), DbType.String, ParameterDirection.Input, 80);
            dp.Add("middlename", residentEntity.MiddleName, DbType.String, ParameterDirection.Input, 80);
            dp.Add("dob", residentEntity.Dob, DbType.Date, ParameterDirection.Input, 80);
            dp.Add("gender", residentEntity.Gender, DbType.String, ParameterDirection.Input, 80);
            dp.Add("maritalstatus", residentEntity.MaritalStatus, DbType.String, ParameterDirection.Input, 80);
            dp.Add("carecategoryid", residentEntity.CareCategoryId, DbType.Int32, ParameterDirection.Input);
            dp.Add("careneed", residentEntity.CareNeed, DbType.String, ParameterDirection.Input, 80);
            dp.Add("staytype", residentEntity.StayType, DbType.String, ParameterDirection.Input, 80);
            dp.Add("roomlocation", residentEntity.RoomLocation, DbType.Int32, ParameterDirection.Input, 80);
            dp.Add("roomnumber", residentEntity.RoomNumber, DbType.Int32, ParameterDirection.Input, 80);
            dp.Add("admissiondate", residentEntity.AdmissionDate, DbType.Date, ParameterDirection.Input, 80);
            dp.Add("exitdate", residentEntity.ExitDate, DbType.Date, ParameterDirection.Input, 80);
            dp.Add("comments", residentEntity.Comments, DbType.String, ParameterDirection.Input, 500);
            dp.Add("status", residentEntity.Status, DbType.String, ParameterDirection.Input, 80);
            dp.Add("updatedbyid", residentEntity.UpdatedById, DbType.Int32, ParameterDirection.Input);
            dp.Add("id", residentEntity.Id, DbType.Int32, ParameterDirection.Input); // for Where clause
            dp.Add("carehomedivisionid", residentEntity.CareHomeDivisionId, DbType.Int32, ParameterDirection.Input);

            DynamicParameters dpAddrResident = new DynamicParameters();

            if (residentEntity.Address != null)
            {
                dpAddrResident.Add("@addrtype", residentEntity.Address.AddrType, DbType.String, ParameterDirection.Input, 80);
                dpAddrResident.Add("@street1", residentEntity.Address.Street1, DbType.String, ParameterDirection.Input, 100);
                dpAddrResident.Add("@street2", residentEntity.Address.Street2, DbType.String, ParameterDirection.Input, 100);
                dpAddrResident.Add("@city", residentEntity.Address.City, DbType.String, ParameterDirection.Input, 80);
                dpAddrResident.Add("@county", residentEntity.Address.County, DbType.String, ParameterDirection.Input, 80);
                dpAddrResident.Add("@postcode", residentEntity.Address.PostCode, DbType.String, ParameterDirection.Input, 80);
                dpAddrResident.Add("residentid", residentEntity.Id, DbType.Int32, ParameterDirection.Input); // for Where clause
            }

            using (IDbConnection conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                using (var tran = conn.BeginTransaction())
                {
                    var affRowsAddr = conn.Execute(sql, dp, transaction: tran);
                    // resident address..
                    if (residentEntity.Address != null)
                    {
                        if (residentEntity.Address.Id <= 0)
                        {
                            affRowsAddr = conn.Execute(sqlInsertAddress, dpAddrResident, transaction: tran);
                        }
                        else
                        {
                            dpAddrResident.Add("id", residentEntity.Address.Id, DbType.Int32, ParameterDirection.Input); // for Where clause
                            affRowsAddr = conn.Execute(sqlUpdateAddress, dpAddrResident, transaction: tran);
                        }
                    }

                    // REDONE-NOW resident contact is JUST email and phone field
                    if (residentEntity.ResidentContacts != null)
                    {
                        residentEntity.ResidentContacts.ForEach((rc) =>
                        {
                            DynamicParameters dpCnt = new DynamicParameters();

                            if (rc.Id <= 0)
                            {
                                // new rec
                                dpCnt.Add("contacttype", rc.ContactType, DbType.String, ParameterDirection.Input, 80);
                                dpCnt.Add("data", rc.Data, DbType.String, ParameterDirection.Input, 100);
                                dpCnt.Add("residentid", residentEntity.Id, DbType.Int32, ParameterDirection.Input);
                                var affRowsCnt = conn.Execute(sqlInsertResidentContact, dpCnt, transaction: tran);
                            }
                            else
                            {
                                dpCnt.Add("data", rc.Data, DbType.String, ParameterDirection.Input, 100);
                                dpCnt.Add("id", rc.Id, DbType.Int32, ParameterDirection.Input); // for Where clause
                                var affRowsCnt = conn.Execute(sqlUpdateResidentContact, dpCnt, transaction: tran);
                            }
                        });
                    }

                    // social worker
                    if (residentEntity.SocialWorker != null && !string.IsNullOrEmpty(residentEntity.SocialWorker.ForeName))
                    {
                        DynamicParameters dpSw = new DynamicParameters();
                        dpSw.Add("forename", residentEntity.SocialWorker.ForeName.Trim(), DbType.String, ParameterDirection.Input, 80);
                        dpSw.Add("surname", residentEntity.SocialWorker.SurName.Trim(), DbType.String, ParameterDirection.Input, 80);
                        dpSw.Add("emailaddress", residentEntity.SocialWorker.EmailAddress, DbType.String, ParameterDirection.Input, 200);
                        dpSw.Add("phonenumber", residentEntity.SocialWorker.PhoneNumber, DbType.String, ParameterDirection.Input, 40);
                        if (residentEntity.SocialWorker.Id <= 0)
                        {
                            // New rec
                            dpSw.Add("residentid", residentEntity.Id, DbType.Int32, ParameterDirection.Input);
                            var affRowsSw = conn.Execute(sqlInsertSocialWorker, dpSw, transaction: tran);
                        }
                        else
                        {
                            dpSw.Add("id", residentEntity.SocialWorker.Id, DbType.Int32, ParameterDirection.Input); // for Where clause
                            var affRowsSw = conn.Execute(sqlUpdateSocialWorker, dpSw, transaction: tran);
                        }
                    }
                    // noks, list of noks
                    if (residentEntity.NextOfKins != null && residentEntity.NextOfKins.Any())
                    {
                        DynamicParameters dpNok = new DynamicParameters();
                        //residentEntity.NextOfKins.ForEach(nok =>
                        //{

/*                            int newNokId = 0;
 *                          dpNok.Add("forename", nok.ForeName.Trim(), DbType.String, ParameterDirection.Input, 80);
 *                          dpNok.Add("surname", nok.SurName.Trim(), DbType.String, ParameterDirection.Input, 80);
 *                          dpNok.Add("relationship", nok.Relationship, DbType.String, ParameterDirection.Input, 80);
 *                          dpNok.Add("updatedbyid", residentEntity.UpdatedById, DbType.Int32, ParameterDirection.Input);
 *
 *                          dpNok.Add("residentid", newResidentId, DbType.Int32, ParameterDirection.Input);
 *                          newNokId = conn.QuerySingle<int>(sqlInsertNok, dpNok, commandType: CommandType.Text, transaction: tran);*/
                        // add nok address to address table
                        // TODO: CREATE a new table for NOK Address and save it. Below is OLD code

                        /*                            if (nok.Address != null && nok.Address.Street1 != "") // ensure atleast one field is filled in?
                         *                          {
                         *                              DynamicParameters dpAddrNok = new DynamicParameters();
                         *                              dpAddrNok.Add("@addrtype", nok.Address.AddrType, DbType.String, ParameterDirection.Input, 80);
                         *                              dpAddrNok.Add("@street1", nok.Address.Street1, DbType.String, ParameterDirection.Input, 100);
                         *                              dpAddrNok.Add("@street2", nok.Address.Street2, DbType.String, ParameterDirection.Input, 100);
                         *                              dpAddrNok.Add("@city", nok.Address.City), DbType.String, ParameterDirection.Input, 80);
                         *                              dpAddrNok.Add("@county", nok.Address.County, DbType.String, ParameterDirection.Input, 80);
                         *                              dpAddrNok.Add("@postcode", nok.Address.PostCode, DbType.String, ParameterDirection.Input, 80);
                         *                              dpAddrNok.Add("residentid", newResidentId, DbType.Int32, ParameterDirection.Input);
                         *                              var affRowsNokCnt = conn.Execute(sqlInsertAddress, dpAddrNok, transaction: tran);
                         *                          }*/
                        // add nok contact to contacts table

                        /*                            if (nok.ContactInfos != null && nok.ContactInfos.Any())
                         *                          {
                         *                              DynamicParameters dpCnt = new DynamicParameters();
                         *                              nok.ContactInfos.ForEach(nokCt =>
                         *                              {
                         *                                  dpCnt.Add("reftype", nokCt.RefType, DbType.String, ParameterDirection.Input, 80);
                         *                                  dpCnt.Add("contacttype", nokCt.ContactType, DbType.String, ParameterDirection.Input, 80);
                         *                                  dpCnt.Add("data", nokCt.Data, DbType.String, ParameterDirection.Input, 100);
                         *
                         *                                  dpCnt.Add("nokid", newNokId, DbType.Int32, ParameterDirection.Input);
                         *                                  dpCnt.Add("residentid", newResidentId, DbType.Int32, ParameterDirection.Input);
                         *                                  var affRowsNokCnt = conn.Execute(sqlInsertContact, dpCnt, transaction: tran);
                         *                              });
                         *                          }*/
                        //});
                    }

                    tran.Commit();
                }
                return(Task.FromResult(residentEntity));
            }
        }
Пример #6
0
        public Task <ResidentEntity> Create(ResidentEntity residentEntity)
        {
            if (residentEntity == null)
            {
                throw new ArgumentNullException("Resident and Enquriy entities required");
            }

            string sql = @"INSERT INTO [dbo].[residents]
                   ([reference_id]
                   ,[care_home_id]
                   ,[local_authority_id]
                   ,[nhs_number]
                   ,[po_number]
                   ,[la_id]
                   ,[nyms_id]
                   ,[forename]
                   ,[surname]
                   ,[middle_name]
                   ,[dob]
                   ,[gender]
                   ,[marital_status]
                   ,[care_category_id]
                   ,[care_need]
                   ,[stay_type]
                   ,[room_location]
                   ,[room_number]
                   ,[admission_date]
                   ,[exit_date]
                   ,[comments]
                   ,[status]
                   ,[updated_by_id]
                   ,[enquiry_ref_id]
                   ,[care_home_division_id])
                VALUES
                   (@referenceid
                   ,@carehomeid
                   ,@localauthorityid
                   ,@nhsnumber
                   ,@ponumber
                   ,@laid
                   ,@nymsid
                   ,@forename
                   ,@surname
                   ,@middlename
                   ,@dob
                   ,@gender
                   ,@maritalstatus
                   ,@carecategoryid
                   ,@careneed
                   ,@staytype
                   ,@roomlocation
                   ,@roomnumber
                   ,@admissiondate
                   ,@exitdate
                   ,@comments
                   ,@status
                   ,@updatedbyid
                   ,@enquiryrefid
                   ,@carehomedivisionid);
                    SELECT CAST(SCOPE_IDENTITY() as int);";
            string sqlInsertAddress         = @"INSERT INTO [dbo].[resident_addresses]
                                       ([resident_id]
                                       ,[addr_type]
                                       ,[street1]
                                       ,[street2]
                                       ,[city]
                                       ,[county]
                                       ,[post_code])
                                 VALUES
                                       (@residentid
                                       ,@addrtype
                                       ,@street1
                                       ,@street2
                                       ,@city
                                       ,@county
                                       ,@postcode)";
            string sqlInsertResidentContact = @"INSERT INTO [dbo].[resident_contacts]
                                       ([resident_id]
                                       ,[contact_type]
                                       ,[data])
                                 VALUES
                                       (@residentid
                                       ,@contacttype
                                       ,@data)";
            string sqlInsertNok             = @"INSERT INTO [dbo].[next_of_kin]
                                       ([resident_id]
                                       ,[forename]
                                       ,[surname]
                                       ,[relationship]
                                       ,[updated_by_id])
                                 VALUES
                                       (@residentid
                                       ,@forename
                                       ,@surname
                                       ,@relationship
                                       ,@updatedbyid);
                                        SELECT CAST(SCOPE_IDENTITY() as int);";
            string sqlInsertSocialWorker    = @"INSERT INTO [dbo].[social_workers]
                                       ([resident_id]
                                       ,[forename]
                                       ,[surname]
                                       ,[email_address]
                                       ,[phone_number])
                                 VALUES
                                       (@residentid
                                       ,@forename
                                       ,@surname
                                       ,@emailaddress
                                       ,@phonenumber);";
            string sqlEnqUpdate             = @"UPDATE [dbo].[enquires] 
                                        SET status = @status
                                            ,[updated_by_id] = @updatedbyid
                                            ,updated_date = GETDATE()
                                        WHERE [reference_id] = @referenceid";

            DynamicParameters dp = new DynamicParameters();

            dp.Add("referenceid", residentEntity.ReferenceId, DbType.Guid, ParameterDirection.Input, 80);
            dp.Add("carehomeid", residentEntity.CareHomeId, DbType.Int32, ParameterDirection.Input);
            dp.Add("localauthorityid", residentEntity.LocalAuthorityId, DbType.Int32, ParameterDirection.Input);
            dp.Add("nhsnumber", residentEntity.NhsNumber, DbType.String, ParameterDirection.Input, 20);
            dp.Add("ponumber", residentEntity.PoNumber, DbType.String, ParameterDirection.Input, 20);
            dp.Add("laid", residentEntity.LaId, DbType.String, ParameterDirection.Input, 20);
            dp.Add("nymsid", residentEntity.NymsId, DbType.String, ParameterDirection.Input, 20);
            dp.Add("forename", residentEntity.ForeName.Trim(), DbType.String, ParameterDirection.Input, 80);
            dp.Add("surname", residentEntity.SurName.Trim(), DbType.String, ParameterDirection.Input, 80);
            dp.Add("middlename", residentEntity.MiddleName, DbType.String, ParameterDirection.Input, 80);
            dp.Add("dob", residentEntity.Dob, DbType.Date, ParameterDirection.Input, 80);
            dp.Add("gender", residentEntity.Gender, DbType.String, ParameterDirection.Input, 80);
            dp.Add("maritalstatus", residentEntity.MaritalStatus, DbType.String, ParameterDirection.Input, 80);
            dp.Add("carecategoryid", residentEntity.CareCategoryId, DbType.Int32, ParameterDirection.Input);
            dp.Add("careneed", residentEntity.CareNeed, DbType.String, ParameterDirection.Input, 80);
            dp.Add("staytype", residentEntity.StayType, DbType.String, ParameterDirection.Input, 80);
            dp.Add("roomlocation", residentEntity.RoomLocation, DbType.Int32, ParameterDirection.Input, 80);
            dp.Add("roomnumber", residentEntity.RoomNumber, DbType.Int32, ParameterDirection.Input, 80);
            dp.Add("admissiondate", residentEntity.AdmissionDate, DbType.Date, ParameterDirection.Input, 80);
            dp.Add("exitdate", residentEntity.ExitDate, DbType.Date, ParameterDirection.Input, 80);
            dp.Add("comments", residentEntity.Comments, DbType.String, ParameterDirection.Input, 500);
            dp.Add("status", residentEntity.Status, DbType.String, ParameterDirection.Input, 80);
            dp.Add("updatedbyid", residentEntity.UpdatedById, DbType.Int32, ParameterDirection.Input);
            dp.Add("enquiryrefid", residentEntity.EnquiryReferenceId, DbType.Guid, ParameterDirection.Input, 80);
            dp.Add("carehomedivisionid", residentEntity.CareHomeDivisionId, DbType.Int32, ParameterDirection.Input);

            DynamicParameters dpAddrResident = new DynamicParameters();

            if (residentEntity.Address != null)
            {
                dpAddrResident.Add("@addrtype", residentEntity.Address.AddrType, DbType.String, ParameterDirection.Input, 80);
                dpAddrResident.Add("@street1", residentEntity.Address.Street1, DbType.String, ParameterDirection.Input, 100);
                dpAddrResident.Add("@street2", residentEntity.Address.Street2, DbType.String, ParameterDirection.Input, 100);
                dpAddrResident.Add("@city", residentEntity.Address.City, DbType.String, ParameterDirection.Input, 80);
                dpAddrResident.Add("@county", residentEntity.Address.County, DbType.String, ParameterDirection.Input, 80);
                dpAddrResident.Add("@postcode", residentEntity.Address.PostCode, DbType.String, ParameterDirection.Input, 80);
            }

            // turing enq into resident
            DynamicParameters dpEnq = new DynamicParameters();

            dpEnq.Add("referenceid", residentEntity.EnquiryReferenceId, DbType.Guid, ParameterDirection.Input, 80);
            dpEnq.Add("status", ENQUIRY_STATUS.admit.ToString(), DbType.String, ParameterDirection.Input, 80);
            dpEnq.Add("updatedbyid", residentEntity.UpdatedById, DbType.Int32, ParameterDirection.Input);

            using (IDbConnection conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                using (var tran = conn.BeginTransaction())
                {
                    var newResidentId = conn.QuerySingle <int>(sql, dp, commandType: CommandType.Text, transaction: tran);
                    // resident address..
                    if (residentEntity.Address != null)
                    {
                        dpAddrResident.Add("residentid", newResidentId, DbType.Int32, ParameterDirection.Input);
                        var affRowsAddr = conn.Execute(sqlInsertAddress, dpAddrResident, transaction: tran);
                    }

                    // resident contacts..// contacts are, list of contacts for res and noks
                    // REDONE-NOW resident contact is JUST email and phone field
                    if (!string.IsNullOrEmpty(residentEntity.EmailAddress))
                    {
                        DynamicParameters dpCnt = new DynamicParameters();
                        dpCnt.Add("residentid", newResidentId, DbType.Int32, ParameterDirection.Input);
                        dpCnt.Add("contacttype", CONTACT_TYPE.email.ToString(), DbType.String, ParameterDirection.Input, 80);
                        dpCnt.Add("data", residentEntity.EmailAddress, DbType.String, ParameterDirection.Input, 100);
                        var affRowsCnt = conn.Execute(sqlInsertResidentContact, dpCnt, transaction: tran);
                    }
                    if (!string.IsNullOrEmpty(residentEntity.PhoneNumber))
                    {
                        DynamicParameters dpCnt = new DynamicParameters();
                        dpCnt.Add("residentid", newResidentId, DbType.Int32, ParameterDirection.Input);
                        dpCnt.Add("contacttype", CONTACT_TYPE.phone.ToString(), DbType.String, ParameterDirection.Input, 80);
                        dpCnt.Add("data", residentEntity.PhoneNumber, DbType.String, ParameterDirection.Input, 100);
                        var affRowsCnt = conn.Execute(sqlInsertResidentContact, dpCnt, transaction: tran);
                    }
                    // social worker
                    if (residentEntity.SocialWorker != null && !string.IsNullOrEmpty(residentEntity.SocialWorker.ForeName))
                    {
                        DynamicParameters dpSw = new DynamicParameters();
                        dpSw.Add("residentid", newResidentId, DbType.Int32, ParameterDirection.Input);
                        dpSw.Add("forename", residentEntity.SocialWorker.ForeName, DbType.String, ParameterDirection.Input, 80);
                        dpSw.Add("surname", residentEntity.SocialWorker.SurName, DbType.String, ParameterDirection.Input, 80);
                        dpSw.Add("emailaddress", residentEntity.SocialWorker.EmailAddress, DbType.String, ParameterDirection.Input, 200);
                        dpSw.Add("phonenumber", residentEntity.SocialWorker.PhoneNumber, DbType.String, ParameterDirection.Input, 40);
                        var affRowsSw = conn.Execute(sqlInsertSocialWorker, dpSw, transaction: tran);
                    }
                    // noks, list of noks
                    if (residentEntity.NextOfKins != null && residentEntity.NextOfKins.Any())
                    {
                        DynamicParameters dpNok = new DynamicParameters();
                        residentEntity.NextOfKins.ForEach(nok =>
                        {
                            int newNokId = 0;
                            dpNok.Add("forename", nok.ForeName, DbType.String, ParameterDirection.Input, 80);
                            dpNok.Add("surname", nok.SurName, DbType.String, ParameterDirection.Input, 80);
                            dpNok.Add("relationship", nok.Relationship, DbType.String, ParameterDirection.Input, 80);
                            dpNok.Add("updatedbyid", residentEntity.UpdatedById, DbType.Int32, ParameterDirection.Input);

                            dpNok.Add("residentid", newResidentId, DbType.Int32, ParameterDirection.Input);
                            newNokId = conn.QuerySingle <int>(sqlInsertNok, dpNok, commandType: CommandType.Text, transaction: tran);
                            // add nok address to address table
                            // TODO: CREATE a new table for NOK Address and save it. Below is OLD code

/*                            if (nok.Address != null && nok.Address.Street1 != "") // ensure atleast one field is filled in?
 *                          {
 *                              DynamicParameters dpAddrNok = new DynamicParameters();
 *                              dpAddrNok.Add("@addrtype", nok.Address.AddrType, DbType.String, ParameterDirection.Input, 80);
 *                              dpAddrNok.Add("@street1", nok.Address.Street1, DbType.String, ParameterDirection.Input, 100);
 *                              dpAddrNok.Add("@street2", nok.Address.Street2, DbType.String, ParameterDirection.Input, 100);
 *                              dpAddrNok.Add("@city", nok.Address.City, DbType.String, ParameterDirection.Input, 80);
 *                              dpAddrNok.Add("@county", nok.Address.County, DbType.String, ParameterDirection.Input, 80);
 *                              dpAddrNok.Add("@postcode", nok.Address.PostCode, DbType.String, ParameterDirection.Input, 80);
 *                              dpAddrNok.Add("residentid", newResidentId, DbType.Int32, ParameterDirection.Input);
 *                              var affRowsNokCnt = conn.Execute(sqlInsertAddress, dpAddrNok, transaction: tran);
 *                          }*/
                            // add nok contact to contacts table

/*                            if (nok.ContactInfos != null && nok.ContactInfos.Any())
 *                          {
 *                              DynamicParameters dpCnt = new DynamicParameters();
 *                              nok.ContactInfos.ForEach(nokCt =>
 *                              {
 *                                  dpCnt.Add("reftype", nokCt.RefType, DbType.String, ParameterDirection.Input, 80);
 *                                  dpCnt.Add("contacttype", nokCt.ContactType, DbType.String, ParameterDirection.Input, 80);
 *                                  dpCnt.Add("data", nokCt.Data, DbType.String, ParameterDirection.Input, 100);
 *
 *                                  dpCnt.Add("nokid", newNokId, DbType.Int32, ParameterDirection.Input);
 *                                  dpCnt.Add("residentid", newResidentId, DbType.Int32, ParameterDirection.Input);
 *                                  var affRowsNokCnt = conn.Execute(sqlInsertContact, dpCnt, transaction: tran);
 *                              });
 *                          }*/
                        });
                    }

                    // update enquiry status with admit
                    var enqUpdated = conn.Execute(sqlEnqUpdate, dpEnq, commandType: CommandType.Text, transaction: tran);

                    tran.Commit();
                }
                return(Task.FromResult(residentEntity));
            }
        }
 public Resident MapToResidentData(ResidentEntity residentEntity)
 {
     return(residentEntity != null?mapper.Map <Resident>(residentEntity) : null);
 }