@appcorp/fusion-storybook 0.2.85 → 0.2.86
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/student-fee/more-actions.js +223 -12
- package/constants.d.ts +5 -1
- package/constants.js +2 -0
- package/package.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -1,26 +1,234 @@
|
|
|
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";
|
|
13
9
|
import { STUDENT_FEE_API_ROUTES } from "../../constants";
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
import { STUDENT_FEE_ACTION_TYPES, useStudentFeeContext } from "./context";
|
|
11
|
+
import { useRef, useEffect, useCallback } from "react";
|
|
12
|
+
const workspace = getCachedWorkspaceSync();
|
|
13
|
+
const POLL_INTERVAL_MS = 2000;
|
|
14
|
+
const POLL_TIMEOUT_MS = 300000;
|
|
15
|
+
const PAGE_LIMIT = 1000;
|
|
16
|
+
const handleGetAllRecords = async (schoolId, pageLimit) => {
|
|
17
|
+
const response = await fetch(`${STUDENT_FEE_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
21
|
await downloadFromUrl(blob, "student-fees.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(STUDENT_FEE_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(STUDENT_FEE_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(
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
+
t, errors) {
|
|
73
|
+
if (!(errors === null || errors === void 0 ? void 0 : errors.length))
|
|
74
|
+
return "";
|
|
75
|
+
const lines = errors
|
|
76
|
+
.slice(0, 5)
|
|
77
|
+
.map((e) => t("messagesBulkRowError", { row: e.row, error: e.error }));
|
|
78
|
+
const remaining = errors.length - 5;
|
|
79
|
+
if (remaining > 0)
|
|
80
|
+
lines.push(t("messagesBulkMoreRows", { count: remaining }));
|
|
81
|
+
return lines.join("\n");
|
|
82
|
+
}
|
|
21
83
|
export const StudentFeeMoreActions = () => {
|
|
22
|
-
const workspace = getCachedWorkspaceSync();
|
|
23
84
|
const t = useTranslations("studentFee");
|
|
85
|
+
const { dispatch } = useStudentFeeContext();
|
|
86
|
+
const abortRef = useRef(null);
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
return () => {
|
|
89
|
+
var _a;
|
|
90
|
+
(_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort();
|
|
91
|
+
};
|
|
92
|
+
}, []);
|
|
93
|
+
const handleBulkFlow = useCallback(async (files, method) => {
|
|
94
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
95
|
+
const closeDrawer = () => {
|
|
96
|
+
dispatch({
|
|
97
|
+
type: STUDENT_FEE_ACTION_TYPES.SET_DRAWER,
|
|
98
|
+
payload: { drawer: null },
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
(_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort();
|
|
102
|
+
const controller = new AbortController();
|
|
103
|
+
abortRef.current = controller;
|
|
104
|
+
const { signal } = controller;
|
|
105
|
+
const label = method === "POST" ? "create" : "update";
|
|
106
|
+
const file = files[0];
|
|
107
|
+
const text = await file.text();
|
|
108
|
+
const records = converter.csv2json(text);
|
|
109
|
+
if (!Array.isArray(records) || records.length === 0) {
|
|
110
|
+
showErrorToast(t("messagesBulkCsvEmpty"));
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const validationErrors = [];
|
|
114
|
+
for (let i = 0; i < records.length; i++) {
|
|
115
|
+
const row = records[i];
|
|
116
|
+
const msgs = [];
|
|
117
|
+
if (method === "POST") {
|
|
118
|
+
if (!((_b = row.familyId) === null || _b === void 0 ? void 0 : _b.trim()))
|
|
119
|
+
msgs.push(t("validationRequiredFamilyId"));
|
|
120
|
+
if (!((_c = row.studentProfileId) === null || _c === void 0 ? void 0 : _c.trim()))
|
|
121
|
+
msgs.push(t("validationRequiredStudentProfileId"));
|
|
122
|
+
if (!((_d = row.feeStructureId) === null || _d === void 0 ? void 0 : _d.trim()))
|
|
123
|
+
msgs.push(t("validationRequiredFeeStructureId"));
|
|
124
|
+
if (!((_e = row.amount) === null || _e === void 0 ? void 0 : _e.trim()))
|
|
125
|
+
msgs.push(t("validationRequiredAmount"));
|
|
126
|
+
if (!((_f = row.dueDate) === null || _f === void 0 ? void 0 : _f.trim()))
|
|
127
|
+
msgs.push(t("validationRequiredDueDate"));
|
|
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 errorsList = validationErrors
|
|
139
|
+
.slice(0, 5)
|
|
140
|
+
.map((e) => t("messagesBulkRowError", {
|
|
141
|
+
row: e.row,
|
|
142
|
+
error: e.messages.join("; "),
|
|
143
|
+
}))
|
|
144
|
+
.join("\n");
|
|
145
|
+
const remaining = validationErrors.length - 5;
|
|
146
|
+
const errorsStr = remaining > 0
|
|
147
|
+
? `${errorsList}\n${t("messagesBulkMoreRows", { count: remaining })}`
|
|
148
|
+
: errorsList;
|
|
149
|
+
showErrorToast(t("messagesBulkValidationFailed", {
|
|
150
|
+
count: validationErrors.length,
|
|
151
|
+
errors: errorsStr,
|
|
152
|
+
}));
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
showInfoToast(t("messagesBulkJobQueued", { action: label, count: records.length }));
|
|
157
|
+
let jobId;
|
|
158
|
+
try {
|
|
159
|
+
jobId = await submitBulkJob(text, method, signal);
|
|
160
|
+
}
|
|
161
|
+
catch (submitError) {
|
|
162
|
+
showErrorToast(t("messagesBulkJobSubmitFailed", {
|
|
163
|
+
action: label,
|
|
164
|
+
error: submitError instanceof Error
|
|
165
|
+
? submitError.message
|
|
166
|
+
: t("unknownError"),
|
|
167
|
+
}));
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const status = await pollBulkJob(jobId, signal, (processed, total) => {
|
|
171
|
+
showInfoToast(t("messagesBulkProgress", { processed, total }));
|
|
172
|
+
});
|
|
173
|
+
if (signal.aborted)
|
|
174
|
+
return;
|
|
175
|
+
if (status.status === "completed") {
|
|
176
|
+
const r = status.results;
|
|
177
|
+
if (r && ((_h = r.errors) === null || _h === void 0 ? void 0 : _h.length) > 0) {
|
|
178
|
+
const summary = formatErrorSummary(t, r.errors);
|
|
179
|
+
showSuccessToast(`${t("messagesBulkResults", { created: r.created, updated: r.updated, skipped: r.skipped })}\n${summary}`);
|
|
180
|
+
}
|
|
181
|
+
else if (r) {
|
|
182
|
+
showSuccessToast(t("messagesBulkResults", {
|
|
183
|
+
created: r.created,
|
|
184
|
+
updated: r.updated,
|
|
185
|
+
skipped: r.skipped,
|
|
186
|
+
}));
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
showSuccessToast(t("messagesBulkSuccess"));
|
|
190
|
+
}
|
|
191
|
+
const schoolId = ((_j = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _j === void 0 ? void 0 : _j.id) || "";
|
|
192
|
+
fetch(`${STUDENT_FEE_API_ROUTES.UNIT}?currentPage=1&pageLimit=${PAGE_LIMIT}&schoolId=${schoolId}`, {
|
|
193
|
+
headers: {
|
|
194
|
+
"Content-Type": "application/json",
|
|
195
|
+
},
|
|
196
|
+
})
|
|
197
|
+
.then(async (res) => {
|
|
198
|
+
var _a, _b;
|
|
199
|
+
if (!res.ok)
|
|
200
|
+
return;
|
|
201
|
+
const data = await res.json();
|
|
202
|
+
dispatch({
|
|
203
|
+
type: STUDENT_FEE_ACTION_TYPES.SET_ITEMS,
|
|
204
|
+
payload: {
|
|
205
|
+
items: (_a = data.items) !== null && _a !== void 0 ? _a : [],
|
|
206
|
+
count: (_b = data.count) !== null && _b !== void 0 ? _b : 0,
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
})
|
|
210
|
+
.catch(() => { });
|
|
211
|
+
closeDrawer();
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
const r = status.results;
|
|
215
|
+
const detail = ((_k = r === null || r === void 0 ? void 0 : r.errors) === null || _k === void 0 ? void 0 : _k.length)
|
|
216
|
+
? formatErrorSummary(t, r.errors)
|
|
217
|
+
: t("unknownError");
|
|
218
|
+
showErrorToast(`${t("messagesBulkFailed", { action: label })}\n${detail}`);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
catch (error) {
|
|
222
|
+
if (error.message === "Polling cancelled")
|
|
223
|
+
return;
|
|
224
|
+
showErrorToast(t("messagesBulkFailedDetail", {
|
|
225
|
+
action: label,
|
|
226
|
+
error: error instanceof Error ? error.message : t("unknownError"),
|
|
227
|
+
}));
|
|
228
|
+
}
|
|
229
|
+
}, [dispatch, t]);
|
|
230
|
+
const handleBulkCreate = useCallback((files) => handleBulkFlow(files, "POST"), [handleBulkFlow]);
|
|
231
|
+
const handleBulkUpdate = useCallback((files) => handleBulkFlow(files, "PUT"), [handleBulkFlow]);
|
|
24
232
|
const create = [
|
|
25
233
|
{
|
|
26
234
|
id: "1",
|
|
@@ -41,11 +249,14 @@ export const StudentFeeMoreActions = () => {
|
|
|
41
249
|
title: t("downloadPopulatedCsvTemplate"),
|
|
42
250
|
handleOnClick: async () => {
|
|
43
251
|
var _a;
|
|
44
|
-
await handleGetAllRecords(((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id) || "");
|
|
252
|
+
await handleGetAllRecords(((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id) || "", PAGE_LIMIT);
|
|
45
253
|
},
|
|
46
254
|
},
|
|
47
255
|
{ id: "2", title: t("updateYourDataInTheCsv") },
|
|
48
|
-
{
|
|
256
|
+
{
|
|
257
|
+
id: "3",
|
|
258
|
+
title: t("uploadTheCompletedCsvToTheSystem"),
|
|
259
|
+
},
|
|
49
260
|
];
|
|
50
|
-
return (_jsxs("div", { className: "space-y-4", children: [_jsx(Timeline, { events: create, heading: t("bulkCreate") }), _jsx(Timeline, { events: update, heading: t("bulkUpdate") })] }));
|
|
261
|
+
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
262
|
};
|
package/constants.d.ts
CHANGED
|
@@ -65,7 +65,11 @@ export declare const SECTION_API_ROUTES: {
|
|
|
65
65
|
readonly BULK_STATUS: (jobId: string) => string;
|
|
66
66
|
readonly CLASS: "/api/v1/class";
|
|
67
67
|
};
|
|
68
|
-
export declare const STUDENT_FEE_API_ROUTES:
|
|
68
|
+
export declare const STUDENT_FEE_API_ROUTES: {
|
|
69
|
+
readonly UNIT: "/api/v1/student-fee";
|
|
70
|
+
readonly BULK: "/api/v1/student-fee/bulk";
|
|
71
|
+
readonly BULK_STATUS: (jobId: string) => string;
|
|
72
|
+
};
|
|
69
73
|
export declare const STUDENT_PROFILE_API_ROUTES: {
|
|
70
74
|
readonly LIST: "/api/v1/student-profile";
|
|
71
75
|
readonly UNIT: "/api/v1/student-profile";
|
package/constants.js
CHANGED
|
@@ -80,6 +80,8 @@ export const SECTION_API_ROUTES = {
|
|
|
80
80
|
};
|
|
81
81
|
export const STUDENT_FEE_API_ROUTES = {
|
|
82
82
|
UNIT: "/api/v1/student-fee",
|
|
83
|
+
BULK: "/api/v1/student-fee/bulk",
|
|
84
|
+
BULK_STATUS: (jobId) => `/api/v1/student-fee/bulk/${jobId}`,
|
|
83
85
|
};
|
|
84
86
|
export const STUDENT_PROFILE_API_ROUTES = {
|
|
85
87
|
LIST: "/api/v1/student-profile",
|