Skip to content

Commit

Permalink
Refactor update-market
Browse files Browse the repository at this point in the history
  • Loading branch information
IanPhilips committed Nov 5, 2024
1 parent 63d31e5 commit 488abc4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 45 deletions.
66 changes: 24 additions & 42 deletions backend/api/src/update-market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const updateMarket: APIHandler<'market/:contractId/update'> = async (
sort,
question,
coverImageUrl,
isSpicePayout,

description: raw,
descriptionHtml: html,
Expand All @@ -38,18 +37,7 @@ export const updateMarket: APIHandler<'market/:contractId/update'> = async (
const pg = createSupabaseDirectClient()
const contract = await getContract(pg, contractId)
if (!contract) throw new APIError(404, `Contract ${contractId} not found`)
if (contract.creatorId !== auth.uid) await throwErrorIfNotMod(auth.uid)
if (isSpicePayout !== undefined) {
if (!isAdminId(auth.uid)) {
throw new APIError(403, 'Only admins choose prize markets')
}
if (isSpicePayout === true) {
throw new APIError(
403,
`We not making spice markets anymore! If you're sure, comment this out. - Sinclair`
)
}
}
if (contract.creatorId !== auth.uid) throwErrorIfNotMod(auth.uid)

if (contract.isResolved && closeTime !== undefined) {
throw new APIError(403, 'Cannot update closeTime for resolved contracts')
Expand All @@ -67,17 +55,6 @@ export const updateMarket: APIHandler<'market/:contractId/update'> = async (
)
}

await trackPublicEvent(
auth.uid,
'update market',
removeUndefinedProps({
contractId,
visibility,
closeTime,
addAnswersMode,
})
)

const update = removeUndefinedProps({
question,
coverImageUrl,
Expand All @@ -87,7 +64,7 @@ export const updateMarket: APIHandler<'market/:contractId/update'> = async (
addAnswersMode,
sort,
description,
isSpicePayout,
lastUpdatedTime: Date.now(),
})
await updateContract(pg, contractId, {
...update,
Expand All @@ -96,26 +73,31 @@ export const updateMarket: APIHandler<'market/:contractId/update'> = async (

log(`updated fields: ${Object.keys(fields).join(', ')}`)

if (question || closeTime || visibility || description) {
await recordContractEdit(
contract,
auth.uid,
buildArray([
question && 'question',
closeTime && 'closeTime',
visibility && 'visibility',
description && 'description',
])
)
}

const continuation = async () => {
log(`Revalidating contract ${contract.id}.`)
await revalidateContractStaticProps(contract)
log(`Updating lastUpdatedTime for contract ${contract.id}.`)
await updateContract(pg, contract.id, {
lastUpdatedTime: Date.now(),
})
await trackPublicEvent(
auth.uid,
'update market',
removeUndefinedProps({
contractId,
visibility,
closeTime,
addAnswersMode,
})
)
if (question || closeTime || visibility || description) {
await recordContractEdit(
contract,
auth.uid,
buildArray([
question && 'question',
closeTime && 'closeTime',
visibility && 'visibility',
description && 'description',
])
)
}
}

return {
Expand Down
2 changes: 1 addition & 1 deletion backend/shared/src/helpers/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { APIError } from 'common//api/utils'
import { isAdminId, isModId } from 'common/envs/constants'

export const throwErrorIfNotMod = async (userId: string) => {
export const throwErrorIfNotMod = (userId: string) => {
if (!isAdminId(userId) && !isModId(userId)) {
throw new APIError(
403,
Expand Down
2 changes: 0 additions & 2 deletions common/src/api/market-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,6 @@ export const updateMarketProps = z
addAnswersMode: z.enum(['ONLY_CREATOR', 'ANYONE']).optional(),
coverImageUrl: z.string().or(z.null()).optional(),
sort: z.string().optional(),
isSpicePayout: z.boolean().optional(),

description: z.string().optional(),
descriptionHtml: z.string().optional(),
descriptionMarkdown: z.string().optional(),
Expand Down

0 comments on commit 488abc4

Please sign in to comment.