Hook up reactions data

This commit is contained in:
Kurt Hutten
2020-11-08 15:41:59 +11:00
parent db06a3852a
commit 9ff1d5043d
13 changed files with 103 additions and 36 deletions

View File

@@ -15,7 +15,7 @@ export const schema = gql`
partReaction(id: String!): PartReaction
}
input CreatePartReactionInput {
input TogglePartReactionInput {
emote: String!
userId: String!
partId: String!
@@ -28,7 +28,7 @@ export const schema = gql`
}
type Mutation {
createPartReaction(input: CreatePartReactionInput!): PartReaction!
togglePartReaction(input: TogglePartReactionInput!): PartReaction!
updatePartReaction(
id: String!
input: UpdatePartReactionInput!

View File

@@ -10,7 +10,7 @@ export const schema = gql`
user: User!
userId: String!
Comment: [Comment]!
Reaction: [PartReaction]!
Reaction(userId: String): [PartReaction]!
}
type Query {

View File

@@ -1,4 +1,4 @@
import { AuthenticationError, ForbiddenError, parseJWT } from '@redwoodjs/api'
import { AuthenticationError, ForbiddenError } from '@redwoodjs/api'
import { db } from 'src/lib/db'
export const requireOwnership = async ({ userId, userName, partId } = {}) => {

View File

@@ -1,3 +1,7 @@
import { UserInputError } from '@redwoodjs/api'
import { requireAuth } from 'src/lib/auth'
import { requireOwnership } from 'src/lib/owner'
import { db } from 'src/lib/db'
import { foreignKeyReplacement } from 'src/services/helpers'
@@ -11,10 +15,26 @@ export const partReaction = ({ id }) => {
})
}
export const createPartReaction = ({ input }) => {
return db.partReaction.create({
data: foreignKeyReplacement(input),
})
export const togglePartReaction = async ({ input }) => {
// if write fails emote_userId_partId @@unique constraint, then delete it instead
requireAuth()
await requireOwnership({userId: input?.userId})
const legalReactions = ['❤️', '👍', '😄', '🙌'] // TODO figure out a way of sharing code between FE and BE, so this is consistent with web/src/components/EmojiReaction/EmojiReaction.js
if(!legalReactions.includes(input.emote)) {
throw new UserInputError(`You can't react with '${input.emote}', only the following are allowed: ${legalReactions.join(', ')}`)
}
let dbPromise
const inputClone = {...input} // TODO foreignKeyReplacement mutates input, which I should fix but am lazy right now
try{
dbPromise = await db.partReaction.create({
data: foreignKeyReplacement(input),
})
} catch(e) {
dbPromise = db.partReaction.delete({
where: { emote_userId_partId: inputClone},
})
}
return dbPromise
}
export const updatePartReaction = ({ id, input }) => {

View File

@@ -57,5 +57,5 @@ export const Part = {
Comment: (_obj, { root }) =>
db.part.findOne({ where: { id: root.id } }).Comment(),
Reaction: (_obj, { root }) =>
db.part.findOne({ where: { id: root.id } }).Reaction(),
db.part.findOne({ where: { id: root.id } }).Reaction({where: {userId: _obj.userId}}),
}