// this file contains code for generating Filter objects from // the selected options in the form let cmageBuildFilters; (function() { const FORM_ARRAYS = { countries: `filters[countries]`, research_periods: `filters[research_periods]`, national_strategy_and_capabilities: `filters[national_strategy_and_capabilities]`, information_sharing: `filters[information_sharing]`, company_locations: `filters[company_locations]`, company_crest: `filters[company_crest]`, company_services: `filters[company_services]`, professional_development: `filters[professional_development]`, banks: `filters[banks]`, } function countryFilters() { const selected = form.array(FORM_ARRAYS.countries); if (selected.length == 0) return []; const countryIds = selected.map(e => parseInt(e)); const countryFilter = new Filter("*", x => { // if can be filtered by country, then filter it if (x.hasOwnProperty("country_ids")) { return hasIntersect(x.country_ids, countryIds) } else if (x.hasOwnProperty("country_id")) { return countryIds.includes(x.country_id) } else { // if can't be filtered by country, then pass through return true; } }); return [countryFilter]; } // function researchPeriodFilters() { // const selected = form.array(FORM_ARRAYS.research_periods); // if (selected.length == 0) return []; // const researchPeriodIds = selected.map(e => parseInt(e)); // const researchPeriodFilter = new Filter("*", x => { // // if can be filtered by research period id, then filter it // if (x.hasOwnProperty("research_period_ids")) { // return hasIntersect(x.research_period_ids, researchPeriodIds) // } else if (x.hasOwnProperty("research_period_id")) { // return researchPeriodIds.includes(x.research_period_id) // } else { // // if can't be filtered by research period id, then pass through // return true; // } // }); // return [researchPeriodFilter]; // } const nationalStrategyCodeMap = { government_strategy_policy: "strategy_initiative", regulator_government_operated_assurance_schemes: "assurance_assessment", law_enforcement_cyber_defence_capabilities: "capability_initiative", } function nationalStrategyFilters() { const selected = form.array(FORM_ARRAYS.national_strategy_and_capabilities); if (selected.length == 0) return []; const unselected = form.array(FORM_ARRAYS.national_strategy_and_capabilities, true); const typesToHide = unselected.map(x => nationalStrategyCodeMap[x]); const filters = Filter.hideTypes(typesToHide); return filters; } function informationSharingFilters() { const selected = form.array(FORM_ARRAYS.information_sharing); if (selected.length == 0) return []; const properties = selected.map(x => `is_${x}`); const f = new Filter("computer_emergency_response_team_assessment", c => { for (const prop of properties) { console.log(c, prop, c[prop]); if (c[prop]) return true; } return false; }) return [f]; } // ========== Filters for the serviceProvider card ========== const companyLocationsMap = { in_country: "In-country", international: "International", } function companyLocationsFilters() { const selected = form.array(FORM_ARRAYS.company_locations); if (selected.length == 0) return []; const locations = selected.map(x => companyLocationsMap[x]); const f = new Filter("company", c => hasIntersect(c.locations, locations)); return [f]; } const companyCRESTMap = { member_of_crest: true, not_a_member_of_crest: false, } function companyCRESTFilters() { const selected = form.array(FORM_ARRAYS.company_crest); if (selected.length == 0) return []; const crestStatuses = selected.map(x => companyCRESTMap[x]); const f = new Filter("company", c => hasIntersect(c.crest_statuses, crestStatuses)); return [f]; } const companyServicesMap = { threat_intelligence: 'Threat Intelligence', vulnerability_assessment: 'Vulnerability Assessment', penetration_testing: 'Penetration Testing', security_operation_centre: 'SOC', incident_response: 'Incident Response', } function companyServicesFilters() { const selected = form.array(FORM_ARRAYS.company_services); if (selected.length == 0) return []; const services = selected.map(x => companyServicesMap[x]); const f = new Filter("company", c => hasIntersect(c.services, services)) return [f]; } // ====================== const professionalDevelopmentTypeMap = { academia_higher_education: "academia", training_providers: "training_provider", professional_certifications: "certification_company", professional_cyber_membership_organisations: "professional_body", specialist_recruitment: "specialist_recruitment_organisation", events_exhibitions: "event_assessment", } function professionalDevelopmentFilters() { const selected = form.array(FORM_ARRAYS.professional_development); if (selected.length == 0) return []; const unselected = form.array(FORM_ARRAYS.professional_development, true); const typesToHide = unselected.map(x => professionalDevelopmentTypeMap[x]); const filters = Filter.hideTypes(typesToHide); return filters; } const bankCategoryMap = { central_bank: 'Central Bank', commercial_bank: 'Commercial Bank', foreign_commercial_bank: 'Foreign Commercial Bank', regulator: 'Regulator', non_scheduled_bank: 'Non-scheduled Bank', non_bank_financial_institution: 'Non-bank Financial Institution', shariah_commercial_bank: 'Shariah Commercial Bank', state_commercial_bank: 'State Commercial Bank', state_specialised_bank: 'State Specialised Bank', private_commercial_bank: 'Private Commercial Bank', } function bankFilters() { const selected = form.array(FORM_ARRAYS.banks); if (selected.length == 0) return []; const categories = selected.map(x => bankCategoryMap[x]); const bankFilter = new Filter("bank", c => hasIntersect(c.categories, categories)); return [bankFilter]; } /** * return all filters for the current state of the form * @returns {Filter[]} */ function buildFilters(search_data) { const typesToHide = new Set(Object.keys(search_data)); let isFiltering = false; const filters = [ ...countryFilters(), // ...researchPeriodFilters(), ...nationalStrategyFilters(), ...informationSharingFilters(), ...companyLocationsFilters(), ...companyCRESTFilters(), ...companyServicesFilters(), ...professionalDevelopmentFilters(), ...bankFilters(), ]; if (form.array(FORM_ARRAYS.national_strategy_and_capabilities).length > 0) { typesToHide.delete("strategy_initiative"); typesToHide.delete("assurance_assessment"); typesToHide.delete("capability_initiative"); isFiltering = true; } if (form.array(FORM_ARRAYS.information_sharing).length > 0) { typesToHide.delete("computer_emergency_response_team_assessment"); isFiltering = true; } if ( form.array(FORM_ARRAYS.company_locations).length + form.array(FORM_ARRAYS.company_crest).length + form.array(FORM_ARRAYS.company_services).length > 0 ) { typesToHide.delete("company"); isFiltering = true; } if (form.array(FORM_ARRAYS.professional_development).length > 0) { typesToHide.delete("academia"); typesToHide.delete("training_provider"); typesToHide.delete("certification_company"); typesToHide.delete("professional_body"); typesToHide.delete("specialist_recruitment_organisation"); typesToHide.delete("event_assessment"); isFiltering = true; } if (form.array(FORM_ARRAYS.banks).length > 0) { typesToHide.delete("bank"); isFiltering = true; } if (isFiltering) { filters.push(...Filter.hideTypes(Array.from(typesToHide))); } return filters; } cmageBuildFilters = buildFilters; })()