akeyless-server-commons 1.0.191 → 1.0.193
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/dist/cjs/helpers/firebase_helpers.d.ts +1 -1
- package/dist/cjs/helpers/firebase_helpers.js +4 -2
- package/dist/cjs/helpers/firebase_helpers.js.map +1 -1
- package/dist/cjs/helpers/global_helpers.d.ts +1 -1
- package/dist/cjs/helpers/tasks.d.ts +12 -0
- package/dist/cjs/helpers/tasks.js +62 -0
- package/dist/cjs/helpers/tasks.js.map +1 -0
- package/dist/cjs/managers/cacche_manager2.d.ts +18 -0
- package/dist/cjs/managers/cacche_manager2.js +41 -0
- package/dist/cjs/managers/cacche_manager2.js.map +1 -0
- package/dist/cjs/managers/logger_manager.d.ts +2 -0
- package/dist/cjs/managers/logger_manager.js +21 -5
- package/dist/cjs/managers/logger_manager.js.map +1 -1
- package/dist/cjs/managers/socket_manager.js +245 -0
- package/dist/cjs/managers/socket_manager.js.map +1 -0
- package/dist/esm/helpers/firebase_helpers.d.ts +1 -1
- package/dist/esm/helpers/firebase_helpers.js +4 -2
- package/dist/esm/helpers/firebase_helpers.js.map +1 -1
- package/dist/esm/helpers/global_helpers.d.ts +1 -1
- package/dist/esm/helpers/tasks.d.ts +12 -0
- package/dist/esm/helpers/tasks.js +58 -0
- package/dist/esm/helpers/tasks.js.map +1 -0
- package/dist/esm/managers/cacche_manager2.d.ts +18 -0
- package/dist/esm/managers/cacche_manager2.js +37 -0
- package/dist/esm/managers/cacche_manager2.js.map +1 -0
- package/dist/esm/managers/logger_manager.d.ts +2 -0
- package/dist/esm/managers/logger_manager.js +21 -5
- package/dist/esm/managers/logger_manager.js.map +1 -1
- package/dist/esm/managers/socket_manager.js +243 -0
- package/dist/esm/managers/socket_manager.js.map +1 -0
- package/package.json +1 -1
- package/dist/helpers/boards_helpers.js +0 -62
- package/dist/helpers/email_helpers.js +0 -153
- package/dist/helpers/firebase_helpers.js +0 -576
- package/dist/helpers/global_helpers.js +0 -147
- package/dist/helpers/index.js +0 -12
- package/dist/helpers/location_helpers.js +0 -15
- package/dist/helpers/login_helpers.js +0 -18
- package/dist/helpers/notification_helpers.js +0 -234
- package/dist/helpers/phone_number_helpers.js +0 -91
- package/dist/helpers/redis/index.js +0 -3
- package/dist/helpers/redis/initialize.js +0 -139
- package/dist/helpers/redis/keys.js +0 -23
- package/dist/helpers/redis/snapshot.js +0 -163
- package/dist/helpers/start.js +0 -43
- package/dist/helpers/tasks_helpers.js +0 -136
- package/dist/helpers/time_helpers.js +0 -167
- package/dist/index.js +0 -5
- package/dist/managers/cache_manager.js +0 -27
- package/dist/managers/index.js +0 -3
- package/dist/managers/logger_manager.js +0 -51
- package/dist/managers/translation_manager.js +0 -38
- package/dist/middlewares/auth_mw.js +0 -50
- package/dist/middlewares/error_handling.js +0 -20
- package/dist/middlewares/global_mw.js +0 -110
- package/dist/middlewares/index.js +0 -4
- package/dist/middlewares/trim_mw.js +0 -7
- package/dist/test.js +0 -1
- package/dist/types/enums/global.js +0 -8
- package/dist/types/enums/index.js +0 -1
- package/dist/types/helpers/boards_helpers.d.ts +0 -10
- package/dist/types/helpers/email_helpers.d.ts +0 -12
- package/dist/types/helpers/firebase_helpers.d.ts +0 -55
- package/dist/types/helpers/global_helpers.d.ts +0 -16
- package/dist/types/helpers/index.d.ts +0 -12
- package/dist/types/helpers/location_helpers.d.ts +0 -3
- package/dist/types/helpers/login_helpers.d.ts +0 -4
- package/dist/types/helpers/notification_helpers.d.ts +0 -11
- package/dist/types/helpers/phone_number_helpers.d.ts +0 -14
- package/dist/types/helpers/redis/index.d.ts +0 -3
- package/dist/types/helpers/redis/initialize.d.ts +0 -6
- package/dist/types/helpers/redis/keys.d.ts +0 -5
- package/dist/types/helpers/redis/snapshot.d.ts +0 -2
- package/dist/types/helpers/start.d.ts +0 -4
- package/dist/types/helpers/tasks_helpers.d.ts +0 -22
- package/dist/types/helpers/time_helpers.d.ts +0 -40
- package/dist/types/index.d.ts +0 -5
- package/dist/types/index.js +0 -3
- package/dist/types/interfaces/index.js +0 -2
- package/dist/types/managers/cache_manager.d.ts +0 -11
- package/dist/types/managers/index.d.ts +0 -3
- package/dist/types/managers/logger_manager.d.ts +0 -12
- package/dist/types/managers/translation_manager.d.ts +0 -14
- package/dist/types/middlewares/auth_mw.d.ts +0 -4
- package/dist/types/middlewares/error_handling.d.ts +0 -11
- package/dist/types/middlewares/global_mw.d.ts +0 -5
- package/dist/types/middlewares/index.d.ts +0 -4
- package/dist/types/middlewares/trim_mw.d.ts +0 -2
- package/dist/types/test.d.ts +0 -0
- package/dist/types/types/enums/global.d.ts +0 -9
- package/dist/types/types/enums/index.d.ts +0 -1
- package/dist/types/types/firebase_types.js +0 -1
- package/dist/types/types/global.js +0 -1
- package/dist/types/types/index.d.ts +0 -3
- package/dist/types/types/index.js +0 -2
- package/dist/types/types/interfaces/email.d.ts +0 -33
- package/dist/types/types/interfaces/global.d.ts +0 -23
- package/dist/types/types/interfaces/index.d.ts +0 -2
- package/dist/types/types/types/firebase_types.d.ts +0 -50
- package/dist/types/types/types/global.d.ts +0 -17
- package/dist/types/types/types/index.d.ts +0 -2
- /package/dist/{types/interfaces/email.js → cjs/managers/socket_manager.d.ts} +0 -0
- /package/dist/{types/interfaces/global.js → esm/managers/socket_manager.d.ts} +0 -0
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { REDIS_UPDATES_PREFIX } from "akeyless-types-commons";
|
|
2
|
-
const get_key = (...args) => {
|
|
3
|
-
return args.join(":");
|
|
4
|
-
};
|
|
5
|
-
export const get_doc_key = (collection, doc_id) => {
|
|
6
|
-
return get_key(collection, doc_id);
|
|
7
|
-
};
|
|
8
|
-
export const get_collection_keys = (collection) => {
|
|
9
|
-
return get_key(collection, "*");
|
|
10
|
-
};
|
|
11
|
-
export const get_channel = (...args) => {
|
|
12
|
-
return get_key(REDIS_UPDATES_PREFIX, ...args);
|
|
13
|
-
};
|
|
14
|
-
export const scan_redis_keys = async (pattern, redis_publisher) => {
|
|
15
|
-
const found_keys = [];
|
|
16
|
-
let cursor = "0";
|
|
17
|
-
do {
|
|
18
|
-
const [next_cursor, keys] = await redis_publisher.scan(cursor, "MATCH", pattern, "COUNT", 100);
|
|
19
|
-
cursor = next_cursor;
|
|
20
|
-
found_keys.push(...keys);
|
|
21
|
-
} while (cursor !== "0");
|
|
22
|
-
return found_keys;
|
|
23
|
-
};
|
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { get_redis_commander, get_redis_listener, redis_commander_connected, redis_listener_connected } from "./initialize";
|
|
2
|
-
import { get_nx_settings, parse_add_update_as_array, parse_add_update_as_object, parse_delete_as_array, parse_delete_as_object, snapshot, } from "../firebase_helpers";
|
|
3
|
-
import { get_collection_keys, scan_redis_keys } from "./keys";
|
|
4
|
-
import { Timestamp } from "firebase-admin/firestore";
|
|
5
|
-
import { logger } from "../../managers";
|
|
6
|
-
const subscription_collections = new Set();
|
|
7
|
-
export const redis_snapshots_bulk = async (configs) => {
|
|
8
|
-
/// on first time
|
|
9
|
-
for (const config of configs) {
|
|
10
|
-
const validation = await validate_config(config);
|
|
11
|
-
if (!validation.success) {
|
|
12
|
-
logger.warn(validation.message, config);
|
|
13
|
-
if (validation.trigger_firebase_snapshot) {
|
|
14
|
-
await snapshot(config);
|
|
15
|
-
}
|
|
16
|
-
continue;
|
|
17
|
-
}
|
|
18
|
-
await parse_redis_snapshot(config);
|
|
19
|
-
}
|
|
20
|
-
/// on update
|
|
21
|
-
get_redis_listener().on("pmessage", async (pattern, channel, message) => {
|
|
22
|
-
const { collection_name, update_type, data: doc } = JSON.parse(message);
|
|
23
|
-
const update = [convert_object_timestamps(doc)];
|
|
24
|
-
const relevant_configs = configs.filter((config) => config.collection_name === collection_name);
|
|
25
|
-
for (const config of relevant_configs) {
|
|
26
|
-
await parse_redis_snapshot(config, { update_type, update });
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
|
-
const validate_config = async (config) => {
|
|
31
|
-
const nx_settings = await get_nx_settings();
|
|
32
|
-
const cache_collections_config = nx_settings.cache_collections_config || {};
|
|
33
|
-
const { collection_name, cache_name = collection_name, subscription_type = "firebase" } = config;
|
|
34
|
-
if (!redis_commander_connected || !redis_listener_connected) {
|
|
35
|
-
return {
|
|
36
|
-
success: false,
|
|
37
|
-
trigger_firebase_snapshot: true,
|
|
38
|
-
message: `⚠️ Redis commander or listener not connected, triggered firebase snapshot for collection: "${collection_name}"`,
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
if (subscription_type === "firebase") {
|
|
42
|
-
return {
|
|
43
|
-
success: false,
|
|
44
|
-
trigger_firebase_snapshot: true,
|
|
45
|
-
message: `⚠️ subscription type is "firebase", triggered firebase snapshot for collection: "${collection_name}"`,
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
if (!cache_collections_config[collection_name] || cache_collections_config[collection_name].sync_direction === "redis_to_firebase") {
|
|
49
|
-
return {
|
|
50
|
-
success: false,
|
|
51
|
-
trigger_firebase_snapshot: true,
|
|
52
|
-
message: `⚠️ collection: "${collection_name}" not found in cache_collections_config in "nx-settings" or sync_direction is "redis_to_firebase", triggered firebase snapshot for collection: "${collection_name}"`,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
if (subscription_collections.has(cache_name)) {
|
|
56
|
-
return {
|
|
57
|
-
success: false,
|
|
58
|
-
trigger_firebase_snapshot: false,
|
|
59
|
-
message: `⚠️ collection: "${collection_name}" already has a subscription, skipped redis snapshot for collection: "${collection_name}"`,
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
subscription_collections.add(cache_name);
|
|
63
|
-
return { success: true };
|
|
64
|
-
};
|
|
65
|
-
const parse_redis_snapshot = async (config, redis_update) => {
|
|
66
|
-
const { collection_name: config_collection_name, cache_name = config_collection_name, extra_parsers, parse_as, debug, on_add, on_remove, on_modify, } = config;
|
|
67
|
-
if (!redis_update) {
|
|
68
|
-
const init_data = await get_collection_data(config_collection_name);
|
|
69
|
-
default_parsers(parse_as, init_data, config);
|
|
70
|
-
if (debug?.on_first_time) {
|
|
71
|
-
logger.log(`${cache_name} => Redis snapshot on first time: `, debug.on_first_time === "documents" ? init_data : { length: init_data.length });
|
|
72
|
-
}
|
|
73
|
-
config.on_first_time?.(init_data, config);
|
|
74
|
-
extra_parsers?.forEach((parser) => {
|
|
75
|
-
if (debug?.extra_parsers?.on_first_time) {
|
|
76
|
-
logger.log(`${cache_name} => Redis snapshot extra parsers on first time: `, debug.extra_parsers.on_first_time === "documents" ? init_data : { length: init_data.length });
|
|
77
|
-
}
|
|
78
|
-
parser?.on_first_time?.(init_data, config);
|
|
79
|
-
});
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
if (!subscription_collections.has(cache_name)) {
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
const { update_type, update } = redis_update;
|
|
86
|
-
switch (update_type) {
|
|
87
|
-
case "add":
|
|
88
|
-
if (debug?.on_add) {
|
|
89
|
-
logger.log(`${cache_name} => Redis snapshot on add: `, update);
|
|
90
|
-
}
|
|
91
|
-
default_parsers(parse_as, update, config);
|
|
92
|
-
on_add?.(update, config);
|
|
93
|
-
extra_parsers?.forEach((parser) => {
|
|
94
|
-
if (debug?.extra_parsers?.on_add) {
|
|
95
|
-
logger.log(`${cache_name} => Redis snapshot extra parsers on add: `, update);
|
|
96
|
-
}
|
|
97
|
-
parser?.on_add?.(update, config);
|
|
98
|
-
});
|
|
99
|
-
return;
|
|
100
|
-
case "update":
|
|
101
|
-
if (debug?.on_modify) {
|
|
102
|
-
logger.log(`${cache_name} => Redis snapshot on modify: `, update);
|
|
103
|
-
}
|
|
104
|
-
default_parsers(parse_as, update, config);
|
|
105
|
-
on_modify?.(update, config);
|
|
106
|
-
extra_parsers?.forEach((parser) => {
|
|
107
|
-
if (debug?.extra_parsers?.on_modify) {
|
|
108
|
-
logger.log(`${cache_name} => Redis snapshot extra parsers on modify: `, update);
|
|
109
|
-
}
|
|
110
|
-
parser?.on_modify?.(update, config);
|
|
111
|
-
});
|
|
112
|
-
return;
|
|
113
|
-
case "delete":
|
|
114
|
-
if (debug?.on_remove) {
|
|
115
|
-
logger.log(`${cache_name} => Redis snapshot on remove: `, update);
|
|
116
|
-
}
|
|
117
|
-
if (parse_as === "object") {
|
|
118
|
-
parse_delete_as_object(update, config);
|
|
119
|
-
}
|
|
120
|
-
if (parse_as === "array") {
|
|
121
|
-
parse_delete_as_array(update, config);
|
|
122
|
-
}
|
|
123
|
-
on_remove?.(update, config);
|
|
124
|
-
extra_parsers?.forEach((parser) => {
|
|
125
|
-
if (debug?.extra_parsers?.on_remove) {
|
|
126
|
-
logger.log(`${cache_name} => Redis snapshot extra parsers on remove: `, update);
|
|
127
|
-
}
|
|
128
|
-
parser?.on_remove?.(update, config);
|
|
129
|
-
});
|
|
130
|
-
return;
|
|
131
|
-
default:
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
const default_parsers = (parse_as, update, config) => {
|
|
136
|
-
if (parse_as === "object") {
|
|
137
|
-
parse_add_update_as_object(update, config);
|
|
138
|
-
}
|
|
139
|
-
if (parse_as === "array") {
|
|
140
|
-
parse_add_update_as_array(update, config);
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
const get_collection_data = async (collection_name) => {
|
|
144
|
-
const redis_commander = get_redis_commander();
|
|
145
|
-
const keys = await scan_redis_keys(get_collection_keys(collection_name), redis_commander);
|
|
146
|
-
let collection_data = [];
|
|
147
|
-
if (keys.length > 0) {
|
|
148
|
-
const values = await redis_commander.mget(keys);
|
|
149
|
-
collection_data = values.filter(Boolean).map((v) => JSON.parse(v).data);
|
|
150
|
-
}
|
|
151
|
-
return collection_data.map((data) => convert_object_timestamps(data));
|
|
152
|
-
};
|
|
153
|
-
const convert_object_timestamps = (data) => {
|
|
154
|
-
Object.entries(data).forEach(([key, value]) => {
|
|
155
|
-
if (typeof value === "object" && value && value._seconds !== undefined) {
|
|
156
|
-
data[key] = new Timestamp(value._seconds, value._nanoseconds);
|
|
157
|
-
}
|
|
158
|
-
if (typeof value === "object" && value && value._seconds === undefined) {
|
|
159
|
-
convert_object_timestamps(value);
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
return data;
|
|
163
|
-
};
|
package/dist/helpers/start.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import express from "express";
|
|
2
|
-
import cors from "cors";
|
|
3
|
-
import { logger } from "../managers";
|
|
4
|
-
import { init_env_variables, init_snapshots } from "./";
|
|
5
|
-
import { error_handler } from "../middlewares/error_handling";
|
|
6
|
-
import { request_logger, trim_body_middleware } from "../middlewares";
|
|
7
|
-
import { init_redis } from "./redis/initialize";
|
|
8
|
-
export const start_server = async (main_router, project_name, version, { port, log_requests } = {}) => {
|
|
9
|
-
const app = express();
|
|
10
|
-
let env_data = init_env_variables(["mode"]);
|
|
11
|
-
port = port || Number(env_data.port);
|
|
12
|
-
app.use(cors());
|
|
13
|
-
app.use(express.json({ limit: "10mb" }));
|
|
14
|
-
app.use(express.urlencoded({ limit: "10mb", extended: true }));
|
|
15
|
-
app.use(trim_body_middleware());
|
|
16
|
-
app.use(request_logger(log_requests || {}));
|
|
17
|
-
main_router(app);
|
|
18
|
-
app.use(error_handler);
|
|
19
|
-
return new Promise((resolve) => {
|
|
20
|
-
app.listen(port, async () => {
|
|
21
|
-
logger.log(`Server is running at http://localhost:${port}`);
|
|
22
|
-
logger.log("project status", { project_name, version, environment: env_data.mode });
|
|
23
|
-
try {
|
|
24
|
-
await init_redis();
|
|
25
|
-
}
|
|
26
|
-
catch (err) {
|
|
27
|
-
logger.warn("Redis unavailable, continuing without it", { err });
|
|
28
|
-
}
|
|
29
|
-
resolve(app);
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
};
|
|
33
|
-
export const basic_init = async (main_router, project_name, version, { port, log_requests, init_snapshot_options } = {}) => {
|
|
34
|
-
try {
|
|
35
|
-
const app = await start_server(main_router, project_name, version, { port, log_requests });
|
|
36
|
-
await init_snapshots(init_snapshot_options);
|
|
37
|
-
return app;
|
|
38
|
-
}
|
|
39
|
-
catch (error) {
|
|
40
|
-
logger.error("Error from init function: ", error);
|
|
41
|
-
process.exit(1);
|
|
42
|
-
}
|
|
43
|
-
};
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import { cache_manager, logger } from "../managers";
|
|
2
|
-
import { get_document_by_id_optional, set_document } from "./firebase_helpers";
|
|
3
|
-
import admin from "firebase-admin";
|
|
4
|
-
export var TaskName;
|
|
5
|
-
(function (TaskName) {
|
|
6
|
-
TaskName["send_reset_sms"] = "send_reset_sms";
|
|
7
|
-
TaskName["collect_devices_health"] = "collect_devices_health";
|
|
8
|
-
TaskName["collect_charge_locations"] = "collect_charge_locations";
|
|
9
|
-
TaskName["collect_charge_cdrs"] = "collect_charge_cdrs";
|
|
10
|
-
})(TaskName || (TaskName = {}));
|
|
11
|
-
export var TaskStatus;
|
|
12
|
-
(function (TaskStatus) {
|
|
13
|
-
TaskStatus["running"] = "running";
|
|
14
|
-
TaskStatus["completed"] = "completed";
|
|
15
|
-
TaskStatus["failed"] = "failed";
|
|
16
|
-
TaskStatus["suspeneded"] = "suspeneded";
|
|
17
|
-
})(TaskStatus || (TaskStatus = {}));
|
|
18
|
-
const save_task_data_in_cache = (task_name, data) => {
|
|
19
|
-
if (Array.isArray(data)) {
|
|
20
|
-
cache_manager.setArrayData(task_name, data);
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
cache_manager.setObjectData(task_name, data);
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
export const execute_task = async (source, task_name, task, options) => {
|
|
27
|
-
const { debug_logs = true, save_in } = options || {};
|
|
28
|
-
try {
|
|
29
|
-
await set_document("nx-tasks", task_name, {
|
|
30
|
-
source,
|
|
31
|
-
status: TaskStatus.running,
|
|
32
|
-
started: new Date(),
|
|
33
|
-
timestamp: new Date(),
|
|
34
|
-
error: "",
|
|
35
|
-
}, false);
|
|
36
|
-
if (debug_logs) {
|
|
37
|
-
logger.log(`Task [${task_name}] started`);
|
|
38
|
-
}
|
|
39
|
-
const start = performance.now();
|
|
40
|
-
const data = await task();
|
|
41
|
-
const update = {
|
|
42
|
-
status: TaskStatus.completed,
|
|
43
|
-
completed: new Date(),
|
|
44
|
-
timestamp: new Date(),
|
|
45
|
-
};
|
|
46
|
-
if (data) {
|
|
47
|
-
if (save_in === "none") {
|
|
48
|
-
update.data = "no data to save";
|
|
49
|
-
}
|
|
50
|
-
else if (save_in === "storage" && typeof data === "object") {
|
|
51
|
-
save_task_data_in_cache(task_name, data);
|
|
52
|
-
const url = await keep_task_data_in_storage(task_name, data);
|
|
53
|
-
update.data = url;
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
save_task_data_in_cache(task_name, data);
|
|
57
|
-
update.data = data;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
update.data = "no data to save";
|
|
62
|
-
}
|
|
63
|
-
await set_document("nx-tasks", task_name, update);
|
|
64
|
-
if (debug_logs) {
|
|
65
|
-
logger.log(`Task [${task_name}] ended. It took ${Math.round(performance.now() - start)} ms`);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
catch (exception) {
|
|
69
|
-
const error_for_db = exception instanceof Error ? exception.message : exception;
|
|
70
|
-
logger.error(`Task [${task_name}] error`, error_for_db);
|
|
71
|
-
await set_document("nx-tasks", task_name, {
|
|
72
|
-
status: TaskStatus.failed,
|
|
73
|
-
completed: new Date(),
|
|
74
|
-
timestamp: new Date(),
|
|
75
|
-
error: error_for_db,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
export const get_task_data = async (task_name) => {
|
|
80
|
-
const cached_data = cache_manager.getArrayData(task_name);
|
|
81
|
-
const cached_data_object = cache_manager.getObjectData(task_name, null);
|
|
82
|
-
if (cached_data.length > 0) {
|
|
83
|
-
return cached_data;
|
|
84
|
-
}
|
|
85
|
-
if (cached_data_object) {
|
|
86
|
-
return cached_data;
|
|
87
|
-
}
|
|
88
|
-
const task_data = await get_document_by_id_optional("nx-tasks", task_name);
|
|
89
|
-
if (typeof task_data?.data === "string" && task_data.data.startsWith("http")) {
|
|
90
|
-
const storage_data = await get_task_data_from_storage(task_name);
|
|
91
|
-
if (storage_data) {
|
|
92
|
-
const value = storage_data;
|
|
93
|
-
save_task_data_in_cache(task_name, storage_data);
|
|
94
|
-
return value;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
const value = typeof task_data?.data === "object" ? task_data.data : null;
|
|
98
|
-
if (value) {
|
|
99
|
-
save_task_data_in_cache(task_name, value);
|
|
100
|
-
}
|
|
101
|
-
return value || [];
|
|
102
|
-
};
|
|
103
|
-
export const get_task_data_from_storage = async (task_name) => {
|
|
104
|
-
const bucket = admin.storage().bucket();
|
|
105
|
-
const file = bucket.file(`tasks_data/${task_name}.json`);
|
|
106
|
-
try {
|
|
107
|
-
const [contents] = await file.download();
|
|
108
|
-
return JSON.parse(contents.toString("utf-8"));
|
|
109
|
-
}
|
|
110
|
-
catch (error) {
|
|
111
|
-
logger.error("Task error reading file from Firebase Storage:", error);
|
|
112
|
-
return null;
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
export const keep_task_data_in_storage = async (task_name, data) => {
|
|
116
|
-
const bucket = admin.storage().bucket();
|
|
117
|
-
const file = bucket.file(`tasks_data/${task_name}.json`);
|
|
118
|
-
try {
|
|
119
|
-
if (typeof data !== "object") {
|
|
120
|
-
throw new Error("Only arrays or objects can be written");
|
|
121
|
-
}
|
|
122
|
-
const json_string = JSON.stringify(data, null);
|
|
123
|
-
await file.save(json_string, {
|
|
124
|
-
contentType: "application/json",
|
|
125
|
-
});
|
|
126
|
-
const [signed_url] = await file.getSignedUrl({
|
|
127
|
-
action: "read",
|
|
128
|
-
expires: Date.now() + 1000 * 60 * 60 * 24 * 7,
|
|
129
|
-
});
|
|
130
|
-
return signed_url;
|
|
131
|
-
}
|
|
132
|
-
catch (error) {
|
|
133
|
-
logger.error("Error writing file to Firebase Storage:", error);
|
|
134
|
-
return "";
|
|
135
|
-
}
|
|
136
|
-
};
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
import { Timestamp } from "firebase-admin/firestore";
|
|
2
|
-
import moment from "moment";
|
|
3
|
-
export function sort_by_timestamp(a, b, reverse = false) {
|
|
4
|
-
return reverse ? any_datetime_to_millis(b) - any_datetime_to_millis(a) : any_datetime_to_millis(a) - any_datetime_to_millis(b);
|
|
5
|
-
}
|
|
6
|
-
export const calculate_time_passed = (datetime) => {
|
|
7
|
-
const now = new Date();
|
|
8
|
-
const diff_in_milliseconds = Math.max(0, now.getTime() - datetime.getTime());
|
|
9
|
-
const seconds_passed = Math.floor(diff_in_milliseconds / 1000);
|
|
10
|
-
const minutes_passed = Math.floor(seconds_passed / 60);
|
|
11
|
-
const hours_passed = Math.floor(minutes_passed / 60);
|
|
12
|
-
const days_passed = Math.floor(hours_passed / 24);
|
|
13
|
-
const remaining_hours = hours_passed % 24;
|
|
14
|
-
const remaining_minutes = minutes_passed % 60;
|
|
15
|
-
const remaining_seconds = seconds_passed % 60;
|
|
16
|
-
const pad_zero = (value) => (value < 10 ? `0${value}` : `${value}`);
|
|
17
|
-
let parts = [];
|
|
18
|
-
if (days_passed > 0)
|
|
19
|
-
parts.push(days_passed);
|
|
20
|
-
if (remaining_hours > 0 || days_passed > 0)
|
|
21
|
-
parts.push(pad_zero(remaining_hours));
|
|
22
|
-
parts.push(pad_zero(remaining_minutes));
|
|
23
|
-
parts.push(pad_zero(remaining_seconds));
|
|
24
|
-
const time_passed_formatted_short = parts.join(":");
|
|
25
|
-
parts = [];
|
|
26
|
-
if (days_passed > 0)
|
|
27
|
-
parts.push(`${days_passed} d`);
|
|
28
|
-
if (days_passed > 0 || remaining_hours > 0)
|
|
29
|
-
parts.push(`${remaining_hours} h`);
|
|
30
|
-
if (days_passed > 0 || remaining_hours > 0 || remaining_minutes > 0)
|
|
31
|
-
parts.push(`${remaining_minutes} min`);
|
|
32
|
-
parts.push(`${remaining_seconds} sec`);
|
|
33
|
-
const time_passed_formatted_long = parts.join(" ");
|
|
34
|
-
return {
|
|
35
|
-
seconds_passed,
|
|
36
|
-
minutes_passed,
|
|
37
|
-
hours_passed,
|
|
38
|
-
days_passed,
|
|
39
|
-
time_passed_formatted_short,
|
|
40
|
-
time_passed_formatted_long,
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
export function any_datetime_to_string(firebaseTimestamp, options) {
|
|
44
|
-
const { defaultReturnedValue, format, fromFormat, tz, debug } = {
|
|
45
|
-
defaultReturnedValue: options?.defaultReturnedValue ?? "-",
|
|
46
|
-
format: options?.format ?? "DD/MM/YYYY HH:mm:ss",
|
|
47
|
-
fromFormat: options?.fromFormat ?? "DD/MM/YYYY HH:mm:ss",
|
|
48
|
-
tz: options?.tz,
|
|
49
|
-
debug: options?.debug ?? false,
|
|
50
|
-
};
|
|
51
|
-
let date;
|
|
52
|
-
switch (true) {
|
|
53
|
-
case !firebaseTimestamp: {
|
|
54
|
-
return defaultReturnedValue;
|
|
55
|
-
}
|
|
56
|
-
case firebaseTimestamp instanceof Timestamp: {
|
|
57
|
-
date = firebaseTimestamp.toDate();
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
case firebaseTimestamp instanceof Date: {
|
|
61
|
-
date = firebaseTimestamp;
|
|
62
|
-
break;
|
|
63
|
-
}
|
|
64
|
-
case typeof firebaseTimestamp === "string" || typeof firebaseTimestamp === "number": {
|
|
65
|
-
const m = moment.utc(firebaseTimestamp, fromFormat);
|
|
66
|
-
switch (m.isValid()) {
|
|
67
|
-
case true:
|
|
68
|
-
date = m.toDate();
|
|
69
|
-
break;
|
|
70
|
-
default:
|
|
71
|
-
return defaultReturnedValue;
|
|
72
|
-
}
|
|
73
|
-
break;
|
|
74
|
-
}
|
|
75
|
-
case !!firebaseTimestamp._seconds: {
|
|
76
|
-
const ft = firebaseTimestamp;
|
|
77
|
-
date = new Date(ft._seconds * 1000 + (ft._nanoseconds ?? 0) / 1000000);
|
|
78
|
-
break;
|
|
79
|
-
}
|
|
80
|
-
case !!firebaseTimestamp.seconds: {
|
|
81
|
-
const ft = firebaseTimestamp;
|
|
82
|
-
date = new Date(ft.seconds * 1000 + ft.nanoseconds / 1000000);
|
|
83
|
-
break;
|
|
84
|
-
}
|
|
85
|
-
default: {
|
|
86
|
-
if (debug) {
|
|
87
|
-
console.error("Invalid timestamp format: ", firebaseTimestamp);
|
|
88
|
-
}
|
|
89
|
-
return defaultReturnedValue;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
switch (Boolean(tz)) {
|
|
93
|
-
case true:
|
|
94
|
-
return moment(date)
|
|
95
|
-
.tz(tz)
|
|
96
|
-
.format(format);
|
|
97
|
-
default:
|
|
98
|
-
return moment.utc(date).format(format);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
export function any_datetime_to_millis(firebaseTimestamp, options) {
|
|
102
|
-
const { defaultReturnedValue, fromFormat, tz, debug } = {
|
|
103
|
-
defaultReturnedValue: options?.defaultReturnedValue ?? 0,
|
|
104
|
-
fromFormat: options?.fromFormat ?? "DD/MM/YYYY HH:mm:ss",
|
|
105
|
-
tz: options?.tz,
|
|
106
|
-
debug: options?.debug ?? false,
|
|
107
|
-
};
|
|
108
|
-
switch (true) {
|
|
109
|
-
case !firebaseTimestamp: {
|
|
110
|
-
return defaultReturnedValue;
|
|
111
|
-
}
|
|
112
|
-
case firebaseTimestamp instanceof Timestamp: {
|
|
113
|
-
return firebaseTimestamp.toMillis();
|
|
114
|
-
}
|
|
115
|
-
case firebaseTimestamp instanceof Date: {
|
|
116
|
-
const ms = firebaseTimestamp.getTime();
|
|
117
|
-
return isNaN(ms) ? defaultReturnedValue : ms;
|
|
118
|
-
}
|
|
119
|
-
case typeof firebaseTimestamp === "string" || typeof firebaseTimestamp === "number": {
|
|
120
|
-
const m = moment.utc(firebaseTimestamp, fromFormat);
|
|
121
|
-
switch (m.isValid()) {
|
|
122
|
-
case true:
|
|
123
|
-
return m.valueOf();
|
|
124
|
-
default:
|
|
125
|
-
return defaultReturnedValue;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
case !!firebaseTimestamp._seconds: {
|
|
129
|
-
const seconds = firebaseTimestamp._seconds;
|
|
130
|
-
const nanos = firebaseTimestamp._nanoseconds ?? 0;
|
|
131
|
-
return seconds * 1000 + Math.floor(nanos / 1000000);
|
|
132
|
-
}
|
|
133
|
-
case !!firebaseTimestamp.seconds: {
|
|
134
|
-
const seconds = firebaseTimestamp.seconds;
|
|
135
|
-
const nanos = firebaseTimestamp.nanoseconds ?? 0;
|
|
136
|
-
return seconds * 1000 + Math.floor(nanos / 1000000);
|
|
137
|
-
}
|
|
138
|
-
default: {
|
|
139
|
-
if (debug) {
|
|
140
|
-
console.error("Invalid timestamp format: ", firebaseTimestamp);
|
|
141
|
-
}
|
|
142
|
-
return defaultReturnedValue;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
// old versions of the functions kept for backward compatibility
|
|
147
|
-
/**
|
|
148
|
-
* Converts a Firebase Timestamp object into a formatted string.
|
|
149
|
-
*
|
|
150
|
-
* @param {firebase_timestamp} firebaseTimestamp - The Firebase timestamp object containing _seconds and _nanoseconds.
|
|
151
|
-
* @param {string} [format="DD-MM-YYYY HH:mm:ss"] - Optional the format string used to format the date. Default is "DD-MM-YYYY HH:mm:ss".
|
|
152
|
-
* @returns {string} - A formatted date string according to the specified format or the default format.
|
|
153
|
-
*/
|
|
154
|
-
export function timestamp_to_string(firebaseTimestamp, format = "DD-MM-YYYY HH:mm:ss") {
|
|
155
|
-
const timestamp = new Timestamp(firebaseTimestamp._seconds, firebaseTimestamp._nanoseconds);
|
|
156
|
-
return moment(timestamp.toDate()).utc().format(format);
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Converts a Firebase Timestamp object into milliseconds since the Unix epoch.
|
|
160
|
-
*
|
|
161
|
-
* @param {firebase_timestamp} firebaseTimestamp - The Firebase timestamp object containing _seconds and _nanoseconds.
|
|
162
|
-
* @returns {number} - Time in milliseconds
|
|
163
|
-
*/
|
|
164
|
-
export function timestamp_to_millis(firebaseTimestamp) {
|
|
165
|
-
const timestamp = new Timestamp(firebaseTimestamp._seconds, firebaseTimestamp._nanoseconds);
|
|
166
|
-
return timestamp.toMillis();
|
|
167
|
-
}
|
package/dist/index.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export class CacheManager {
|
|
2
|
-
constructor() {
|
|
3
|
-
this.data = {};
|
|
4
|
-
}
|
|
5
|
-
static getInstance() {
|
|
6
|
-
if (!CacheManager.instance) {
|
|
7
|
-
CacheManager.instance = new CacheManager();
|
|
8
|
-
}
|
|
9
|
-
return CacheManager.instance;
|
|
10
|
-
}
|
|
11
|
-
setArrayData(key, data) {
|
|
12
|
-
this.data[key] = data;
|
|
13
|
-
}
|
|
14
|
-
getArrayData(key) {
|
|
15
|
-
if (!this.data[key]) {
|
|
16
|
-
return [];
|
|
17
|
-
}
|
|
18
|
-
return this.data[key];
|
|
19
|
-
}
|
|
20
|
-
setObjectData(key, data) {
|
|
21
|
-
this.data[key] = data;
|
|
22
|
-
}
|
|
23
|
-
getObjectData(key, default_value = null) {
|
|
24
|
-
return this.data[key] || default_value;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
export const cache_manager = CacheManager.getInstance();
|
package/dist/managers/index.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import moment_timezone from "moment-timezone";
|
|
2
|
-
import axios from "axios";
|
|
3
|
-
import { isObject } from "lodash";
|
|
4
|
-
import { parse_error } from "../helpers";
|
|
5
|
-
class LoggerManager {
|
|
6
|
-
constructor() { }
|
|
7
|
-
static getInstance() {
|
|
8
|
-
if (!LoggerManager.instance) {
|
|
9
|
-
LoggerManager.instance = new LoggerManager();
|
|
10
|
-
}
|
|
11
|
-
return LoggerManager.instance;
|
|
12
|
-
}
|
|
13
|
-
get_date() {
|
|
14
|
-
return moment_timezone().tz("Asia/Jerusalem").format("DD/MM/YYYY HH:mm:ss.SS");
|
|
15
|
-
}
|
|
16
|
-
log(msg, data) {
|
|
17
|
-
const is_table = !process.env.KUBERNETES_SERVICE_HOST &&
|
|
18
|
-
Array.isArray(data) &&
|
|
19
|
-
data.length > 1 &&
|
|
20
|
-
data.every((val) => {
|
|
21
|
-
if (typeof val === "object" && !Array.isArray(val)) {
|
|
22
|
-
return Object.values(val).every((v) => ["string", "number", "boolean"].includes(typeof v) || v === null);
|
|
23
|
-
}
|
|
24
|
-
return false;
|
|
25
|
-
}) &&
|
|
26
|
-
data.some((val) => Object.values(val).length > 1);
|
|
27
|
-
if (is_table) {
|
|
28
|
-
console.log(`${this.get_date()} - `, msg, ": ");
|
|
29
|
-
console.table(data);
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
console.log(`${this.get_date()} - ${msg}`, data === undefined ? "" : `: ${isObject(data) || Array.isArray(data) ? JSON.stringify(data) : data}`);
|
|
33
|
-
}
|
|
34
|
-
error(msg, data) {
|
|
35
|
-
if (axios.isAxiosError(data)) {
|
|
36
|
-
if (!!data.response?.data) {
|
|
37
|
-
console.error(`${this.get_date()} - ${msg}, axios error: ${data.message}, data: ${JSON.stringify(data)}`);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
console.error(`${this.get_date()} - ${msg}, axios error: ${data.message}`);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
console.error(`${this.get_date()} - ${msg}`, data === undefined ? "" : `: ${JSON.stringify(parse_error(data))}`);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
warn(msg, data) {
|
|
48
|
-
console.warn(`${this.get_date()} - ${msg}`, data === undefined ? "" : `: ${JSON.stringify(data)}`);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
export const logger = LoggerManager.getInstance();
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
export class TranslationManager {
|
|
2
|
-
constructor() {
|
|
3
|
-
this.data = {};
|
|
4
|
-
}
|
|
5
|
-
static getInstance() {
|
|
6
|
-
if (!TranslationManager.instance) {
|
|
7
|
-
TranslationManager.instance = new TranslationManager();
|
|
8
|
-
}
|
|
9
|
-
return TranslationManager.instance;
|
|
10
|
-
}
|
|
11
|
-
setTranslationData(data) {
|
|
12
|
-
this.data = data;
|
|
13
|
-
}
|
|
14
|
-
getTranslationData() {
|
|
15
|
-
return this.data;
|
|
16
|
-
}
|
|
17
|
-
get_translation(scope, lang, entity, key) {
|
|
18
|
-
key = entity + (entity === "" ? "" : "__") + key;
|
|
19
|
-
return this.data[scope][lang][key] || key;
|
|
20
|
-
}
|
|
21
|
-
get_sms(lang, entity, key) {
|
|
22
|
-
try {
|
|
23
|
-
return this.data["sms"][lang][entity + "__" + key];
|
|
24
|
-
}
|
|
25
|
-
catch (error) {
|
|
26
|
-
return "N/A";
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
get_email(lang, entity, key) {
|
|
30
|
-
try {
|
|
31
|
-
return this.data["email"][lang][entity + "__" + key];
|
|
32
|
-
}
|
|
33
|
-
catch (error) {
|
|
34
|
-
return "N/A";
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
export const translation_manager = TranslationManager.getInstance();
|