Merge pull request #415 from Human-Connection/350-query_current_users_notifications

350 query current users notifications
This commit is contained in:
Robert Schäfer 2019-04-05 12:00:11 +02:00 committed by GitHub
commit c543d7558c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 154 additions and 2 deletions

View File

@ -13,9 +13,11 @@ export default {
CreatePost: setCreatedAt, CreatePost: setCreatedAt,
CreateComment: setCreatedAt, CreateComment: setCreatedAt,
CreateOrganization: setCreatedAt, CreateOrganization: setCreatedAt,
CreateNotification: setCreatedAt,
UpdateUser: setUpdatedAt, UpdateUser: setUpdatedAt,
UpdatePost: setUpdatedAt, UpdatePost: setUpdatedAt,
UpdateComment: setUpdatedAt, UpdateComment: setUpdatedAt,
UpdateOrganization: setUpdatedAt UpdateOrganization: setUpdatedAt,
UpdateNotification: setUpdatedAt
} }
} }

View File

@ -44,6 +44,7 @@ const isAuthor = rule({ cache: 'no_cache' })(async (parent, args, { user, driver
// Permissions // Permissions
const permissions = shield({ const permissions = shield({
Query: { Query: {
Notification: isAdmin,
statistics: allow, statistics: allow,
currentUser: allow, currentUser: allow,
Post: or(onlyEnabledContent, isModerator) Post: or(onlyEnabledContent, isModerator)

View File

@ -0,0 +1,120 @@
import Factory from '../seed/factories'
import { GraphQLClient } from 'graphql-request'
import { host, login } from '../jest/helpers'
const factory = Factory()
let client
beforeEach(async () => {
await factory.create('User', {
id: 'you',
email: 'test@example.org',
password: '1234'
})
})
afterEach(async () => {
await factory.cleanDatabase()
})
describe('Notification', () => {
const query = `{
Notification {
id
}
}`
describe('unauthenticated', () => {
it('throws authorization error', async () => {
client = new GraphQLClient(host)
await expect(client.request(query)).rejects.toThrow('Not Authorised')
})
})
})
describe('currentUser { notifications }', () => {
let variables = {}
describe('authenticated', () => {
let headers
beforeEach(async () => {
headers = await login({ email: 'test@example.org', password: '1234' })
client = new GraphQLClient(host, { headers })
})
describe('given some notifications', () => {
beforeEach(async () => {
const neighborParams = {
email: 'neighbor@example.org',
password: '1234',
id: 'neighbor'
}
await Promise.all([
factory.create('User', neighborParams),
factory.create('Notification', { id: 'not-for-you' }),
factory.create('Notification', { id: 'already-seen', read: true })
])
await factory.create('Notification', { id: 'unseen' })
await factory.authenticateAs(neighborParams)
await factory.create('Post', { id: 'p1' })
await Promise.all([
factory.relate('Notification', 'User', { from: 'not-for-you', to: 'neighbor' }),
factory.relate('Notification', 'Post', { from: 'p1', to: 'not-for-you' }),
factory.relate('Notification', 'User', { from: 'unseen', to: 'you' }),
factory.relate('Notification', 'Post', { from: 'p1', to: 'unseen' }),
factory.relate('Notification', 'User', { from: 'already-seen', to: 'you' }),
factory.relate('Notification', 'Post', { from: 'p1', to: 'already-seen' })
])
})
describe('filter for read: false', () => {
const query = `query($read: Boolean) {
currentUser {
notifications(read: $read, orderBy: createdAt_desc) {
id
post {
id
}
}
}
}`
let variables = { read: false }
it('returns only unread notifications of current user', async () => {
const expected = {
currentUser: {
notifications: [
{ id: 'unseen', post: { id: 'p1' } }
]
}
}
await expect(client.request(query, variables)).resolves.toEqual(expected)
})
})
describe('no filters', () => {
const query = `{
currentUser {
notifications(orderBy: createdAt_desc) {
id
post {
id
}
}
}
}`
it('returns all notifications of current user', async () => {
const expected = {
currentUser: {
notifications: [
{ id: 'unseen', post: { id: 'p1' } },
{ id: 'already-seen', post: { id: 'p1' } }
]
}
}
await expect(client.request(query, variables)).resolves.toEqual(expected)
})
})
})
})
})

View File

@ -69,6 +69,14 @@ type Statistics {
countShouts: Int! countShouts: Int!
} }
type Notification {
id: ID!
read: Boolean,
user: User @relation(name: "NOTIFIED", direction: "OUT")
post: Post @relation(name: "NOTIFIED", direction: "IN")
createdAt: String
}
scalar Date scalar Date
scalar Time scalar Time
scalar DateTime scalar DateTime
@ -124,6 +132,8 @@ type User {
createdAt: String createdAt: String
updatedAt: String updatedAt: String
notifications(read: Boolean): [Notification]! @relation(name: "NOTIFIED", direction: "IN")
friends: [User]! @relation(name: "FRIENDS", direction: "BOTH") friends: [User]! @relation(name: "FRIENDS", direction: "BOTH")
friendsCount: Int! @cypher(statement: "MATCH (this)<-[:FRIENDS]->(r:User) RETURN COUNT(DISTINCT r)") friendsCount: Int! @cypher(statement: "MATCH (this)<-[:FRIENDS]->(r:User) RETURN COUNT(DISTINCT r)")

View File

@ -8,6 +8,7 @@ import createComment from './comments.js'
import createCategory from './categories.js' import createCategory from './categories.js'
import createTag from './tags.js' import createTag from './tags.js'
import createReport from './reports.js' import createReport from './reports.js'
import createNotification from './notifications.js'
export const seedServerHost = 'http://127.0.0.1:4001' export const seedServerHost = 'http://127.0.0.1:4001'
@ -29,7 +30,8 @@ const factories = {
Comment: createComment, Comment: createComment,
Category: createCategory, Category: createCategory,
Tag: createTag, Tag: createTag,
Report: createReport Report: createReport,
Notification: createNotification
} }
export const cleanDatabase = async (options = {}) => { export const cleanDatabase = async (options = {}) => {

View File

@ -0,0 +1,17 @@
import uuid from 'uuid/v4'
export default function (params) {
const {
id = uuid(),
read = false
} = params
return `
mutation {
CreateNotification(
id: "${id}",
read: ${read},
) { id, read }
}
`
}