Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #116 from solocommand/graphql-create-website-schedule
Browse files Browse the repository at this point in the history
Add mutation to create a website schedule
  • Loading branch information
zarathustra323 authored Jul 22, 2021
2 parents b58ef75 + 559d999 commit e4841e0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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!]!
Expand Down
39 changes: 39 additions & 0 deletions services/graphql-server/src/graphql/resolvers/website/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -128,5 +129,43 @@ 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, { published: 1, type: 1 }),
basedb.strictFindOne('website.Section', { _id: sectionId }, { projection: { site: 1 } }),
]);

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 } = response.data;
const projection = buildProjection({ info, type: 'WebsiteSchedule' });
return basedb.findById('website.Schedule', id, { projection });
},
},
};

0 comments on commit e4841e0

Please sign in to comment.