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'
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) => {

View File

@ -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()

View File

@ -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,
})
})
})
})