Skip to content
This repository has been archived by the owner on Mar 22, 2019. It is now read-only.

[🚨 DO NOT MERGE 🚨] ONLY AS REFERENCE FOR 204 - try and error #229

Open
wants to merge 2 commits into
base: 204-tests-for-rewarding-badges
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/graphql-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs'
import path from 'path'
import userManagement from './resolvers/user_management.js'
import statistics from './resolvers/statistics.js'
import rewards from './resolvers/rewards.js'
import reports from './resolvers/reports.js'
import posts from './resolvers/posts.js'
import moderation from './resolvers/moderation.js'
Expand All @@ -17,6 +18,7 @@ export const resolvers = {
},
Mutation: {
...userManagement.Mutation,
...rewards.Mutation,
...reports.Mutation,
...moderation.Mutation,
...posts.Mutation
Expand Down
1 change: 0 additions & 1 deletion src/middleware/permissionsMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const permissions = shield({
CreatePost: isAuthenticated,
UpdatePost: isAuthor,
DeletePost: isAuthor,
report: isAuthenticated,
CreateBadge: isAdmin,
UpdateBadge: isAdmin,
DeleteBadge: isAdmin,
Expand Down
57 changes: 57 additions & 0 deletions src/resolvers/rewards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import isEmpty from 'lodash/isEmpty'

export default {
Mutation: {
reward: async (parent, { userId, badgeId }, { driver, req, user }, resolveInfo) => {
const session = driver.session()

let data = {
userId,
badgeId,
rewarderId: user.id,
rewardedBy: null
}

let reward = await session.run(
'MATCH (b:Badge {id: $badgeId})-[r:REWARDED]->(u:User {id: $userId}), (by:User {id: $rewarderId}) ' +
'RETURN r.rewarderId, r.createdAt, by.role, by.deleted, by.name, by.disabled, by.avatar, by.id, by.slug', data
)
if (isEmpty(reward.records)) {
reward = await session.run(
'MATCH (u:User {id: $userId}), (b:Badge {id: $badgeId}), (by:User {id: $rewarderId}) ' +
'MERGE (b)-[r:REWARDED {rewarderId: $rewarderId, createdAt: DateTime()}]->(u) ' +
'RETURN r.rewarderId, r.createdAt, by.role, by.deleted, by.name, by.disabled, by.avatar, by.id, by.slug', data
)
}

session.close()

const [res] = reward.records.map(record => {
return {
rewarderId: record.get('r.rewarderId'),
rewardedBy: {
role: record.get('by.role'),
deleted: record.get('by.deleted'),
name: record.get('by.name'),
disabled: record.get('by.disabled'),
avatar: record.get('by.avatar'),
id: record.get('by.id'),
slug: record.get('by.slug')
},
createdAt: record.get('r.createdAt')
}
})
data.rewarderId = res.rewarderId
data.rewardedBy = res.rewardedBy
data.createdAt = res.createdAt

return {
from: { id: badgeId },
to: { id: userId },
rewarderId: data.rewarderId,
rewardedBy: data.rewardedBy,
createdAt: data.createdAt
}
}
}
}
21 changes: 10 additions & 11 deletions src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ type Statistics {
countShouts: Int!
}

scalar Date
scalar Time
scalar DateTime
# scalar Date
# scalar Time
# scalar DateTime

enum VisibilityEnum {
public
Expand Down Expand Up @@ -151,8 +151,8 @@ type User {

categories: [Category]! @relation(name: "CATEGORIZED", direction: "OUT")

badges: [Badge]! @relation(name: "REWARDED", direction: "IN")
badgesCount: Int! @cypher(statement: "MATCH (this)<-[:REWARDED]-(r:Badge) RETURN COUNT(r)")
rewards: [Reward]! @cypher(statement: "MATCH (this)<-[r:REWARDED]-(:Badge) RETURN r")
rewardsCount: Int! @cypher(statement: "MATCH (this)<-[:REWARDED]-(b:Badge) RETURN COUNT(b)")
}

type Post {
Expand Down Expand Up @@ -232,7 +232,7 @@ type Badge {
status: BadgeStatusEnum!
icon: String!

rewarded: [User]! @relation(name: "REWARDED", direction: "OUT")
rewarded: [Reward]! @cypher(statement: "MATCH (this)-[r:REWARDED]->(:User) RETURN r")
}

enum BadgeTypeEnum {
Expand All @@ -255,11 +255,10 @@ enum FollowTypeEnum {
}

type Reward {
id: ID!
user: User @relation(name: "REWARDED", direction: "IN")
rewarderId: ID
createdAt: String
badge: Badge @relation(name: "REWARDED", direction: "OUT")
from: Badge @cypher(statement: "MATCH (b:Badge)-[:REWARDED]->(:User) RETURN b")
to: User @cypher(statement: "MATCH (:Badge)-[:REWARDED]->(u:User) RETURN u")
rewardedBy: User
createdAt: DateTime
}

type Organization {
Expand Down
13 changes: 8 additions & 5 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import express from 'express'
import dotenv from 'dotenv'
import mocks from './mocks'
import middleware from './middleware'
import applyDirectives from './bootstrap/directives'
import applyScalars from './bootstrap/scalars'
// import applyDirectives from './bootstrap/directives'
// import applyScalars from './bootstrap/scalars'
import { getDriver } from './bootstrap/neo4j'
import decode from './jwt/decode'

Expand All @@ -27,15 +27,18 @@ let schema = makeAugmentedSchema({
resolvers,
config: {
query: {
exclude: ['Statistics', 'LoggedInUser']
exclude: ['Statistics', 'LoggedInUser', '_RewardInput']
},
mutation: {
exclude: ['Statistics', 'LoggedInUser']
exclude: [
'Statistics',
'LoggedInUser'
]
},
debug: debug
}
})
schema = applyScalars(applyDirectives(schema))
// schema = applyScalars(applyDirectives(schema))

const createServer = (options) => {
const defaults = {
Expand Down