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

@@ -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}}),
}