mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
55 lines
1.9 KiB
TypeScript
55 lines
1.9 KiB
TypeScript
import { UserInputError } from 'apollo-server'
|
|
|
|
export const validateEventParams = (params) => {
|
|
let locationName = null
|
|
if (params.postType && params.postType === 'Event') {
|
|
const { eventInput } = params
|
|
validateEventDate(eventInput.eventStart)
|
|
params.eventStart = eventInput.eventStart
|
|
|
|
if (eventInput.eventEnd) {
|
|
validateEventEnd(eventInput.eventStart, eventInput.eventEnd)
|
|
params.eventEnd = eventInput.eventEnd
|
|
} else {
|
|
params.eventEnd = null
|
|
}
|
|
|
|
if (eventInput.eventLocationName && !eventInput.eventVenue) {
|
|
throw new UserInputError('Event venue must be present if event location is given!')
|
|
}
|
|
params.eventVenue = eventInput.eventVenue
|
|
params.eventLocationName = eventInput.eventLocationName && eventInput.eventLocationName.trim()
|
|
if (params.eventLocationName) {
|
|
locationName = params.eventLocationName
|
|
} else {
|
|
params.eventLocationName = null
|
|
}
|
|
params.eventIsOnline = !!eventInput.eventIsOnline
|
|
}
|
|
delete params.eventInput
|
|
return locationName
|
|
}
|
|
|
|
const validateEventDate = (dateString) => {
|
|
const date = new Date(dateString)
|
|
if (date.toString() === 'Invalid Date')
|
|
throw new UserInputError('Event start date must be a valid date!')
|
|
if (date.toISOString() !== dateString)
|
|
throw new UserInputError('Event start date must be in ISO format!')
|
|
const now = new Date()
|
|
if (date.getTime() < now.getTime()) {
|
|
throw new UserInputError('Event start date must be in the future!')
|
|
}
|
|
}
|
|
|
|
const validateEventEnd = (start, end) => {
|
|
const endDate = new Date(end)
|
|
if (endDate.toString() === 'Invalid Date')
|
|
throw new UserInputError('Event end date must be a valid date!')
|
|
if (endDate.toISOString() !== end)
|
|
throw new UserInputError('Event end date must be in ISO format!')
|
|
const startDate = new Date(start)
|
|
if (endDate < startDate)
|
|
throw new UserInputError('Event end date must be a after event start date!')
|
|
}
|