From 05b4d6c3940241bd2ef282e027885d57c81a270b Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Thu, 7 Dec 2023 17:20:04 -0300 Subject: [PATCH 1/7] feat: started to add geographic context --- src/models/page.model.ts | 3 ++ .../addDashboardWithContext.mutation.ts | 39 ++++++++++++++----- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/models/page.model.ts b/src/models/page.model.ts index 968522c17..3172f8799 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -49,6 +49,9 @@ export interface Page extends Document { | { record: mongoose.Types.ObjectId | Record; } + | { + geographic: string | number; + } ) & { content: mongoose.Types.ObjectId | Form | Workflow | Dashboard; })[]; diff --git a/src/schema/mutation/addDashboardWithContext.mutation.ts b/src/schema/mutation/addDashboardWithContext.mutation.ts index 5488aaef3..b80936a40 100644 --- a/src/schema/mutation/addDashboardWithContext.mutation.ts +++ b/src/schema/mutation/addDashboardWithContext.mutation.ts @@ -1,4 +1,4 @@ -import { GraphQLNonNull, GraphQLError, GraphQLID } from 'graphql'; +import { GraphQLNonNull, GraphQLError, GraphQLID, GraphQLString } from 'graphql'; import { ApiConfiguration, Dashboard, @@ -72,6 +72,7 @@ const hasDuplicate = ( entry: { element?: any; record?: string | Types.ObjectId; + geographic: string; } ) => { const uniqueEntries = new Set(); @@ -84,7 +85,7 @@ const hasDuplicate = ( if (uniqueEntries.has(entry.record.toString())) { return true; } - } else { + } else if (!isNil(get(context, 'element'))) { for (const item of contentWithContext) { if (get(item, 'element')) { uniqueEntries.add((item as any).element.toString()); @@ -93,6 +94,15 @@ const hasDuplicate = ( if (uniqueEntries.has(entry.element.toString())) { return true; } + } else { + for (const item of contentWithContext) { + if (get(item, 'geographic')) { + uniqueEntries.add((item as any).element.toString()); + } + } + if (uniqueEntries.has(entry.element.toString())) { + return true; + } } return false; }; @@ -102,6 +112,7 @@ type AddDashboardWithContextArgs = { page: string; element?: any; record?: string | Types.ObjectId; + geographic?: string; }; /** @@ -114,6 +125,7 @@ export default { page: { type: new GraphQLNonNull(GraphQLID) }, element: { type: GraphQLJSON }, record: { type: GraphQLID }, + geographic: { type: GraphQLString } }, async resolve(parent, args: AddDashboardWithContextArgs, context: Context) { // Authentication check @@ -121,7 +133,8 @@ export default { try { const user = context.user; // Check arguments - if ((!args.element && !args.record) || (args.element && args.record)) { + if ((!args.element && !args.record && !args.geographic) || + ((args.element && args.record) || (args.element && args.geographic) || (args.record && args.geographic))) { throw new GraphQLError( context.i18next.t( 'mutations.dashboard.addWithContext.errors.invalidArguments' @@ -167,6 +180,7 @@ export default { hasDuplicate(page.context, page.contentWithContext, { ...(args.record && { record: args.record }), ...(args.element && { element: args.element }), + ...(args.geographic && { geographic: args.geographic }) }) ) { throw new GraphQLError( @@ -181,23 +195,30 @@ export default { name: await getNewDashboardName( template, page.context, - args.record || args.element, + args.record || args.element || args.geographic, context.dataSources ), // Copy structure from template dashboard structure: template.structure || [], }).save(); - - const newContentWithContext = args.record - ? ({ + + let newContentWithContext: any; + if (args.record) { + newContentWithContext = ({ record: args.record, content: newDashboard._id, } as Page['contentWithContext'][number]) - : ({ + } else if (args.element){ + newContentWithContext = ({ element: args.element, content: newDashboard._id, } as Page['contentWithContext'][number]); - + } else { + newContentWithContext = ({ + geographic: args.geographic, + content: newDashboard._id, + } as Page['contentWithContext'][number]); + } // Adds the dashboard to the page page.contentWithContext.push(newContentWithContext); await page.save(); From 9759dd1e21f63b16c7d60da64bcf66e7ae99ba53 Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Fri, 8 Dec 2023 16:16:03 -0300 Subject: [PATCH 2/7] feat: continued allow to use region and contry context with element or record contet --- src/models/page.model.ts | 6 +- .../addDashboardWithContext.mutation.ts | 121 ++++++++++++++---- 2 files changed, 98 insertions(+), 29 deletions(-) diff --git a/src/models/page.model.ts b/src/models/page.model.ts index 3172f8799..d9c83a224 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -49,10 +49,9 @@ export interface Page extends Document { | { record: mongoose.Types.ObjectId | Record; } - | { - geographic: string | number; - } ) & { + geographic?: string; + } & { content: mongoose.Types.ObjectId | Form | Workflow | Dashboard; })[]; geographicContext: PageGeographicContextT; @@ -91,6 +90,7 @@ const pageSchema = new Schema( contentWithContext: [ { element: mongoose.Schema.Types.Mixed, + geographic: mongoose.Schema.Types.Mixed, record: { type: mongoose.Schema.Types.ObjectId, ref: 'Record', diff --git a/src/schema/mutation/addDashboardWithContext.mutation.ts b/src/schema/mutation/addDashboardWithContext.mutation.ts index b80936a40..8dd71b9be 100644 --- a/src/schema/mutation/addDashboardWithContext.mutation.ts +++ b/src/schema/mutation/addDashboardWithContext.mutation.ts @@ -33,7 +33,10 @@ const getNewDashboardName = async ( id: string | Types.ObjectId, dataSources: any ) => { + console.log("context = ", context); + console.log("id = ", id); if ('refData' in context && context.refData) { + console.log("HERE2"); // Get items from reference data const referenceData = await ReferenceData.findById(context.refData); const apiConfiguration = await ApiConfiguration.findById( @@ -48,6 +51,7 @@ const getNewDashboardName = async ( const item = data.find((x) => x[referenceData.valueField] === id); return `${item?.[context.displayField]}`; } else if ('resource' in context && context.resource) { + console.log("HERE3"); const record = await Record.findById(id); return `${record.data[context.displayField]}`; } @@ -72,38 +76,92 @@ const hasDuplicate = ( entry: { element?: any; record?: string | Types.ObjectId; - geographic: string; + geographic?: string; } ) => { + console.log("contentWithContext = ", contentWithContext); + console.log("entry = ", entry); const uniqueEntries = new Set(); - if (!isNil(get(context, 'resource'))) { - for (const item of contentWithContext) { - if (get(item, 'record')) { - uniqueEntries.add((item as any).record.toString()); + + if ('geographic' in entry && 'geography') { + // record and geographic + if ('record' in entry) { + + const contains = contentWithContext.some((item: any) => item.record === entry.record && item.geographic === entry.geographic); + console.log(contains); + if (contains) { + return true; } - } - if (uniqueEntries.has(entry.record.toString())) { - return true; - } - } else if (!isNil(get(context, 'element'))) { - for (const item of contentWithContext) { - if (get(item, 'element')) { - uniqueEntries.add((item as any).element.toString()); + + // element and geographic + } else if ('element' in entry) { + const contains = contentWithContext.some((item: any) => item.element === entry.element && item.element === entry.element); + console.log(contains); + if (contains) { + return true; + } + // geographic + } else { + for (const item of contentWithContext) { + if (get(item, 'geographic')) { + uniqueEntries.add((item as any).geographic.toString()); + } + } + if (uniqueEntries.has(entry.geographic.toString())) { + return true; } } - if (uniqueEntries.has(entry.element.toString())) { - return true; - } + // record or element } else { - for (const item of contentWithContext) { - if (get(item, 'geographic')) { - uniqueEntries.add((item as any).element.toString()); + if (!isNil(get(context, 'resource'))) { + for (const item of contentWithContext) { + if (get(item, 'record')) { + uniqueEntries.add((item as any).record.toString()); + } + } + if (uniqueEntries.has(entry.record.toString())) { + return true; + } + } else if (!isNil(get(context, 'element'))) { + for (const item of contentWithContext) { + if (get(item, 'element')) { + uniqueEntries.add((item as any).element.toString()); + } + } + if (uniqueEntries.has(entry.element.toString())) { + return true; } - } - if (uniqueEntries.has(entry.element.toString())) { - return true; } } + + // if (!isNil(get(context, 'resource'))) { + // for (const item of contentWithContext) { + // if (get(item, 'record')) { + // uniqueEntries.add((item as any).record.toString()); + // } + // } + // if (uniqueEntries.has(entry.record.toString())) { + // return true; + // } + // } else if (!isNil(get(context, 'element'))) { + // for (const item of contentWithContext) { + // if (get(item, 'element')) { + // uniqueEntries.add((item as any).element.toString()); + // } + // } + // if (uniqueEntries.has(entry.element.toString())) { + // return true; + // } + // } else { + // for (const item of contentWithContext) { + // if (get(item, 'geographic')) { + // uniqueEntries.add((item as any).geographic.toString()); + // } + // } + // if (uniqueEntries.has(entry.geographic.toString())) { + // return true; + // } + // } return false; }; @@ -128,13 +186,19 @@ export default { geographic: { type: GraphQLString } }, async resolve(parent, args: AddDashboardWithContextArgs, context: Context) { + console.log("AQUI123"); + console.log(args); + // Authentication check graphQLAuthCheck(context); try { const user = context.user; // Check arguments - if ((!args.element && !args.record && !args.geographic) || - ((args.element && args.record) || (args.element && args.geographic) || (args.record && args.geographic))) { + if ( + (!args.element && !args.record && !args.geographic) || + (args.element && args.record && args.geographic) && + (args.element && args.record) + ) { throw new GraphQLError( context.i18next.t( 'mutations.dashboard.addWithContext.errors.invalidArguments' @@ -187,7 +251,6 @@ export default { context.i18next.t('mutations.dashboard.add.errors.invalidPageType') ); } - // Fetches the dashboard from the page const template = await Dashboard.findById(page.content); // Duplicates the dashboard @@ -195,12 +258,14 @@ export default { name: await getNewDashboardName( template, page.context, - args.record || args.element || args.geographic, + args.record || args.element, context.dataSources ), // Copy structure from template dashboard structure: template.structure || [], }).save(); + + console.log("OUT"); let newContentWithContext: any; if (args.record) { @@ -219,10 +284,14 @@ export default { content: newDashboard._id, } as Page['contentWithContext'][number]); } + console.log("newContentWithContext = ", newContentWithContext); // Adds the dashboard to the page page.contentWithContext.push(newContentWithContext); + console.log("page.contentWithContext = ", page.contentWithContext); await page.save(); + console.log("page = ", page); + console.log("newDashboard = ", newDashboard); return newDashboard; } catch (err) { logger.error(err.message, { stack: err.stack }); From 2089698db0c58b16eb7f9e93a027084b4197a756 Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Mon, 11 Dec 2023 15:32:32 -0300 Subject: [PATCH 3/7] feat: finished geographic context --- .../addDashboardWithContext.mutation.ts | 91 +++++++------------ 1 file changed, 32 insertions(+), 59 deletions(-) diff --git a/src/schema/mutation/addDashboardWithContext.mutation.ts b/src/schema/mutation/addDashboardWithContext.mutation.ts index 8dd71b9be..d0654ca1e 100644 --- a/src/schema/mutation/addDashboardWithContext.mutation.ts +++ b/src/schema/mutation/addDashboardWithContext.mutation.ts @@ -31,12 +31,10 @@ const getNewDashboardName = async ( dashboard: Dashboard, context: Page['context'], id: string | Types.ObjectId, + geographicContext: string, dataSources: any ) => { - console.log("context = ", context); - console.log("id = ", id); - if ('refData' in context && context.refData) { - console.log("HERE2"); + if ('refData' in context && context.refData && id) { // Get items from reference data const referenceData = await ReferenceData.findById(context.refData); const apiConfiguration = await ApiConfiguration.findById( @@ -49,15 +47,23 @@ const getNewDashboardName = async ( : referenceData.data; const item = data.find((x) => x[referenceData.valueField] === id); + if (geographicContext) { + return `${item?.[context.displayField]} - ${geographicContext}`; + } return `${item?.[context.displayField]}`; - } else if ('resource' in context && context.resource) { - console.log("HERE3"); + } else if ('resource' in context && context.resource && id) { const record = await Record.findById(id); + if (geographicContext) { + return `${record.data[context.displayField]} - ${geographicContext}`; + } return `${record.data[context.displayField]}`; } + // Default return + if (geographicContext) { + return `${dashboard.name} - ${geographicContext}`; + } + return `${dashboard.name}`; - // Default return, should never happen - return dashboard.name; }; /** @@ -79,24 +85,18 @@ const hasDuplicate = ( geographic?: string; } ) => { - console.log("contentWithContext = ", contentWithContext); - console.log("entry = ", entry); const uniqueEntries = new Set(); if ('geographic' in entry && 'geography') { // record and geographic if ('record' in entry) { - const contains = contentWithContext.some((item: any) => item.record === entry.record && item.geographic === entry.geographic); - console.log(contains); if (contains) { return true; } - // element and geographic } else if ('element' in entry) { - const contains = contentWithContext.some((item: any) => item.element === entry.element && item.element === entry.element); - console.log(contains); + const contains = contentWithContext.some((item: any) => item.element === entry.element && item.geographic === entry.geographic); if (contains) { return true; } @@ -133,35 +133,6 @@ const hasDuplicate = ( } } } - - // if (!isNil(get(context, 'resource'))) { - // for (const item of contentWithContext) { - // if (get(item, 'record')) { - // uniqueEntries.add((item as any).record.toString()); - // } - // } - // if (uniqueEntries.has(entry.record.toString())) { - // return true; - // } - // } else if (!isNil(get(context, 'element'))) { - // for (const item of contentWithContext) { - // if (get(item, 'element')) { - // uniqueEntries.add((item as any).element.toString()); - // } - // } - // if (uniqueEntries.has(entry.element.toString())) { - // return true; - // } - // } else { - // for (const item of contentWithContext) { - // if (get(item, 'geographic')) { - // uniqueEntries.add((item as any).geographic.toString()); - // } - // } - // if (uniqueEntries.has(entry.geographic.toString())) { - // return true; - // } - // } return false; }; @@ -185,10 +156,7 @@ export default { record: { type: GraphQLID }, geographic: { type: GraphQLString } }, - async resolve(parent, args: AddDashboardWithContextArgs, context: Context) { - console.log("AQUI123"); - console.log(args); - + async resolve(parent, args: AddDashboardWithContextArgs, context: Context) { // Authentication check graphQLAuthCheck(context); try { @@ -259,39 +227,44 @@ export default { template, page.context, args.record || args.element, + args.geographic, context.dataSources ), // Copy structure from template dashboard structure: template.structure || [], - }).save(); - - console.log("OUT"); - + }).save(); let newContentWithContext: any; - if (args.record) { + if (args.record && !args.geographic) { newContentWithContext = ({ record: args.record, content: newDashboard._id, } as Page['contentWithContext'][number]) - } else if (args.element){ + } else if (args.element && !args.geographic){ newContentWithContext = ({ element: args.element, content: newDashboard._id, } as Page['contentWithContext'][number]); + } else if (args.record && args.geographic) { + newContentWithContext = ({ + record: args.record, + geographic: args.geographic, + content: newDashboard._id, + } as Page['contentWithContext'][number]); + } else if (args.element && args.geographic) { + newContentWithContext = ({ + element: args.element, + geographic: args.geographic, + content: newDashboard._id, + } as Page['contentWithContext'][number]); } else { newContentWithContext = ({ geographic: args.geographic, content: newDashboard._id, } as Page['contentWithContext'][number]); } - console.log("newContentWithContext = ", newContentWithContext); // Adds the dashboard to the page page.contentWithContext.push(newContentWithContext); - console.log("page.contentWithContext = ", page.contentWithContext); await page.save(); - - console.log("page = ", page); - console.log("newDashboard = ", newDashboard); return newDashboard; } catch (err) { logger.error(err.message, { stack: err.stack }); From 7b3dbac2ccca3d55ac280987a87427bececc89a6 Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Mon, 11 Dec 2023 16:06:40 -0300 Subject: [PATCH 4/7] fix: lint --- .../addDashboardWithContext.mutation.ts | 65 +++++++++++-------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/src/schema/mutation/addDashboardWithContext.mutation.ts b/src/schema/mutation/addDashboardWithContext.mutation.ts index d0654ca1e..ffd743009 100644 --- a/src/schema/mutation/addDashboardWithContext.mutation.ts +++ b/src/schema/mutation/addDashboardWithContext.mutation.ts @@ -1,4 +1,9 @@ -import { GraphQLNonNull, GraphQLError, GraphQLID, GraphQLString } from 'graphql'; +import { + GraphQLNonNull, + GraphQLError, + GraphQLID, + GraphQLString, +} from 'graphql'; import { ApiConfiguration, Dashboard, @@ -24,6 +29,7 @@ import { get, isNil } from 'lodash'; * @param dashboard The dashboard being duplicated * @param context The context of the dashboard * @param id The id of the record or element + * @param geographicContext Geographic context value * @param dataSources The data sources * @returns The name of the new dashboard */ @@ -63,7 +69,6 @@ const getNewDashboardName = async ( return `${dashboard.name} - ${geographicContext}`; } return `${dashboard.name}`; - }; /** @@ -74,6 +79,7 @@ const getNewDashboardName = async ( * @param entry new entry * @param entry.element new element ( if ref data ) * @param entry.record new record ( if resource ) + * @param entry.geographic new geographic * @returns is entry duplicated or not */ const hasDuplicate = ( @@ -90,17 +96,23 @@ const hasDuplicate = ( if ('geographic' in entry && 'geography') { // record and geographic if ('record' in entry) { - const contains = contentWithContext.some((item: any) => item.record === entry.record && item.geographic === entry.geographic); + const contains = contentWithContext.some( + (item: any) => + item.record === entry.record && item.geographic === entry.geographic + ); if (contains) { return true; } - // element and geographic + // element and geographic } else if ('element' in entry) { - const contains = contentWithContext.some((item: any) => item.element === entry.element && item.geographic === entry.geographic); + const contains = contentWithContext.some( + (item: any) => + item.element === entry.element && item.geographic === entry.geographic + ); if (contains) { return true; } - // geographic + // geographic } else { for (const item of contentWithContext) { if (get(item, 'geographic')) { @@ -111,7 +123,7 @@ const hasDuplicate = ( return true; } } - // record or element + // record or element } else { if (!isNil(get(context, 'resource'))) { for (const item of contentWithContext) { @@ -154,9 +166,9 @@ export default { page: { type: new GraphQLNonNull(GraphQLID) }, element: { type: GraphQLJSON }, record: { type: GraphQLID }, - geographic: { type: GraphQLString } + geographic: { type: GraphQLString }, }, - async resolve(parent, args: AddDashboardWithContextArgs, context: Context) { + async resolve(parent, args: AddDashboardWithContextArgs, context: Context) { // Authentication check graphQLAuthCheck(context); try { @@ -164,7 +176,6 @@ export default { // Check arguments if ( (!args.element && !args.record && !args.geographic) || - (args.element && args.record && args.geographic) && (args.element && args.record) ) { throw new GraphQLError( @@ -212,7 +223,7 @@ export default { hasDuplicate(page.context, page.contentWithContext, { ...(args.record && { record: args.record }), ...(args.element && { element: args.element }), - ...(args.geographic && { geographic: args.geographic }) + ...(args.geographic && { geographic: args.geographic }), }) ) { throw new GraphQLError( @@ -232,35 +243,35 @@ export default { ), // Copy structure from template dashboard structure: template.structure || [], - }).save(); + }).save(); let newContentWithContext: any; if (args.record && !args.geographic) { - newContentWithContext = ({ - record: args.record, - content: newDashboard._id, - } as Page['contentWithContext'][number]) - } else if (args.element && !args.geographic){ - newContentWithContext = ({ - element: args.element, - content: newDashboard._id, - } as Page['contentWithContext'][number]); + newContentWithContext = { + record: args.record, + content: newDashboard._id, + } as Page['contentWithContext'][number]; + } else if (args.element && !args.geographic) { + newContentWithContext = { + element: args.element, + content: newDashboard._id, + } as Page['contentWithContext'][number]; } else if (args.record && args.geographic) { - newContentWithContext = ({ + newContentWithContext = { record: args.record, geographic: args.geographic, content: newDashboard._id, - } as Page['contentWithContext'][number]); + } as Page['contentWithContext'][number]; } else if (args.element && args.geographic) { - newContentWithContext = ({ + newContentWithContext = { element: args.element, geographic: args.geographic, content: newDashboard._id, - } as Page['contentWithContext'][number]); + } as Page['contentWithContext'][number]; } else { - newContentWithContext = ({ + newContentWithContext = { geographic: args.geographic, content: newDashboard._id, - } as Page['contentWithContext'][number]); + } as Page['contentWithContext'][number]; } // Adds the dashboard to the page page.contentWithContext.push(newContentWithContext); From a3e842bedbd026b6f9be05d75982d5b36d3bbfc7 Mon Sep 17 00:00:00 2001 From: Unai Zalba Date: Tue, 12 Dec 2023 10:47:59 +0100 Subject: [PATCH 5/7] feat/AB#80877_add_logic_to_create_edit_load_page_with_geographic_context fix: avoid the possibility of saving both region and country properties as they are mutually exclusive --- src/schema/mutation/editPage.mutation.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/schema/mutation/editPage.mutation.ts b/src/schema/mutation/editPage.mutation.ts index 91dcb26ea..a7510b010 100644 --- a/src/schema/mutation/editPage.mutation.ts +++ b/src/schema/mutation/editPage.mutation.ts @@ -133,10 +133,8 @@ export default { // Update geographic context if (!isNil(args.geographicContext)) { - const geographicContext = page.geographicContext; Object.assign(update, { geographicContext: { - ...geographicContext, ...args.geographicContext, }, }); From e4ee7a2fbdcc194444bed0bf08d369213b4aa989 Mon Sep 17 00:00:00 2001 From: Unai Zalba Date: Tue, 12 Dec 2023 11:25:20 +0100 Subject: [PATCH 6/7] feat/AB#80877_add_logic_to_create_edit_load_page_with_geographic_context refactor: context property build for the dashboard object in order to delete all unecessary lines of code --- .../addDashboardWithContext.mutation.ts | 74 +++++++------------ 1 file changed, 28 insertions(+), 46 deletions(-) diff --git a/src/schema/mutation/addDashboardWithContext.mutation.ts b/src/schema/mutation/addDashboardWithContext.mutation.ts index ffd743009..7d31ed725 100644 --- a/src/schema/mutation/addDashboardWithContext.mutation.ts +++ b/src/schema/mutation/addDashboardWithContext.mutation.ts @@ -94,20 +94,14 @@ const hasDuplicate = ( const uniqueEntries = new Set(); if ('geographic' in entry && 'geography') { + const contextTypeKey = + 'record' in entry ? 'record' : 'element' in entry ? 'element' : null; // record and geographic - if ('record' in entry) { + if (contextTypeKey) { const contains = contentWithContext.some( (item: any) => - item.record === entry.record && item.geographic === entry.geographic - ); - if (contains) { - return true; - } - // element and geographic - } else if ('element' in entry) { - const contains = contentWithContext.some( - (item: any) => - item.element === entry.element && item.geographic === entry.geographic + item[contextTypeKey] === entry[contextTypeKey] && + item.geographic === entry.geographic ); if (contains) { return true; @@ -125,22 +119,18 @@ const hasDuplicate = ( } // record or element } else { - if (!isNil(get(context, 'resource'))) { - for (const item of contentWithContext) { - if (get(item, 'record')) { - uniqueEntries.add((item as any).record.toString()); - } - } - if (uniqueEntries.has(entry.record.toString())) { - return true; - } - } else if (!isNil(get(context, 'element'))) { + const contextTypeKey = !isNil(get(context, 'resource')) + ? 'record' + : !isNil(get(context, 'element')) + ? 'element' + : null; + if (contextTypeKey) { for (const item of contentWithContext) { - if (get(item, 'element')) { - uniqueEntries.add((item as any).element.toString()); + if (get(item, contextTypeKey)) { + uniqueEntries.add((item as any)[contextTypeKey].toString()); } } - if (uniqueEntries.has(entry.element.toString())) { + if (uniqueEntries.has(entry[contextTypeKey].toString())) { return true; } } @@ -244,31 +234,23 @@ export default { // Copy structure from template dashboard structure: template.structure || [], }).save(); + let newContentWithContext: any; - if (args.record && !args.geographic) { - newContentWithContext = { - record: args.record, - content: newDashboard._id, - } as Page['contentWithContext'][number]; - } else if (args.element && !args.geographic) { - newContentWithContext = { - element: args.element, - content: newDashboard._id, - } as Page['contentWithContext'][number]; - } else if (args.record && args.geographic) { - newContentWithContext = { - record: args.record, - geographic: args.geographic, - content: newDashboard._id, - } as Page['contentWithContext'][number]; - } else if (args.element && args.geographic) { - newContentWithContext = { - element: args.element, - geographic: args.geographic, - content: newDashboard._id, - } as Page['contentWithContext'][number]; + const contextKey = args.record + ? 'record' + : args.element + ? 'element' + : null; + if (!args.geographic) { + if (contextKey) { + newContentWithContext = { + [contextKey]: args[contextKey], + content: newDashboard._id, + } as Page['contentWithContext'][number]; + } } else { newContentWithContext = { + ...(contextKey && { [contextKey]: args[contextKey] }), geographic: args.geographic, content: newDashboard._id, } as Page['contentWithContext'][number]; From 706d3956443039398ebb8455119a5d270369f1e2 Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Tue, 12 Dec 2023 09:16:10 -0300 Subject: [PATCH 7/7] fix: removed useless code --- src/schema/mutation/addDashboardWithContext.mutation.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/schema/mutation/addDashboardWithContext.mutation.ts b/src/schema/mutation/addDashboardWithContext.mutation.ts index 7d31ed725..6d1a62592 100644 --- a/src/schema/mutation/addDashboardWithContext.mutation.ts +++ b/src/schema/mutation/addDashboardWithContext.mutation.ts @@ -93,10 +93,10 @@ const hasDuplicate = ( ) => { const uniqueEntries = new Set(); - if ('geographic' in entry && 'geography') { + if ('geographic' in entry) { const contextTypeKey = 'record' in entry ? 'record' : 'element' in entry ? 'element' : null; - // record and geographic + // record or element and geographic if (contextTypeKey) { const contains = contentWithContext.some( (item: any) =>