File size: 2,730 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
94
95
96
97
98
99
100
101
102
import { farmApplications, FarmApplication, User } from "@/data/dummyData";

const LOCAL_KEY = "farm_local_applications";

function readLocal(): FarmApplication[] {
  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 FarmApplication[];
  } catch {
    return [];
  }
}

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

export function getUserFarmApplications(user?: User | null): FarmApplication[] {
  if (!user) return [];
  const base = farmApplications.filter(
    (app) => app.email.toLowerCase() === user.email.toLowerCase(),
  );
  const local = readLocal().filter(
    (app) => app.email.toLowerCase() === user.email.toLowerCase(),
  );
  return [...base, ...local].sort(
    (a, b) => new Date(b.submittedDate).getTime() - new Date(a.submittedDate).getTime(),
  );
}

export function getFarmApplicationById(id: string): FarmApplication | undefined {
  const base = farmApplications.find((app) => app.id === id);
  if (base) return base;
  const local = readLocal().find((app) => app.id === id);
  return local;
}

export interface NewFarmApplicationInput {
  farmName: string;
  farmerName: string;
  waterArea: string;
  species: string;
  state: string;
  district: string;
  address: string;
  enrollmentId: string;
  email: string;
  phone: string;
}

export function createFarmApplicationForUser(

  user: User,

  data: NewFarmApplicationInput,

): FarmApplication {
  const now = new Date();
  const id = `FL${now.getTime()}`;
  const applicationNo = `SHAP/F/${now.getFullYear()}/${now

    .getTime()

    .toString()

    .slice(-4)}`;

  const app: FarmApplication = {
    id,
    applicationNo,
    farmName: data.farmName,
    farmerName: data.farmerName,
    enrollmentId: data.enrollmentId,
    email: user.email,
    phone: data.phone,
    state: data.state,
    district: data.district,
    address: data.address,
    waterArea: data.waterArea,
    species: data.species,
    status: "submitted",
    currentStage: "submission",
    submittedDate: now.toISOString().slice(0, 10),
    assignedFOI: undefined,
    assignedFO: undefined,
    assignedCCOfficer: undefined,
    assignedAuditor: undefined,
    paymentStatus: "pending",
    paymentAmount: 0,
    audits: [],
    certificates: [],
    cropDetails: [],
  };

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