From ab4a516672db64c4a8e6c9fbb796f3540c7f345e Mon Sep 17 00:00:00 2001 From: David Featherston Date: Mon, 18 Mar 2024 09:51:58 +1100 Subject: [PATCH 1/8] feat(@dpc-sdp/ripple-tide-search): merge config and drupal ui fields --- packages/ripple-tide-api/types.d.ts | 2 +- .../data/expected-listing-data-config.ts | 84 ++++ .../data/expected-listing-data-mix.ts | 90 ++++ .../__test__/data/expected-listing-data-ui.ts | 58 +++ .../mapping/__test__/data/index.ts | 15 + .../__test__/data/test-listing-data-config.ts | 81 ++++ .../__test__/data/test-listing-data-mix.ts | 406 ++++++++++++++++ .../__test__/data/test-listing-data-ui.ts | 433 ++++++++++++++++++ .../getProcessedSearchListingConfig.test.ts | 54 +++ packages/ripple-tide-search/mapping/index.ts | 132 +----- .../ripple-tide-search/mapping/utils/index.ts | 138 ++++++ tsconfig.json | 1 + 12 files changed, 1362 insertions(+), 132 deletions(-) create mode 100644 packages/ripple-tide-search/mapping/__test__/data/expected-listing-data-config.ts create mode 100644 packages/ripple-tide-search/mapping/__test__/data/expected-listing-data-mix.ts create mode 100644 packages/ripple-tide-search/mapping/__test__/data/expected-listing-data-ui.ts create mode 100644 packages/ripple-tide-search/mapping/__test__/data/index.ts create mode 100644 packages/ripple-tide-search/mapping/__test__/data/test-listing-data-config.ts create mode 100644 packages/ripple-tide-search/mapping/__test__/data/test-listing-data-mix.ts create mode 100644 packages/ripple-tide-search/mapping/__test__/data/test-listing-data-ui.ts create mode 100644 packages/ripple-tide-search/mapping/__test__/getProcessedSearchListingConfig.test.ts diff --git a/packages/ripple-tide-api/types.d.ts b/packages/ripple-tide-api/types.d.ts index 33283272ee..7564157728 100644 --- a/packages/ripple-tide-api/types.d.ts +++ b/packages/ripple-tide-api/types.d.ts @@ -288,7 +288,7 @@ export function getBody(body: any): string export function getBodyFromField( field: string, path: string | string[], - fallback: any + fallback?: any ): string export function getField( field: string, diff --git a/packages/ripple-tide-search/mapping/__test__/data/expected-listing-data-config.ts b/packages/ripple-tide-search/mapping/__test__/data/expected-listing-data-config.ts new file mode 100644 index 0000000000..c47e740845 --- /dev/null +++ b/packages/ripple-tide-search/mapping/__test__/data/expected-listing-data-config.ts @@ -0,0 +1,84 @@ +export default { + searchListingConfig: { + resultsPerPage: 20, + formTheme: 'default', + hideSearchForm: false, + labels: { + submit: 'Submit search', + placeholder: 'Start typing search term...' + }, + customSort: [{ 'title.keyword': 'asc' }] + }, + queryConfig: { + multi_match: { + query: '{{query}}', + fields: [ + 'title^3', + 'field_landing_page_summary^2', + 'body', + 'field_paragraph_body', + 'summary_processed' + ] + } + }, + globalFilters: [ + { terms: { type: ['news'] } }, + { terms: { field_node_site: [8888] } } + ], + userFilters: [ + { + id: 'topic', + component: 'TideSearchFilterDropdown', + filter: { type: 'terms', value: 'field_topic_name' }, + aggregations: { field: 'topic', source: 'taxonomy' }, + props: { + id: 'topic', + label: 'Topics', + placeholder: 'Select a topic', + type: 'RplFormDropdown', + multiple: true, + options: [ + { + id: 1, + label: 'Option 1', + value: 'Option 1', + parent: null + } + ] + } + }, + { + id: 'tags', + component: 'TideSearchFilterCheckbox', + filter: { + type: 'terms', + value: 'field_tags_name', + multiple: false + }, + props: { + id: 'tags', + label: 'Tags', + checkboxLabel: 'Only show demo tag', + onValue: 'Demo Tag' + } + } + ], + results: { + layout: { + component: 'TideSearchResultsTable', + props: { + showExtraContent: true, + columns: [ + { label: 'Title', objectKey: 'title', cols: 4 }, + { label: 'Tags', objectKey: 'field_tags_name' } + ], + extraContent: { + items: [ + { label: 'Topics', objectKey: 'field_topic_name' }, + { label: 'Date', objectKey: 'field_news_date' } + ] + } + } + } + } +} diff --git a/packages/ripple-tide-search/mapping/__test__/data/expected-listing-data-mix.ts b/packages/ripple-tide-search/mapping/__test__/data/expected-listing-data-mix.ts new file mode 100644 index 0000000000..46f9b09dfe --- /dev/null +++ b/packages/ripple-tide-search/mapping/__test__/data/expected-listing-data-mix.ts @@ -0,0 +1,90 @@ +export default { + searchListingConfig: { + hideSearchForm: false, + resultsPerPage: 12, + labels: { + submit: 'Submit search', + placeholder: 'Start typing search term...' + }, + customSort: [{ created: 'desc' }, { 'title.keyword': 'asc' }], + formTheme: 'default' + }, + queryConfig: { + multi_match: { + query: '{{query}}', + fields: [ + 'field_landing_page_summary^2', + 'field_paragraph_body', + 'summary_processed', + 'title', + 'body' + ] + } + }, + globalFilters: [ + { terms: { status: [true] } }, + { terms: { field_node_site: [8888] } }, + { terms: { type: ['news'] } }, + { terms: { field_topic_name: ['Demo Topic'] } } + ], + userFilters: [ + { + id: 'department', + component: 'TideSearchFilterDropdown', + filter: { type: 'terms', value: 'field_department_name' }, + aggregations: { field: 'department', source: 'taxonomy' }, + props: { + id: 'department', + label: 'Departments', + placeholder: 'Select a department', + type: 'RplFormDropdown', + multiple: true, + options: [ + { + id: 1, + label: 'Option 1', + value: 'Option 1', + parent: null + } + ] + } + }, + { + id: 'topic', + component: 'TideSearchFilterDropdown', + aggregations: { field: 'topic', source: 'taxonomy' }, + filter: { type: 'terms', value: 'field_topic_name' }, + props: { + id: 'topic', + label: 'Topic', + placeholder: 'Select a topic', + type: 'RplFormDropdown', + multiple: true, + options: [ + { + id: 1, + label: 'Option 1', + value: 'Option 1', + parent: null + } + ] + } + }, + { + id: 'tags', + component: 'TideSearchFilterCheckbox', + filter: { type: 'terms', value: 'field_tags_name', multiple: false }, + props: { + id: 'tags', + label: 'Tags', + checkboxLabel: 'Only show demo tag', + onValue: 'Demo Tag' + } + } + ], + results: { + layout: { component: 'TideSearchResultsList', props: { customProp: true } }, + item: { '*': { component: 'TideSearchResult' } } + }, + layoutConfig: { belowFilter: { component: 'TideCustomBelowFilterComponent' } } +} diff --git a/packages/ripple-tide-search/mapping/__test__/data/expected-listing-data-ui.ts b/packages/ripple-tide-search/mapping/__test__/data/expected-listing-data-ui.ts new file mode 100644 index 0000000000..436f329557 --- /dev/null +++ b/packages/ripple-tide-search/mapping/__test__/data/expected-listing-data-ui.ts @@ -0,0 +1,58 @@ +export default { + searchListingConfig: { + hideSearchForm: false, + resultsPerPage: 15, + labels: { submit: 'Submit', placeholder: 'Search me....' }, + customSort: [{ 'title.keyword': 'asc' }] + }, + queryConfig: { + multi_match: { query: '{{query}}', fields: ['title', 'body'] } + }, + globalFilters: [ + { terms: { field_node_site: [8888] } }, + { terms: { type: ['news'] } }, + { terms: { field_topic_name: ['Demo Topic'] } } + ], + userFilters: [ + { + id: 'topic', + component: 'TideSearchFilterDropdown', + aggregations: { field: 'topic', source: 'taxonomy' }, + filter: { type: 'terms', value: 'field_topic_name' }, + props: { + id: 'topic', + label: 'Topic', + placeholder: 'Select a topic', + type: 'RplFormDropdown', + multiple: true, + options: [ + { + id: 1, + label: 'Option 1', + value: 'Option 1', + parent: null + } + ] + } + }, + { + id: 'tags', + component: 'TideSearchFilterCheckbox', + filter: { + type: 'terms', + value: 'field_tags_name', + multiple: false + }, + props: { + id: 'tags', + label: 'Tags', + checkboxLabel: 'Only show demo tag', + onValue: 'Demo Tag' + } + } + ], + results: { + layout: { component: 'TideSearchResultsList' }, + item: { '*': { component: 'TideSearchResult' } } + } +} diff --git a/packages/ripple-tide-search/mapping/__test__/data/index.ts b/packages/ripple-tide-search/mapping/__test__/data/index.ts new file mode 100644 index 0000000000..a0082de6d6 --- /dev/null +++ b/packages/ripple-tide-search/mapping/__test__/data/index.ts @@ -0,0 +1,15 @@ +import testListingUI from './test-listing-data-ui' +import testListingConfig from './test-listing-data-config' +import testListingMix from './test-listing-data-mix' +import expectedListingUI from './expected-listing-data-ui' +import expectedListingConfig from './expected-listing-data-config' +import expectedListingMix from './expected-listing-data-mix' + +export { + testListingUI, + testListingConfig, + testListingMix, + expectedListingUI, + expectedListingConfig, + expectedListingMix +} diff --git a/packages/ripple-tide-search/mapping/__test__/data/test-listing-data-config.ts b/packages/ripple-tide-search/mapping/__test__/data/test-listing-data-config.ts new file mode 100644 index 0000000000..2e39499d5f --- /dev/null +++ b/packages/ripple-tide-search/mapping/__test__/data/test-listing-data-config.ts @@ -0,0 +1,81 @@ +export default { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/node/tide_search_listing/9e58c8f5-4ab7-428f-a60a-675c2cf94779?resourceVersion=id%3A386' + } + }, + meta: {}, + drupal_internal__nid: 289, + drupal_internal__vid: 386, + langcode: 'en', + revision_timestamp: '2024-03-17T01:52:21+00:00', + revision_log: null, + status: true, + title: 'TEST: SLC', + created: '2024-03-17T01:15:25+00:00', + changed: '2024-03-17T01:52:21+00:00', + promote: true, + sticky: false, + default_langcode: true, + revision_translation_affected: true, + moderation_state: 'published', + metatag: [ + { + tag: 'meta', + attributes: { + name: 'title', + content: 'TEST: SLC | Single Digital Presence Content Management System' + } + }, + { + tag: 'link', + attributes: { + rel: 'canonical', + href: 'https://develop.content.reference.sdp.vic.gov.au/test-slc' + } + }, + { + tag: 'meta', + attributes: { property: 'og:locale', content: 'en-AU' } + } + ], + path: { alias: '/site-8888/test-slc', pid: 1998, langcode: 'en' }, + field_above_results_content: null, + field_below_results_content: null, + field_custom_sort_configuration: null, + field_landing_page_intro_text: null, + field_landing_page_summary: null, + field_listing_query_config: null, + field_listing_results_config: null, + field_listing_results_per_page: null, + field_metatags: null, + field_search_configuration: + '{\r\n "searchListingConfig": {\r\n "resultsPerPage": 20,\r\n "formTheme": "default",\r\n "labels": {\r\n "submit": "Submit search",\r\n "placeholder": "Start typing search term..."\r\n },\r\n "customSort": [\r\n {\r\n "title.keyword": "asc"\r\n }\r\n ]\r\n },\r\n "queryConfig": {\r\n "multi_match": {\r\n "query": "{{query}}",\r\n "fields": [\r\n "title^3",\r\n "field_landing_page_summary^2",\r\n "body",\r\n "field_paragraph_body",\r\n "summary_processed"\r\n ]\r\n }\r\n },\r\n "globalFilters": [\r\n {\r\n "terms": {\r\n "type": [\r\n "news"\r\n ]\r\n }\r\n },\r\n {\r\n "terms": {\r\n "field_node_site": [\r\n 8888\r\n ]\r\n }\r\n }\r\n ],\r\n "userFilters": [\r\n {\r\n "id": "topic",\r\n "component": "TideSearchFilterDropdown",\r\n "filter": {\r\n "type": "terms",\r\n "value": "field_topic_name"\r\n },\r\n "aggregations": {\r\n "field": "topic",\r\n "source": "taxonomy"\r\n },\r\n "props": {\r\n "id": "topic",\r\n "label": "Topics",\r\n "placeholder": "Select a topic",\r\n "type": "RplFormDropdown",\r\n "multiple": true\r\n }\r\n },\r\n {\r\n "id": "tags",\r\n "component": "TideSearchFilterCheckbox",\r\n "filter": {\r\n "type": "terms",\r\n "value": "field_tags_name",\r\n "multiple": false\r\n },\r\n "props": {\r\n "id": "tags",\r\n "label": "Tags",\r\n "checkboxLabel": "Only show demo tag",\r\n "onValue": "Demo Tag"\r\n }\r\n }\r\n ],\r\n "results": {\r\n "layout": {\r\n "component": "TideSearchResultsTable",\r\n "props": {\r\n "showExtraContent": true,\r\n "columns": [\r\n {\r\n "label": "Title",\r\n "objectKey": "title",\r\n "cols": 4\r\n },\r\n {\r\n "label": "Tags",\r\n "objectKey": "field_tags_name"\r\n }\r\n ],\r\n "extraContent": {\r\n "items": [\r\n {\r\n "label": "Topics",\r\n "objectKey": "field_topic_name"\r\n },\r\n {\r\n "label": "Date",\r\n "objectKey": "field_news_date"\r\n }\r\n ]\r\n }\r\n }\r\n }\r\n }\r\n}\r\n', + field_search_input_placeholder: null, + field_search_submit_label: null, + field_show_content_rating: true, + node_type: { + type: 'node_type--node_type', + id: '717687af-b1eb-49cb-8a2e-2b23a19551b6', + meta: { drupal_internal__target_id: 'tide_search_listing' } + }, + field_header_components: [], + field_listing_global_filters: [], + field_listing_user_filters: [], + field_node_primary_site: { + type: 'taxonomy_term--sites', + id: '11dede11-10c0-111e1-1100-000000000040', + meta: { drupal_internal__target_id: 8888 } + }, + field_node_site: [ + { + type: 'taxonomy_term--sites', + id: '11dede11-10c0-111e1-1100-000000000040', + meta: { drupal_internal__target_id: 8888 } + } + ], + field_tags: [], + id: '9e58c8f5-4ab7-428f-a60a-675c2cf94779', + type: 'node--tide_search_listing', + _sectionId: '8888' +} diff --git a/packages/ripple-tide-search/mapping/__test__/data/test-listing-data-mix.ts b/packages/ripple-tide-search/mapping/__test__/data/test-listing-data-mix.ts new file mode 100644 index 0000000000..fcf81337c2 --- /dev/null +++ b/packages/ripple-tide-search/mapping/__test__/data/test-listing-data-mix.ts @@ -0,0 +1,406 @@ +export default { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/node/tide_search_listing/d86052a5-2ffd-441b-be5f-dfa998c28839?resourceVersion=id%3A388' + } + }, + meta: {}, + drupal_internal__nid: 290, + drupal_internal__vid: 388, + langcode: 'en', + revision_timestamp: '2024-03-17T22:00:56+00:00', + revision_log: null, + status: true, + title: 'TEST: SLM', + created: '2024-03-17T21:50:54+00:00', + changed: '2024-03-17T22:00:56+00:00', + promote: true, + sticky: false, + default_langcode: true, + revision_translation_affected: true, + moderation_state: 'published', + metatag: [ + { + tag: 'meta', + attributes: { + name: 'title', + content: 'TEST: SLM | Single Digital Presence Content Management System' + } + }, + { + tag: 'link', + attributes: { + rel: 'canonical', + href: 'https://develop.content.reference.sdp.vic.gov.au/test-slm' + } + }, + { tag: 'meta', attributes: { property: 'og:locale', content: 'en-AU' } } + ], + path: { alias: '/site-8888/test-slm', pid: 2000, langcode: 'en' }, + field_above_results_content: null, + field_below_results_content: null, + field_custom_sort_configuration: '[{ "title.keyword": "asc" }]', + field_landing_page_intro_text: null, + field_landing_page_summary: null, + field_listing_query_config: + '{\r\n "multi_match": {\r\n "query": "{{query}}",\r\n "fields": [\r\n "title",\r\n "body"\r\n ]\r\n }\r\n }', + field_listing_results_config: null, + field_listing_results_per_page: 15, + field_metatags: null, + field_search_configuration: + '{\r\n "searchListingConfig": {\r\n "resultsPerPage": 12,\r\n "formTheme": "default",\r\n "labels": {\r\n "submit": "Submit search",\r\n "placeholder": "Start typing search term..."\r\n },\r\n "customSort": [\r\n { "created": "desc" }\r\n ]\r\n },\r\n "queryConfig": {\r\n "multi_match": {\r\n "query": "{{query}}",\r\n "fields": [\r\n "field_landing_page_summary^2",\r\n "field_paragraph_body",\r\n "summary_processed"\r\n ]\r\n }\r\n },\r\n "globalFilters": [\r\n {\r\n "terms": {\r\n "status": [\r\n true\r\n ]\r\n }\r\n }\r\n ],\r\n "userFilters": [\r\n {\r\n "id": "department",\r\n "component": "TideSearchFilterDropdown",\r\n "filter": {\r\n "type": "terms",\r\n "value": "field_department_name"\r\n },\r\n "aggregations": {\r\n "field": "department",\r\n "source": "taxonomy"\r\n },\r\n "props": {\r\n "id": "department",\r\n "label": "Departments",\r\n "placeholder": "Select a department",\r\n "type": "RplFormDropdown",\r\n "multiple": true\r\n }\r\n }\r\n ],\r\n "results": {\r\n "layout": {\r\n "component": "TideSearchResultsList",\r\n "props": {\r\n "customProp": true\r\n }\r\n }\r\n },\r\n "layoutConfig": {\r\n "belowFilter": { "component": "TideCustomBelowFilterComponent" }\r\n }\r\n}\r\n', + field_search_input_placeholder: 'Search me....', + field_search_submit_label: 'Submit', + field_show_content_rating: true, + node_type: { + type: 'node_type--node_type', + id: '717687af-b1eb-49cb-8a2e-2b23a19551b6', + meta: { drupal_internal__target_id: 'tide_search_listing' } + }, + field_header_components: [], + field_layout_component: { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/taxonomy_term/listing_layout_comp_taxonomy/4ce52903-da74-4f18-88b9-8e9b396e3bf9?resourceVersion=id%3A159' + } + }, + meta: { drupal_internal__target_id: 9020 }, + drupal_internal__tid: 9020, + drupal_internal__revision_id: 159, + langcode: 'en', + revision_created: '2023-09-04T06:17:35+00:00', + revision_log_message: null, + status: true, + name: 'TideSearchResultsList', + description: null, + weight: 0, + changed: '2023-09-04T06:17:35+00:00', + default_langcode: true, + revision_translation_affected: true, + metatag: [ + { + tag: 'meta', + attributes: { + name: 'title', + content: + 'TideSearchResultsList | Single Digital Presence Content Management System' + } + }, + { + tag: 'link', + attributes: { + rel: 'canonical', + href: 'https://develop.content.reference.sdp.vic.gov.au/listinglayoutcomptaxonomy/tidesearchresultslist' + } + }, + { tag: 'meta', attributes: { property: 'og:locale', content: 'en-AU' } } + ], + path: { + alias: '/listinglayoutcomptaxonomy/tidesearchresultslist', + pid: 827, + langcode: 'en' + }, + vid: { + type: 'taxonomy_vocabulary--taxonomy_vocabulary', + id: 'd0bee75b-f7f2-4405-9935-030d5e172548', + meta: { drupal_internal__target_id: 'listing_layout_comp_taxonomy' } + }, + parent: [ + { + type: 'taxonomy_term--listing_layout_comp_taxonomy', + id: 'virtual', + meta: { + links: { + help: { + href: 'https://www.drupal.org/docs/8/modules/json-api/core-concepts#virtual', + meta: { + about: "Usage and meaning of the 'virtual' resource identifier." + } + } + } + } + } + ], + id: '4ce52903-da74-4f18-88b9-8e9b396e3bf9', + type: 'taxonomy_term--listing_layout_comp_taxonomy' + }, + field_listing_global_filters: [ + { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/paragraph/listing_site/e1251779-062f-4ef7-8bda-ed43781f5b49?resourceVersion=id%3A2325' + } + }, + meta: { target_revision_id: 2325, drupal_internal__target_id: 1917 }, + drupal_internal__id: 1917, + drupal_internal__revision_id: 2325, + langcode: 'en', + status: true, + created: '2024-03-17T21:39:40+00:00', + parent_id: '290', + parent_type: 'node', + parent_field_name: 'field_listing_global_filters', + behavior_settings: [], + default_langcode: true, + revision_translation_affected: true, + field_listing_site_site: [ + { + type: 'taxonomy_term--sites', + id: '11dede11-10c0-111e1-1100-000000000040', + meta: { drupal_internal__target_id: 8888 } + } + ], + id: 'e1251779-062f-4ef7-8bda-ed43781f5b49', + type: 'paragraph--listing_site' + }, + { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/paragraph/listing_content_type/48d4721f-d75e-44db-b3ec-361a8869807a?resourceVersion=id%3A2326' + } + }, + meta: { target_revision_id: 2326, drupal_internal__target_id: 1918 }, + drupal_internal__id: 1918, + drupal_internal__revision_id: 2326, + langcode: 'en', + status: true, + created: '2024-03-17T21:39:49+00:00', + parent_id: '290', + parent_type: 'node', + parent_field_name: 'field_listing_global_filters', + behavior_settings: [], + default_langcode: true, + revision_translation_affected: true, + field_listing_global_contenttype: [ + { + type: 'node_type--node_type', + id: '50c73204-2077-4d2f-82b5-a62b3c6a3f50', + meta: { drupal_internal__target_id: 'news' } + } + ], + id: '48d4721f-d75e-44db-b3ec-361a8869807a', + type: 'paragraph--listing_content_type' + }, + { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/paragraph/listing_custom_filter/ca8fce1c-5e9a-486d-930d-1272d40b2d71?resourceVersion=id%3A2327' + } + }, + meta: { target_revision_id: 2327, drupal_internal__target_id: 1919 }, + drupal_internal__id: 1919, + drupal_internal__revision_id: 2327, + langcode: 'en', + status: true, + created: '2024-03-17T21:39:58+00:00', + parent_id: '290', + parent_type: 'node', + parent_field_name: 'field_listing_global_filters', + behavior_settings: [], + default_langcode: true, + revision_translation_affected: true, + field_listing_custom_filter_conf: + '{ "terms": { "field_topic_name": ["Demo Topic"] } }', + id: 'ca8fce1c-5e9a-486d-930d-1272d40b2d71', + type: 'paragraph--listing_custom_filter' + } + ], + field_listing_user_filters: [ + { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/paragraph/searchable_fields/2fa3c2d2-b62c-4e5e-b4ce-6f1fbe81eae3?resourceVersion=id%3A2328' + } + }, + meta: { target_revision_id: 2328, drupal_internal__target_id: 1920 }, + drupal_internal__id: 1920, + drupal_internal__revision_id: 2328, + langcode: 'en', + status: true, + created: '2024-03-17T21:40:33+00:00', + parent_id: '290', + parent_type: 'node', + parent_field_name: 'field_listing_user_filters', + behavior_settings: [], + default_langcode: true, + revision_translation_affected: true, + field_input_label: 'Topic', + field_placeholder: 'Select a topic', + field_field: { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/taxonomy_term/searchable_fields/c63158a6-8c1b-4195-843c-02e7d95d209c?resourceVersion=id%3A168' + } + }, + meta: { drupal_internal__target_id: 9029 }, + drupal_internal__tid: 9029, + drupal_internal__revision_id: 168, + langcode: 'en', + revision_created: '2023-09-04T06:17:35+00:00', + revision_log_message: null, + status: true, + name: 'Topic', + description: null, + weight: 0, + changed: '2023-09-04T06:17:35+00:00', + default_langcode: true, + revision_translation_affected: true, + metatag: [ + { + tag: 'meta', + attributes: { + name: 'title', + content: + 'Topic | Single Digital Presence Content Management System' + } + }, + { + tag: 'link', + attributes: { + rel: 'canonical', + href: 'https://develop.content.reference.sdp.vic.gov.au/searchablefields/topic' + } + }, + { + tag: 'meta', + attributes: { property: 'og:locale', content: 'en-AU' } + } + ], + path: { alias: '/searchablefields/topic', pid: 836, langcode: 'en' }, + field_elasticsearch_field: 'field_topic_name', + field_taxonomy_machine_name: 'topic', + vid: { + type: 'taxonomy_vocabulary--taxonomy_vocabulary', + id: 'aaa39ae4-5009-4045-a830-70272b2d2ffa', + meta: { drupal_internal__target_id: 'searchable_fields' } + }, + parent: [ + { + type: 'taxonomy_term--searchable_fields', + id: 'virtual', + meta: { + links: { + help: { + href: 'https://www.drupal.org/docs/8/modules/json-api/core-concepts#virtual', + meta: { + about: + "Usage and meaning of the 'virtual' resource identifier." + } + } + } + } + } + ], + id: 'c63158a6-8c1b-4195-843c-02e7d95d209c', + type: 'taxonomy_term--searchable_fields' + }, + id: '2fa3c2d2-b62c-4e5e-b4ce-6f1fbe81eae3', + type: 'paragraph--searchable_fields' + }, + { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/paragraph/listing_user_custom_filter/44ad0499-824a-4484-9ff4-ba8314b5a412?resourceVersion=id%3A2329' + } + }, + meta: { target_revision_id: 2329, drupal_internal__target_id: 1921 }, + drupal_internal__id: 1921, + drupal_internal__revision_id: 2329, + langcode: 'en', + status: true, + created: '2024-03-17T21:40:53+00:00', + parent_id: '290', + parent_type: 'node', + parent_field_name: 'field_listing_user_filters', + behavior_settings: [], + default_langcode: true, + revision_translation_affected: true, + field_user_filter_configuration: + '{\r\n "id": "tags",\r\n "component": "TideSearchFilterCheckbox",\r\n "filter": {\r\n "type": "terms",\r\n "value": "field_tags_name",\r\n "multiple": false\r\n },\r\n "props": {\r\n "id": "tags",\r\n "label": "Tags",\r\n "checkboxLabel": "Only show demo tag",\r\n "onValue": "Demo Tag"\r\n }\r\n }', + id: '44ad0499-824a-4484-9ff4-ba8314b5a412', + type: 'paragraph--listing_user_custom_filter' + } + ], + field_node_primary_site: { + type: 'taxonomy_term--sites', + id: '11dede11-10c0-111e1-1100-000000000040', + meta: { drupal_internal__target_id: 8888 } + }, + field_node_site: [ + { + type: 'taxonomy_term--sites', + id: '11dede11-10c0-111e1-1100-000000000040', + meta: { drupal_internal__target_id: 8888 } + } + ], + field_results_component: { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/taxonomy_term/listing_results_comp_taxonomy/998de0a1-c300-4c75-838e-805cba6a18a3?resourceVersion=id%3A161' + } + }, + meta: { drupal_internal__target_id: 9022 }, + drupal_internal__tid: 9022, + drupal_internal__revision_id: 161, + langcode: 'en', + revision_created: '2023-09-04T06:17:35+00:00', + revision_log_message: null, + status: true, + name: 'TideSearchResult', + description: null, + weight: 0, + changed: '2023-09-04T06:17:35+00:00', + default_langcode: true, + revision_translation_affected: true, + metatag: [ + { + tag: 'meta', + attributes: { + name: 'title', + content: + 'TideSearchResult | Single Digital Presence Content Management System' + } + }, + { + tag: 'link', + attributes: { + rel: 'canonical', + href: 'https://develop.content.reference.sdp.vic.gov.au/listingresultscomptaxonomy/tidesearchresult' + } + }, + { tag: 'meta', attributes: { property: 'og:locale', content: 'en-AU' } } + ], + path: { + alias: '/listingresultscomptaxonomy/tidesearchresult', + pid: 829, + langcode: 'en' + }, + vid: { + type: 'taxonomy_vocabulary--taxonomy_vocabulary', + id: 'c1ba4250-7a1a-4660-9ecc-854e09be6723', + meta: { drupal_internal__target_id: 'listing_results_comp_taxonomy' } + }, + parent: [ + { + type: 'taxonomy_term--listing_results_comp_taxonomy', + id: 'virtual', + meta: { + links: { + help: { + href: 'https://www.drupal.org/docs/8/modules/json-api/core-concepts#virtual', + meta: { + about: "Usage and meaning of the 'virtual' resource identifier." + } + } + } + } + } + ], + id: '998de0a1-c300-4c75-838e-805cba6a18a3', + type: 'taxonomy_term--listing_results_comp_taxonomy' + }, + field_tags: [], + id: 'd86052a5-2ffd-441b-be5f-dfa998c28839', + type: 'node--tide_search_listing', + _sectionId: '8888' +} diff --git a/packages/ripple-tide-search/mapping/__test__/data/test-listing-data-ui.ts b/packages/ripple-tide-search/mapping/__test__/data/test-listing-data-ui.ts new file mode 100644 index 0000000000..ed71f9e693 --- /dev/null +++ b/packages/ripple-tide-search/mapping/__test__/data/test-listing-data-ui.ts @@ -0,0 +1,433 @@ +export default { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/node/tide_search_listing/7b600187-a46a-4e32-becb-978bfe047ba0?resourceVersion=id%3A383' + } + }, + meta: {}, + drupal_internal__nid: 289, + drupal_internal__vid: 383, + langcode: 'en', + revision_timestamp: '2024-03-17T21:41:51+00:00', + revision_log: null, + status: true, + title: 'TEST: SLU', + created: '2024-03-17T21:41:51+00:00', + changed: '2024-03-17T21:41:51+00:00', + promote: true, + sticky: false, + default_langcode: true, + revision_translation_affected: true, + moderation_state: 'published', + metatag: [ + { + tag: 'meta', + attributes: { + name: 'title', + content: 'TEST: SLU | Single Digital Presence Content Management System' + } + }, + { + tag: 'link', + attributes: { + rel: 'canonical', + href: 'https://develop.content.reference.sdp.vic.gov.au/test-slu' + } + }, + { + tag: 'meta', + attributes: { property: 'og:locale', content: 'en-AU' } + } + ], + path: { alias: '/site-8888/test-slu', pid: 1998, langcode: 'en' }, + field_above_results_content: null, + field_below_results_content: null, + field_custom_sort_configuration: '[{ "title.keyword": "asc" }]', + field_landing_page_intro_text: null, + field_landing_page_summary: null, + field_listing_query_config: + '{\r\n "multi_match": {\r\n "query": "{{query}}",\r\n "fields": [\r\n "title",\r\n "body"\r\n ]\r\n }\r\n }', + field_listing_results_config: null, + field_listing_results_per_page: 15, + field_metatags: null, + field_search_configuration: null, + field_search_input_placeholder: 'Search me....', + field_search_submit_label: 'Submit', + field_show_content_rating: true, + node_type: { + type: 'node_type--node_type', + id: '717687af-b1eb-49cb-8a2e-2b23a19551b6', + meta: { drupal_internal__target_id: 'tide_search_listing' } + }, + field_header_components: [], + field_layout_component: { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/taxonomy_term/listing_layout_comp_taxonomy/4ce52903-da74-4f18-88b9-8e9b396e3bf9?resourceVersion=id%3A159' + } + }, + meta: { drupal_internal__target_id: 9020 }, + drupal_internal__tid: 9020, + drupal_internal__revision_id: 159, + langcode: 'en', + revision_created: '2023-09-04T06:17:35+00:00', + revision_log_message: null, + status: true, + name: 'TideSearchResultsList', + description: null, + weight: 0, + changed: '2023-09-04T06:17:35+00:00', + default_langcode: true, + revision_translation_affected: true, + metatag: [ + { + tag: 'meta', + attributes: { + name: 'title', + content: + 'TideSearchResultsList | Single Digital Presence Content Management System' + } + }, + { + tag: 'link', + attributes: { + rel: 'canonical', + href: 'https://develop.content.reference.sdp.vic.gov.au/listinglayoutcomptaxonomy/tidesearchresultslist' + } + }, + { + tag: 'meta', + attributes: { property: 'og:locale', content: 'en-AU' } + } + ], + path: { + alias: '/listinglayoutcomptaxonomy/tidesearchresultslist', + pid: 827, + langcode: 'en' + }, + vid: { + type: 'taxonomy_vocabulary--taxonomy_vocabulary', + id: 'd0bee75b-f7f2-4405-9935-030d5e172548', + meta: { drupal_internal__target_id: 'listing_layout_comp_taxonomy' } + }, + parent: [ + { + type: 'taxonomy_term--listing_layout_comp_taxonomy', + id: 'virtual', + meta: { + links: { + help: { + href: 'https://www.drupal.org/docs/8/modules/json-api/core-concepts#virtual', + meta: { + about: "Usage and meaning of the 'virtual' resource identifier." + } + } + } + } + } + ], + id: '4ce52903-da74-4f18-88b9-8e9b396e3bf9', + type: 'taxonomy_term--listing_layout_comp_taxonomy' + }, + field_listing_global_filters: [ + { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/paragraph/listing_site/ae7f7379-de79-4109-957c-f589277dac5b?resourceVersion=id%3A2305' + } + }, + meta: { + target_revision_id: 2305, + drupal_internal__target_id: 1912 + }, + drupal_internal__id: 1912, + drupal_internal__revision_id: 2305, + langcode: 'en', + status: true, + created: '2024-03-17T21:39:40+00:00', + parent_id: '289', + parent_type: 'node', + parent_field_name: 'field_listing_global_filters', + behavior_settings: [], + default_langcode: true, + revision_translation_affected: true, + field_listing_site_site: [ + { + type: 'taxonomy_term--sites', + id: '11dede11-10c0-111e1-1100-000000000040', + meta: { drupal_internal__target_id: 8888 } + } + ], + id: 'ae7f7379-de79-4109-957c-f589277dac5b', + type: 'paragraph--listing_site' + }, + { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/paragraph/listing_content_type/5bcce0cb-1985-4918-a4a2-b6b4aea063f2?resourceVersion=id%3A2306' + } + }, + meta: { + target_revision_id: 2306, + drupal_internal__target_id: 1913 + }, + drupal_internal__id: 1913, + drupal_internal__revision_id: 2306, + langcode: 'en', + status: true, + created: '2024-03-17T21:39:49+00:00', + parent_id: '289', + parent_type: 'node', + parent_field_name: 'field_listing_global_filters', + behavior_settings: [], + default_langcode: true, + revision_translation_affected: true, + field_listing_global_contenttype: [ + { + type: 'node_type--node_type', + id: '50c73204-2077-4d2f-82b5-a62b3c6a3f50', + meta: { drupal_internal__target_id: 'news' } + } + ], + id: '5bcce0cb-1985-4918-a4a2-b6b4aea063f2', + type: 'paragraph--listing_content_type' + }, + { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/paragraph/listing_custom_filter/89f2ff98-657a-40f9-988a-54e9f7cdd72a?resourceVersion=id%3A2307' + } + }, + meta: { + target_revision_id: 2307, + drupal_internal__target_id: 1914 + }, + drupal_internal__id: 1914, + drupal_internal__revision_id: 2307, + langcode: 'en', + status: true, + created: '2024-03-17T21:39:58+00:00', + parent_id: '289', + parent_type: 'node', + parent_field_name: 'field_listing_global_filters', + behavior_settings: [], + default_langcode: true, + revision_translation_affected: true, + field_listing_custom_filter_conf: + '{ "terms": { "field_topic_name": ["Demo Topic"] } }', + id: '89f2ff98-657a-40f9-988a-54e9f7cdd72a', + type: 'paragraph--listing_custom_filter' + } + ], + field_listing_user_filters: [ + { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/paragraph/searchable_fields/9aaedfc7-2b83-4a74-be7a-6d1a66a02577?resourceVersion=id%3A2308' + } + }, + meta: { + target_revision_id: 2308, + drupal_internal__target_id: 1915 + }, + drupal_internal__id: 1915, + drupal_internal__revision_id: 2308, + langcode: 'en', + status: true, + created: '2024-03-17T21:40:33+00:00', + parent_id: '289', + parent_type: 'node', + parent_field_name: 'field_listing_user_filters', + behavior_settings: [], + default_langcode: true, + revision_translation_affected: true, + field_input_label: 'Topic', + field_placeholder: 'Select a topic', + field_field: { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/taxonomy_term/searchable_fields/c63158a6-8c1b-4195-843c-02e7d95d209c?resourceVersion=id%3A168' + } + }, + meta: { drupal_internal__target_id: 9029 }, + drupal_internal__tid: 9029, + drupal_internal__revision_id: 168, + langcode: 'en', + revision_created: '2023-09-04T06:17:35+00:00', + revision_log_message: null, + status: true, + name: 'Topic', + description: null, + weight: 0, + changed: '2023-09-04T06:17:35+00:00', + default_langcode: true, + revision_translation_affected: true, + metatag: [ + { + tag: 'meta', + attributes: { + name: 'title', + content: + 'Topic | Single Digital Presence Content Management System' + } + }, + { + tag: 'link', + attributes: { + rel: 'canonical', + href: 'https://develop.content.reference.sdp.vic.gov.au/searchablefields/topic' + } + }, + { + tag: 'meta', + attributes: { property: 'og:locale', content: 'en-AU' } + } + ], + path: { + alias: '/searchablefields/topic', + pid: 836, + langcode: 'en' + }, + field_elasticsearch_field: 'field_topic_name', + field_taxonomy_machine_name: 'topic', + vid: { + type: 'taxonomy_vocabulary--taxonomy_vocabulary', + id: 'aaa39ae4-5009-4045-a830-70272b2d2ffa', + meta: { drupal_internal__target_id: 'searchable_fields' } + }, + parent: [ + { + type: 'taxonomy_term--searchable_fields', + id: 'virtual', + meta: { + links: { + help: { + href: 'https://www.drupal.org/docs/8/modules/json-api/core-concepts#virtual', + meta: { + about: + "Usage and meaning of the 'virtual' resource identifier." + } + } + } + } + } + ], + id: 'c63158a6-8c1b-4195-843c-02e7d95d209c', + type: 'taxonomy_term--searchable_fields' + }, + id: '9aaedfc7-2b83-4a74-be7a-6d1a66a02577', + type: 'paragraph--searchable_fields' + }, + { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/paragraph/listing_user_custom_filter/2e35bb71-3fbe-41d5-b705-d9809ec6bd45?resourceVersion=id%3A2309' + } + }, + meta: { + target_revision_id: 2309, + drupal_internal__target_id: 1916 + }, + drupal_internal__id: 1916, + drupal_internal__revision_id: 2309, + langcode: 'en', + status: true, + created: '2024-03-17T21:40:53+00:00', + parent_id: '289', + parent_type: 'node', + parent_field_name: 'field_listing_user_filters', + behavior_settings: [], + default_langcode: true, + revision_translation_affected: true, + field_user_filter_configuration: + '{\r\n "id": "tags",\r\n "component": "TideSearchFilterCheckbox",\r\n "filter": {\r\n "type": "terms",\r\n "value": "field_tags_name",\r\n "multiple": false\r\n },\r\n "props": {\r\n "id": "tags",\r\n "label": "Tags",\r\n "checkboxLabel": "Only show demo tag",\r\n "onValue": "Demo Tag"\r\n }\r\n }', + id: '2e35bb71-3fbe-41d5-b705-d9809ec6bd45', + type: 'paragraph--listing_user_custom_filter' + } + ], + field_node_primary_site: { + type: 'taxonomy_term--sites', + id: '11dede11-10c0-111e1-1100-000000000040', + meta: { drupal_internal__target_id: 8888 } + }, + field_node_site: [ + { + type: 'taxonomy_term--sites', + id: '11dede11-10c0-111e1-1100-000000000040', + meta: { drupal_internal__target_id: 8888 } + } + ], + field_results_component: { + links: { + self: { + href: 'https://develop.content.reference.sdp.vic.gov.au/api/v1/taxonomy_term/listing_results_comp_taxonomy/998de0a1-c300-4c75-838e-805cba6a18a3?resourceVersion=id%3A161' + } + }, + meta: { drupal_internal__target_id: 9022 }, + drupal_internal__tid: 9022, + drupal_internal__revision_id: 161, + langcode: 'en', + revision_created: '2023-09-04T06:17:35+00:00', + revision_log_message: null, + status: true, + name: 'TideSearchResult', + description: null, + weight: 0, + changed: '2023-09-04T06:17:35+00:00', + default_langcode: true, + revision_translation_affected: true, + metatag: [ + { + tag: 'meta', + attributes: { + name: 'title', + content: + 'TideSearchResult | Single Digital Presence Content Management System' + } + }, + { + tag: 'link', + attributes: { + rel: 'canonical', + href: 'https://develop.content.reference.sdp.vic.gov.au/listingresultscomptaxonomy/tidesearchresult' + } + }, + { + tag: 'meta', + attributes: { property: 'og:locale', content: 'en-AU' } + } + ], + path: { + alias: '/listingresultscomptaxonomy/tidesearchresult', + pid: 829, + langcode: 'en' + }, + vid: { + type: 'taxonomy_vocabulary--taxonomy_vocabulary', + id: 'c1ba4250-7a1a-4660-9ecc-854e09be6723', + meta: { drupal_internal__target_id: 'listing_results_comp_taxonomy' } + }, + parent: [ + { + type: 'taxonomy_term--listing_results_comp_taxonomy', + id: 'virtual', + meta: { + links: { + help: { + href: 'https://www.drupal.org/docs/8/modules/json-api/core-concepts#virtual', + meta: { + about: "Usage and meaning of the 'virtual' resource identifier." + } + } + } + } + } + ], + id: '998de0a1-c300-4c75-838e-805cba6a18a3', + type: 'taxonomy_term--listing_results_comp_taxonomy' + }, + field_tags: [], + id: '7b600187-a46a-4e32-becb-978bfe047ba0', + type: 'node--tide_search_listing', + _sectionId: '8888' +} diff --git a/packages/ripple-tide-search/mapping/__test__/getProcessedSearchListingConfig.test.ts b/packages/ripple-tide-search/mapping/__test__/getProcessedSearchListingConfig.test.ts new file mode 100644 index 0000000000..24dd43c1cc --- /dev/null +++ b/packages/ripple-tide-search/mapping/__test__/getProcessedSearchListingConfig.test.ts @@ -0,0 +1,54 @@ +import { expect, describe, it } from '@jest/globals' +import { getProcessedSearchListingConfig } from '../utils' +import { + testListingUI, + testListingConfig, + testListingMix, + expectedListingUI, + expectedListingConfig, + expectedListingMix +} from './data' + +const tidePageApi = new (class mockTidePageApi { + async getTaxonomy(): Promise { + return new Promise((resolve) => + resolve([ + { + drupal_internal__tid: 1, + name: 'Option 1', + weight: 0, + status: true + } + ]) + ) + } +})() + +describe('getProcessedSearchListingConfig', () => { + it('it maps the Drupal UI fields into the correct structure', async () => { + const output = await getProcessedSearchListingConfig( + testListingUI, + tidePageApi + ) + + expect(output).toEqual(expectedListingUI) + }) + + it('it handles using a supplied config directly, taking into account Drupal defaults', async () => { + const output = await getProcessedSearchListingConfig( + testListingConfig, + tidePageApi + ) + + expect(output).toEqual(expectedListingConfig) + }) + + it('it merges the data from Drupal UI fields and the JSON blob, with the blob taking precedence', async () => { + const output = await getProcessedSearchListingConfig( + testListingMix, + tidePageApi + ) + + expect(output).toEqual(expectedListingMix) + }) +}) diff --git a/packages/ripple-tide-search/mapping/index.ts b/packages/ripple-tide-search/mapping/index.ts index 03dc47edb5..c068f8b650 100644 --- a/packages/ripple-tide-search/mapping/index.ts +++ b/packages/ripple-tide-search/mapping/index.ts @@ -4,141 +4,11 @@ import { } from '@dpc-sdp/nuxt-ripple/mapping' import { getBodyFromField } from '@dpc-sdp/ripple-tide-api' import type { IRplTideModuleMapping } from '@dpc-sdp/ripple-tide-api/types' -import { ApplicationError } from '@dpc-sdp/ripple-tide-api/errors' -import { processConfig, parseJSONField } from './../mapping/utils' import { secondaryCampaignIncludes, secondaryCampaignMapping } from '@dpc-sdp/ripple-tide-landing-page/mapping' - -const getProcessedSearchListingConfig = async (src, tidePageApi) => { - let rawConfig = null - rawConfig = getMaybeRawConfig(src) - - if (!rawConfig) { - rawConfig = buildConfigFromDrupalFields(src) - } - - return await processConfig(rawConfig, tidePageApi) -} - -const parseGlobalFiltersFromDrupal = (rawFilters) => { - return rawFilters.map((rawFilter) => { - switch (rawFilter.type) { - case 'paragraph--listing_content_type': - return { - terms: { - type: rawFilter.field_listing_global_contenttype.map( - (contentType) => contentType.meta.drupal_internal__target_id - ) - } - } - case 'paragraph--listing_site': - return { - terms: { - field_node_site: rawFilter.field_listing_site_site.map( - (site) => site.meta.drupal_internal__target_id - ) - } - } - case 'paragraph--listing_custom_filter': - return parseJSONField(rawFilter.field_listing_custom_filter_conf) - default: - return null - } - }) -} - -const parseUserFiltersFromDrupal = (rawFilters) => { - const test = rawFilters.map((rawFilter) => { - switch (rawFilter.type) { - case 'paragraph--searchable_fields': { - const taxonomyName = rawFilter.field_field.field_taxonomy_machine_name - - return { - id: taxonomyName, - component: 'TideSearchFilterDropdown', - aggregations: { - field: taxonomyName, - source: 'taxonomy' - }, - filter: { - type: 'terms', - value: rawFilter.field_field.field_elasticsearch_field - }, - props: { - id: taxonomyName, - label: rawFilter.field_input_label, - placeholder: rawFilter.field_placeholder, - type: 'RplFormDropdown', - multiple: true - } - } - } - case 'paragraph--listing_user_custom_filter': - return parseJSONField(rawFilter.field_user_filter_configuration) - default: - return null - } - }) - - return test -} - -const parseResultsConfigFromDrupal = (src) => { - if (src.field_listing_results_config) { - return parseJSONField(src.field_listing_results_config) - } - - return { - layout: { - component: src.field_layout_component.name - }, - item: { - '*': { - component: src.field_results_component.name - } - } - } -} - -const buildConfigFromDrupalFields = (src) => { - return { - searchListingConfig: { - hideSearchForm: src?.field_listing_hide_search_form ?? false, - resultsPerPage: src.field_listing_results_per_page || 10, - labels: { - submit: src.field_search_submit_label, - placeholder: src.field_search_input_placeholder - }, - customSort: src.field_custom_sort_configuration - ? parseJSONField(src.field_custom_sort_configuration) - : undefined - }, - queryConfig: parseJSONField(src.field_listing_query_config), - globalFilters: parseGlobalFiltersFromDrupal( - src.field_listing_global_filters - ), - userFilters: parseUserFiltersFromDrupal(src.field_listing_user_filters), - results: parseResultsConfigFromDrupal(src) - } -} - -const getMaybeRawConfig = (src) => { - let parsedConfig = null - - if (src.field_search_configuration) { - try { - parsedConfig = JSON.parse(src.field_search_configuration) - } catch (e) { - throw new ApplicationError( - "Couldn't parse raw search listing config JSON field ('field_search_configuration')" - ) - } - } - - return parsedConfig -} +import { getProcessedSearchListingConfig } from './utils' const tideCollectionModule: IRplTideModuleMapping = { mapping: { diff --git a/packages/ripple-tide-search/mapping/utils/index.ts b/packages/ripple-tide-search/mapping/utils/index.ts index d1bfdd5908..5c5e0b3aca 100644 --- a/packages/ripple-tide-search/mapping/utils/index.ts +++ b/packages/ripple-tide-search/mapping/utils/index.ts @@ -1,3 +1,6 @@ +import { ApplicationError } from '@dpc-sdp/ripple-tide-api/errors' +import { defu as defuMerge } from 'defu' + export const getSearchListingConfig = (src) => src.hasOwnProperty('field_search_configuration') && JSON.parse(src.field_search_configuration) @@ -50,3 +53,138 @@ export const processConfig = async (config, tidePageApi) => { userFilters: filters } } + +export const getProcessedSearchListingConfig = async (src, tidePageApi) => { + const rawConfig = getMaybeRawConfig(src) + const drupalFields = buildConfigFromDrupalFields(src) + + const config = defuMerge(rawConfig, drupalFields) + + return await processConfig(config, tidePageApi) +} + +const parseGlobalFiltersFromDrupal = (rawFilters) => { + return rawFilters.map((rawFilter) => { + switch (rawFilter.type) { + case 'paragraph--listing_content_type': + return { + terms: { + type: rawFilter.field_listing_global_contenttype.map( + (contentType) => contentType.meta.drupal_internal__target_id + ) + } + } + case 'paragraph--listing_site': + return { + terms: { + field_node_site: rawFilter.field_listing_site_site.map( + (site) => site.meta.drupal_internal__target_id + ) + } + } + case 'paragraph--listing_custom_filter': + return parseJSONField(rawFilter.field_listing_custom_filter_conf) + default: + return null + } + }) +} + +const parseUserFiltersFromDrupal = (rawFilters) => { + const test = rawFilters.map((rawFilter) => { + switch (rawFilter.type) { + case 'paragraph--searchable_fields': { + const taxonomyName = rawFilter.field_field.field_taxonomy_machine_name + + return { + id: taxonomyName, + component: 'TideSearchFilterDropdown', + aggregations: { + field: taxonomyName, + source: 'taxonomy' + }, + filter: { + type: 'terms', + value: rawFilter.field_field.field_elasticsearch_field + }, + props: { + id: taxonomyName, + label: rawFilter.field_input_label, + placeholder: rawFilter.field_placeholder, + type: 'RplFormDropdown', + multiple: true + } + } + } + case 'paragraph--listing_user_custom_filter': + return parseJSONField(rawFilter.field_user_filter_configuration) + default: + return null + } + }) + + return test +} + +const parseResultsConfigFromDrupal = (src) => { + if (src?.field_listing_results_config) { + return parseJSONField(src.field_listing_results_config) + } + + if (src?.field_layout_component && src?.field_results_component) { + return { + layout: { + component: src.field_layout_component.name + }, + item: { + '*': { + component: src.field_results_component.name + } + } + } + } + + return null +} + +const buildConfigFromDrupalFields = (src) => { + return { + searchListingConfig: { + hideSearchForm: src?.field_listing_hide_search_form ?? false, + resultsPerPage: src?.field_listing_results_per_page || 10, + labels: { + submit: src?.field_search_submit_label, + placeholder: src?.field_search_input_placeholder + }, + customSort: src?.field_custom_sort_configuration + ? parseJSONField(src.field_custom_sort_configuration) + : undefined + }, + queryConfig: src?.field_listing_query_config + ? parseJSONField(src.field_listing_query_config) + : undefined, + globalFilters: src?.field_listing_global_filters?.length + ? parseGlobalFiltersFromDrupal(src.field_listing_global_filters) + : undefined, + userFilters: src?.field_listing_user_filters?.length + ? parseUserFiltersFromDrupal(src.field_listing_user_filters) + : undefined, + results: parseResultsConfigFromDrupal(src) + } +} + +const getMaybeRawConfig = (src) => { + let parsedConfig = {} + + if (src.field_search_configuration) { + try { + parsedConfig = JSON.parse(src.field_search_configuration) + } catch (e) { + throw new ApplicationError( + "Couldn't parse raw search listing config JSON field ('field_search_configuration')" + ) + } + } + + return parsedConfig +} diff --git a/tsconfig.json b/tsconfig.json index 31c0f49cac..7ca3c32d3d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,6 +21,7 @@ "@types/react" ], "paths": { + "@dpc-sdp/ripple-tide-api/errors": ["ripple-tide-api/src/errors/errors"], "@dpc-sdp/ripple-tide-api": ["ripple-tide-api/src"], "@dpc-sdp/ripple-ui-core": ["ripple-ui-core/src"], "@dpc-sdp/ripple-tide-event": ["ripple-tide-event/src"], From 22e9d62231dd8203fb1c132e6c7ea7c2a69b2981 Mon Sep 17 00:00:00 2001 From: David Featherston Date: Tue, 26 Mar 2024 12:32:03 +1100 Subject: [PATCH 2/8] fix(@dpc-sdp/ripple-tide-search): fix dependent filter reset for content collection --- .../components/global/TideCustomCollection.vue | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/packages/ripple-tide-search/components/global/TideCustomCollection.vue b/packages/ripple-tide-search/components/global/TideCustomCollection.vue index f004b4c9c1..0e608aa0b2 100644 --- a/packages/ripple-tide-search/components/global/TideCustomCollection.vue +++ b/packages/ripple-tide-search/components/global/TideCustomCollection.vue @@ -1,5 +1,5 @@