cypher to track user interactions

This commit is contained in:
Moriz Wahl 2021-02-22 19:07:44 +01:00
parent 7bbdd5852f
commit 7c5e5d971e

View File

@ -1,8 +1,38 @@
const createRelatedCypher = (relation) => `
MATCH (user:User { id: $currentUser})
MATCH (post:Post { id: $postId})<-[r:${relation}]-(u:User)
WHERE NOT u.disabled AND NOT u.deleted
WITH user, post, count(DISTINCT u) AS count
MERGE (user)-[relation:${relation} { }]->(post)
ON CREATE
SET relation.count = 1,
relation.createdAt = toString(datetime()),
post.clickCount = count + 1
ON MATCH
SET relation.count = relation.count + 1,
relation.updatedAt = toString(datetime()),
post.clickCount = count
RETURN user, post, relation
`
const setPostCounter = async (postId, relation, context) => {
const { user: currentUser } = context
const session = context.driver.session()
try {
await session.writeTransaction((txc) => {
return txc.run(
createRelatedCypher(relation),
{ currentUser, postId },
)
})
} finally {
session.close()
}
}
const userClickedPost = async (resolve, root, args, context, info) => {
if (args.id) {
console.log('post clicked--', args.id)
await setPostCounter(args.id, 'CLICKED', context)
}
return resolve(root, args, context, info)
}