Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/npm_and_yarn/backend/validator-…
Browse files Browse the repository at this point in the history
…13.11.0
  • Loading branch information
mahula authored Nov 3, 2023
2 parents e63281d + 1775aab commit f0e90aa
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 203 deletions.
3 changes: 2 additions & 1 deletion backend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = {
'!**/node_modules/**',
'!**/test/**',
'!**/build/**',
'!**/src/**/?(*.)+(spec|test).ts?(x)'
'!**/src/**/?(*.)+(spec|test).ts?(x)',
'!**/src/db/migrations/**'
],
coverageThreshold: {
global: {
Expand Down
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"eslint": "^8.37.0",
"eslint-config-prettier": "^9.0.0",
"eslint-config-standard": "^17.0.0",
"eslint-import-resolver-typescript": "^3.5.4",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-n": "^15.7.0",
Expand Down
68 changes: 68 additions & 0 deletions backend/src/db/migrations/20231017141022-fix-event-dates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { getDriver } from '../../db/neo4j'

export const description = `
Transform event start and end date of format 'YYYY-MM-DD HH:MM:SS' in CEST
to ISOString in UTC.
`

export async function up(next) {
const driver = getDriver()
const session = driver.session()
const transaction = session.beginTransaction()

try {
const events = await transaction.run(`
MATCH (event:Event)
WHERE NOT event.eventStart CONTAINS 'T'
RETURN event.id, event.eventStart, event.eventEnd
`)
for (const event of events.records) {
let [id, eventStart, eventEnd] = event
let date = new Date(eventStart)
date.setHours(date.getHours() - 1)
eventStart = date.toISOString()
if (eventEnd) {
date = new Date(eventEnd)
date.setHours(date.getHours() - 1)
eventEnd = date.toISOString()
}
await transaction.run(`
MATCH (e:Event { id: '${id}' })
SET e.eventStart = '${eventStart}'
SET (CASE WHEN exists(e.eventEnd) THEN e END).eventEnd = '${eventEnd}'
RETURN e
`)
}
await transaction.commit()
next()
} catch (error) {
// eslint-disable-next-line no-console
console.log(error)
await transaction.rollback()
// eslint-disable-next-line no-console
console.log('rolled back')
throw new Error(error)
} finally {
session.close()
}
}

export async function down(next) {
const driver = getDriver()
const session = driver.session()
const transaction = session.beginTransaction()

try {
// No sense in running this down
next()
} catch (error) {
// eslint-disable-next-line no-console
console.log(error)
await transaction.rollback()
// eslint-disable-next-line no-console
console.log('rolled back')
throw new Error(error)
} finally {
session.close()
}
}
4 changes: 4 additions & 0 deletions backend/src/schema/resolvers/helpers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ 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!')
Expand All @@ -44,6 +46,8 @@ 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!')
Expand Down
51 changes: 51 additions & 0 deletions backend/src/schema/resolvers/posts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,31 @@ describe('CreatePost', () => {
})
})

describe('with event start in no ISO format', () => {
it('throws an error', async () => {
const now = new Date()
const eventStart = new Date(now.getFullYear(), now.getMonth() - 1).toISOString()
await expect(
mutate({
mutation: createPostMutation(),
variables: {
...variables,
postType: 'Event',
eventInput: {
eventStart: eventStart.split('T')[0],
},
},
}),
).resolves.toMatchObject({
errors: [
{
message: 'Event start date must be in ISO format!',
},
],
})
})
})

describe('with event start date in the past', () => {
it('throws an error', async () => {
const now = new Date()
Expand Down Expand Up @@ -423,6 +448,32 @@ describe('CreatePost', () => {
})
})

describe('with valid start date and not ISO formated end date', () => {
it('throws an error', async () => {
const now = new Date()
const eventEnd = new Date(now.getFullYear(), now.getMonth() + 2).toISOString()
await expect(
mutate({
mutation: createPostMutation(),
variables: {
...variables,
postType: 'Event',
eventInput: {
eventStart: new Date(now.getFullYear(), now.getMonth() + 1).toISOString(),
eventEnd: eventEnd.split('T')[0],
},
},
}),
).resolves.toMatchObject({
errors: [
{
message: 'Event end date must be in ISO format!',
},
],
})
})
})

describe('with valid start date and end date before start date', () => {
it('throws an error', async () => {
const now = new Date()
Expand Down
Loading

0 comments on commit f0e90aa

Please sign in to comment.