mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
updatePost with event type
This commit is contained in:
parent
0c225715fa
commit
2a8a993ff8
@ -1,6 +1,7 @@
|
|||||||
import { UserInputError } from 'apollo-server'
|
import { UserInputError } from 'apollo-server'
|
||||||
|
|
||||||
export const validateEventParams = (params) => {
|
export const validateEventParams = (params) => {
|
||||||
|
if (params.postType && params.postType === 'Event') {
|
||||||
const { eventInput } = params
|
const { eventInput } = params
|
||||||
validateEventDate(eventInput.eventStart)
|
validateEventDate(eventInput.eventStart)
|
||||||
params.eventStart = eventInput.eventStart
|
params.eventStart = eventInput.eventStart
|
||||||
@ -10,6 +11,18 @@ export const validateEventParams = (params) => {
|
|||||||
params.eventVenue = eventInput.eventVenue
|
params.eventVenue = eventInput.eventVenue
|
||||||
params.eventLocation = eventInput.eventLocation
|
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) => {
|
const validateEventDate = (dateString) => {
|
||||||
const date = new Date(dateString)
|
const date = new Date(dateString)
|
||||||
|
|||||||
@ -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()
|
||||||
|
|||||||
@ -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,15 +648,115 @@ describe('UpdatePost', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('post type', () => {
|
describe('change post type to event', () => {
|
||||||
it('changes the post type', async () => {
|
describe('with missing event start date', () => {
|
||||||
|
it('throws an error', async () => {
|
||||||
await expect(
|
await expect(
|
||||||
mutate({ mutation: updatePostMutation, variables: { ...variables, postType: 'Event' } }),
|
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({
|
).resolves.toMatchObject({
|
||||||
data: {
|
data: {
|
||||||
UpdatePost: {
|
UpdatePost: {
|
||||||
id: newlyCreatedPost.id,
|
|
||||||
postType: ['Event'],
|
postType: ['Event'],
|
||||||
|
eventStart: new Date(now.getFullYear(), now.getMonth() + 1).toISOString(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
errors: undefined,
|
errors: undefined,
|
||||||
@ -655,6 +764,41 @@ describe('UpdatePost', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe.skip('params.image', () => {
|
describe.skip('params.image', () => {
|
||||||
describe('is object', () => {
|
describe('is object', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user