63 lines
2.1 KiB
TypeScript
63 lines
2.1 KiB
TypeScript
import type { H3Event } from 'h3'
|
|
|
|
interface SelectValue {
|
|
value: string
|
|
}
|
|
|
|
export interface PackerRow {
|
|
id: number
|
|
item: string | null
|
|
category: SelectValue | null
|
|
sharable: boolean
|
|
every_n_days?: string | null
|
|
weight_g?: string | null
|
|
season?: SelectValue[] | null
|
|
activity?: SelectValue[] | null
|
|
/** A single select; a multiple select works too */
|
|
weather?: SelectValue | SelectValue[] | null
|
|
}
|
|
|
|
interface PackerField {
|
|
name: string
|
|
select_options?: SelectValue[]
|
|
}
|
|
|
|
export function packerTable(event: H3Event): string {
|
|
const { baserowPackerTableId } = useRuntimeConfig(event)
|
|
if (!baserowPackerTableId) {
|
|
throw createError({ statusCode: 500, statusMessage: 'NUXT_BASEROW_PACKER_TABLE_ID ist nicht gesetzt' })
|
|
}
|
|
return baserowPackerTableId
|
|
}
|
|
|
|
/** Baserow sends numbers as strings; empty, zero and negative mean "not set" */
|
|
function positive(value: string | null | undefined): number | null {
|
|
const number = Number(value)
|
|
return value && number > 0 ? number : null
|
|
}
|
|
|
|
export function toPackerItem(row: PackerRow): PackerItem | null {
|
|
const name = (row.item ?? '').trim()
|
|
if (!name) return null
|
|
return {
|
|
id: row.id,
|
|
name,
|
|
category: row.category?.value ?? 'Andere',
|
|
sharable: !!row.sharable,
|
|
everyNDays: positive(row.every_n_days),
|
|
weightG: positive(row.weight_g),
|
|
seasons: (row.season ?? [])
|
|
.map(option => option.value as PackerSeason)
|
|
.filter(season => PACKER_SEASONS.includes(season)),
|
|
activities: (row.activity ?? []).map(option => option.value),
|
|
weathers: [row.weather ?? []].flat().map(option => option.value)
|
|
}
|
|
}
|
|
|
|
/** The options of the `activity` and `weather` fields, in the order set in Baserow */
|
|
export async function listPackerOptions(event: H3Event): Promise<Pick<PackerData, 'activities' | 'weathers'>> {
|
|
const fields = await listFields<PackerField>(event, packerTable(event))
|
|
const options = (name: string) => (fields.find(field => field.name === name)?.select_options ?? []).map(option => option.value)
|
|
return { activities: options('activity'), weathers: options('weather') }
|
|
}
|