@appcorp/fusion-storybook 0.2.80 → 0.2.82
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/base-modules/attendance/more-actions.d.ts +1 -8
- package/base-modules/attendance/more-actions.js +250 -56
- package/base-modules/attendance/page.d.ts +0 -3
- package/base-modules/attendance/page.js +3 -11
- package/base-modules/expense/more-actions.js +228 -14
- package/base-modules/family/more-actions.js +252 -2
- package/base-modules/family-member/more-actions.js +258 -2
- package/base-modules/student-profile/more-actions.js +5 -3
- package/constants.d.ts +8 -0
- package/constants.js +8 -0
- package/package.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -1,8 +1 @@
|
|
|
1
|
-
|
|
2
|
-
interface AttendanceMoreActionsProps {
|
|
3
|
-
labelAttendanceStatus: string;
|
|
4
|
-
labelRemarksInfo: string;
|
|
5
|
-
labelRemarksPlaceholder: string;
|
|
6
|
-
}
|
|
7
|
-
export declare const AttendanceMoreActions: FC<AttendanceMoreActionsProps>;
|
|
8
|
-
export {};
|
|
1
|
+
export declare const AttendanceMoreActions: () => import("react").JSX.Element;
|
|
@@ -1,63 +1,257 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
3
|
+
import { downloadFromUrl } from "@react-pakistan/util-functions";
|
|
4
|
+
import { showErrorToast, showSuccessToast, showInfoToast, } from "@appcorp/shadcn/lib/toast-utils";
|
|
5
|
+
import { getCachedWorkspaceSync } from "../workspace/cache";
|
|
6
|
+
import converter from "json-2-csv";
|
|
7
|
+
import { Timeline } from "../../components/timeline";
|
|
7
8
|
import { useTranslations } from "next-intl";
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const
|
|
9
|
+
import { pageLimit } from "./constants";
|
|
10
|
+
import { ATTENDANCE_API_ROUTES } from "../../constants";
|
|
11
|
+
import { ATTENDANCE_ACTION_TYPES, useAttendanceContext } from "./context";
|
|
12
|
+
import { useRef, useEffect, useCallback } from "react";
|
|
13
|
+
const workspace = getCachedWorkspaceSync();
|
|
14
|
+
const POLL_INTERVAL_MS = 2000;
|
|
15
|
+
const POLL_TIMEOUT_MS = 300000;
|
|
16
|
+
const handleGetAllRecords = async (schoolId, pageLimit) => {
|
|
17
|
+
const response = await fetch(`${ATTENDANCE_API_ROUTES.UNIT}?pageLimit=${pageLimit}¤tPage=1&schoolId=${schoolId}`);
|
|
18
|
+
const result = await response.json();
|
|
19
|
+
const csv = await converter.json2csv(result.items || [], {});
|
|
20
|
+
const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
|
|
21
|
+
await downloadFromUrl(blob, "attendance.csv");
|
|
22
|
+
};
|
|
23
|
+
async function submitBulkJob(csvData, method, signal) {
|
|
24
|
+
var _a;
|
|
25
|
+
const schoolId = ((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id) || "";
|
|
26
|
+
if (!schoolId)
|
|
27
|
+
throw new Error("School ID not found");
|
|
28
|
+
const res = await fetch(ATTENDANCE_API_ROUTES.BULK, {
|
|
29
|
+
method,
|
|
30
|
+
headers: {
|
|
31
|
+
"Content-Type": "application/json",
|
|
32
|
+
},
|
|
33
|
+
body: JSON.stringify({ schoolId, csvData }),
|
|
34
|
+
signal,
|
|
35
|
+
});
|
|
36
|
+
if (!res.ok) {
|
|
37
|
+
const errorData = (await res.json().catch(() => ({})));
|
|
38
|
+
throw new Error(errorData.error || `Bulk operation failed with status ${res.status}`);
|
|
39
|
+
}
|
|
40
|
+
const data = (await res.json());
|
|
41
|
+
return data.jobId;
|
|
42
|
+
}
|
|
43
|
+
async function pollBulkJob(jobId, signal, onProgress) {
|
|
44
|
+
const startTime = Date.now();
|
|
45
|
+
let lastProgressToast = 0;
|
|
46
|
+
while (true) {
|
|
47
|
+
if (signal.aborted)
|
|
48
|
+
throw new Error("Polling cancelled");
|
|
49
|
+
if (Date.now() - startTime > POLL_TIMEOUT_MS) {
|
|
50
|
+
throw new Error("Bulk operation timed out");
|
|
51
|
+
}
|
|
52
|
+
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
|
|
53
|
+
if (signal.aborted)
|
|
54
|
+
throw new Error("Polling cancelled");
|
|
55
|
+
const res = await fetch(ATTENDANCE_API_ROUTES.BULK_STATUS(jobId), {
|
|
56
|
+
signal,
|
|
57
|
+
});
|
|
58
|
+
if (!res.ok)
|
|
59
|
+
continue;
|
|
60
|
+
const data = (await res.json());
|
|
61
|
+
if (data.status === "completed" || data.status === "failed") {
|
|
62
|
+
return data;
|
|
63
|
+
}
|
|
64
|
+
if (data.status === "running" && Date.now() - lastProgressToast > 10000) {
|
|
65
|
+
lastProgressToast = Date.now();
|
|
66
|
+
onProgress === null || onProgress === void 0 ? void 0 : onProgress(data.processed, data.total);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function formatErrorSummary(t, errors) {
|
|
71
|
+
if (!(errors === null || errors === void 0 ? void 0 : errors.length))
|
|
72
|
+
return "";
|
|
73
|
+
const lines = errors
|
|
74
|
+
.slice(0, 5)
|
|
75
|
+
.map((e) => t("messagesBulkRowError", { row: e.row, error: e.error }));
|
|
76
|
+
const remaining = errors.length - 5;
|
|
77
|
+
if (remaining > 0)
|
|
78
|
+
lines.push(t("messagesBulkMoreRows", { count: remaining }));
|
|
79
|
+
return lines.join("\n");
|
|
80
|
+
}
|
|
81
|
+
export const AttendanceMoreActions = () => {
|
|
17
82
|
const t = useTranslations("attendance");
|
|
18
|
-
const {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
83
|
+
const { dispatch } = useAttendanceContext();
|
|
84
|
+
const abortRef = useRef(null);
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
return () => {
|
|
87
|
+
var _a;
|
|
88
|
+
(_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort();
|
|
89
|
+
};
|
|
90
|
+
}, []);
|
|
91
|
+
const handleBulkFlow = useCallback(async (files, method) => {
|
|
92
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
93
|
+
const closeDrawer = () => {
|
|
94
|
+
dispatch({
|
|
95
|
+
type: ATTENDANCE_ACTION_TYPES.SET_DRAWER,
|
|
96
|
+
payload: { drawer: null },
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
(_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort();
|
|
100
|
+
const controller = new AbortController();
|
|
101
|
+
abortRef.current = controller;
|
|
102
|
+
const { signal } = controller;
|
|
103
|
+
const label = method === "POST" ? "create" : "update";
|
|
104
|
+
const file = files[0];
|
|
105
|
+
const text = await file.text();
|
|
106
|
+
const records = converter.csv2json(text);
|
|
107
|
+
if (!Array.isArray(records) || records.length === 0) {
|
|
108
|
+
showErrorToast(t("messagesBulkCsvEmpty"));
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const validationErrors = [];
|
|
112
|
+
for (let i = 0; i < records.length; i++) {
|
|
113
|
+
const row = records[i];
|
|
114
|
+
const msgs = [];
|
|
115
|
+
if (method === "POST") {
|
|
116
|
+
if (!((_b = row.date) === null || _b === void 0 ? void 0 : _b.trim()))
|
|
117
|
+
msgs.push(t("validationRequiredDate"));
|
|
118
|
+
if (!((_c = row.status) === null || _c === void 0 ? void 0 : _c.trim()))
|
|
119
|
+
msgs.push(t("validationRequiredStatus"));
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
if (!((_d = row.id) === null || _d === void 0 ? void 0 : _d.trim()))
|
|
123
|
+
msgs.push(t("validationRequiredIdForUpdate"));
|
|
124
|
+
}
|
|
125
|
+
if (msgs.length > 0) {
|
|
126
|
+
validationErrors.push({ row: i + 1, messages: msgs });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (validationErrors.length > 0) {
|
|
130
|
+
const summary = validationErrors
|
|
131
|
+
.slice(0, 5)
|
|
132
|
+
.map((e) => t("messagesBulkRowError", {
|
|
133
|
+
row: e.row,
|
|
134
|
+
error: e.messages.join("; "),
|
|
135
|
+
}))
|
|
136
|
+
.join("\n");
|
|
137
|
+
showErrorToast(t("messagesBulkValidationFailed", {
|
|
138
|
+
count: validationErrors.length,
|
|
139
|
+
errors: summary,
|
|
140
|
+
}));
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
showInfoToast(t("messagesBulkJobQueued", { action: label, count: records.length }));
|
|
145
|
+
let jobId;
|
|
146
|
+
try {
|
|
147
|
+
jobId = await submitBulkJob(text, method, signal);
|
|
148
|
+
}
|
|
149
|
+
catch (submitError) {
|
|
150
|
+
showErrorToast(t("messagesBulkJobSubmitFailed", {
|
|
151
|
+
action: label,
|
|
152
|
+
error: submitError instanceof Error
|
|
153
|
+
? submitError.message
|
|
154
|
+
: t("unknownError"),
|
|
155
|
+
}));
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const status = await pollBulkJob(jobId, signal, (processed, total) => {
|
|
159
|
+
showInfoToast(t("messagesBulkProgress", { processed, total }));
|
|
37
160
|
});
|
|
161
|
+
if (signal.aborted)
|
|
162
|
+
return;
|
|
163
|
+
if (status.status === "completed") {
|
|
164
|
+
const r = status.results;
|
|
165
|
+
if (r && ((_e = r.errors) === null || _e === void 0 ? void 0 : _e.length) > 0) {
|
|
166
|
+
const summary = formatErrorSummary(t, r.errors);
|
|
167
|
+
showSuccessToast(t("messagesBulkResults", {
|
|
168
|
+
created: r.created,
|
|
169
|
+
updated: r.updated,
|
|
170
|
+
skipped: r.skipped,
|
|
171
|
+
}) +
|
|
172
|
+
"\n" +
|
|
173
|
+
summary);
|
|
174
|
+
}
|
|
175
|
+
else if (r) {
|
|
176
|
+
showSuccessToast(t("messagesBulkResults", {
|
|
177
|
+
created: r.created,
|
|
178
|
+
updated: r.updated,
|
|
179
|
+
skipped: r.skipped,
|
|
180
|
+
}));
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
showSuccessToast(t("messagesBulkSuccess"));
|
|
184
|
+
}
|
|
185
|
+
const schoolId = ((_f = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _f === void 0 ? void 0 : _f.id) || "";
|
|
186
|
+
fetch(`${ATTENDANCE_API_ROUTES.UNIT}?currentPage=1&pageLimit=${pageLimit}&schoolId=${schoolId}`, {
|
|
187
|
+
headers: {
|
|
188
|
+
"Content-Type": "application/json",
|
|
189
|
+
},
|
|
190
|
+
})
|
|
191
|
+
.then(async (res) => {
|
|
192
|
+
var _a, _b;
|
|
193
|
+
if (!res.ok)
|
|
194
|
+
return;
|
|
195
|
+
const data = await res.json();
|
|
196
|
+
dispatch({
|
|
197
|
+
type: ATTENDANCE_ACTION_TYPES.SET_ITEMS,
|
|
198
|
+
payload: {
|
|
199
|
+
items: (_a = data.items) !== null && _a !== void 0 ? _a : [],
|
|
200
|
+
count: (_b = data.count) !== null && _b !== void 0 ? _b : 0,
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
})
|
|
204
|
+
.catch(() => { });
|
|
205
|
+
closeDrawer();
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
const r = status.results;
|
|
209
|
+
const detail = ((_g = r === null || r === void 0 ? void 0 : r.errors) === null || _g === void 0 ? void 0 : _g.length)
|
|
210
|
+
? formatErrorSummary(t, r.errors)
|
|
211
|
+
: t("unknownError");
|
|
212
|
+
showErrorToast(t("messagesBulkFailed", { action: label }) + "\n" + detail);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
if (error.message === "Polling cancelled")
|
|
217
|
+
return;
|
|
218
|
+
showErrorToast(t("messagesBulkFailedDetail", {
|
|
219
|
+
action: label,
|
|
220
|
+
error: error instanceof Error ? error.message : t("unknownError"),
|
|
221
|
+
}));
|
|
222
|
+
}
|
|
223
|
+
}, [dispatch, t]);
|
|
224
|
+
const handleBulkCreate = useCallback((files) => handleBulkFlow(files, "POST"), [handleBulkFlow]);
|
|
225
|
+
const handleBulkUpdate = useCallback((files) => handleBulkFlow(files, "PUT"), [handleBulkFlow]);
|
|
226
|
+
const create = [
|
|
227
|
+
{
|
|
228
|
+
id: "1",
|
|
229
|
+
title: t("downloadEmptyCsvTemplate"),
|
|
230
|
+
handleOnClick: async () => {
|
|
231
|
+
var _a;
|
|
232
|
+
await handleGetAllRecords(((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id) || "", 1);
|
|
233
|
+
},
|
|
38
234
|
},
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
} }) })] }), _jsx(EnhancedTextarea, { error: errors.remarks, id: "remarks", info: labelRemarksInfo, label: t("formRemarksLabel"), onChange: (e) => setRemarks((prev) => (Object.assign(Object.assign({}, prev), { [s]: e.target.value }))), placeholder: labelRemarksPlaceholder, rows: 0, value: remarks[s] })] }, s));
|
|
62
|
-
})] }));
|
|
235
|
+
{ id: "2", title: t("addYourDataToTheCsv") },
|
|
236
|
+
{
|
|
237
|
+
id: "3",
|
|
238
|
+
title: t("uploadTheCompletedCsvToTheSystem"),
|
|
239
|
+
},
|
|
240
|
+
];
|
|
241
|
+
const update = [
|
|
242
|
+
{
|
|
243
|
+
id: "1",
|
|
244
|
+
title: t("downloadPopulatedCsvTemplate"),
|
|
245
|
+
handleOnClick: async () => {
|
|
246
|
+
var _a;
|
|
247
|
+
await handleGetAllRecords(((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id) || "", 1000);
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
{ id: "2", title: t("updateYourDataInTheCsv") },
|
|
251
|
+
{
|
|
252
|
+
id: "3",
|
|
253
|
+
title: t("uploadTheCompletedCsvToTheSystem"),
|
|
254
|
+
},
|
|
255
|
+
];
|
|
256
|
+
return (_jsxs("div", { className: "space-y-4", children: [_jsx(Timeline, { events: create, heading: t("bulkCreate"), handleOnBulkCreate: handleBulkCreate }), _jsx(Timeline, { events: update, heading: t("bulkUpdate"), handleOnBulkCreate: handleBulkUpdate })] }));
|
|
63
257
|
};
|
|
@@ -24,13 +24,10 @@ interface Props {
|
|
|
24
24
|
drawerViewDescription: string;
|
|
25
25
|
drawerViewTitle: string;
|
|
26
26
|
labelActions: string;
|
|
27
|
-
labelAttendanceStatus: string;
|
|
28
27
|
labelDate: string;
|
|
29
28
|
labelEnabled: string;
|
|
30
29
|
labelId: string;
|
|
31
30
|
labelRemarks: string;
|
|
32
|
-
labelRemarksInfo: string;
|
|
33
|
-
labelRemarksPlaceholder: string;
|
|
34
31
|
labelStatus: string;
|
|
35
32
|
labelStudent: string;
|
|
36
33
|
saveLabel: string;
|
|
@@ -55,10 +55,10 @@ const tableBodyCols = [
|
|
|
55
55
|
// ============================================================================
|
|
56
56
|
// COMPONENT INSTANCES (memoized — no runtime deps)
|
|
57
57
|
// ============================================================================
|
|
58
|
-
const createComponentInstances = (
|
|
58
|
+
const createComponentInstances = () => ({
|
|
59
59
|
filter: _jsx(AttendanceFilter, {}),
|
|
60
60
|
form: _jsx(AttendanceForm, {}),
|
|
61
|
-
moreActions:
|
|
61
|
+
moreActions: _jsx(AttendanceMoreActions, {}),
|
|
62
62
|
view: _jsx(AttendanceView, {}),
|
|
63
63
|
});
|
|
64
64
|
const createAttendanceConfig = ({ cancelLabel, dispatch, drawerFilterDescription, drawerFilterTitle, drawerFormDescription, drawerFormTitle, drawerMoreActionsDescription, drawerMoreActionsTitle, drawerViewDescription, drawerViewTitle, labelActions, labelDate, labelEnabled, labelId, labelRemarks, labelStatus, labelStudent, saveLabel, searchPlaceholder, tableDescription, tableTitle, components, }) => {
|
|
@@ -101,15 +101,7 @@ const createAttendanceConfig = ({ cancelLabel, dispatch, drawerFilterDescription
|
|
|
101
101
|
const GenericAttendancePage = createGenericModulePage();
|
|
102
102
|
const AttendancePageInner = (props) => {
|
|
103
103
|
const context = useAttendanceModule();
|
|
104
|
-
const components = useMemo(() => createComponentInstances(
|
|
105
|
-
labelAttendanceStatus: props.labelAttendanceStatus,
|
|
106
|
-
labelRemarksInfo: props.labelRemarksInfo,
|
|
107
|
-
labelRemarksPlaceholder: props.labelRemarksPlaceholder,
|
|
108
|
-
}), [
|
|
109
|
-
props.labelAttendanceStatus,
|
|
110
|
-
props.labelRemarksInfo,
|
|
111
|
-
props.labelRemarksPlaceholder,
|
|
112
|
-
]);
|
|
104
|
+
const components = useMemo(() => createComponentInstances(), []);
|
|
113
105
|
const attendanceConfig = useMemo(() => createAttendanceConfig({
|
|
114
106
|
dispatch: context.dispatch,
|
|
115
107
|
cancelLabel: props.cancelLabel,
|
|
@@ -1,32 +1,243 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
* CSV bulk-create and bulk-update workflows using the Timeline pattern.
|
|
7
|
-
*/
|
|
8
|
-
import { API_METHODS, downloadFromUrl } from "@react-pakistan/util-functions";
|
|
3
|
+
import { downloadFromUrl } from "@react-pakistan/util-functions";
|
|
4
|
+
import { showErrorToast, showSuccessToast, showInfoToast, } from "@appcorp/shadcn/lib/toast-utils";
|
|
9
5
|
import { getCachedWorkspaceSync } from "../workspace/cache";
|
|
10
6
|
import converter from "json-2-csv";
|
|
11
7
|
import { Timeline } from "../../components/timeline";
|
|
12
8
|
import { useTranslations } from "next-intl";
|
|
9
|
+
import { pageLimit } from "./constants";
|
|
13
10
|
import { EXPENSE_API_ROUTES } from "../../constants";
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
import { EXPENSE_ACTION_TYPES, useExpenseContext } from "./context";
|
|
12
|
+
import { useRef, useEffect, useCallback } from "react";
|
|
13
|
+
const workspace = getCachedWorkspaceSync();
|
|
14
|
+
const POLL_INTERVAL_MS = 2000;
|
|
15
|
+
const POLL_TIMEOUT_MS = 300000;
|
|
16
|
+
const handleGetAllRecords = async (schoolId, pageLimit) => {
|
|
17
|
+
const response = await fetch(`${EXPENSE_API_ROUTES.UNIT}?pageLimit=${pageLimit}¤tPage=1&schoolId=${schoolId}`);
|
|
16
18
|
const result = await response.json();
|
|
17
19
|
const csv = await converter.json2csv(result.items || [], {});
|
|
18
20
|
const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
|
|
19
|
-
await downloadFromUrl(blob, "
|
|
21
|
+
await downloadFromUrl(blob, "expense.csv");
|
|
20
22
|
};
|
|
23
|
+
async function submitBulkJob(csvData, method, signal) {
|
|
24
|
+
var _a;
|
|
25
|
+
const schoolId = ((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id) || "";
|
|
26
|
+
if (!schoolId)
|
|
27
|
+
throw new Error("School ID not found");
|
|
28
|
+
const res = await fetch(EXPENSE_API_ROUTES.BULK, {
|
|
29
|
+
method,
|
|
30
|
+
headers: {
|
|
31
|
+
"Content-Type": "application/json",
|
|
32
|
+
},
|
|
33
|
+
body: JSON.stringify({ schoolId, csvData }),
|
|
34
|
+
signal,
|
|
35
|
+
});
|
|
36
|
+
if (!res.ok) {
|
|
37
|
+
const errorData = (await res.json().catch(() => ({})));
|
|
38
|
+
throw new Error(errorData.error || `Bulk operation failed with status ${res.status}`);
|
|
39
|
+
}
|
|
40
|
+
const data = (await res.json());
|
|
41
|
+
return data.jobId;
|
|
42
|
+
}
|
|
43
|
+
async function pollBulkJob(jobId, signal, onProgress) {
|
|
44
|
+
const startTime = Date.now();
|
|
45
|
+
let lastProgressToast = 0;
|
|
46
|
+
while (true) {
|
|
47
|
+
if (signal.aborted)
|
|
48
|
+
throw new Error("Polling cancelled");
|
|
49
|
+
if (Date.now() - startTime > POLL_TIMEOUT_MS) {
|
|
50
|
+
throw new Error("Bulk operation timed out");
|
|
51
|
+
}
|
|
52
|
+
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
|
|
53
|
+
if (signal.aborted)
|
|
54
|
+
throw new Error("Polling cancelled");
|
|
55
|
+
const res = await fetch(EXPENSE_API_ROUTES.BULK_STATUS(jobId), {
|
|
56
|
+
signal,
|
|
57
|
+
});
|
|
58
|
+
if (!res.ok)
|
|
59
|
+
continue;
|
|
60
|
+
const data = (await res.json());
|
|
61
|
+
if (data.status === "completed" || data.status === "failed") {
|
|
62
|
+
return data;
|
|
63
|
+
}
|
|
64
|
+
if (data.status === "running" && Date.now() - lastProgressToast > 10000) {
|
|
65
|
+
lastProgressToast = Date.now();
|
|
66
|
+
onProgress === null || onProgress === void 0 ? void 0 : onProgress(data.processed, data.total);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function formatErrorSummary(t, errors) {
|
|
71
|
+
if (!(errors === null || errors === void 0 ? void 0 : errors.length))
|
|
72
|
+
return "";
|
|
73
|
+
const lines = errors
|
|
74
|
+
.slice(0, 5)
|
|
75
|
+
.map((e) => t("messagesBulkRowError", { row: e.row, error: e.error }));
|
|
76
|
+
const remaining = errors.length - 5;
|
|
77
|
+
if (remaining > 0)
|
|
78
|
+
lines.push(t("messagesBulkMoreRows", { count: remaining }));
|
|
79
|
+
return lines.join("\n");
|
|
80
|
+
}
|
|
21
81
|
export const ExpenseMoreActions = () => {
|
|
22
|
-
const workspace = getCachedWorkspaceSync();
|
|
23
82
|
const t = useTranslations("expense");
|
|
83
|
+
const { dispatch } = useExpenseContext();
|
|
84
|
+
const abortRef = useRef(null);
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
return () => {
|
|
87
|
+
var _a;
|
|
88
|
+
(_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort();
|
|
89
|
+
};
|
|
90
|
+
}, []);
|
|
91
|
+
const handleBulkFlow = useCallback(async (files, method) => {
|
|
92
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
93
|
+
const closeDrawer = () => {
|
|
94
|
+
dispatch({
|
|
95
|
+
type: EXPENSE_ACTION_TYPES.SET_DRAWER,
|
|
96
|
+
payload: { drawer: null },
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
(_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort();
|
|
100
|
+
const controller = new AbortController();
|
|
101
|
+
abortRef.current = controller;
|
|
102
|
+
const { signal } = controller;
|
|
103
|
+
const label = method === "POST" ? "create" : "update";
|
|
104
|
+
const file = files[0];
|
|
105
|
+
const text = await file.text();
|
|
106
|
+
const records = converter.csv2json(text);
|
|
107
|
+
if (!Array.isArray(records) || records.length === 0) {
|
|
108
|
+
showErrorToast(t("messagesBulkCsvEmpty"));
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const validationErrors = [];
|
|
112
|
+
for (let i = 0; i < records.length; i++) {
|
|
113
|
+
const row = records[i];
|
|
114
|
+
const msgs = [];
|
|
115
|
+
if (method === "POST") {
|
|
116
|
+
if (!((_b = row.title) === null || _b === void 0 ? void 0 : _b.trim()))
|
|
117
|
+
msgs.push(t("validationRequiredTitle"));
|
|
118
|
+
if (!((_c = row.amount) === null || _c === void 0 ? void 0 : _c.trim()) ||
|
|
119
|
+
isNaN(Number(row.amount)) ||
|
|
120
|
+
Number(row.amount) < 0.01)
|
|
121
|
+
msgs.push(t("validationRequiredAmount"));
|
|
122
|
+
if (!((_d = row.category) === null || _d === void 0 ? void 0 : _d.trim()))
|
|
123
|
+
msgs.push(t("validationRequiredCategory"));
|
|
124
|
+
if (!((_e = row.expenseDate) === null || _e === void 0 ? void 0 : _e.trim()))
|
|
125
|
+
msgs.push(t("validationRequiredExpenseDate"));
|
|
126
|
+
if (!((_f = row.status) === null || _f === void 0 ? void 0 : _f.trim()))
|
|
127
|
+
msgs.push(t("validationRequiredStatus"));
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
if (!((_g = row.id) === null || _g === void 0 ? void 0 : _g.trim()))
|
|
131
|
+
msgs.push(t("validationRequiredIdForUpdate"));
|
|
132
|
+
}
|
|
133
|
+
if (msgs.length > 0) {
|
|
134
|
+
validationErrors.push({ row: i + 1, messages: msgs });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (validationErrors.length > 0) {
|
|
138
|
+
const summary = validationErrors
|
|
139
|
+
.slice(0, 5)
|
|
140
|
+
.map((e) => t("messagesBulkRowError", {
|
|
141
|
+
row: e.row,
|
|
142
|
+
error: e.messages.join("; "),
|
|
143
|
+
}))
|
|
144
|
+
.join("\n");
|
|
145
|
+
showErrorToast(t("messagesBulkValidationFailed", {
|
|
146
|
+
count: validationErrors.length,
|
|
147
|
+
errors: summary,
|
|
148
|
+
}));
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
showInfoToast(t("messagesBulkJobQueued", { action: label, count: records.length }));
|
|
153
|
+
let jobId;
|
|
154
|
+
try {
|
|
155
|
+
jobId = await submitBulkJob(text, method, signal);
|
|
156
|
+
}
|
|
157
|
+
catch (submitError) {
|
|
158
|
+
showErrorToast(t("messagesBulkJobSubmitFailed", {
|
|
159
|
+
action: label,
|
|
160
|
+
error: submitError instanceof Error
|
|
161
|
+
? submitError.message
|
|
162
|
+
: t("unknownError"),
|
|
163
|
+
}));
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const status = await pollBulkJob(jobId, signal, (processed, total) => {
|
|
167
|
+
showInfoToast(t("messagesBulkProgress", { processed, total }));
|
|
168
|
+
});
|
|
169
|
+
if (signal.aborted)
|
|
170
|
+
return;
|
|
171
|
+
if (status.status === "completed") {
|
|
172
|
+
const r = status.results;
|
|
173
|
+
if (r && ((_h = r.errors) === null || _h === void 0 ? void 0 : _h.length) > 0) {
|
|
174
|
+
const summary = formatErrorSummary(t, r.errors);
|
|
175
|
+
showSuccessToast(t("messagesBulkResults", {
|
|
176
|
+
created: r.created,
|
|
177
|
+
updated: r.updated,
|
|
178
|
+
skipped: r.skipped,
|
|
179
|
+
}) +
|
|
180
|
+
"\n" +
|
|
181
|
+
summary);
|
|
182
|
+
}
|
|
183
|
+
else if (r) {
|
|
184
|
+
showSuccessToast(t("messagesBulkResults", {
|
|
185
|
+
created: r.created,
|
|
186
|
+
updated: r.updated,
|
|
187
|
+
skipped: r.skipped,
|
|
188
|
+
}));
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
showSuccessToast(t("messagesBulkSuccess"));
|
|
192
|
+
}
|
|
193
|
+
const schoolId = ((_j = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _j === void 0 ? void 0 : _j.id) || "";
|
|
194
|
+
fetch(`${EXPENSE_API_ROUTES.UNIT}?currentPage=1&pageLimit=${pageLimit}&schoolId=${schoolId}`, {
|
|
195
|
+
headers: {
|
|
196
|
+
"Content-Type": "application/json",
|
|
197
|
+
},
|
|
198
|
+
})
|
|
199
|
+
.then(async (res) => {
|
|
200
|
+
var _a, _b;
|
|
201
|
+
if (!res.ok)
|
|
202
|
+
return;
|
|
203
|
+
const data = await res.json();
|
|
204
|
+
dispatch({
|
|
205
|
+
type: EXPENSE_ACTION_TYPES.SET_ITEMS,
|
|
206
|
+
payload: {
|
|
207
|
+
items: (_a = data.items) !== null && _a !== void 0 ? _a : [],
|
|
208
|
+
count: (_b = data.count) !== null && _b !== void 0 ? _b : 0,
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
})
|
|
212
|
+
.catch(() => { });
|
|
213
|
+
closeDrawer();
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
const r = status.results;
|
|
217
|
+
const detail = ((_k = r === null || r === void 0 ? void 0 : r.errors) === null || _k === void 0 ? void 0 : _k.length)
|
|
218
|
+
? formatErrorSummary(t, r.errors)
|
|
219
|
+
: t("unknownError");
|
|
220
|
+
showErrorToast(t("messagesBulkFailed", { action: label }) + "\n" + detail);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
if (error.message === "Polling cancelled")
|
|
225
|
+
return;
|
|
226
|
+
showErrorToast(t("messagesBulkFailedDetail", {
|
|
227
|
+
action: label,
|
|
228
|
+
error: error instanceof Error ? error.message : t("unknownError"),
|
|
229
|
+
}));
|
|
230
|
+
}
|
|
231
|
+
}, [dispatch, t]);
|
|
232
|
+
const handleBulkCreate = useCallback((files) => handleBulkFlow(files, "POST"), [handleBulkFlow]);
|
|
233
|
+
const handleBulkUpdate = useCallback((files) => handleBulkFlow(files, "PUT"), [handleBulkFlow]);
|
|
24
234
|
const create = [
|
|
25
235
|
{
|
|
26
236
|
id: "1",
|
|
27
237
|
title: t("downloadEmptyCsvTemplate"),
|
|
28
238
|
handleOnClick: async () => {
|
|
29
|
-
|
|
239
|
+
var _a;
|
|
240
|
+
await handleGetAllRecords(((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id) || "", 1);
|
|
30
241
|
},
|
|
31
242
|
},
|
|
32
243
|
{ id: "2", title: t("addYourDataToTheCsv") },
|
|
@@ -41,11 +252,14 @@ export const ExpenseMoreActions = () => {
|
|
|
41
252
|
title: t("downloadPopulatedCsvTemplate"),
|
|
42
253
|
handleOnClick: async () => {
|
|
43
254
|
var _a;
|
|
44
|
-
await handleGetAllRecords(((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id) || "");
|
|
255
|
+
await handleGetAllRecords(((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id) || "", 1000);
|
|
45
256
|
},
|
|
46
257
|
},
|
|
47
258
|
{ id: "2", title: t("updateYourDataInTheCsv") },
|
|
48
|
-
{
|
|
259
|
+
{
|
|
260
|
+
id: "3",
|
|
261
|
+
title: t("uploadTheCompletedCsvToTheSystem"),
|
|
262
|
+
},
|
|
49
263
|
];
|
|
50
|
-
return (_jsxs("div", { className: "space-y-4", children: [_jsx(Timeline, { events: create, heading: t("bulkCreate") }), _jsx(Timeline, { events: update, heading: t("bulkUpdate") })] }));
|
|
264
|
+
return (_jsxs("div", { className: "space-y-4", children: [_jsx(Timeline, { events: create, heading: t("bulkCreate"), handleOnBulkCreate: handleBulkCreate }), _jsx(Timeline, { events: update, heading: t("bulkUpdate"), handleOnBulkCreate: handleBulkUpdate })] }));
|
|
51
265
|
};
|