public Task <Result> SetAvailabilitySearchSettings(int agentId, int agencyId, AgentAccommodationBookingSettingsInfo settings)
        {
            return(CheckRelationExists(agentId, agencyId)
                   .BindWithTransaction(_context, () => Result.Success()
                                        .Map(() => GetOrDefault(agentId, agencyId))
                                        .Tap(Update)
                                        .Bind(WriteAuditLog)));


            Task Update(AgentSystemSettings existingSettings)
            {
                if (existingSettings is null)
                {
                    var newSettings = new AgentSystemSettings
                    {
                        AgencyId = agencyId,
                        AgentId  = agentId,
                        AccommodationBookingSettings = settings.ToAgentAccommodationBookingSettings()
                    };
                    _context.Add(newSettings);
                }
                else
                {
                    existingSettings.AccommodationBookingSettings = settings.ToAgentAccommodationBookingSettings();
                    _context.Update(existingSettings);
                }

                return(_context.SaveChangesAsync());
            }

            Task <Result> WriteAuditLog(AgentSystemSettings _)
            => _managementAuditService.Write(ManagementEventType.AgentSystemSettingsCreateOrEdit,
                                             new AgentSystemSettingsCreateOrEditEventData(agentId, agencyId, settings));
        }
        public async Task <Result> SetAvailabilitySearchSettings(int agentId, int agencyId, AgentAccommodationBookingSettings settings)
        {
            var doesRelationExist = await _context.AgentAgencyRelations
                                    .AnyAsync(r => r.AgentId == agentId || r.AgencyId == agencyId);

            if (!doesRelationExist)
            {
                return(Result.Failure("Could not find specified agent in given agency"));
            }

            var existingSettings = await _context.AgentSystemSettings.SingleOrDefaultAsync(s => s.AgentId == agentId && s.AgencyId == agencyId);

            if (existingSettings is null)
            {
                var newSettings = new AgentSystemSettings
                {
                    AgencyId = agencyId,
                    AgentId  = agentId,
                    AccommodationBookingSettings = settings
                };
                _context.Add(newSettings);
            }
            else
            {
                existingSettings.AccommodationBookingSettings = settings;
                _context.Update(existingSettings);
            }

            await _context.SaveChangesAsync();

            return(Result.Success());
        }