All files / lib taxonomies.ts

0% Statements 0/63
0% Branches 0/1
0% Functions 0/1
0% Lines 0/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                                                                                                                               
import { prisma } from './db';
import { cached } from './cache';

// Read-only repositories for reference data (Branch, Category+Subtype, IssueType).
// CRUD UI for these lives in the future Settings module (MASTER-only).
// Cache TTL is long because these change rarely.

const REF_TTL = 600;

export type BranchLite = { id: string; code: string; name: string };

export function listActiveBranches() {
  return cached(
    'taxonomy:branches:active',
    () =>
      prisma.branch.findMany({
        where: { active: true },
        select: { id: true, code: true, name: true },
        orderBy: { code: 'asc' },
      }),
    { ttlSeconds: REF_TTL },
  );
}

export type CategoryWithSubtypes = {
  id: string;
  code: string;
  name: string;
  icon: string | null;
  subtypes: { id: string; code: string; name: string }[];
};

export function listCategoriesWithSubtypes() {
  return cached(
    'taxonomy:categories:tree',
    () =>
      prisma.productCategory.findMany({
        where: { active: true },
        include: {
          subtypes: {
            where: { active: true },
            select: { id: true, code: true, name: true },
            orderBy: { code: 'asc' },
          },
        },
        orderBy: { code: 'asc' },
      }),
    { ttlSeconds: REF_TTL },
  );
}

export function listIssueTypes() {
  return cached(
    'taxonomy:issue-types',
    () =>
      prisma.issueType.findMany({
        where: { active: true },
        select: { id: true, code: true, name: true },
        orderBy: { code: 'asc' },
      }),
    { ttlSeconds: REF_TTL },
  );
}