diff --git a/backend/src/schema/resolvers/helpers/events.js b/backend/src/schema/resolvers/helpers/events.js index b725d1e5f..b2f204f1d 100644 --- a/backend/src/schema/resolvers/helpers/events.js +++ b/backend/src/schema/resolvers/helpers/events.js @@ -1,14 +1,27 @@ import { UserInputError } from 'apollo-server' export const validateEventParams = (params) => { - const { eventInput } = params - validateEventDate(eventInput.eventStart) - params.eventStart = eventInput.eventStart - if (eventInput.eventLocation && !eventInput.eventVenue) { - throw new UserInputError('Event venue must be present if event location is given!') + if (params.postType && params.postType === 'Event') { + const { eventInput } = params + validateEventDate(eventInput.eventStart) + params.eventStart = eventInput.eventStart + if (eventInput.eventLocation && !eventInput.eventVenue) { + throw new UserInputError('Event venue must be present if event location is given!') + } + params.eventVenue = eventInput.eventVenue + params.eventLocation = eventInput.eventLocation } - params.eventVenue = eventInput.eventVenue - params.eventLocation = eventInput.eventLocation + delete params.eventInput + let locationName + if (params.eventLocation) { + params.eventLocationName = params.eventLocation + locationName = params.eventLocation + } else { + params.eventLocationName = null + locationName = null + } + delete params.eventLocation + return locationName } const validateEventDate = (dateString) => { diff --git a/backend/src/schema/resolvers/posts.js b/backend/src/schema/resolvers/posts.js index 354ae49fa..42a755e8f 100644 --- a/backend/src/schema/resolvers/posts.js +++ b/backend/src/schema/resolvers/posts.js @@ -84,20 +84,7 @@ export default { const { categoryIds, groupId } = params const { image: imageInput } = params - if (params.postType && params.postType === 'Event') { - validateEventParams(params) - } - delete params.eventInput - - let locationName - if (params.eventLocation) { - params.eventLocationName = params.eventLocation - locationName = params.eventLocation - } else { - params.eventLocationName = null - locationName = null - } - delete params.eventLocation + const locationName = validateEventParams(params) delete params.categoryIds delete params.image @@ -176,6 +163,9 @@ export default { UpdatePost: async (_parent, params, context, _resolveInfo) => { const { categoryIds } = params const { image: imageInput } = params + + const locationName = validateEventParams(params) + delete params.categoryIds delete params.image const session = context.driver.session() @@ -227,6 +217,9 @@ export default { return post }) const post = await writeTxResultPromise + if (locationName) { + await createOrUpdateLocations('Post', post.id, locationName, session) + } return post } finally { session.close() diff --git a/backend/src/schema/resolvers/posts.spec.js b/backend/src/schema/resolvers/posts.spec.js index 499bd463d..b3dd28f38 100644 --- a/backend/src/schema/resolvers/posts.spec.js +++ b/backend/src/schema/resolvers/posts.spec.js @@ -496,6 +496,7 @@ describe('UpdatePost', () => { $image: ImageInput $categoryIds: [ID] $postType: PostType + $eventInput: _EventInput ) { UpdatePost( id: $id @@ -504,6 +505,7 @@ describe('UpdatePost', () => { image: $image categoryIds: $categoryIds postType: $postType + eventInput: $eventInput ) { id title @@ -518,6 +520,13 @@ describe('UpdatePost', () => { id } postType + eventStart + eventLocationName + eventVenue + eventLocation { + lng + lat + } } } ` @@ -639,18 +648,153 @@ describe('UpdatePost', () => { }) }) - describe('post type', () => { - it('changes the post type', async () => { - await expect( - mutate({ mutation: updatePostMutation, variables: { ...variables, postType: 'Event' } }), - ).resolves.toMatchObject({ - data: { - UpdatePost: { - id: newlyCreatedPost.id, - postType: ['Event'], + describe('change post type to event', () => { + describe('with missing event start date', () => { + it('throws an error', async () => { + await expect( + mutate({ + mutation: updatePostMutation, + variables: { ...variables, postType: 'Event' }, + }), + ).resolves.toMatchObject({ + errors: [ + { + message: "Cannot read properties of undefined (reading 'eventStart')", + }, + ], + }) + }) + }) + + describe('with invalid event start date', () => { + it('throws an error', async () => { + await expect( + mutate({ + mutation: updatePostMutation, + variables: { + ...variables, + postType: 'Event', + eventInput: { + eventStart: 'no-date', + }, + }, + }), + ).resolves.toMatchObject({ + errors: [ + { + message: 'Event start date must be a valid date!', + }, + ], + }) + }) + }) + + describe('with event start date in the past', () => { + it('throws an error', async () => { + const now = new Date() + await expect( + mutate({ + mutation: updatePostMutation, + variables: { + ...variables, + postType: 'Event', + eventInput: { + eventStart: new Date(now.getFullYear(), now.getMonth() - 1).toISOString(), + }, + }, + }), + ).resolves.toMatchObject({ + errors: [ + { + message: 'Event start date must be in the future!', + }, + ], + }) + }) + }) + + describe('event location is given but event venue is missing', () => { + it('throws an error', async () => { + const now = new Date() + await expect( + mutate({ + mutation: updatePostMutation, + variables: { + ...variables, + postType: 'Event', + eventInput: { + eventStart: new Date(now.getFullYear(), now.getMonth() + 1).toISOString(), + eventLocation: 'Berlin', + }, + }, + }), + ).resolves.toMatchObject({ + errors: [ + { + message: 'Event venue must be present if event location is given!', + }, + ], + }) + }) + }) + + describe('valid event input without location', () => { + it('has label "Event" set', async () => { + const now = new Date() + await expect( + mutate({ + mutation: updatePostMutation, + variables: { + ...variables, + postType: 'Event', + eventInput: { + eventStart: new Date(now.getFullYear(), now.getMonth() + 1).toISOString(), + }, + }, + }), + ).resolves.toMatchObject({ + data: { + UpdatePost: { + postType: ['Event'], + eventStart: new Date(now.getFullYear(), now.getMonth() + 1).toISOString(), + }, }, - }, - errors: undefined, + errors: undefined, + }) + }) + }) + + describe('valid event input with location', () => { + it('has label "Event" set', async () => { + const now = new Date() + await expect( + mutate({ + mutation: updatePostMutation, + variables: { + ...variables, + postType: 'Event', + eventInput: { + eventStart: new Date(now.getFullYear(), now.getMonth() + 1).toISOString(), + eventLocation: 'Leipzig', + eventVenue: 'Connewitzer Kreuz', + }, + }, + }), + ).resolves.toMatchObject({ + data: { + UpdatePost: { + postType: ['Event'], + eventStart: new Date(now.getFullYear(), now.getMonth() + 1).toISOString(), + eventLocationName: 'Leipzig', + eventVenue: 'Connewitzer Kreuz', + eventLocation: { + lng: 12.374733, + lat: 51.340632, + }, + }, + }, + errors: undefined, + }) }) }) })