From ca386d91a9e7d7b05d3e3314f2b7bc11ea3af5d9 Mon Sep 17 00:00:00 2001 From: Christo Date: Sat, 16 Nov 2019 01:44:45 +0700 Subject: [PATCH] fix(plugin-workflow): merge queries through the queries envelope not the unit field --- .../lib/plugins/merge-query.js | 61 +++++++++++-------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/packages/plugin-workflow/lib/plugins/merge-query.js b/packages/plugin-workflow/lib/plugins/merge-query.js index 81209341..bc6b1d02 100644 --- a/packages/plugin-workflow/lib/plugins/merge-query.js +++ b/packages/plugin-workflow/lib/plugins/merge-query.js @@ -17,37 +17,44 @@ const plugin = (envelope, {cfg, log}) => { )}`, ); - return env.fmapData( - unit => - unit._sc_queries.reduce((memo, query) => { - const toMerge = fields.reduce((acc, field) => { - const oldValue = get(field, unit); - const newValue = get(field, query); + return env.fmapData(unit => { + const queries = envelope.queries.filter( + ({type, term}) => + unit._sc_queries.find( + query => query.type === type && query.term === term, + ) != null, + ); - if (!overwrite && oldValue != null) { - log.info( - `Not overwriting ${oldValue} with ${newValue} for field ${field} in ${query.type}/${unit._sc_id_hash}`, - ); - return acc; - } + return queries.reduce((memo, query) => { + const toMerge = fields.reduce((acc, field) => { + const oldValue = get(field, unit); + const newValue = get(field, query); - if (overwrite && oldValue != null) { - log.info( - `Overwriting ${oldValue} with ${newValue} for field ${field} in ${query.type}/${unit._sc_id_hash}`, - ); - } else { - log.info( - `Merging ${newValue} for field ${field} in ${query.type}/${unit._sc_id_hash}`, - ); - } + if (!overwrite && oldValue != null) { + log.info( + `Not overwriting ${oldValue} with ${newValue} for field ${field} in ${query.type}/${unit._sc_id_hash}`, + ); + return acc; + } - return deepmerge(acc, set(field, newValue, {})); - }, {}); + if (overwrite && oldValue != null) { + log.info( + `Overwriting ${oldValue} with ${newValue} for field ${field} in ${query.type}/${unit._sc_id_hash}`, + ); + } else { + log.info( + `Merging ${newValue} for field ${field} in ${query.type}/${unit._sc_id_hash}`, + ); + } - return deepmerge(memo, toMerge); - }, unit), - envelope, - ); + if (newValue == null) return acc; + + return deepmerge(acc, set(field, newValue, {})); + }, {}); + + return deepmerge(memo, toMerge); + }, unit); + }, envelope); }; plugin.argv = {