diff --git a/backend/src/graphql/posts.js b/backend/src/graphql/posts.js index ee01243ea..f1b62a286 100644 --- a/backend/src/graphql/posts.js +++ b/backend/src/graphql/posts.js @@ -12,6 +12,7 @@ export const createPostMutation = () => { $categoryIds: [ID] $groupId: ID $postType: PostType + $eventInput: _EventInput ) { CreatePost( id: $id @@ -21,6 +22,7 @@ export const createPostMutation = () => { categoryIds: $categoryIds groupId: $groupId postType: $postType + eventInput: $eventInput ) { id slug @@ -35,6 +37,13 @@ export const createPostMutation = () => { categories { id } + eventStart + eventLocationName + eventVenue + eventLocation { + lng + lat + } } } ` diff --git a/backend/src/schema/resolvers/posts.js b/backend/src/schema/resolvers/posts.js index 4afa10c77..354ae49fa 100644 --- a/backend/src/schema/resolvers/posts.js +++ b/backend/src/schema/resolvers/posts.js @@ -89,8 +89,15 @@ export default { } delete params.eventInput - const locationName = params.eventLocation ? params.eventLocation : null - delete params.eventLoaction + let locationName + if (params.eventLocation) { + params.eventLocationName = params.eventLocation + locationName = params.eventLocation + } else { + params.eventLocationName = null + locationName = null + } + delete params.eventLocation delete params.categoryIds delete params.image @@ -406,7 +413,17 @@ export default { }, Post: { ...Resolver('Post', { - undefinedToNull: ['activityId', 'objectId', 'language', 'pinnedAt', 'pinned'], + undefinedToNull: [ + 'activityId', + 'objectId', + 'language', + 'pinnedAt', + 'pinned', + 'eventVenue', + 'eventLocation', + 'eventLocationName', + 'eventStart', + ], hasMany: { tags: '-[:TAGGED]->(related:Tag)', categories: '-[:CATEGORIZED]->(related:Category)', @@ -419,6 +436,7 @@ export default { pinnedBy: '<-[:PINNED]-(related:User)', image: '-[:HERO_IMAGE]->(related:Image)', group: '-[:IN]->(related:Group)', + eventLocation: '-[:IS_IN]->(related:Location)', }, count: { commentsCount: diff --git a/backend/src/schema/resolvers/posts.spec.js b/backend/src/schema/resolvers/posts.spec.js index 1f9b646e8..499bd463d 100644 --- a/backend/src/schema/resolvers/posts.spec.js +++ b/backend/src/schema/resolvers/posts.spec.js @@ -312,19 +312,6 @@ describe('CreatePost', () => { }) }) - describe('with post type "Event"', () => { - it('has label "Event" set', async () => { - await expect( - mutate({ - mutation: createPostMutation(), - variables: { ...variables, postType: 'Event' }, - }), - ).resolves.toMatchObject({ - data: { CreatePost: { postType: ['Event'] } }, - }) - }) - }) - describe('with invalid post type', () => { it('throws an error', async () => { await expect( @@ -342,6 +329,160 @@ describe('CreatePost', () => { }) }) }) + + describe('with post type "Event"', () => { + describe('without event start date', () => { + it('throws an error', async () => { + await expect( + mutate({ + mutation: createPostMutation(), + 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: createPostMutation(), + 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: createPostMutation(), + 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: createPostMutation(), + 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: createPostMutation(), + variables: { + ...variables, + postType: 'Event', + eventInput: { + eventStart: new Date(now.getFullYear(), now.getMonth() + 1).toISOString(), + }, + }, + }), + ).resolves.toMatchObject({ + data: { + CreatePost: { + postType: ['Event'], + eventStart: new Date(now.getFullYear(), now.getMonth() + 1).toISOString(), + }, + }, + errors: undefined, + }) + }) + }) + + describe('valid event input with location', () => { + it('has label "Event" set', async () => { + const now = new Date() + await expect( + mutate({ + mutation: createPostMutation(), + variables: { + ...variables, + postType: 'Event', + eventInput: { + eventStart: new Date(now.getFullYear(), now.getMonth() + 1).toISOString(), + eventLocation: 'Leipzig', + eventVenue: 'Connewitzer Kreuz', + }, + }, + }), + ).resolves.toMatchObject({ + data: { + CreatePost: { + 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, + }) + }) + }) + }) }) }) @@ -499,7 +640,7 @@ describe('UpdatePost', () => { }) describe('post type', () => { - it.only('changes the post type', async () => { + it('changes the post type', async () => { await expect( mutate({ mutation: updatePostMutation, variables: { ...variables, postType: 'Event' } }), ).resolves.toMatchObject({