From 4b71dc530076ba6f0618fb082ea1bacb94f7c296 Mon Sep 17 00:00:00 2001 From: estelafs Date: Tue, 5 Dec 2023 17:40:12 -0300 Subject: [PATCH] added geographicContext to page --- src/models/page.model.ts | 16 ++++++++++++++++ src/schema/inputs/pageContext.input.ts | 2 +- .../inputs/pageGeographicContext.input.ts | 18 ++++++++++++++++++ src/schema/mutation/editPage.mutation.ts | 17 +++++++++++++++++ src/schema/types/page.type.ts | 8 ++++++++ 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/schema/inputs/pageGeographicContext.input.ts diff --git a/src/models/page.model.ts b/src/models/page.model.ts index 072a1c51e..968522c17 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -24,6 +24,13 @@ export type PageContextT = ( displayField: string; }; +/** Interface for the page geographic context */ +export type PageGeographicContextT = { + enabled: boolean; + region?: string; + country?: string; +}; + /** Page documents interface declaration */ export interface Page extends Document { kind: 'Page'; @@ -45,6 +52,7 @@ export interface Page extends Document { ) & { content: mongoose.Types.ObjectId | Form | Workflow | Dashboard; })[]; + geographicContext: PageGeographicContextT; permissions?: { canSee?: (mongoose.Types.ObjectId | Role)[]; canUpdate?: (mongoose.Types.ObjectId | Role)[]; @@ -90,6 +98,14 @@ const pageSchema = new Schema( _id: false, }, ], + geographicContext: { + enabled: { + type: Boolean, + default: false, + }, + region: String, + country: String, + }, permissions: { canSee: [ { diff --git a/src/schema/inputs/pageContext.input.ts b/src/schema/inputs/pageContext.input.ts index 7a6abf167..2304656ba 100644 --- a/src/schema/inputs/pageContext.input.ts +++ b/src/schema/inputs/pageContext.input.ts @@ -6,7 +6,7 @@ import { } from 'graphql'; import { Types } from 'mongoose'; -/** Aggregation type for queries/mutations argument */ +/** Page context type for queries/mutations argument */ export type PageContextArgs = { refData?: string | Types.ObjectId; resource: Types.ObjectId; diff --git a/src/schema/inputs/pageGeographicContext.input.ts b/src/schema/inputs/pageGeographicContext.input.ts new file mode 100644 index 000000000..2802e1418 --- /dev/null +++ b/src/schema/inputs/pageGeographicContext.input.ts @@ -0,0 +1,18 @@ +import { GraphQLBoolean, GraphQLInputObjectType, GraphQLString } from 'graphql'; + +/** Page geographic context type for queries/mutations argument */ +export type PageGeographicContextArgs = { + enabled?: boolean; + region?: string; + country?: string; +}; + +/** GraphQL Input Type for the page geographic context */ +export const PageGeographicContextInputType = new GraphQLInputObjectType({ + name: 'PageGeographicContextInputType', + fields: () => ({ + enabled: { type: GraphQLBoolean }, + region: { type: GraphQLString }, + country: { type: GraphQLString }, + }), +}); diff --git a/src/schema/mutation/editPage.mutation.ts b/src/schema/mutation/editPage.mutation.ts index 5cf7b9fb1..91dcb26ea 100644 --- a/src/schema/mutation/editPage.mutation.ts +++ b/src/schema/mutation/editPage.mutation.ts @@ -15,6 +15,10 @@ import { logger } from '@services/logger.service'; import { graphQLAuthCheck } from '@schema/shared'; import { Types } from 'mongoose'; import { Context } from '@server/apollo/context'; +import { + PageGeographicContextArgs, + PageGeographicContextInputType, +} from '@schema/inputs/pageGeographicContext.input'; /** Simple form permission change type */ type SimplePermissionChange = @@ -38,6 +42,7 @@ type EditPageArgs = { permissions?: any; icon?: string; visible?: boolean; + geographicContext?: PageGeographicContextArgs; }; /** @@ -53,6 +58,7 @@ export default { icon: { type: GraphQLString }, permissions: { type: GraphQLJSON }, visible: { type: GraphQLBoolean }, + geographicContext: { type: PageGeographicContextInputType }, }, async resolve(parent, args: EditPageArgs, context: Context) { graphQLAuthCheck(context); @@ -125,6 +131,17 @@ export default { // Update visibility Object.assign(update, !isNil(args.visible) && { visible: args.visible }); + // Update geographic context + if (!isNil(args.geographicContext)) { + const geographicContext = page.geographicContext; + Object.assign(update, { + geographicContext: { + ...geographicContext, + ...args.geographicContext, + }, + }); + } + // apply the update page = await Page.findByIdAndUpdate( page._id, diff --git a/src/schema/types/page.type.ts b/src/schema/types/page.type.ts index 950a6ab49..c7fac8776 100644 --- a/src/schema/types/page.type.ts +++ b/src/schema/types/page.type.ts @@ -54,6 +54,14 @@ export const PageType = new GraphQLObjectType({ return null; }, }, + geographicContext: { + type: GraphQLJSON, + async resolve(parent, args, context) { + const ability = await extendAbilityForPage(context.user, parent); + if (ability.can('read', parent)) return parent.geographicContext; + return null; + }, + }, permissions: { type: AccessType, async resolve(parent, args, context) {