-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrenderStructureUsagersExcel.ts
More file actions
70 lines (61 loc) · 1.96 KB
/
renderStructureUsagersExcel.ts
File metadata and controls
70 lines (61 loc) · 1.96 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
import { StructureCommon } from "@domifa/common";
import * as XLSX from "xlsx";
import { renderStructureUsagersRows } from "./renderStructureUsagersRows";
import { StructureUsagerExport } from "./StructureUsagerExport.type";
import { StructureCustomDocTags } from "../../../_common/model";
import { isValid, parse } from "date-fns";
const applyDateFormat = (
worksheet: StructureCustomDocTags[],
elements: Array<keyof StructureCustomDocTags>
): void => {
worksheet.forEach((ws: StructureCustomDocTags) => {
elements.forEach((element: keyof StructureCustomDocTags) => {
if (ws[element]) {
const value = ws[element] as string;
ws[element] = isValid(parse(value, "dd/MM/yyyy", new Date()))
? parse(value, "dd/MM/yyyy", new Date())
: value;
}
});
});
};
export const renderStructureUsagersExcel = (
usagers: StructureUsagerExport[],
structure: StructureCommon
): Buffer => {
const { firstSheetUsagers, secondSheetEntretiens } =
renderStructureUsagersRows(usagers, structure);
const wb: XLSX.WorkBook = XLSX.utils.book_new();
applyDateFormat(firstSheetUsagers, [
"USAGER_DATE_NAISSANCE",
"DATE_RADIATION",
"DATE_REFUS",
"DATE_DEBUT_DOM",
"DATE_FIN_DOM",
"DATE_PREMIERE_DOM",
"DATE_DERNIER_PASSAGE",
]);
// Appliquer le format de date aux colonnes de dates dans wsEntretiens
applyDateFormat(secondSheetEntretiens, ["USAGER_DATE_NAISSANCE"]);
const wsUsagers: XLSX.WorkSheet = XLSX.utils.json_to_sheet(
firstSheetUsagers,
{
skipHeader: true,
cellDates: true,
dateNF: "DD/MM/YYYY",
}
);
const wsEntretiens: XLSX.WorkSheet = XLSX.utils.json_to_sheet(
secondSheetEntretiens,
{
skipHeader: true,
}
);
XLSX.utils.book_append_sheet(wb, wsUsagers, "Liste des domiciliés");
XLSX.utils.book_append_sheet(wb, wsEntretiens, "Entretiens");
return XLSX.writeXLSX(wb, {
bookType: "xlsx",
compression: true,
type: "buffer",
});
};