Barazo AppView backend barazo.forum
at main 42 lines 1.6 kB view raw
1// --------------------------------------------------------------------------- 2// Maturity rating helpers (shared between categories and admin-settings) 3// --------------------------------------------------------------------------- 4 5import type { MaturityRating as ZodMaturityRating } from '../validation/categories.js' 6 7/** Valid maturity rating values. Derived from Zod schema as single source of truth. */ 8export type MaturityRating = ZodMaturityRating 9 10/** Numeric order of maturity ratings for comparison. */ 11export const MATURITY_ORDER: Record<MaturityRating, number> = { 12 safe: 0, 13 mature: 1, 14 adult: 2, 15} as const 16 17/** 18 * Check if maturity rating `a` is lower than `b` in the hierarchy. 19 * Hierarchy: safe < mature < adult 20 */ 21export function isMaturityLowerThan(a: MaturityRating, b: MaturityRating): boolean { 22 return MATURITY_ORDER[a] < MATURITY_ORDER[b] 23} 24 25/** 26 * Check if maturity rating `a` is at most `b` in the hierarchy (a <= b). 27 * Used for content visibility: content rating must be at most user's max allowed. 28 */ 29export function isMaturityAtMost(a: MaturityRating, b: MaturityRating): boolean { 30 return MATURITY_ORDER[a] <= MATURITY_ORDER[b] 31} 32 33/** 34 * Return all maturity ratings that are at most `maxLevel` in the hierarchy. 35 * Useful for building SQL IN clauses. 36 */ 37export function ratingsAtMost(maxLevel: MaturityRating): MaturityRating[] { 38 const max = MATURITY_ORDER[maxLevel] 39 return (Object.entries(MATURITY_ORDER) as Array<[MaturityRating, number]>) 40 .filter(([, order]) => order <= max) 41 .map(([rating]) => rating) 42}