@temboplus/frontend-core 0.2.2 → 0.2.4
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/README.md +101 -49
- package/esm/mod.d.ts +2 -0
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +2 -0
- package/esm/src/config/config_service.d.ts +11 -0
- package/esm/src/config/config_service.d.ts.map +1 -0
- package/esm/src/config/config_service.js +19 -0
- package/esm/src/config/index.d.ts +2 -0
- package/esm/src/config/index.d.ts.map +1 -0
- package/esm/src/config/index.js +1 -0
- package/esm/src/models/phone_number/tz/network_operator.d.ts.map +1 -1
- package/esm/src/models/phone_number/tz/network_operator.js +5 -2
- package/esm/src/reports/index.d.ts +2 -0
- package/esm/src/reports/index.d.ts.map +1 -0
- package/esm/src/reports/index.js +1 -0
- package/esm/src/reports/report_manager.d.ts +144 -0
- package/esm/src/reports/report_manager.d.ts.map +1 -0
- package/esm/src/reports/report_manager.js +318 -0
- package/package.json +2 -1
- package/script/mod.d.ts +2 -0
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +2 -0
- package/script/src/config/config_service.d.ts +11 -0
- package/script/src/config/config_service.d.ts.map +1 -0
- package/script/src/config/config_service.js +23 -0
- package/script/src/config/index.d.ts +2 -0
- package/script/src/config/index.d.ts.map +1 -0
- package/script/src/config/index.js +17 -0
- package/script/src/models/phone_number/tz/network_operator.d.ts.map +1 -1
- package/script/src/models/phone_number/tz/network_operator.js +5 -2
- package/script/src/reports/index.d.ts +2 -0
- package/script/src/reports/index.d.ts.map +1 -0
- package/script/src/reports/index.js +17 -0
- package/script/src/reports/report_manager.d.ts +144 -0
- package/script/src/reports/report_manager.d.ts.map +1 -0
- package/script/src/reports/report_manager.js +328 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ReportManager = exports.REPORTS = exports.ReportType = exports.ProjectType = exports.FileFormat = void 0;
|
|
7
|
+
exports.getReportsByProject = getReportsByProject;
|
|
8
|
+
exports.getReportByType = getReportByType;
|
|
9
|
+
exports.isReportAvailableForProject = isReportAvailableForProject;
|
|
10
|
+
const file_saver_1 = __importDefault(require("file-saver"));
|
|
11
|
+
const index_js_1 = require("../config/index.js");
|
|
12
|
+
/**
|
|
13
|
+
* Available file formats for reports
|
|
14
|
+
*/
|
|
15
|
+
var FileFormat;
|
|
16
|
+
(function (FileFormat) {
|
|
17
|
+
/** PDF file format */
|
|
18
|
+
FileFormat["PDF"] = "pdf";
|
|
19
|
+
/** Excel file format */
|
|
20
|
+
FileFormat["EXCEL"] = "excel";
|
|
21
|
+
})(FileFormat || (exports.FileFormat = FileFormat = {}));
|
|
22
|
+
/**
|
|
23
|
+
* Available project types
|
|
24
|
+
*/
|
|
25
|
+
var ProjectType;
|
|
26
|
+
(function (ProjectType) {
|
|
27
|
+
/** Tembo Dashboard project */
|
|
28
|
+
ProjectType["DASHBOARD"] = "dashboard";
|
|
29
|
+
/** Afloat project */
|
|
30
|
+
ProjectType["AFLOAT"] = "afloat";
|
|
31
|
+
/** VertoX project */
|
|
32
|
+
ProjectType["VERTO_X"] = "verto_x";
|
|
33
|
+
})(ProjectType || (exports.ProjectType = ProjectType = {}));
|
|
34
|
+
/**
|
|
35
|
+
* Available report types with improved naming
|
|
36
|
+
*/
|
|
37
|
+
var ReportType;
|
|
38
|
+
(function (ReportType) {
|
|
39
|
+
/** Merchant payout statement (Dashboard) */
|
|
40
|
+
ReportType["MERCHANT_DISBURSEMENT_REPORT"] = "merchant_disbursement_report";
|
|
41
|
+
/** Transaction revenue summary (Dashboard) */
|
|
42
|
+
ReportType["TRANSACTION_REVENUE_SUMMARY"] = "transaction_revenue_summary";
|
|
43
|
+
/** Customer wallet activity (Afloat) */
|
|
44
|
+
ReportType["CUSTOMER_WALLET_ACTIVITY"] = "customer_wallet_activity";
|
|
45
|
+
/** Customer profile information (Afloat) */
|
|
46
|
+
ReportType["CUSTOMER_PROFILE_SNAPSHOT"] = "customer_profile_snapshot";
|
|
47
|
+
/** Gateway transaction log (VertoX) */
|
|
48
|
+
ReportType["GATEWAY_TRANSACTION_LOG"] = "gateway_transaction_log";
|
|
49
|
+
})(ReportType || (exports.ReportType = ReportType = {}));
|
|
50
|
+
/**
|
|
51
|
+
* Registry of all available reports
|
|
52
|
+
*/
|
|
53
|
+
exports.REPORTS = {
|
|
54
|
+
// Dashboard Reports
|
|
55
|
+
[ReportType.MERCHANT_DISBURSEMENT_REPORT]: {
|
|
56
|
+
id: "merchant_disbursement_report",
|
|
57
|
+
displayName: "Merchant Disbursement Report",
|
|
58
|
+
endpoint: "/dashboard/merchant_disbursements",
|
|
59
|
+
availableFormats: [FileFormat.PDF, FileFormat.EXCEL],
|
|
60
|
+
projectType: ProjectType.DASHBOARD,
|
|
61
|
+
reportType: ReportType.MERCHANT_DISBURSEMENT_REPORT,
|
|
62
|
+
description: "Detailed breakdown of payments made to merchants",
|
|
63
|
+
},
|
|
64
|
+
[ReportType.TRANSACTION_REVENUE_SUMMARY]: {
|
|
65
|
+
id: "transaction_revenue_summary",
|
|
66
|
+
displayName: "Transaction Revenue Summary",
|
|
67
|
+
endpoint: "/dashboard/revenue_summary",
|
|
68
|
+
availableFormats: [FileFormat.PDF, FileFormat.EXCEL],
|
|
69
|
+
projectType: ProjectType.DASHBOARD,
|
|
70
|
+
reportType: ReportType.TRANSACTION_REVENUE_SUMMARY,
|
|
71
|
+
description: "Summary of all revenue transactions by period",
|
|
72
|
+
},
|
|
73
|
+
// Afloat Reports
|
|
74
|
+
[ReportType.CUSTOMER_WALLET_ACTIVITY]: {
|
|
75
|
+
id: "customer_wallet_activity",
|
|
76
|
+
displayName: "Customer Wallet Activity",
|
|
77
|
+
endpoint: "/afloat/wallet_activity",
|
|
78
|
+
availableFormats: [FileFormat.PDF, FileFormat.EXCEL],
|
|
79
|
+
projectType: ProjectType.AFLOAT,
|
|
80
|
+
reportType: ReportType.CUSTOMER_WALLET_ACTIVITY,
|
|
81
|
+
description: "Detailed record of all customer wallet transactions",
|
|
82
|
+
},
|
|
83
|
+
[ReportType.CUSTOMER_PROFILE_SNAPSHOT]: {
|
|
84
|
+
id: "customer_profile_snapshot",
|
|
85
|
+
displayName: "Customer Profile Snapshot",
|
|
86
|
+
endpoint: "/afloat/profile_snapshot",
|
|
87
|
+
availableFormats: [FileFormat.PDF],
|
|
88
|
+
projectType: ProjectType.AFLOAT,
|
|
89
|
+
reportType: ReportType.CUSTOMER_PROFILE_SNAPSHOT,
|
|
90
|
+
description: "Current account information and status",
|
|
91
|
+
},
|
|
92
|
+
// VertoX Reports
|
|
93
|
+
[ReportType.GATEWAY_TRANSACTION_LOG]: {
|
|
94
|
+
id: "gateway_transaction_log",
|
|
95
|
+
displayName: "Gateway Transaction Log",
|
|
96
|
+
endpoint: "/vertox/gateway_transactions",
|
|
97
|
+
availableFormats: [FileFormat.EXCEL],
|
|
98
|
+
projectType: ProjectType.VERTO_X,
|
|
99
|
+
reportType: ReportType.GATEWAY_TRANSACTION_LOG,
|
|
100
|
+
description: "Log of all payment gateway API transactions",
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Get all reports for a specific project
|
|
105
|
+
* @param projectType The project type to filter by
|
|
106
|
+
* @returns Array of report definitions for the project
|
|
107
|
+
*/
|
|
108
|
+
function getReportsByProject(projectType) {
|
|
109
|
+
return Object.values(exports.REPORTS).filter((report) => report.projectType === projectType);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Get a report by its type
|
|
113
|
+
* @param reportType The report type to retrieve
|
|
114
|
+
* @returns The report definition or undefined if not found
|
|
115
|
+
*/
|
|
116
|
+
function getReportByType(reportType) {
|
|
117
|
+
return exports.REPORTS[reportType];
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Validates if a report type is available for a project
|
|
121
|
+
* @param projectType The project type
|
|
122
|
+
* @param reportType The report type
|
|
123
|
+
* @returns True if the report is available for the project, false otherwise
|
|
124
|
+
*/
|
|
125
|
+
function isReportAvailableForProject(projectType, reportType) {
|
|
126
|
+
const report = exports.REPORTS[reportType];
|
|
127
|
+
return report !== undefined && report.projectType === projectType;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Report Manager class for handling report downloads
|
|
131
|
+
*/
|
|
132
|
+
class ReportManager {
|
|
133
|
+
constructor() {
|
|
134
|
+
/**
|
|
135
|
+
* Get the base URL for the report API
|
|
136
|
+
* @returns The base URL
|
|
137
|
+
*/
|
|
138
|
+
Object.defineProperty(this, "getBaseURL", {
|
|
139
|
+
enumerable: true,
|
|
140
|
+
configurable: true,
|
|
141
|
+
writable: true,
|
|
142
|
+
value: () => {
|
|
143
|
+
let url = index_js_1.ConfigService.instance.pdfMakerBaseUrl;
|
|
144
|
+
if (url.trim().length === 0) {
|
|
145
|
+
url = "https://api.afloat.money/pdf-maker";
|
|
146
|
+
}
|
|
147
|
+
if (url.endsWith("/"))
|
|
148
|
+
return url.slice(0, -1);
|
|
149
|
+
return url;
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
/**
|
|
153
|
+
* Converts a base64 string to a Blob
|
|
154
|
+
* @param base64 The base64 string
|
|
155
|
+
* @returns A Blob
|
|
156
|
+
*/
|
|
157
|
+
Object.defineProperty(this, "b64toBlob", {
|
|
158
|
+
enumerable: true,
|
|
159
|
+
configurable: true,
|
|
160
|
+
writable: true,
|
|
161
|
+
value: (base64) => fetch(base64).then((res) => res.blob())
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Get the singleton instance of ReportManager
|
|
166
|
+
*/
|
|
167
|
+
static get instance() {
|
|
168
|
+
return this._instance || (this._instance = new this());
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Downloads a report based on project type and report type
|
|
172
|
+
* @param args Arguments for the report download
|
|
173
|
+
* @returns Promise that resolves when download is complete
|
|
174
|
+
*/
|
|
175
|
+
async downloadReport(args) {
|
|
176
|
+
try {
|
|
177
|
+
// Get the report from the registry
|
|
178
|
+
const report = exports.REPORTS[args.reportType];
|
|
179
|
+
if (!report) {
|
|
180
|
+
throw new Error(`Report type ${args.reportType} not configured`);
|
|
181
|
+
}
|
|
182
|
+
// Validate that the report belongs to the specified project
|
|
183
|
+
if (report.projectType !== args.projectType) {
|
|
184
|
+
throw new Error(`Report type ${args.reportType} does not belong to project ${args.projectType}`);
|
|
185
|
+
}
|
|
186
|
+
// Check if requested format is supported
|
|
187
|
+
if (!report.availableFormats.includes(args.fileFormat)) {
|
|
188
|
+
throw new Error(`File format ${args.fileFormat} not supported for ${report.displayName}`);
|
|
189
|
+
}
|
|
190
|
+
// Build URL using the report's endpoint
|
|
191
|
+
let url = `${this.getBaseURL()}${report.endpoint}`;
|
|
192
|
+
// Create a properly typed query parameters object
|
|
193
|
+
// deno-lint-ignore no-explicit-any
|
|
194
|
+
const queryParams = {
|
|
195
|
+
...(args.query || {}),
|
|
196
|
+
fileFormat: args.fileFormat,
|
|
197
|
+
};
|
|
198
|
+
// Build the query string
|
|
199
|
+
const searchParams = new URLSearchParams();
|
|
200
|
+
for (const key in queryParams) {
|
|
201
|
+
if (Object.prototype.hasOwnProperty.call(queryParams, key)) {
|
|
202
|
+
const value = queryParams[key];
|
|
203
|
+
if (value !== undefined && value !== null) {
|
|
204
|
+
if (Array.isArray(value)) {
|
|
205
|
+
value.forEach((item) => {
|
|
206
|
+
searchParams.append(key + "[]", String(item));
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
searchParams.append(key, String(value));
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const queryString = searchParams.toString();
|
|
216
|
+
if (queryString) {
|
|
217
|
+
url += `?${queryString}`;
|
|
218
|
+
}
|
|
219
|
+
// Make the request
|
|
220
|
+
const response = await fetch(url, {
|
|
221
|
+
method: "GET",
|
|
222
|
+
headers: {
|
|
223
|
+
"Accept": "*/*",
|
|
224
|
+
"Content-Type": "application/json",
|
|
225
|
+
"Authorization": `Bearer ${args.token}`,
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
if (response.status !== 200) {
|
|
229
|
+
await this.handleErrorResponse(response);
|
|
230
|
+
}
|
|
231
|
+
// Process the download
|
|
232
|
+
await this.processDownload(response, report, args.fileFormat);
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
console.error("Report download failed:", error);
|
|
236
|
+
throw error;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Process the download
|
|
241
|
+
* @param response The response from the API
|
|
242
|
+
* @param report The report definition
|
|
243
|
+
* @param fileFormat The requested file format
|
|
244
|
+
*/
|
|
245
|
+
async processDownload(response, report, fileFormat) {
|
|
246
|
+
const contentDisposition = response.headers.get("Content-Disposition");
|
|
247
|
+
// Get default filename based on report and file format
|
|
248
|
+
const defaultFilename = this.getDefaultFilename(report, fileFormat);
|
|
249
|
+
// Try to get filename from Content-Disposition, fall back to default
|
|
250
|
+
const fileName = this.extractFilenameFromContentDisposition(contentDisposition) ||
|
|
251
|
+
defaultFilename;
|
|
252
|
+
// Handle the response based on content type
|
|
253
|
+
// deno-lint-ignore no-explicit-any
|
|
254
|
+
const blob = await this.b64toBlob(await response.text());
|
|
255
|
+
file_saver_1.default.saveAs(blob, fileName);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Handle error responses from the API
|
|
259
|
+
* @param response The response from the API
|
|
260
|
+
* @throws Error with appropriate message
|
|
261
|
+
*/
|
|
262
|
+
async handleErrorResponse(response) {
|
|
263
|
+
let errorMessage = "Encountered an error while generating report";
|
|
264
|
+
try {
|
|
265
|
+
// Try to parse as JSON first
|
|
266
|
+
const contentType = response.headers.get("Content-Type");
|
|
267
|
+
if (contentType && contentType.includes("application/json")) {
|
|
268
|
+
const errorData = await response.json();
|
|
269
|
+
errorMessage = errorData.message || errorData.error ||
|
|
270
|
+
errorMessage;
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
// Try to get error as text
|
|
274
|
+
const textError = await response.text();
|
|
275
|
+
if (textError) {
|
|
276
|
+
errorMessage = textError;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
catch (parseError) {
|
|
281
|
+
console.error("Error parsing error response:", parseError);
|
|
282
|
+
}
|
|
283
|
+
throw new Error(errorMessage);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Generates a default filename based on report and file format
|
|
287
|
+
* @param report The report definition
|
|
288
|
+
* @param fileFormat The requested file format
|
|
289
|
+
* @returns A suitable default filename with proper extension
|
|
290
|
+
*/
|
|
291
|
+
getDefaultFilename(report, fileFormat) {
|
|
292
|
+
const date = new Date().toISOString().slice(0, 10); // YYYY-MM-DD format
|
|
293
|
+
// Build the filename with project, report type, and date
|
|
294
|
+
const baseFilename = `${report.id}_${date}`;
|
|
295
|
+
// Add the extension based on the file format
|
|
296
|
+
return fileFormat === FileFormat.PDF
|
|
297
|
+
? `${baseFilename}.pdf`
|
|
298
|
+
: `${baseFilename}.xlsx`;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Extracts the filename from the Content-Disposition header
|
|
302
|
+
* @param contentDisposition The Content-Disposition header value
|
|
303
|
+
* @returns The extracted filename or null if not found
|
|
304
|
+
*/
|
|
305
|
+
extractFilenameFromContentDisposition(contentDisposition) {
|
|
306
|
+
// Check if the header exists
|
|
307
|
+
if (!contentDisposition) {
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
// Try to match the filename pattern
|
|
311
|
+
const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
|
|
312
|
+
const matches = filenameRegex.exec(contentDisposition);
|
|
313
|
+
if (matches && matches[1]) {
|
|
314
|
+
// Clean up the filename by removing quotes if present
|
|
315
|
+
let filename = matches[1].trim();
|
|
316
|
+
// Remove surrounding quotes if they exist
|
|
317
|
+
if (filename.startsWith('"') && filename.endsWith('"')) {
|
|
318
|
+
filename = filename.substring(1, filename.length - 1);
|
|
319
|
+
}
|
|
320
|
+
else if (filename.startsWith("'") && filename.endsWith("'")) {
|
|
321
|
+
filename = filename.substring(1, filename.length - 1);
|
|
322
|
+
}
|
|
323
|
+
return filename;
|
|
324
|
+
}
|
|
325
|
+
return null;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
exports.ReportManager = ReportManager;
|