Skip to content

Commit

Permalink
chore: drop duplicate category apis
Browse files Browse the repository at this point in the history
  • Loading branch information
benfurber committed Jan 21, 2025
1 parent a0d9414 commit fa7147e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 41 deletions.
34 changes: 30 additions & 4 deletions src/routes/api.categories.$type.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
import Keyv from 'keyv'
import { isProductionEnvironment } from 'src/config/config'
import { createSupabaseServerClient } from 'src/repository/supabase.server'

import type { LoaderFunctionArgs } from '@remix-run/node'
import type { CategorizableContentTypes, DBCategory } from 'oa-shared'

const cache = new Keyv<DBCategory[]>({ ttl: 3600000 }) // ttl: 60 minutes

const filterByType = (
categories: DBCategory[],
type: CategorizableContentTypes,
) => {
return categories.filter((category) => category.type === type)
}

export async function loader({ request, params }: LoaderFunctionArgs) {
const type = params.type as CategorizableContentTypes
const { client, headers } = createSupabaseServerClient(request)

if (!params.type) {
if (!type) {
return Response.json(
{},
{ headers, status: 400, statusText: 'type is required' },
)
}

const categoriesResult = await client
const cachedCategories = await cache.get('categories')

if (cachedCategories && isProductionEnvironment()) {
return Response.json(
{ categories: filterByType(cachedCategories, type) },
{ headers, status: 200 },
)
}

const { data } = await client
.from('categories')
.select('id,name,created_at')
.select('id,name,created_at,type')
.eq('type', params.type)

const categories = categoriesResult.data || []
if (data && data.length > 0) {
cache.set('categories', data, 3600000)
}

const categories = filterByType(data as DBCategory[], type) || []

return Response.json(categories, { headers, status: 200 })
}
37 changes: 0 additions & 37 deletions src/routes/api.questions.categories.ts

This file was deleted.

0 comments on commit fa7147e

Please sign in to comment.