Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Dec 11, 2024
1 parent b09af83 commit dc28990
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
import {
DEFAULT_PAGINATION_SIZE,
LogSources,
Workspace,
} from '@latitude-data/core/browser'
import { Workspace } from '@latitude-data/core/browser'
import { computeDocumentLogsQuery } from '@latitude-data/core/services/documentLogs/computeDocumentLogs'
import { computeDocumentLogsWithMetadataQuery } from '@latitude-data/core/services/documentLogs/computeDocumentLogsWithMetadata'
import { authHandler } from '$/middlewares/authHandler'
import { errorHandler } from '$/middlewares/errorHandler'
import { NextRequest, NextResponse } from 'next/server'
import { decodeParameters } from '$/services/helpers'

function parsePage(page: string | null): string {
if (!page) return '1'

const parsed = parseInt(page, 10)
if (isNaN(parsed)) return '1'

return parsed < 1 ? '1' : parsed.toString()
}
import { parseApiDocumentLogParams } from '@latitude-data/core/services/documentLogs/index'

export const GET = errorHandler(
authHandler(
Expand All @@ -36,41 +23,22 @@ export const GET = errorHandler(
) => {
const { documentUuid } = params
const searchParams = req.nextUrl.searchParams
const excludeErrors = searchParams.get('excludeErrors') === 'true'

const page = parsePage(searchParams.get('page'))
const pageSize =
searchParams.get('pageSize') ?? String(DEFAULT_PAGINATION_SIZE)

const { commitIds: _commitIds, logSources: _logSources } =
decodeParameters(req.nextUrl.search)
const commitIds = (Array.isArray(_commitIds) ? _commitIds : [_commitIds])
.filter((c) => c !== undefined)
.map(Number)

const logSources = (
Array.isArray(_logSources) ? _logSources : [_logSources]
).filter((l) => l !== undefined) as LogSources[]

const filterOptions = {
commitIds,
logSources,
}
const queryParams = parseApiDocumentLogParams({ searchParams })

if (!filterOptions.commitIds.length || !filterOptions.logSources.length) {
if (queryParams.isEmptyResponse) {
return NextResponse.json([], { status: 200 })
}

const buildQueryFn = excludeErrors
const buildQueryFn = queryParams.excludeErrors
? computeDocumentLogsQuery
: computeDocumentLogsWithMetadataQuery

const rows = await buildQueryFn({
workspaceId: workspace.id,
documentUuid,
filterOptions,
page,
pageSize,
filterOptions: queryParams.filterOptions,
page: queryParams.page,
pageSize: queryParams.pageSize,
})

return NextResponse.json(rows, { status: 200 })
Expand Down
18 changes: 0 additions & 18 deletions apps/web/src/services/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,3 @@ export function addParameters(

return `${path}?${params.join('&')}`
}

export function decodeParameters(route: string): Record<string, UriParam> {
const [_, query] = route.split('?')
if (!query) return {}

const params = query.split('&')
return params.reduce((acc: Record<string, UriParam>, param: string) => {
const [key, value] = param.split('=')
if (!key || !value) return acc
if (value.includes(',')) {
return {
...acc,
[key]: value.split(',').map(decodeURIComponent),
}
}
return { ...acc, [key]: decodeURIComponent(value) }
}, {})
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './parseLogFilterParams'
export * from './parseApiLogFilterParams'
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { DEFAULT_PAGINATION_SIZE, LogSources } from '../../../constants'
import { parseSafeCreatedAtRange } from './parseLogFilterParams'

function parsePage(page: string | null): string {
if (!page) return '1'

const parsed = parseInt(page, 10)
if (isNaN(parsed)) return '1'

return parsed < 1 ? '1' : parsed.toString()
}

function parseCommitIds(commitIds: string | undefined) {
return commitIds?.split(',')?.map?.(Number) ?? []
}

function parseLogSources(logSources: string | undefined) {
return (logSources?.split?.(',') as LogSources[]) ?? []
}

export function parseApiDocumentLogParams({
searchParams,
}: {
searchParams: URLSearchParams
}) {
const params = Object.fromEntries(searchParams.entries())
const commitIds = parseCommitIds(params.commitIds)
const logSources = parseLogSources(params.logSources)
const excludeErrors = searchParams.get('excludeErrors') === 'true'

const filterOptions = {
commitIds,
logSources,
createdAt: parseSafeCreatedAtRange(params.createdAt),
}

const isEmptyResponse =
filterOptions.commitIds.length === 0 ||
filterOptions.logSources.length === 0

return {
excludeErrors,
isEmptyResponse,
filterOptions,
page: parsePage(searchParams.get('page')),
pageSize: searchParams.get('pageSize') ?? String(DEFAULT_PAGINATION_SIZE),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ function parseLogSourcesSafe(origins: string[] | string | undefined) {
return logSources.filter((s) => LOG_SOURCES.includes(s as LogSources))
}

function parseSafeCreatedAtRange(createdAt: string[] | string | undefined) {
export function parseSafeCreatedAtRange(
createdAt: string[] | string | undefined,
) {
const [from, to] = createdAt?.toString()?.split(',') ?? []
if (!from || !to) return undefined

Expand Down

0 comments on commit dc28990

Please sign in to comment.