akeyless-server-commons 1.0.13 → 1.0.14
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/global_helpers.d.ts +1 -0
- package/dist/cjs/helpers/global_helpers.js +3 -1
- package/dist/cjs/helpers/global_helpers.js.map +1 -1
- package/dist/esm/helpers/global_helpers.d.ts +1 -0
- package/dist/esm/helpers/global_helpers.js +1 -0
- package/dist/esm/helpers/global_helpers.js.map +1 -1
- package/package.json +1 -1
- package/dist/helpers/email_helpers.js +0 -69
- package/dist/helpers/firebase_helpers.js +0 -370
- package/dist/helpers/global_helpers.js +0 -113
- package/dist/helpers/index.js +0 -8
- package/dist/helpers/login_helpers.js +0 -18
- package/dist/helpers/notification_helpers.js +0 -230
- package/dist/helpers/phone_number_helpers.js +0 -88
- package/dist/helpers/start.js +0 -42
- package/dist/helpers/time_helpers.js +0 -26
- 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 -49
- package/dist/managers/translation_manager.js +0 -38
- package/dist/middlewares/auth_mw.js +0 -46
- package/dist/middlewares/error_handling.js +0 -19
- package/dist/middlewares/global_mw.js +0 -50
- package/dist/middlewares/index.js +0 -3
- package/dist/test.js +0 -15
- package/dist/types/enums/global.js +0 -18
- package/dist/types/enums/index.js +0 -1
- package/dist/types/helpers/email_helpers.d.ts +0 -2
- package/dist/types/helpers/firebase_helpers.d.ts +0 -24
- package/dist/types/helpers/global_helpers.d.ts +0 -11
- package/dist/types/helpers/index.d.ts +0 -8
- package/dist/types/helpers/login_helpers.d.ts +0 -3
- package/dist/types/helpers/notification_helpers.d.ts +0 -11
- package/dist/types/helpers/phone_number_helpers.d.ts +0 -13
- package/dist/types/helpers/start.d.ts +0 -5
- package/dist/types/helpers/time_helpers.d.ts +0 -17
- package/dist/types/index.d.ts +0 -5
- package/dist/types/index.js +0 -3
- package/dist/types/interfaces/email.js +0 -1
- package/dist/types/interfaces/global.js +0 -1
- 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 -2
- package/dist/types/middlewares/index.d.ts +0 -3
- package/dist/types/test.d.ts +0 -1
- package/dist/types/types/enums/global.d.ts +0 -16
- 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 -24
- package/dist/types/types/interfaces/global.d.ts +0 -12
- package/dist/types/types/interfaces/index.d.ts +0 -2
- package/dist/types/types/types/firebase_types.d.ts +0 -30
- package/dist/types/types/types/global.d.ts +0 -15
- package/dist/types/types/types/index.d.ts +0 -2
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { query_document_optional } from "./firebase_helpers";
|
|
2
|
-
export const convert_to_short_phone_number = (phone_number) => {
|
|
3
|
-
return `0${phone_number.split("+972")[1]}`;
|
|
4
|
-
};
|
|
5
|
-
export const get_user_by_identifier = async (identifier) => {
|
|
6
|
-
const phone_query = await query_document_optional("nx-users", "phone_number", "in", [
|
|
7
|
-
convert_to_short_phone_number(identifier),
|
|
8
|
-
identifier,
|
|
9
|
-
]);
|
|
10
|
-
if (!phone_query) {
|
|
11
|
-
const email_query = await query_document_optional("nx-users", "email", "==", identifier);
|
|
12
|
-
if (!email_query) {
|
|
13
|
-
throw "User not found " + identifier;
|
|
14
|
-
}
|
|
15
|
-
return email_query;
|
|
16
|
-
}
|
|
17
|
-
return phone_query;
|
|
18
|
-
};
|
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
|
-
import { cache_manager, logger, translation_manager } from "../managers";
|
|
3
|
-
import { add_audit_record } from "./global_helpers";
|
|
4
|
-
import { add_document, messaging } from "./firebase_helpers";
|
|
5
|
-
import { is_iccid, is_international_phone_number } from "./phone_number_helpers";
|
|
6
|
-
import { Twilio } from "twilio";
|
|
7
|
-
import { Timestamp } from "firebase-admin/firestore";
|
|
8
|
-
import { v4 as uniqId } from "uuid";
|
|
9
|
-
import FormData from "form-data";
|
|
10
|
-
const send_local_sms = async (recepient, text, details) => {
|
|
11
|
-
const { sms_provider: { multisend }, } = cache_manager.getObjectData("nx-settings");
|
|
12
|
-
const msgId = uniqId();
|
|
13
|
-
let data = new FormData();
|
|
14
|
-
data.append("user", multisend.user);
|
|
15
|
-
data.append("password", multisend.password);
|
|
16
|
-
data.append("from", multisend.from);
|
|
17
|
-
data.append("recipient", recepient);
|
|
18
|
-
data.append("message", text);
|
|
19
|
-
data.append("customermessageid", msgId);
|
|
20
|
-
const config = {
|
|
21
|
-
method: "post",
|
|
22
|
-
maxBodyLength: Infinity,
|
|
23
|
-
url: "https://api.multisend.co.il/v2/sendsms",
|
|
24
|
-
data: data,
|
|
25
|
-
};
|
|
26
|
-
const response = await axios(config);
|
|
27
|
-
if (response.status !== 200) {
|
|
28
|
-
throw `multisend request failed , status: ${response.status} , data: ${JSON.stringify(response.data)}`;
|
|
29
|
-
}
|
|
30
|
-
if (!response.data.success) {
|
|
31
|
-
throw `http request to multisend error ${JSON.stringify(response.data.error)}`;
|
|
32
|
-
}
|
|
33
|
-
await keep_outgoing_sms(recepient, text, "multisend", msgId, details);
|
|
34
|
-
logger.log("send_local_sms. message sent successfully", { number: recepient, text, response: response.data });
|
|
35
|
-
return "multisend";
|
|
36
|
-
};
|
|
37
|
-
const send_international_sms = async (recepient, text, details) => {
|
|
38
|
-
const { sms_provider: { twilio }, } = cache_manager.getObjectData("nx-settings");
|
|
39
|
-
const twilioClient = new Twilio(twilio.account_sid, twilio.token);
|
|
40
|
-
const message = await twilioClient.messages.create({
|
|
41
|
-
messagingServiceSid: twilio.messaging_service_sid,
|
|
42
|
-
body: text,
|
|
43
|
-
to: recepient,
|
|
44
|
-
});
|
|
45
|
-
if (message.errorMessage) {
|
|
46
|
-
throw `twilioClient.messages.create failed: ${message.errorMessage} `;
|
|
47
|
-
}
|
|
48
|
-
await keep_outgoing_sms(recepient, text, "twilio", message.sid, details);
|
|
49
|
-
logger.log("send_international_sms. message sent successfully", { number: recepient, text, response: message });
|
|
50
|
-
return "twilio";
|
|
51
|
-
};
|
|
52
|
-
const login_to_monogoto = async () => {
|
|
53
|
-
try {
|
|
54
|
-
const { sms_provider: { monogoto }, } = cache_manager.getObjectData("nx-settings");
|
|
55
|
-
const data = { UserName: monogoto.user, Password: monogoto.password };
|
|
56
|
-
const response = await axios({
|
|
57
|
-
method: "post",
|
|
58
|
-
url: `https://console.monogoto.io/Auth`,
|
|
59
|
-
data,
|
|
60
|
-
});
|
|
61
|
-
return response.data;
|
|
62
|
-
}
|
|
63
|
-
catch (error) {
|
|
64
|
-
throw `login_to_monogoto failed: ` + error;
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
const send_iccid_sms = async (recepient, text, details) => {
|
|
68
|
-
const { sms_provider: { monogoto }, } = cache_manager.getObjectData("nx-settings");
|
|
69
|
-
const monogoto_auth = await login_to_monogoto();
|
|
70
|
-
const data = { Message: text, From: monogoto.from };
|
|
71
|
-
const response = await axios({
|
|
72
|
-
method: "post",
|
|
73
|
-
url: `https://console.monogoto.io/thing/ThingId_ICCID_${recepient}/sms`,
|
|
74
|
-
data: data,
|
|
75
|
-
headers: {
|
|
76
|
-
Authorization: `Bearer ${monogoto_auth.token}`,
|
|
77
|
-
apikey: monogoto_auth.CustomerId,
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
if (response.status !== 200) {
|
|
81
|
-
throw `monogoto request failed , status: ${response.status} , data: ${JSON.stringify(response.data)}`;
|
|
82
|
-
}
|
|
83
|
-
await keep_outgoing_sms(recepient, text, "monogoto", response.data, details);
|
|
84
|
-
logger.log("send_iccid_sms. message sent successfully", { number: recepient, text, response: response.data });
|
|
85
|
-
return "monogoto";
|
|
86
|
-
};
|
|
87
|
-
export const send_sms = async (recepient, text, entity_for_audit, details) => {
|
|
88
|
-
try {
|
|
89
|
-
const send = async () => {
|
|
90
|
-
if (is_iccid(recepient)) {
|
|
91
|
-
return await send_iccid_sms(recepient, text, details);
|
|
92
|
-
}
|
|
93
|
-
if (is_international_phone_number(recepient)) {
|
|
94
|
-
return send_international_sms(recepient, text, details);
|
|
95
|
-
}
|
|
96
|
-
return await send_local_sms(recepient, text, details);
|
|
97
|
-
};
|
|
98
|
-
const service = await send();
|
|
99
|
-
await add_audit_record("send_sms", entity_for_audit || "general", {
|
|
100
|
-
recepient,
|
|
101
|
-
message: text,
|
|
102
|
-
service,
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
catch (error) {
|
|
106
|
-
logger.error(`${entity_for_audit}, send_sms failed:`, error);
|
|
107
|
-
throw `${entity_for_audit}, send_sms failed: ` + error;
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
const keep_outgoing_sms = async (recepient, content, service, external_id, details) => {
|
|
111
|
-
const timestamp = Timestamp.now();
|
|
112
|
-
const data = {
|
|
113
|
-
external_id,
|
|
114
|
-
content,
|
|
115
|
-
recipient: recepient,
|
|
116
|
-
service,
|
|
117
|
-
timestamp,
|
|
118
|
-
status: "new",
|
|
119
|
-
};
|
|
120
|
-
if (details) {
|
|
121
|
-
data.details = details;
|
|
122
|
-
}
|
|
123
|
-
await add_document("nx-sms-out", data);
|
|
124
|
-
};
|
|
125
|
-
export const push_event_to_mobile_users = async (event) => {
|
|
126
|
-
const units = cache_manager.getArrayData("units");
|
|
127
|
-
const users_units = cache_manager.getArrayData("usersUnits");
|
|
128
|
-
const mobile_users_app_pro = cache_manager.getArrayData("mobile_users_app_pro");
|
|
129
|
-
const app_pro_extra_pushes = cache_manager.getArrayData("app_pro_extra_pushes");
|
|
130
|
-
console.log(`units: ${units.length}, users_units: ${users_units.length}, mobile_users_app_pro: ${mobile_users_app_pro.length}, app_pro_extra_pushes: ${app_pro_extra_pushes.length}`);
|
|
131
|
-
if (!units.length || !users_units.length || !mobile_users_app_pro.length) {
|
|
132
|
-
throw "push_event_to_mobile_users. missing cached data for any of the following: units, usersUnits, mobile_users_app_pro, app_pro_extra_pushes";
|
|
133
|
-
}
|
|
134
|
-
///-- main driver
|
|
135
|
-
const unit = units.find((unit) => unit.carId == event.car_number);
|
|
136
|
-
const main_driver = mobile_users_app_pro.find((user) => user.short_phone_number == unit.userPhone);
|
|
137
|
-
///-- secondary drivers
|
|
138
|
-
const secondary_units = users_units.filter((unit) => unit.carId == event.car_number);
|
|
139
|
-
const secondary_phone_numbers = secondary_units.map((unit) => unit.phone);
|
|
140
|
-
const secondary_drivers = mobile_users_app_pro.filter((user) => secondary_phone_numbers.includes(user.long_phone_number));
|
|
141
|
-
///-- extra users
|
|
142
|
-
const extra_uids = app_pro_extra_pushes.filter((doc) => doc.car_number == event.car_number).map((doc) => doc.uid);
|
|
143
|
-
const extra_drivers = extra_uids.length > 0 ? mobile_users_app_pro.filter((user) => extra_uids.includes(user.uid)) : [];
|
|
144
|
-
const drivers = [main_driver, ...secondary_drivers, ...extra_drivers];
|
|
145
|
-
for (const mobile_user of drivers) {
|
|
146
|
-
const source = event.source == "erm" || event.source == "erm2" ? "erm" : event.source;
|
|
147
|
-
if (mobile_user.disabled_events?.[event.car_number]?.[source]?.includes(event.event_id)) {
|
|
148
|
-
logger.log(`push_event_to_mobile_users. event ${event.event_id} / ${event.event_name} is disabled for user ${mobile_user.uid} / ${mobile_user.short_phone_number}`);
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
const mobile_user_language = mobile_user.language;
|
|
152
|
-
const language = { heb: "he", en: "en", ru: "ru" }[mobile_user_language];
|
|
153
|
-
const message_title = translation_manager.get_translation("push_notifications", language, "title", "event_from_device");
|
|
154
|
-
const message_body = translation_manager.get_translation("events_from_device", language, "", event.event_name);
|
|
155
|
-
await send_fcm_message(message_title, message_body, [mobile_user.fcm_token], "");
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
export const send_fcm_message = async (title, body, fcm_tokens, custom_sound) => {
|
|
159
|
-
fcm_tokens = [...new Set(fcm_tokens)];
|
|
160
|
-
if (fcm_tokens.length == 0) {
|
|
161
|
-
return {
|
|
162
|
-
success: false,
|
|
163
|
-
response: "No recipients",
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
if (fcm_tokens.length == 0) {
|
|
167
|
-
return {
|
|
168
|
-
success: false,
|
|
169
|
-
response: "No recipients",
|
|
170
|
-
success_count: 0,
|
|
171
|
-
failure_count: 0,
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
const message = {
|
|
175
|
-
tokens: fcm_tokens,
|
|
176
|
-
notification: {
|
|
177
|
-
title: title,
|
|
178
|
-
body: body,
|
|
179
|
-
},
|
|
180
|
-
android: {
|
|
181
|
-
ttl: 3600 * 1000,
|
|
182
|
-
priority: "high",
|
|
183
|
-
notification: {
|
|
184
|
-
sound: custom_sound || "default",
|
|
185
|
-
channelId: custom_sound,
|
|
186
|
-
title: title,
|
|
187
|
-
body: body,
|
|
188
|
-
},
|
|
189
|
-
},
|
|
190
|
-
apns: {
|
|
191
|
-
payload: {
|
|
192
|
-
aps: {
|
|
193
|
-
sound: custom_sound ? `${custom_sound}.wav` : "default",
|
|
194
|
-
alert: {
|
|
195
|
-
title: title,
|
|
196
|
-
body: body,
|
|
197
|
-
},
|
|
198
|
-
},
|
|
199
|
-
},
|
|
200
|
-
},
|
|
201
|
-
};
|
|
202
|
-
try {
|
|
203
|
-
const response = await messaging.sendEachForMulticast(message);
|
|
204
|
-
const { successCount: success_count, failureCount: failure_count, responses } = response;
|
|
205
|
-
if (success_count && !failure_count) {
|
|
206
|
-
logger.log(`send_fcm_message. Successfully sent to all ${success_count} recipients. `, { title, body });
|
|
207
|
-
}
|
|
208
|
-
else if (success_count && failure_count) {
|
|
209
|
-
logger.log(`send_fcm_message. Successfully sent to ${success_count} recipients, but failed to sent to ${failure_count} recipients`, {
|
|
210
|
-
title,
|
|
211
|
-
body,
|
|
212
|
-
responses,
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
else {
|
|
216
|
-
logger.log(`send_fcm_message. Failed to sent to ${failure_count} recipients`, { title, body, responses: response.responses });
|
|
217
|
-
}
|
|
218
|
-
return {
|
|
219
|
-
success: success_count > 0 && failure_count == 0,
|
|
220
|
-
response: JSON.stringify(response.responses),
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
catch (error) {
|
|
224
|
-
logger.error("Exception", error.message);
|
|
225
|
-
return {
|
|
226
|
-
success: false,
|
|
227
|
-
response: `Exception: ${error.message}`,
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
};
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { SimProvider } from "../types/enums";
|
|
2
|
-
export const is_long_phone_number = (phone_number) => {
|
|
3
|
-
return phone_number.startsWith("+");
|
|
4
|
-
};
|
|
5
|
-
export const is_israel_long_phone_number = (phone_number) => {
|
|
6
|
-
return phone_number.startsWith("+9725");
|
|
7
|
-
};
|
|
8
|
-
export const is_international_phone_number = (phone_number) => {
|
|
9
|
-
return is_long_phone_number(phone_number) && !is_israel_long_phone_number(phone_number);
|
|
10
|
-
};
|
|
11
|
-
export const is_iccid = (number) => {
|
|
12
|
-
if (number.length < 19 || number.length > 22)
|
|
13
|
-
return false;
|
|
14
|
-
if (!/^\d+$/.test(number))
|
|
15
|
-
return false;
|
|
16
|
-
if (!number.startsWith("89"))
|
|
17
|
-
return false;
|
|
18
|
-
return true;
|
|
19
|
-
};
|
|
20
|
-
export const convert_to_short_israel_phone = (international_number) => {
|
|
21
|
-
return international_number.replace("+972", "0");
|
|
22
|
-
};
|
|
23
|
-
export const is_sim_provider_partner = (phone_number) => {
|
|
24
|
-
const { short_phone_number } = long_short_phone_numbers(phone_number);
|
|
25
|
-
return short_phone_number.startsWith("054");
|
|
26
|
-
};
|
|
27
|
-
export const is_sim_provider_pelephone = (phone_number) => {
|
|
28
|
-
const { short_phone_number } = long_short_phone_numbers(phone_number);
|
|
29
|
-
return short_phone_number.startsWith("050");
|
|
30
|
-
};
|
|
31
|
-
export const is_sim_provider_celcom = (phone_number) => {
|
|
32
|
-
const { short_phone_number } = long_short_phone_numbers(phone_number);
|
|
33
|
-
return short_phone_number.startsWith("052");
|
|
34
|
-
};
|
|
35
|
-
export const is_sim_provider_monogoto = (phone_number) => {
|
|
36
|
-
return is_iccid(phone_number);
|
|
37
|
-
};
|
|
38
|
-
export const get_sim_provider = (phone_number) => {
|
|
39
|
-
if (is_sim_provider_partner(phone_number)) {
|
|
40
|
-
return SimProvider.partner;
|
|
41
|
-
}
|
|
42
|
-
if (is_sim_provider_pelephone(phone_number)) {
|
|
43
|
-
return SimProvider.pelephone;
|
|
44
|
-
}
|
|
45
|
-
if (is_sim_provider_celcom(phone_number)) {
|
|
46
|
-
return SimProvider.celcom;
|
|
47
|
-
}
|
|
48
|
-
if (is_sim_provider_monogoto(phone_number)) {
|
|
49
|
-
return SimProvider.monogoto;
|
|
50
|
-
}
|
|
51
|
-
return SimProvider.unknown;
|
|
52
|
-
};
|
|
53
|
-
export const long_short_phone_numbers = (phone_number) => {
|
|
54
|
-
phone_number = phone_number.trim();
|
|
55
|
-
if (!phone_number.length) {
|
|
56
|
-
return {
|
|
57
|
-
short_phone_number: phone_number,
|
|
58
|
-
long_phone_number: phone_number,
|
|
59
|
-
is_israeli: true,
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
if (is_iccid(phone_number)) {
|
|
63
|
-
return {
|
|
64
|
-
short_phone_number: phone_number,
|
|
65
|
-
long_phone_number: phone_number,
|
|
66
|
-
is_israeli: false,
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
let short_phone_number = phone_number;
|
|
70
|
-
let long_phone_number = phone_number;
|
|
71
|
-
if (phone_number.startsWith("05")) {
|
|
72
|
-
short_phone_number = phone_number;
|
|
73
|
-
long_phone_number = `+9725${short_phone_number.slice(2)}`;
|
|
74
|
-
}
|
|
75
|
-
else if (phone_number.startsWith("+972")) {
|
|
76
|
-
long_phone_number = phone_number;
|
|
77
|
-
short_phone_number = long_phone_number.replace("+9725", "05");
|
|
78
|
-
}
|
|
79
|
-
else if (phone_number.startsWith("+1")) {
|
|
80
|
-
long_phone_number = phone_number;
|
|
81
|
-
short_phone_number = long_phone_number.replace("+1", "");
|
|
82
|
-
}
|
|
83
|
-
return {
|
|
84
|
-
short_phone_number,
|
|
85
|
-
long_phone_number,
|
|
86
|
-
is_israeli: long_phone_number.startsWith("+972"),
|
|
87
|
-
};
|
|
88
|
-
};
|
package/dist/helpers/start.js
DELETED
|
@@ -1,42 +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
|
-
export const start_server = async (main_router, project_name, version) => {
|
|
7
|
-
const app = express();
|
|
8
|
-
const env_data = init_env_variables(["port", "mode"]);
|
|
9
|
-
app.use(cors());
|
|
10
|
-
app.use(express.json());
|
|
11
|
-
main_router(app);
|
|
12
|
-
app.use(error_handler);
|
|
13
|
-
return new Promise((resolve, reject) => {
|
|
14
|
-
app.listen(Number(env_data.port), () => {
|
|
15
|
-
logger.log(`Server is running at http://localhost:${env_data.port}`);
|
|
16
|
-
logger.log("project status", { project_name, version, environment: env_data.mode });
|
|
17
|
-
resolve(app);
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
export const basic_init = async (main_router, project_name, version) => {
|
|
22
|
-
try {
|
|
23
|
-
await init_snapshots();
|
|
24
|
-
const app = await start_server(main_router, project_name, version);
|
|
25
|
-
return app;
|
|
26
|
-
}
|
|
27
|
-
catch (error) {
|
|
28
|
-
logger.error("Error from init function: ", error);
|
|
29
|
-
process.exit(1);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
export const nextjs_init = async (project_name, version) => {
|
|
33
|
-
try {
|
|
34
|
-
await init_snapshots();
|
|
35
|
-
console.log("project name:", project_name);
|
|
36
|
-
console.log("version :", version);
|
|
37
|
-
}
|
|
38
|
-
catch (error) {
|
|
39
|
-
logger.error("Error from nextjs init function: ", error);
|
|
40
|
-
process.exit(1);
|
|
41
|
-
}
|
|
42
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { Timestamp } from "firebase-admin/firestore";
|
|
2
|
-
import moment from "moment";
|
|
3
|
-
/**
|
|
4
|
-
* Converts a Firebase Timestamp object into a formatted string.
|
|
5
|
-
*
|
|
6
|
-
* @param {firebase_timestamp} firebaseTimestamp - The Firebase timestamp object containing _seconds and _nanoseconds.
|
|
7
|
-
* @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".
|
|
8
|
-
* @returns {string} - A formatted date string according to the specified format or the default format.
|
|
9
|
-
*/
|
|
10
|
-
export function timestamp_to_string(firebaseTimestamp, format = "DD-MM-YYYY HH:mm:ss") {
|
|
11
|
-
const timestamp = new Timestamp(firebaseTimestamp._seconds, firebaseTimestamp._nanoseconds);
|
|
12
|
-
return moment(timestamp.toDate()).utc().format(format);
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Converts a Firebase Timestamp object into milliseconds since the Unix epoch.
|
|
16
|
-
*
|
|
17
|
-
* @param {firebase_timestamp} firebaseTimestamp - The Firebase timestamp object containing _seconds and _nanoseconds.
|
|
18
|
-
* @returns {number} - Time in milliseconds
|
|
19
|
-
*/
|
|
20
|
-
export function timestamp_to_millis(firebaseTimestamp) {
|
|
21
|
-
const timestamp = new Timestamp(firebaseTimestamp._seconds, firebaseTimestamp._nanoseconds);
|
|
22
|
-
return timestamp.toMillis();
|
|
23
|
-
}
|
|
24
|
-
export function sort_by_timestamp(a, b, reverse = false) {
|
|
25
|
-
return reverse ? timestamp_to_millis(b) - timestamp_to_millis(a) : timestamp_to_millis(a) - timestamp_to_millis(b);
|
|
26
|
-
}
|
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,49 +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
|
-
console.error(`${this.get_date()} - ${msg}, axios error message: ${data.message}`);
|
|
37
|
-
if (data.response?.data) {
|
|
38
|
-
this.error("axios error data", data.response.data);
|
|
39
|
-
console.error(`axios error data`, data === undefined ? "" : `: ${JSON.stringify(data)}`);
|
|
40
|
-
}
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
console.error(`${this.get_date()} - ${msg}`, data === undefined ? "" : `: ${JSON.stringify(parse_error(data))}`);
|
|
44
|
-
}
|
|
45
|
-
warn(msg, data) {
|
|
46
|
-
console.warn(`${this.get_date()} - ${msg}`, data === undefined ? "" : `: ${JSON.stringify(data)}`);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
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();
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { verify_token, json_failed, query_document_optional, get_user_by_identifier } from "../helpers";
|
|
2
|
-
import { logger } from "../managers";
|
|
3
|
-
export const verify_user_auth = async (req, res, next) => {
|
|
4
|
-
try {
|
|
5
|
-
const authorization = req.headers.authorization;
|
|
6
|
-
await verify_token(authorization);
|
|
7
|
-
next();
|
|
8
|
-
}
|
|
9
|
-
catch (error) {
|
|
10
|
-
logger.error("error from verify user auth", error);
|
|
11
|
-
res.status(403).send(json_failed(error));
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
export const nx_user_login = async (req, res, next) => {
|
|
15
|
-
try {
|
|
16
|
-
const authorization = req.headers.authorization;
|
|
17
|
-
const user_data = await verify_token(authorization);
|
|
18
|
-
const { phone_number, email } = user_data;
|
|
19
|
-
if (!phone_number && !email) {
|
|
20
|
-
throw "Invalid authorization token";
|
|
21
|
-
}
|
|
22
|
-
const nx_user = await get_user_by_identifier(phone_number || email);
|
|
23
|
-
req.body.user = nx_user;
|
|
24
|
-
next();
|
|
25
|
-
}
|
|
26
|
-
catch (error) {
|
|
27
|
-
res.status(403).send(json_failed(error));
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
export const client_login = async (req, res, next) => {
|
|
31
|
-
try {
|
|
32
|
-
const token = req.headers.authorization;
|
|
33
|
-
if (!token) {
|
|
34
|
-
throw new Error("Authorization token not found.");
|
|
35
|
-
}
|
|
36
|
-
const client_data = (await query_document_optional("nx-clients", "api_token", "==", token));
|
|
37
|
-
if (!client_data) {
|
|
38
|
-
throw new Error(`No client found with token: ${token} .`);
|
|
39
|
-
}
|
|
40
|
-
req.body.client = client_data;
|
|
41
|
-
next();
|
|
42
|
-
}
|
|
43
|
-
catch (error) {
|
|
44
|
-
res.status(403).send(json_failed(error.message || error));
|
|
45
|
-
}
|
|
46
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Utility to handle async errors
|
|
3
|
-
*/
|
|
4
|
-
const async_error_handler = (service) => {
|
|
5
|
-
return (req, res, next) => {
|
|
6
|
-
Promise.resolve(service(req, res)).catch(next);
|
|
7
|
-
};
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* Global error-handling middleware.
|
|
11
|
-
*/
|
|
12
|
-
const error_handler = (err, req, res, next) => {
|
|
13
|
-
console.error("Global Error Handler:", err.stack);
|
|
14
|
-
res.status(500).json({
|
|
15
|
-
status: "error",
|
|
16
|
-
message: err.message || "Internal Server Error",
|
|
17
|
-
});
|
|
18
|
-
};
|
|
19
|
-
export { error_handler, async_error_handler };
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { json_failed } from "../helpers";
|
|
2
|
-
const validateParameter = (data, parameter) => {
|
|
3
|
-
if (data[parameter.key] === undefined) {
|
|
4
|
-
throw `missing mandatory parameter: ${parameter.key}`;
|
|
5
|
-
}
|
|
6
|
-
if (parameter.type === "array" && !Array.isArray(data[parameter.key])) {
|
|
7
|
-
throw `parameter ${parameter.key} must be of type: Array`;
|
|
8
|
-
}
|
|
9
|
-
if (typeof data[parameter.key] !== parameter.type && parameter.type !== "array") {
|
|
10
|
-
throw `parameter ${parameter.key} must be of type: ${parameter.type}`;
|
|
11
|
-
}
|
|
12
|
-
if ((Array.isArray(data[parameter.key]) && parameter.length && data[parameter.key].length < parameter.length) ||
|
|
13
|
-
(parameter.type === "string" && parameter.length && data[parameter.key].length < parameter.length)) {
|
|
14
|
-
throw `parameter ${parameter.key} must have minimum length: ${parameter.length}`;
|
|
15
|
-
}
|
|
16
|
-
if (parameter.type === "object" && parameter.required_keys) {
|
|
17
|
-
const missingKeys = parameter.required_keys.filter((key) => data[parameter.key][key] === undefined);
|
|
18
|
-
if (missingKeys.length > 0) {
|
|
19
|
-
throw `parameter ${parameter.key} is missing required keys: ${missingKeys.join(", ")}`;
|
|
20
|
-
}
|
|
21
|
-
parameter.required_keys.forEach((key) => {
|
|
22
|
-
const value = data[parameter.key][key];
|
|
23
|
-
if ((typeof value === "string" || Array.isArray(value)) && value.length === 0) {
|
|
24
|
-
throw `parameter ${key} in ${parameter.key} must have some length `;
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
export const mandatory = ({ body, headers }) => {
|
|
30
|
-
return (req, res, next) => {
|
|
31
|
-
try {
|
|
32
|
-
const body_data = req.body;
|
|
33
|
-
const headers_data = req.headers;
|
|
34
|
-
if (body) {
|
|
35
|
-
body.forEach((parameter) => {
|
|
36
|
-
validateParameter(body_data, parameter);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
if (headers) {
|
|
40
|
-
headers.forEach((parameter) => {
|
|
41
|
-
validateParameter(headers_data, parameter);
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
next();
|
|
45
|
-
}
|
|
46
|
-
catch (error) {
|
|
47
|
-
return res.status(500).send(json_failed(error));
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
};
|
package/dist/test.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { get_nx_service_urls } from "./helpers";
|
|
2
|
-
const paul = "+972546361975";
|
|
3
|
-
const paulTestPhone = "0547380791";
|
|
4
|
-
const avraham = "+972546559314";
|
|
5
|
-
const avrahamLocal = "0546559314";
|
|
6
|
-
const from2 = "+12185857393";
|
|
7
|
-
const ami_friend = "+18182775344";
|
|
8
|
-
const iccid = "8999925200420110809";
|
|
9
|
-
const iccid2 = "8999925200420110808";
|
|
10
|
-
const ruptelaMsg = "imei";
|
|
11
|
-
// send_sms(avrahamLocal, "test 7", "testing");
|
|
12
|
-
// send_sms(avraham, "test 8", "testing");
|
|
13
|
-
// send_sms(iccid2, ruptelaMsg, "testing") ;
|
|
14
|
-
const nx_service_urls = get_nx_service_urls();
|
|
15
|
-
console.log(nx_service_urls);
|