Skip to content

Commit

Permalink
Merge pull request #84 from casesandberg/feature/tags
Browse files Browse the repository at this point in the history
Tags
  • Loading branch information
casesandberg authored Aug 29, 2024
2 parents a7d124e + f7ac4b4 commit d4b353d
Show file tree
Hide file tree
Showing 28 changed files with 1,028 additions and 18 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ NEXTAUTH_SECRET=""

AUTH_RESEND_KEY=""

DEV_DB_SEED_EMAIL="" # Optional. Set your email for the DB seed to add data to your user account
DEV_DB_SEED_EMAIL="" # Optional. Set your email for the DB seed to add data to your user account

# Optional. Set OpenAI key to auto-generate question tags
OPENAI_API_KEY=""
4 changes: 2 additions & 2 deletions apps/api/app/api/markets/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ export async function PATCH(

const { id } = schema.patch.parameters.parse(params)
const body = (await req.json()) as unknown
const { question, description, closeDate } = schema.patch.requestBody.transform(stripUndefined).parse(body)
const { question, description, closeDate, tags } = schema.patch.requestBody.transform(stripUndefined).parse(body)

const market = await getMarket({ id })
if (market.createdBy !== session.user.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}

const updatedComment = await updateMarket({ id, question, description, closeDate })
const updatedComment = await updateMarket({ id, question, description, closeDate, tags })

return NextResponse.json(updatedComment)
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion apps/api/app/api/markets/[id]/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default createSchema({
},
patch: {
parameters: MarketSchema.pick({ id: true }),
requestBody: MarketSchema.pick({ question: true, description: true, closeDate: true }).partial(),
requestBody: MarketSchema.pick({ question: true, description: true, closeDate: true, tags: true }).partial(),
responses: {
200: MarketSchema,
404: ServerErrorSchema,
Expand Down
32 changes: 32 additions & 0 deletions apps/api/app/api/markets/generate-tags/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NextResponse } from 'next/server'
import type { SchemaResponse } from '@play-money/api-helpers'
import { auth } from '@play-money/auth'
import { getMarketTagsLLM } from '@play-money/markets/lib/getMarketTagsLLM'
import schema from './schema'

export const dynamic = 'force-dynamic'

export async function POST(req: Request): Promise<SchemaResponse<typeof schema.post.responses>> {
try {
const body = (await req.json()) as unknown
const { question } = schema.post.requestBody.parse(body)

const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({
tags: [],
})
}

const tags = await getMarketTagsLLM({
question,
})

return NextResponse.json({
tags,
})
} catch (error) {
console.log(error) // eslint-disable-line no-console -- Log error for debugging
return NextResponse.json({ error: 'Error processing request' }, { status: 500 })
}
}
15 changes: 15 additions & 0 deletions apps/api/app/api/markets/generate-tags/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from 'zod'
import { ServerErrorSchema, createSchema } from '@play-money/api-helpers'

export default createSchema({
post: {
requestBody: z.object({ question: z.string() }),
responses: {
200: z.object({
tags: z.array(z.string()),
}),
404: ServerErrorSchema,
500: ServerErrorSchema,
},
},
})
4 changes: 2 additions & 2 deletions apps/api/app/api/markets/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export async function GET(req: Request): Promise<SchemaResponse<typeof schema.ge
const searchParams = new URLSearchParams(url.search)
const params = Object.fromEntries(searchParams)

const { createdBy, limit } = schema.get.parameters.parse(params) ?? {}
const { createdBy, limit, tag } = schema.get.parameters.parse(params) ?? {}

const markets = await getMarkets({ createdBy }, undefined, { skip: 0, take: limit ?? 50 })
const markets = await getMarkets({ createdBy, tag }, undefined, { skip: 0, take: limit ?? 50 })

return NextResponse.json({
markets,
Expand Down
2 changes: 2 additions & 0 deletions apps/api/app/api/markets/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default createSchema({
.object({
createdBy: z.string().optional(),
limit: z.coerce.number().optional(),
tag: z.string().optional(),
})
.optional(),
responses: {
Expand All @@ -23,6 +24,7 @@ export default createSchema({
question: true,
description: true,
closeDate: true,
tags: true,
}).extend({
options: z.array(
MarketOptionSchema.pick({
Expand Down
18 changes: 18 additions & 0 deletions apps/web/app/(app)/questions/tagged/[tag]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react'
import { getMarkets } from '@play-money/api-helpers/client'
import { MarketList } from '@play-money/markets/components/MarketList'

export default async function AppQuestionsPage({ params }: { params: { tag: string } }) {
const { markets } = await getMarkets({ tag: params.tag })

return (
<div className="mx-auto flex max-w-screen-lg flex-1 flex-col gap-8 md:flex-row">
<div className="w-full">
<div className="mb-2 text-2xl font-semibold">{params.tag}</div>
<MarketList markets={markets} />
</div>

<div className="space-y-8 md:w-80" />
</div>
)
}
146 changes: 142 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d4b353d

Please sign in to comment.