From e87d9afbaf41152b35b0f5d229ca469bce83e86e Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 16 Oct 2020 11:59:59 +0100 Subject: [PATCH] Fixing an issue whereby in some enrichment calls the count of links would be doubled (counting both sides). --- packages/server/src/db/linkedRows/index.js | 9 +++++++-- packages/server/src/db/linkedRows/linkUtils.js | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/server/src/db/linkedRows/index.js b/packages/server/src/db/linkedRows/index.js index 0932fc665f..dedb0f84a5 100644 --- a/packages/server/src/db/linkedRows/index.js +++ b/packages/server/src/db/linkedRows/index.js @@ -1,5 +1,5 @@ const LinkController = require("./LinkController") -const { IncludeDocs, getLinkDocuments, createLinkView } = require("./linkUtils") +const { IncludeDocs, getLinkDocuments, createLinkView, getUniqueByProp } = require("./linkUtils") const _ = require("lodash") /** @@ -110,7 +110,12 @@ exports.attachLinkInfo = async (instanceId, rows) => { // now iterate through the rows and all field information for (let row of rows) { // get all links for row, ignore fieldName for now - const linkVals = responses.filter(el => el.thisId === row._id) + // have to get unique as the previous table query can + // return duplicates, could be querying for both tables in a relation + const linkVals = getUniqueByProp( + responses.filter(el => el.thisId === row._id), + "id" + ) for (let linkVal of linkVals) { // work out which link pertains to this row if (!(row[linkVal.fieldName] instanceof Array)) { diff --git a/packages/server/src/db/linkedRows/linkUtils.js b/packages/server/src/db/linkedRows/linkUtils.js index 8f548eed3d..5f600cc3cb 100644 --- a/packages/server/src/db/linkedRows/linkUtils.js +++ b/packages/server/src/db/linkedRows/linkUtils.js @@ -92,3 +92,9 @@ exports.getLinkDocuments = async function({ } } } + +exports.getUniqueByProp = (array, prop) => { + return array.filter((obj, pos, arr) => { + return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos + }) +}