Hook up reactions data
This commit is contained in:
@@ -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!
|
||||
|
||||
@@ -10,7 +10,7 @@ export const schema = gql`
|
||||
user: User!
|
||||
userId: String!
|
||||
Comment: [Comment]!
|
||||
Reaction: [PartReaction]!
|
||||
Reaction(userId: String): [PartReaction]!
|
||||
}
|
||||
|
||||
type Query {
|
||||
|
||||
@@ -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 } = {}) => {
|
||||
|
||||
@@ -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 }) => {
|
||||
|
||||
@@ -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}}),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user