Repository navigation
Expand file tree
/
Copy pathserialize.ts
More file actions
111 lines (107 loc) · 3.18 KB
/
Copy pathserialize.ts
File metadata and controls
111 lines (107 loc) · 3.18 KB
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
103
104
105
106
107
108
109
110
111
// Shared server-side scheme serializer
import type { Scheme, SchemeEligibilityRule, SourceEvidence, GovernmentSource } from "@prisma/client";
export type SchemeWithRelations = Scheme & {
eligibilityRules: SchemeEligibilityRule[];
evidence?: SourceEvidence[];
source?: GovernmentSource | null;
};
export type SchemeBaseSerialized = ReturnType<typeof baseFields>;
export type SchemeDetailSerialized = SchemeBaseSerialized & {
description: string;
applicationWindow: string | null;
applicationDeadline: string | null;
documents: string[];
process: string[];
keywords: string;
rules: Array<{
attribute: string;
operator: string;
value: string;
required: boolean;
label: string;
labelHi: string | null;
ruleGroup: string | null;
}>;
evidence: Array<{
sourceUrl: string;
excerpt: string;
evidenceType: string;
note: string | null;
retrievedAt: Date;
}>;
sourceMeta: {
name: string;
domain: string;
department: string;
lastCheckedAt: Date | null;
lastCheckStatus: string | null;
lastCheckTitle: string | null;
} | null;
};
function baseFields(s: SchemeWithRelations) {
return {
id: s.id,
slug: s.slug,
name: s.name,
nameHi: s.nameHi,
shortDescription: s.shortDescription,
shortDescriptionHi: s.shortDescriptionHi,
category: s.category,
ministry: s.ministry,
department: s.department,
jurisdiction: s.jurisdiction,
state: s.state,
beneficiaryType: s.beneficiaryType,
benefits: s.benefits,
benefitAmount: s.benefitAmount,
applicationUrl: s.applicationUrl,
infoUrl: s.infoUrl,
verificationStatus: s.verificationStatus,
extractionMethod: s.extractionMethod,
confidence: s.confidence,
dataCompiledAt: s.dataCompiledAt,
lastLiveCheckedAt: s.lastLiveCheckedAt,
ruleCount: s.eligibilityRules.length,
sourceName: s.source?.name ?? null,
sourceDomain: s.source?.domain ?? null,
};
}
export function serializeScheme(s: SchemeWithRelations, withDetail = false): SchemeBaseSerialized | SchemeDetailSerialized {
const base = baseFields(s);
if (!withDetail) return base;
return {
...base,
description: s.description,
applicationWindow: s.applicationWindow,
applicationDeadline: s.applicationDeadline,
documents: s.documentsJson ? (JSON.parse(s.documentsJson) as string[]) : [],
process: s.processJson ? (JSON.parse(s.processJson) as string[]) : [],
keywords: s.keywords,
rules: s.eligibilityRules.map((r) => ({
attribute: r.attribute,
operator: r.operator,
value: r.value,
required: r.required,
label: r.label,
labelHi: r.labelHi,
ruleGroup: r.ruleGroup,
})),
evidence: (s.evidence ?? []).map((e) => ({
sourceUrl: e.sourceUrl,
excerpt: e.excerpt,
evidenceType: e.evidenceType,
note: e.note,
retrievedAt: e.retrievedAt,
})),
sourceMeta: s.source
? {
name: s.source.name,
domain: s.source.domain,
department: s.source.department,
lastCheckedAt: s.source.lastCheckedAt,
lastCheckStatus: s.source.lastCheckStatus,
lastCheckTitle: s.source.lastCheckTitle,
}
: null,
};
}