updatePost with event type

This commit is contained in:
Moriz Wahl 2023-03-30 15:03:21 +02:00
parent 0c225715fa
commit 2a8a993ff8
3 changed files with 182 additions and 32 deletions

View File

@ -1,14 +1,27 @@
import { UserInputError } from 'apollo-server' import { UserInputError } from 'apollo-server'
export const validateEventParams = (params) => { export const validateEventParams = (params) => {
const { eventInput } = params if (params.postType && params.postType === 'Event') {
validateEventDate(eventInput.eventStart) const { eventInput } = params
params.eventStart = eventInput.eventStart validateEventDate(eventInput.eventStart)
if (eventInput.eventLocation && !eventInput.eventVenue) { params.eventStart = eventInput.eventStart
throw new UserInputError('Event venue must be present if event location is given!') 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 delete params.eventInput
params.eventLocation = eventInput.eventLocation 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) => { const validateEventDate = (dateString) => {

View File

@ -84,20 +84,7 @@ export default {
const { categoryIds, groupId } = params const { categoryIds, groupId } = params
const { image: imageInput } = params const { image: imageInput } = params
if (params.postType && params.postType === 'Event') { const locationName = validateEventParams(params)
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
delete params.categoryIds delete params.categoryIds
delete params.image delete params.image
@ -176,6 +163,9 @@ export default {
UpdatePost: async (_parent, params, context, _resolveInfo) => { UpdatePost: async (_parent, params, context, _resolveInfo) => {
const { categoryIds } = params const { categoryIds } = params
const { image: imageInput } = params const { image: imageInput } = params
const locationName = validateEventParams(params)
delete params.categoryIds delete params.categoryIds
delete params.image delete params.image
const session = context.driver.session() const session = context.driver.session()
@ -227,6 +217,9 @@ export default {
return post return post
}) })
const post = await writeTxResultPromise const post = await writeTxResultPromise
if (locationName) {
await createOrUpdateLocations('Post', post.id, locationName, session)
}
return post return post
} finally { } finally {
session.close() session.close()

View File

@ -496,6 +496,7 @@ describe('UpdatePost', () => {
$image: ImageInput $image: ImageInput
$categoryIds: [ID] $categoryIds: [ID]
$postType: PostType $postType: PostType
$eventInput: _EventInput
) { ) {
UpdatePost( UpdatePost(
id: $id id: $id
@ -504,6 +505,7 @@ describe('UpdatePost', () => {
image: $image image: $image
categoryIds: $categoryIds categoryIds: $categoryIds
postType: $postType postType: $postType
eventInput: $eventInput
) { ) {
id id
title title
@ -518,6 +520,13 @@ describe('UpdatePost', () => {
id id
} }
postType postType
eventStart
eventLocationName
eventVenue
eventLocation {
lng
lat
}
} }
} }
` `
@ -639,18 +648,153 @@ describe('UpdatePost', () => {
}) })
}) })
describe('post type', () => { describe('change post type to event', () => {
it('changes the post type', async () => { describe('with missing event start date', () => {
await expect( it('throws an error', async () => {
mutate({ mutation: updatePostMutation, variables: { ...variables, postType: 'Event' } }), await expect(
).resolves.toMatchObject({ mutate({
data: { mutation: updatePostMutation,
UpdatePost: { variables: { ...variables, postType: 'Event' },
id: newlyCreatedPost.id, }),
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,
})
}) })
}) })
}) })