From 1f999d7b116e7ac46f4ef40bd07abad4f2471497 Mon Sep 17 00:00:00 2001 From: Josh Worden Date: Tue, 20 Jul 2021 14:13:21 -0500 Subject: [PATCH 1/3] Create mutation to create website schedules --- .../graphql/definitions/website/schedule.js | 9 ++++ .../src/graphql/resolvers/website/schedule.js | 41 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/services/graphql-server/src/graphql/definitions/website/schedule.js b/services/graphql-server/src/graphql/definitions/website/schedule.js index d124c88b7..f65f74c0c 100644 --- a/services/graphql-server/src/graphql/definitions/website/schedule.js +++ b/services/graphql-server/src/graphql/definitions/website/schedule.js @@ -18,6 +18,7 @@ extend type Query { } extend type Mutation { + createWebsiteSchedule(input: CreateWebsiteScheduleMutationInput!): WebsiteSchedule! @requiresAuth quickCreateWebsiteSchedules(input: QuickCreateWebsiteSchedulesMutationInput!): [WebsiteSchedule!]! updateWebsiteSchedule(input: UpdateWebsiteScheduleMutationInput!): WebsiteSchedule! deleteWebsiteSchedule(input: DeleteWebsiteScheduleMutationInput!): String! @@ -98,6 +99,14 @@ input WebsiteScheduleOptionInput { status: ModelStatus = active } +input CreateWebsiteScheduleMutationInput { + contentId: Int! + sectionId: Int! + optionId: Int + startDate: Date + endDate: Date +} + input QuickCreateWebsiteSchedulesMutationInput { contentId: Int! sectionIds: [Int!]! diff --git a/services/graphql-server/src/graphql/resolvers/website/schedule.js b/services/graphql-server/src/graphql/resolvers/website/schedule.js index 23799b6cf..e4e1ac794 100644 --- a/services/graphql-server/src/graphql/resolvers/website/schedule.js +++ b/services/graphql-server/src/graphql/resolvers/website/schedule.js @@ -128,5 +128,46 @@ module.exports = { const projection = getProjection(schema, schema.getType('WebsiteSchedule'), fieldNodes[0].selectionSet, fragments); return basedb.find('website.Schedule', { _id: { $in: ids } }, { projection }); }, + + /** + * + */ + createWebsiteSchedule: async (_, { input }, { basedb, base4rest, load }, info) => { + validateRest(base4rest); + + const { contentId, sectionId } = input; + const [content, section] = await Promise.all([ + load('platformContent', contentId, { status: 1, published: 1, type: 1 }), + basedb.strictFindOne('website.Section', { _id: sectionId }, { projection: { site: 1 } }), + ]); + if (content.status !== 1) throw new UserInputError('The content status must be published.'); + + const startDate = input.startDate || content.published || new Date(); + const siteId = BaseDB.extractRefId(section.site); + if (!siteId) throw new Error(`Unable to extract a site ID for section ${section._id}.`); + + let { optionId } = input; + if (!optionId) { + const option = await basedb.strictFindOne('website.Option', { name: 'Standard', status: 1, 'site.$id': siteId }, { projection: { _id: 1 } }); + optionId = option._id; + } + + const body = new Base4RestPayload({ type: 'website/schedule' }); + body + .set('startDate', startDate) + .set('status', 1) + .setLink('product', { id: siteId, type: 'website/product/site' }) + .setLink('section', { id: sectionId, type: 'website/section' }) + .setLink('option', { id: optionId, type: 'website/option' }) + .setLink('content', { id: content._id, type: `platform/content/${dasherize(content.type)}` }); + if (input.endDate) body.set('endDate', input.endDate); + + const response = await base4rest.insertOne({ model: 'website/schedule', body }); + const id = BaseDB.coerceID(response.data.id); + + const { fieldNodes, schema, fragments } = info; + const projection = getProjection(schema, schema.getType('WebsiteSchedule'), fieldNodes[0].selectionSet, fragments); + return basedb.findOne('website.Schedule', { _id: id }, { projection }); + }, }, }; From 3dd5907ee66c531eea3691580b9bfcb98bf68d78 Mon Sep 17 00:00:00 2001 From: Josh Worden Date: Wed, 21 Jul 2021 13:41:05 -0500 Subject: [PATCH 2/3] Use buildProjection utility --- .../src/graphql/resolvers/website/schedule.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/services/graphql-server/src/graphql/resolvers/website/schedule.js b/services/graphql-server/src/graphql/resolvers/website/schedule.js index e4e1ac794..0cd9a58a2 100644 --- a/services/graphql-server/src/graphql/resolvers/website/schedule.js +++ b/services/graphql-server/src/graphql/resolvers/website/schedule.js @@ -5,6 +5,7 @@ const { UserInputError } = require('apollo-server-express'); const validateRest = require('../../utils/validate-rest'); const getProjection = require('../../utils/get-projection'); +const buildProjection = require('../../utils/build-projection'); const { ObjectID } = MongoDB; @@ -163,11 +164,9 @@ module.exports = { if (input.endDate) body.set('endDate', input.endDate); const response = await base4rest.insertOne({ model: 'website/schedule', body }); - const id = BaseDB.coerceID(response.data.id); - - const { fieldNodes, schema, fragments } = info; - const projection = getProjection(schema, schema.getType('WebsiteSchedule'), fieldNodes[0].selectionSet, fragments); - return basedb.findOne('website.Schedule', { _id: id }, { projection }); + const { id } = response.data; + const projection = buildProjection({ info, type: 'WebsiteSchedule' }); + return basedb.findById('website.Schedule', id, { projection }); }, }, }; From 559d9994ca15ceb2efaf62331d600ae1f9d5d90b Mon Sep 17 00:00:00 2001 From: Josh Worden Date: Thu, 22 Jul 2021 09:52:47 -0500 Subject: [PATCH 3/3] Remove content status check --- .../graphql-server/src/graphql/resolvers/website/schedule.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/graphql-server/src/graphql/resolvers/website/schedule.js b/services/graphql-server/src/graphql/resolvers/website/schedule.js index 0cd9a58a2..702863d12 100644 --- a/services/graphql-server/src/graphql/resolvers/website/schedule.js +++ b/services/graphql-server/src/graphql/resolvers/website/schedule.js @@ -138,10 +138,9 @@ module.exports = { const { contentId, sectionId } = input; const [content, section] = await Promise.all([ - load('platformContent', contentId, { status: 1, published: 1, type: 1 }), + load('platformContent', contentId, { published: 1, type: 1 }), basedb.strictFindOne('website.Section', { _id: sectionId }, { projection: { site: 1 } }), ]); - if (content.status !== 1) throw new UserInputError('The content status must be published.'); const startDate = input.startDate || content.published || new Date(); const siteId = BaseDB.extractRefId(section.site);