File size: 2,657 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
86
87
88
89
90
91
92
93
import { hatcheryApplications, HatcheryApplication } from "@/data/dummyData";

const LOCAL_KEY = "hatchery_local_applications";

function readLocal(): HatcheryApplication[] {
  if (typeof window === "undefined") return [];
  try {
    const raw = localStorage.getItem(LOCAL_KEY);
    if (!raw) return [];
    const parsed = JSON.parse(raw);
    if (!Array.isArray(parsed)) return [];
    return parsed as HatcheryApplication[];
  } catch {
    return [];
  }
}

function writeLocal(apps: HatcheryApplication[]) {
  if (typeof window === "undefined") return;
  try {
    localStorage.setItem(LOCAL_KEY, JSON.stringify(apps));
  } catch {
    // ignore
  }
}

export function getUserHatcheryApplications(userId?: string | null): HatcheryApplication[] {
  if (!userId) return [];
  const base = hatcheryApplications.filter((app) => app.applicantId === userId);
  const local = readLocal().filter((app) => app.applicantId === userId);
  return [...base, ...local].sort(
    (a, b) => new Date(b.submittedDate).getTime() - new Date(a.submittedDate).getTime(),
  );
}

export function getHatcheryApplicationById(id: string): HatcheryApplication | undefined {
  const base = hatcheryApplications.find((app) => app.id === id);
  if (base) return base;
  const local = readLocal().find((app) => app.id === id);
  return local;
}

export interface NewHatcheryApplicationInput {
  hatcheryName: string;
  ownerName: string;
  species: string;
  capacity: string;
  state: string;
  district: string;
  address: string;
  email: string;
  phone: string;
}

export function createHatcheryApplicationForUser(

  userId: string,

  data: NewHatcheryApplicationInput,

): HatcheryApplication {
  const now = new Date();
  const id = `HL${now.getTime()}`;
  const applicationNo = `SHAP/H/${now.getFullYear()}/${now.getTime().toString().slice(-4)}`;

  const app: HatcheryApplication = {
    id,
    applicationNo,
    hatcheryName: data.hatcheryName,
    ownerName: data.ownerName,
    applicantId: userId,
    email: data.email,
    phone: data.phone,
    state: data.state,
    district: data.district,
    address: data.address,
    species: data.species,
    capacity: data.capacity,
    status: "submitted",
    currentStage: "submission",
    submittedDate: now.toISOString().slice(0, 10),
    assignedFOI: undefined,
    assignedFO: undefined,
    assignedCCOfficer: undefined,
    assignedAuditor: undefined,
    paymentStatus: "pending",
    paymentAmount: 0,
    audits: [],
    certificates: [],
  };

  const all = readLocal();
  writeLocal([...all, app]);
  return app;
}