File size: 2,234 Bytes
f305a41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { ApplicationStatus, WorkflowStage } from "@/data/dummyData";

const LOCAL_KEY = "certification_overrides";

export type CertificationStage = WorkflowStage | "cc_review" | "cc_recommended" | "jd_recommended";

export interface CertificationOverride {
  currentStage?: CertificationStage;
  status?: ApplicationStatus;
}

export type EntityType = "hatchery" | "farm";

interface OverridesMap {
  [compositeId: string]: CertificationOverride;
}

function getKey(entityType: EntityType, id: string) {
  return `${entityType}:${id}`;
}

function readOverrides(): OverridesMap {
  if (typeof window === "undefined") return {};
  try {
    const raw = localStorage.getItem(LOCAL_KEY);
    if (!raw) return {};
    const parsed = JSON.parse(raw);
    if (!parsed || typeof parsed !== "object") return {};
    return parsed as OverridesMap;
  } catch {
    return {};
  }
}

function writeOverrides(map: OverridesMap) {
  if (typeof window === "undefined") return;
  try {
    localStorage.setItem(LOCAL_KEY, JSON.stringify(map));
  } catch {
    // ignore
  }
}

export function getCertificationOverride(

  entityType: EntityType,

  id: string,

): CertificationOverride | undefined {
  const map = readOverrides();
  return map[getKey(entityType, id)];
}

export function updateCertificationOverride(

  entityType: EntityType,

  id: string,

  patch: CertificationOverride,

): CertificationOverride {
  const map = readOverrides();
  const key = getKey(entityType, id);
  const existing = map[key] || {};
  const merged: CertificationOverride = { ...existing, ...patch };
  map[key] = merged;
  writeOverrides(map);
  return merged;
}

interface HasCertificationFields {
  id: string;
  entityType: EntityType;
  currentStage: string;
  status: string;
}

export function applyCertificationOverrides<T extends HasCertificationFields>(rows: T[]): T[] {
  const map = readOverrides();
  return rows.map((row) => {
    const override = map[getKey(row.entityType, row.id)];
    if (!override) return row;
    return {
      ...row,
      currentStage: override.currentStage ?? row.currentStage,
      status: override.status ?? row.status,
    };
  });
}