refactor link/dicussion to upserts and reuse more code

This commit is contained in:
keyan 2022-04-18 17:10:26 -05:00
parent 2b594109ea
commit 822fa9113a
6 changed files with 57 additions and 159 deletions

View File

@ -445,70 +445,26 @@ export default {
}, },
Mutation: { Mutation: {
createLink: async (parent, { title, url, boost }, { me, models }) => { upsertLink: async (parent, args, { me, models }) => {
if (!title) { const { id, ...data } = args
throw new UserInputError('link must have title', { argumentName: 'title' }) data.url = ensureProtocol(data.url)
}
if (!url) { if (id) {
throw new UserInputError('link must have url', { argumentName: 'url' }) const { forward, boost, ...remaining } = data
return await updateItem(parent, { id, data: remaining }, { me, models })
} else {
return await createItem(parent, data, { me, models })
} }
return await createItem(parent, { title, url: ensureProtocol(url), boost }, { me, models })
}, },
updateLink: async (parent, { id, title, url }, { me, models }) => { upsertDiscussion: async (parent, args, { me, models }) => {
if (!id) { const { id, ...data } = args
throw new UserInputError('link must have id', { argumentName: 'id' })
}
if (!title) { if (id) {
throw new UserInputError('link must have title', { argumentName: 'title' }) const { forward, boost, ...remaining } = data
return await updateItem(parent, { id, data: remaining }, { me, models })
} else {
return await createItem(parent, data, { me, models })
} }
if (!url) {
throw new UserInputError('link must have url', { argumentName: 'url' })
}
// update iff this item belongs to me
const item = await models.item.findUnique({ where: { id: Number(id) } })
if (Number(item.userId) !== Number(me.id)) {
throw new AuthenticationError('item does not belong to you')
}
if (Date.now() > new Date(item.createdAt).getTime() + 10 * 60000) {
throw new UserInputError('item can no longer be editted')
}
return await updateItem(parent, { id, data: { title, url: ensureProtocol(url) } }, { me, models })
},
createDiscussion: async (parent, { title, text, boost }, { me, models }) => {
if (!title) {
throw new UserInputError('discussion must have title', { argumentName: 'title' })
}
return await createItem(parent, { title, text, boost }, { me, models })
},
updateDiscussion: async (parent, { id, title, text }, { me, models }) => {
if (!id) {
throw new UserInputError('discussion must have id', { argumentName: 'id' })
}
if (!title) {
throw new UserInputError('discussion must have title', { argumentName: 'title' })
}
// update iff this item belongs to me
const item = await models.item.findUnique({ where: { id: Number(id) } })
if (Number(item.userId) !== Number(me.id)) {
throw new AuthenticationError('item does not belong to you')
}
// if it's not the FAQ and older than 10 minutes
if (item.id !== 349 && Date.now() > new Date(item.createdAt).getTime() + 10 * 60000) {
throw new UserInputError('item can no longer be editted')
}
return await updateItem(parent, { id, data: { title, text } }, { me, models })
}, },
upsertJob: async (parent, { id, sub, title, company, location, remote, text, url, maxBid, status }, { me, models }) => { upsertJob: async (parent, { id, sub, title, company, location, remote, text, url, maxBid, status }, { me, models }) => {
if (!me) { if (!me) {
@ -559,6 +515,11 @@ export default {
} }
} }
const old = await models.item.findUnique({ where: { id: Number(id) } })
if (Number(old.userId) !== Number(me?.id)) {
throw new AuthenticationError('item does not belong to you')
}
return await models.item.update({ return await models.item.update({
where: { id: Number(id) }, where: { id: Number(id) },
data data
@ -572,35 +533,9 @@ export default {
}) })
}, },
createComment: async (parent, { text, parentId }, { me, models }) => { createComment: async (parent, { text, parentId }, { me, models }) => {
if (!text) {
throw new UserInputError('comment must have text', { argumentName: 'text' })
}
if (!parentId) {
throw new UserInputError('comment must have parent', { argumentName: 'parentId' })
}
return await createItem(parent, { text, parentId }, { me, models }) return await createItem(parent, { text, parentId }, { me, models })
}, },
updateComment: async (parent, { id, text }, { me, models }) => { updateComment: async (parent, { id, text }, { me, models }) => {
if (!text) {
throw new UserInputError('comment must have text', { argumentName: 'text' })
}
if (!id) {
throw new UserInputError('comment must have id', { argumentName: 'id' })
}
// update iff this comment belongs to me
const comment = await models.item.findUnique({ where: { id: Number(id) } })
if (Number(comment.userId) !== Number(me.id)) {
throw new AuthenticationError('comment does not belong to you')
}
if (Date.now() > new Date(comment.createdAt).getTime() + 10 * 60000) {
throw new UserInputError('comment can no longer be editted')
}
return await updateItem(parent, { id, data: { text } }, { me, models }) return await updateItem(parent, { id, data: { text } }, { me, models })
}, },
act: async (parent, { id, sats }, { me, models }) => { act: async (parent, { id, sats }, { me, models }) => {
@ -828,6 +763,17 @@ export const createMentions = async (item, models) => {
} }
const updateItem = async (parent, { id, data }, { me, models }) => { const updateItem = async (parent, { id, data }, { me, models }) => {
// update iff this item belongs to me
const old = await models.item.findUnique({ where: { id: Number(id) } })
if (Number(old.userId) !== Number(me?.id)) {
throw new AuthenticationError('item does not belong to you')
}
// if it's not the FAQ and older than 10 minutes
if (old.id !== 349 && Date.now() > new Date(old.createdAt).getTime() + 10 * 60000) {
throw new UserInputError('item can no longer be editted')
}
const item = await models.item.update({ const item = await models.item.update({
where: { id: Number(id) }, where: { id: Number(id) },
data data
@ -838,7 +784,7 @@ const updateItem = async (parent, { id, data }, { me, models }) => {
return item return item
} }
const createItem = async (parent, { title, url, text, boost, parentId }, { me, models }) => { const createItem = async (parent, { title, url, text, boost, forward, parentId }, { me, models }) => {
if (!me) { if (!me) {
throw new AuthenticationError('you must be logged in') throw new AuthenticationError('you must be logged in')
} }

View File

@ -19,10 +19,11 @@ export default gql`
} }
extend type Mutation { extend type Mutation {
upsertStory(title: String!, text: String, url: String, boost: Int): Item! upsertLink(id: ID, title: String!, url: String!, boost: Int, forward: String): Item!
upsertDiscussion(id: ID, title: String!, text: String, boost: Int, forward: String): Item!
upsertJob(id: ID, sub: ID!, title: String!, company: String!, location: String, remote: Boolean, text: String!, url: String!, maxBid: Int!, status: String): Item!
createComment(text: String!, parentId: ID!): Item! createComment(text: String!, parentId: ID!): Item!
updateComment(id: ID!, text: String!): Item! updateComment(id: ID!, text: String!): Item!
upsertJob(id: ID, sub: ID!, title: String!, company: String!, location: String, remote: Boolean, text: String!, url: String!, maxBid: Int!, status: String): Item!
act(id: ID!, sats: Int): ItemActResult! act(id: ID!, sats: Int): ItemActResult!
} }

View File

@ -7,7 +7,7 @@ import { BOOST_MIN } from '../lib/constants'
export const AdvPostSchema = { export const AdvPostSchema = {
boost: Yup.number().typeError('must be a number') boost: Yup.number().typeError('must be a number')
.min(BOOST_MIN, `must be at least ${BOOST_MIN}`).integer('must be whole'), .min(BOOST_MIN, `must be at least ${BOOST_MIN}`).integer('must be whole'),
forward: Yup.string().required('required').trim() forward: Yup.string().trim()
} }
export const AdvPostInitial = { export const AdvPostInitial = {

View File

@ -18,36 +18,14 @@ export function DiscussionForm ({
adv, handleSubmit adv, handleSubmit
}) { }) {
const router = useRouter() const router = useRouter()
const [createDiscussion] = useMutation( const [upsertDiscussion] = useMutation(
gql` gql`
mutation createDiscussion($title: String!, $text: String, $boost: Int, $forward: String) { mutation upsertDiscussion($id: ID, $title: String!, $text: String, $boost: Int, $forward: String) {
createDiscussion(title: $title, text: $text, boost: $boost, forward: $forward) { upsertDiscussion(id: $id, title: $title, text: $text, boost: $boost, forward: $forward) {
id id
} }
}` }`
) )
const [updateDiscussion] = useMutation(
gql`
mutation updateDiscussion($id: ID!, $title: String!, $text: String!, $forward: String) {
updateDiscussion(id: $id, title: $title, text: $text, forward: $forward) {
id
}
}`, {
update (cache, { data: { updateDiscussion } }) {
cache.modify({
id: `Item:${item.id}`,
fields: {
title () {
return updateDiscussion.title
},
text () {
return updateDiscussion.text
}
}
})
}
}
)
return ( return (
<Form <Form
@ -58,12 +36,9 @@ export function DiscussionForm ({
}} }}
schema={DiscussionSchema} schema={DiscussionSchema}
onSubmit={handleSubmit || (async ({ boost, ...values }) => { onSubmit={handleSubmit || (async ({ boost, ...values }) => {
let error const { error } = await upsertDiscussion({
if (item) { variables: { id: item?.id, boost: Number(boost), ...values }
({ error } = await updateDiscussion({ variables: { ...values, id: item.id } })) })
} else {
({ error } = await createDiscussion({ variables: { boost: Number(boost), ...values } }))
}
if (error) { if (error) {
throw new Error({ message: error.toString() }) throw new Error({ message: error.toString() })
} }

View File

@ -112,11 +112,15 @@ export default function JobForm ({ item, sub }) {
status = 'STOPPED' status = 'STOPPED'
} }
const variables = { sub: sub.name, maxBid: Number(maxBid), status, ...values } const { error } = await upsertJob({
if (item) { variables: {
variables.id = item.id id: item?.id,
} sub: sub.name,
const { error } = await upsertJob({ variables }) maxBid: Number(maxBid),
status,
...values
}
})
if (error) { if (error) {
throw new Error({ message: error.toString() }) throw new Error({ message: error.toString() })
} }

View File

@ -36,40 +36,15 @@ export function LinkForm ({ item, editThreshold }) {
fetchPolicy: 'network-only' fetchPolicy: 'network-only'
}) })
const [createLink] = useMutation( const [upsertLink] = useMutation(
gql` gql`
mutation createLink($title: String!, $url: String!, $boost: Int, $forward: String) { mutation upsertLink($id: ID, $title: String!, $url: String!, $boost: Int, $forward: String) {
createLink(title: $title, url: $url, boost: $boost, forward: $forward) { upsertLink(id: $id, title: $title, url: $url, boost: $boost, forward: $forward) {
id id
} }
}` }`
) )
const [updateLink] = useMutation(
gql`
mutation updateLink($id: ID!, $title: String!, $url: String!, $forward: String) {
updateLink(id: $id, title: $title, url: $url, forward: $forward) {
id
title
url
}
}`, {
update (cache, { data: { updateLink } }) {
cache.modify({
id: `Item:${item.id}`,
fields: {
title () {
return updateLink.title
},
url () {
return updateLink.url
}
}
})
}
}
)
return ( return (
<Form <Form
initial={{ initial={{
@ -79,12 +54,9 @@ export function LinkForm ({ item, editThreshold }) {
}} }}
schema={LinkSchema} schema={LinkSchema}
onSubmit={async ({ boost, ...values }) => { onSubmit={async ({ boost, ...values }) => {
let error const { error } = await upsertLink({
if (item) { variables: { id: item?.id, boost: Number(boost), ...values }
({ error } = await updateLink({ variables: { ...values, id: item.id } })) })
} else {
({ error } = await createLink({ variables: { boost: Number(boost), ...values } }))
}
if (error) { if (error) {
throw new Error({ message: error.toString() }) throw new Error({ message: error.toString() })
} }