akeyless-server-commons 1.0.205 → 1.0.207
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/postponed_actions.d.ts +7 -17
- package/dist/cjs/helpers/postponed_actions.js +25 -26
- package/dist/cjs/helpers/postponed_actions.js.map +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/helpers/tasks_helpers.d.ts +2 -1
- package/dist/cjs/helpers/tasks_helpers.js +1 -0
- package/dist/cjs/helpers/tasks_helpers.js.map +1 -1
- 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/socket_manager.js +245 -0
- package/dist/cjs/managers/socket_manager.js.map +1 -0
- package/dist/esm/helpers/postponed_actions.d.ts +7 -17
- package/dist/esm/helpers/postponed_actions.js +12 -12
- package/dist/esm/helpers/postponed_actions.js.map +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/helpers/tasks_helpers.d.ts +2 -1
- package/dist/esm/helpers/tasks_helpers.js +1 -0
- package/dist/esm/helpers/tasks_helpers.js.map +1 -1
- 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/socket_manager.js +243 -0
- package/dist/esm/managers/socket_manager.js.map +1 -0
- package/package.json +1 -1
- package/dist/cjs/helpers/rabbitmq_helpers.d.ts +0 -64
- package/dist/cjs/helpers/rabbitmq_helpers.js +0 -193
- package/dist/cjs/helpers/rabbitmq_helpers.js.map +0 -1
- package/dist/esm/helpers/rabbitmq_helpers.d.ts +0 -64
- package/dist/esm/helpers/rabbitmq_helpers.js +0 -186
- package/dist/esm/helpers/rabbitmq_helpers.js.map +0 -1
- package/dist/helpers/boards_helpers.js +0 -62
- package/dist/helpers/email_helpers.js +0 -153
- package/dist/helpers/firebase_helpers.js +0 -601
- package/dist/helpers/global_helpers.js +0 -147
- package/dist/helpers/index.js +0 -13
- package/dist/helpers/location_helpers.js +0 -15
- package/dist/helpers/login_helpers.js +0 -18
- package/dist/helpers/notification_helpers.js +0 -248
- package/dist/helpers/phone_number_helpers.js +0 -96
- package/dist/helpers/postponed_actions.js +0 -358
- package/dist/helpers/redis/helpers.js +0 -215
- package/dist/helpers/redis/index.js +0 -4
- package/dist/helpers/redis/initialize.js +0 -139
- package/dist/helpers/redis/keys.js +0 -23
- package/dist/helpers/redis/snapshot.js +0 -166
- package/dist/helpers/start.js +0 -83
- package/dist/helpers/tasks_helpers.js +0 -139
- package/dist/helpers/time_helpers.js +0 -167
- package/dist/index.js +0 -5
- package/dist/managers/cache_manager.js +0 -37
- package/dist/managers/index.js +0 -4
- package/dist/managers/logger_manager.js +0 -68
- package/dist/managers/rabbitmq_manager.js +0 -182
- 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 -119
- 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 -56
- package/dist/types/helpers/global_helpers.d.ts +0 -16
- package/dist/types/helpers/index.d.ts +0 -13
- 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 -19
- package/dist/types/helpers/postponed_actions.d.ts +0 -113
- package/dist/types/helpers/redis/helpers.d.ts +0 -11
- package/dist/types/helpers/redis/index.d.ts +0 -4
- 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 -25
- 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 -12
- package/dist/types/managers/index.d.ts +0 -4
- package/dist/types/managers/logger_manager.d.ts +0 -14
- package/dist/types/managers/rabbitmq_manager.d.ts +0 -60
- 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 -27
- 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,186 +0,0 @@
|
|
|
1
|
-
import amqp from "amqplib";
|
|
2
|
-
import { randomUUID as random_uuid } from "node:crypto";
|
|
3
|
-
import { logger } from "../managers/logger_manager";
|
|
4
|
-
const SAFE_NAME = /^[A-Za-z0-9_.:-]{1,120}$/;
|
|
5
|
-
/**
|
|
6
|
-
* JSON event transport backed by durable fanout exchanges and durable queues.
|
|
7
|
-
*
|
|
8
|
-
* Each channel name maps to an exchange. Each distinct subscriber name gets a
|
|
9
|
-
* queue and therefore its own copy of every event. Instances sharing the same
|
|
10
|
-
* subscriber name compete for work, which supports horizontal scaling.
|
|
11
|
-
*/
|
|
12
|
-
export class RabbitMQHelper {
|
|
13
|
-
url;
|
|
14
|
-
prefetch_count;
|
|
15
|
-
connection;
|
|
16
|
-
channel;
|
|
17
|
-
connecting;
|
|
18
|
-
constructor(options = {}) {
|
|
19
|
-
this.url = process.env.RABBITMQ_URL ?? "";
|
|
20
|
-
if (!this.url) {
|
|
21
|
-
logger.warn("Missing ENV parameter RABBITMQ_URL");
|
|
22
|
-
}
|
|
23
|
-
this.prefetch_count = options.prefetch_count ?? 10;
|
|
24
|
-
if (!Number.isInteger(this.prefetch_count) || this.prefetch_count <= 0) {
|
|
25
|
-
throw new Error("prefetch_count must be a positive integer");
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
async connect() {
|
|
29
|
-
if (this.connection && this.channel)
|
|
30
|
-
return;
|
|
31
|
-
this.connecting ??= this.open_connection().finally(() => {
|
|
32
|
-
this.connecting = undefined;
|
|
33
|
-
});
|
|
34
|
-
await this.connecting;
|
|
35
|
-
}
|
|
36
|
-
async close() {
|
|
37
|
-
const channel = this.channel;
|
|
38
|
-
const connection = this.connection;
|
|
39
|
-
this.channel = undefined;
|
|
40
|
-
this.connection = undefined;
|
|
41
|
-
if (channel)
|
|
42
|
-
await channel.close().catch(() => undefined);
|
|
43
|
-
if (connection)
|
|
44
|
-
await connection.close().catch(() => undefined);
|
|
45
|
-
}
|
|
46
|
-
async push_event(channel_name, payload, options = {}) {
|
|
47
|
-
const channel = await this.declare_exchange(channel_name);
|
|
48
|
-
const { ttl_seconds, headers = {} } = options;
|
|
49
|
-
if (ttl_seconds !== undefined && (!Number.isFinite(ttl_seconds) || ttl_seconds <= 0)) {
|
|
50
|
-
throw new Error("ttl_seconds must be a finite number greater than zero");
|
|
51
|
-
}
|
|
52
|
-
let encoded;
|
|
53
|
-
try {
|
|
54
|
-
encoded = JSON.stringify(payload);
|
|
55
|
-
}
|
|
56
|
-
catch (error) {
|
|
57
|
-
throw new Error("payload must be JSON serializable", { cause: error });
|
|
58
|
-
}
|
|
59
|
-
if (encoded === undefined)
|
|
60
|
-
throw new Error("payload must be JSON serializable");
|
|
61
|
-
const event_id = random_uuid();
|
|
62
|
-
const created_at = new Date().toISOString();
|
|
63
|
-
channel.publish(channel_name, "", Buffer.from(encoded, "utf8"), {
|
|
64
|
-
contentType: "application/json",
|
|
65
|
-
contentEncoding: "utf-8",
|
|
66
|
-
persistent: true,
|
|
67
|
-
messageId: event_id,
|
|
68
|
-
timestamp: Math.floor(Date.now() / 1_000),
|
|
69
|
-
...(ttl_seconds === undefined ? {} : { expiration: String(Math.round(ttl_seconds * 1_000)) }),
|
|
70
|
-
headers: { created_at, ...headers },
|
|
71
|
-
});
|
|
72
|
-
await channel.waitForConfirms();
|
|
73
|
-
return event_id;
|
|
74
|
-
}
|
|
75
|
-
subscriptions = [];
|
|
76
|
-
async subscribe_for_channel(channel_name, subscriber_name, handler, options = {}) {
|
|
77
|
-
const { channel, queue_name } = await this.declare_subscription(channel_name, subscriber_name);
|
|
78
|
-
const requeue_on_error = options.requeue_on_error ?? true;
|
|
79
|
-
const reply = await channel.consume(queue_name, (message) => {
|
|
80
|
-
if (!message)
|
|
81
|
-
return;
|
|
82
|
-
void this.handle_message(channel, message, handler, requeue_on_error);
|
|
83
|
-
}, { noAck: false });
|
|
84
|
-
const subscription = {
|
|
85
|
-
queue_name,
|
|
86
|
-
consumer_tag: reply.consumerTag,
|
|
87
|
-
cancel: async () => {
|
|
88
|
-
await channel.cancel(reply.consumerTag);
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
this.subscriptions.push(subscription);
|
|
92
|
-
return subscription;
|
|
93
|
-
}
|
|
94
|
-
async cancel_subscriptions() {
|
|
95
|
-
await Promise.all(this.subscriptions.map((subscription) => subscription.cancel()));
|
|
96
|
-
}
|
|
97
|
-
async pull_and_delete(channel_name, subscriber_name) {
|
|
98
|
-
const { channel, queue_name } = await this.declare_subscription(channel_name, subscriber_name);
|
|
99
|
-
const message = await channel.get(queue_name, { noAck: false });
|
|
100
|
-
if (!message)
|
|
101
|
-
return undefined;
|
|
102
|
-
const event = this.to_event(message);
|
|
103
|
-
channel.ack(message);
|
|
104
|
-
return event;
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Diagnostic peek implemented as get + nack + requeue.
|
|
108
|
-
* It is not a reliable queue browser: the same event can repeat or move.
|
|
109
|
-
*/
|
|
110
|
-
async pull_and_leave(channel_name, subscriber_name) {
|
|
111
|
-
const { channel, queue_name } = await this.declare_subscription(channel_name, subscriber_name);
|
|
112
|
-
const message = await channel.get(queue_name, { noAck: false });
|
|
113
|
-
if (!message)
|
|
114
|
-
return undefined;
|
|
115
|
-
const event = this.to_event(message);
|
|
116
|
-
channel.nack(message, false, true);
|
|
117
|
-
return event;
|
|
118
|
-
}
|
|
119
|
-
async open_connection() {
|
|
120
|
-
const connection_url = new URL(this.url);
|
|
121
|
-
if (!connection_url.searchParams.has("heartbeat")) {
|
|
122
|
-
connection_url.searchParams.set("heartbeat", "30");
|
|
123
|
-
}
|
|
124
|
-
const connection = await amqp.connect(connection_url.toString(), { timeout: 30_000 });
|
|
125
|
-
const channel = await connection.createConfirmChannel();
|
|
126
|
-
await channel.prefetch(this.prefetch_count);
|
|
127
|
-
this.connection = connection;
|
|
128
|
-
this.channel = channel;
|
|
129
|
-
}
|
|
130
|
-
async get_channel() {
|
|
131
|
-
await this.connect();
|
|
132
|
-
if (!this.channel)
|
|
133
|
-
throw new Error("RabbitMQ channel is not available");
|
|
134
|
-
return this.channel;
|
|
135
|
-
}
|
|
136
|
-
async declare_exchange(channel_name) {
|
|
137
|
-
this.validate_name(channel_name, "channel_name");
|
|
138
|
-
const channel = await this.get_channel();
|
|
139
|
-
await channel.assertExchange(channel_name, "fanout", { durable: true });
|
|
140
|
-
return channel;
|
|
141
|
-
}
|
|
142
|
-
async declare_subscription(channel_name, subscriber_name) {
|
|
143
|
-
this.validate_name(channel_name, "channel_name");
|
|
144
|
-
this.validate_name(subscriber_name, "subscriber_name");
|
|
145
|
-
const channel = await this.declare_exchange(channel_name);
|
|
146
|
-
const queue_name = `${channel_name}.${subscriber_name}`;
|
|
147
|
-
await channel.assertQueue(queue_name, { durable: true });
|
|
148
|
-
await channel.bindQueue(queue_name, channel_name, "");
|
|
149
|
-
return { channel, queue_name };
|
|
150
|
-
}
|
|
151
|
-
async handle_message(channel, message, handler, requeue_on_error) {
|
|
152
|
-
try {
|
|
153
|
-
await handler(this.to_event(message));
|
|
154
|
-
channel.ack(message);
|
|
155
|
-
}
|
|
156
|
-
catch (error) {
|
|
157
|
-
console.error(`RabbitMQ handler failed for ${message.properties.messageId ?? "unknown"}`, error);
|
|
158
|
-
channel.nack(message, false, requeue_on_error);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
to_event(message) {
|
|
162
|
-
let body;
|
|
163
|
-
try {
|
|
164
|
-
body = JSON.parse(message.content.toString("utf8"));
|
|
165
|
-
}
|
|
166
|
-
catch (error) {
|
|
167
|
-
throw new Error("received a message that is not valid JSON", { cause: error });
|
|
168
|
-
}
|
|
169
|
-
const headers = { ...message.properties.headers };
|
|
170
|
-
const created_at = headers.created_at;
|
|
171
|
-
return {
|
|
172
|
-
body,
|
|
173
|
-
redelivered: message.fields.redelivered,
|
|
174
|
-
headers,
|
|
175
|
-
...(message.properties.messageId ? { event_id: message.properties.messageId } : {}),
|
|
176
|
-
...(typeof created_at === "string" ? { created_at } : {}),
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
validate_name(value, parameter) {
|
|
180
|
-
if (!SAFE_NAME.test(value)) {
|
|
181
|
-
throw new Error(`${parameter} must be 1-120 characters using letters, digits, _, ., :, or -`);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
export const rabbitmq = new RabbitMQHelper();
|
|
186
|
-
//# sourceMappingURL=rabbitmq_helpers.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rabbitmq_helpers.js","sourceRoot":"","sources":["../../../src/helpers/rabbitmq_helpers.ts"],"names":[],"mappings":"AAAA,OAAO,IAAoG,MAAM,SAAS,CAAC;AAC3H,OAAO,EAAE,UAAU,IAAI,WAAW,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AAEpD,MAAM,SAAS,GAAG,0BAA0B,CAAC;AAiC7C;;;;;;GAMG;AACH,MAAM,OAAO,cAAc;IACd,GAAG,CAAS;IACZ,cAAc,CAAS;IAExB,UAAU,CAA2B;IACrC,OAAO,CAA6B;IACpC,UAAU,CAA4B;IAE9C,YAAY,UAAqD,EAAE;QAC/D,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,IAAI,EAAE,CAAC;QAC1C,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;YACZ,MAAM,CAAC,IAAI,CAAC,oCAAoC,CAAC,CAAC;QACtD,CAAC;QAED,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,cAAc,IAAI,EAAE,CAAC;QAEnD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,IAAI,CAAC,cAAc,IAAI,CAAC,EAAE,CAAC;YACrE,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;QACjE,CAAC;IACL,CAAC;IAED,KAAK,CAAC,OAAO;QACT,IAAI,IAAI,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
|
|
|
1
|
-
import { cache_manager } from "../managers";
|
|
2
|
-
export const extract_board_types_from_settings = (settings) => {
|
|
3
|
-
const { erm_board_types, jimi_board_types, jimi_iothub_board_types, ruptela_board_types, servision_board_types } = settings;
|
|
4
|
-
const result = [
|
|
5
|
-
{ board_provider: "erm", board_types: erm_board_types.values },
|
|
6
|
-
{ board_provider: "jimi", board_types: jimi_board_types.values },
|
|
7
|
-
{ board_provider: "ruptela", board_types: ruptela_board_types.values },
|
|
8
|
-
{ board_provider: "servision", board_types: servision_board_types.values },
|
|
9
|
-
{ board_provider: "jimi_iothub", board_types: jimi_iothub_board_types.values },
|
|
10
|
-
];
|
|
11
|
-
return result;
|
|
12
|
-
};
|
|
13
|
-
export const get_board_maker_by_board_type = (type) => {
|
|
14
|
-
const settings = cache_manager.getObjectData("settings");
|
|
15
|
-
if (settings.erm_board_types.values.includes(type)) {
|
|
16
|
-
return "erm";
|
|
17
|
-
}
|
|
18
|
-
if (settings.jimi_board_types.values.includes(type)) {
|
|
19
|
-
return "jimi";
|
|
20
|
-
}
|
|
21
|
-
if (settings.jimi_iothub_board_types.values.includes(type)) {
|
|
22
|
-
return "jimi_iothub";
|
|
23
|
-
}
|
|
24
|
-
if (settings.ruptela_board_types.values.includes(type)) {
|
|
25
|
-
return "ruptela";
|
|
26
|
-
}
|
|
27
|
-
if (settings.servision_board_types.values.includes(type)) {
|
|
28
|
-
return "servision";
|
|
29
|
-
}
|
|
30
|
-
throw new Error("failed to get board maker from DB, type: " + type);
|
|
31
|
-
};
|
|
32
|
-
export const get_board_maker_by_car = (car) => {
|
|
33
|
-
const imei = car.peripherals?.[0]?.mac;
|
|
34
|
-
if (!imei) {
|
|
35
|
-
throw new Error("IMEI not found for car number: " + car.carId);
|
|
36
|
-
}
|
|
37
|
-
const boards = cache_manager.getArrayData("boards");
|
|
38
|
-
if (!boards.length) {
|
|
39
|
-
throw new Error("Boards are not in cache");
|
|
40
|
-
}
|
|
41
|
-
const board = boards.find((board) => board.imei === imei);
|
|
42
|
-
if (!board) {
|
|
43
|
-
throw new Error("Board not found for imei: " + imei);
|
|
44
|
-
}
|
|
45
|
-
const settings = cache_manager.getObjectData("settings");
|
|
46
|
-
if (settings.erm_board_types.values.includes(board.type)) {
|
|
47
|
-
return "erm";
|
|
48
|
-
}
|
|
49
|
-
if (settings.jimi_board_types.values.includes(board.type)) {
|
|
50
|
-
return "jimi";
|
|
51
|
-
}
|
|
52
|
-
if (settings.jimi_iothub_board_types.values.includes(board.type)) {
|
|
53
|
-
return "jimi_iothub";
|
|
54
|
-
}
|
|
55
|
-
if (settings.ruptela_board_types.values.includes(board.type)) {
|
|
56
|
-
return "ruptela";
|
|
57
|
-
}
|
|
58
|
-
if (settings.servision_board_types.values.includes(board.type)) {
|
|
59
|
-
return "servision";
|
|
60
|
-
}
|
|
61
|
-
throw new Error("failed to get board maker for board type: " + board.type);
|
|
62
|
-
};
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import sendgrid from "@sendgrid/mail";
|
|
2
|
-
import { add_audit_record, get_document_by_id, ignore_ssl_request } from ".";
|
|
3
|
-
import { logger } from "../managers";
|
|
4
|
-
import * as fs from "fs";
|
|
5
|
-
import * as path from "path";
|
|
6
|
-
export const create_attachment_from_file = async (file_path, filename, disposition = "attachment") => {
|
|
7
|
-
try {
|
|
8
|
-
const content = fs.readFileSync(file_path);
|
|
9
|
-
const base64_content = content.toString("base64");
|
|
10
|
-
const mime_type = get_mime_type(file_path);
|
|
11
|
-
return {
|
|
12
|
-
content: base64_content,
|
|
13
|
-
filename: filename || path.basename(file_path),
|
|
14
|
-
type: mime_type,
|
|
15
|
-
disposition,
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
catch (error) {
|
|
19
|
-
logger.error("error creating attachment from file", { error, file_path });
|
|
20
|
-
throw error;
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
export const create_attachment_from_buffer = (buffer, filename, mime_type, disposition = "attachment") => {
|
|
24
|
-
const buf = Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
|
|
25
|
-
return {
|
|
26
|
-
content: buf.toString("base64"),
|
|
27
|
-
filename,
|
|
28
|
-
type: mime_type,
|
|
29
|
-
disposition,
|
|
30
|
-
};
|
|
31
|
-
};
|
|
32
|
-
export const create_attachment_from_url = async (url, filename) => {
|
|
33
|
-
try {
|
|
34
|
-
const response = await ignore_ssl_request({
|
|
35
|
-
method: "GET",
|
|
36
|
-
url,
|
|
37
|
-
responseType: "arraybuffer",
|
|
38
|
-
timeout: 20000,
|
|
39
|
-
});
|
|
40
|
-
const buffer = Buffer.from(response.data);
|
|
41
|
-
const content_type = response.headers?.["content-type"] || "application/pdf";
|
|
42
|
-
return {
|
|
43
|
-
content: buffer.toString("base64"),
|
|
44
|
-
filename: filename,
|
|
45
|
-
type: content_type,
|
|
46
|
-
disposition: "attachment",
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
logger.error("error creating attachment from url", { error, url });
|
|
51
|
-
throw error;
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
const get_mime_type = (file_path) => {
|
|
55
|
-
const ext = path.extname(file_path).toLowerCase();
|
|
56
|
-
const mime_types = {
|
|
57
|
-
".pdf": "application/pdf",
|
|
58
|
-
".doc": "application/msword",
|
|
59
|
-
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
60
|
-
".xls": "application/vnd.ms-excel",
|
|
61
|
-
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
62
|
-
".ppt": "application/vnd.ms-powerpoint",
|
|
63
|
-
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
64
|
-
".txt": "text/plain",
|
|
65
|
-
".csv": "text/csv",
|
|
66
|
-
".json": "application/json",
|
|
67
|
-
".xml": "application/xml",
|
|
68
|
-
".zip": "application/zip",
|
|
69
|
-
".rar": "application/x-rar-compressed",
|
|
70
|
-
".7z": "application/x-7z-compressed",
|
|
71
|
-
".jpg": "image/jpeg",
|
|
72
|
-
".jpeg": "image/jpeg",
|
|
73
|
-
".png": "image/png",
|
|
74
|
-
".gif": "image/gif",
|
|
75
|
-
".bmp": "image/bmp",
|
|
76
|
-
".svg": "image/svg+xml",
|
|
77
|
-
".mp3": "audio/mpeg",
|
|
78
|
-
".mp4": "video/mp4",
|
|
79
|
-
".avi": "video/x-msvideo",
|
|
80
|
-
".mov": "video/quicktime",
|
|
81
|
-
};
|
|
82
|
-
return mime_types[ext] || "application/octet-stream";
|
|
83
|
-
};
|
|
84
|
-
export const send_email = async (email_data, options) => {
|
|
85
|
-
try {
|
|
86
|
-
const emails_settings = (await get_document_by_id("nx-settings", "emails"));
|
|
87
|
-
const { sendgrid_api_key, groups, default_from, default_cc } = emails_settings;
|
|
88
|
-
let { from, to, cc, group_name, body_html, body_plain_text, subject, entity_for_audit, attachments } = email_data;
|
|
89
|
-
const cc_array = cc ? (Array.isArray(cc) ? cc : [cc]) : [];
|
|
90
|
-
cc = [...cc_array, ...default_cc];
|
|
91
|
-
/// validate data
|
|
92
|
-
if (!to?.length && !group_name?.length) {
|
|
93
|
-
throw "must supply a 'group_name' or 'to' value ";
|
|
94
|
-
}
|
|
95
|
-
if (!body_html?.length && !body_plain_text?.length) {
|
|
96
|
-
throw "must supply a 'body_plain_text' or 'html' value ";
|
|
97
|
-
}
|
|
98
|
-
if (to?.length && !group_name?.length) {
|
|
99
|
-
}
|
|
100
|
-
/// merge to and cc
|
|
101
|
-
if (group_name) {
|
|
102
|
-
if (!groups[group_name]) {
|
|
103
|
-
throw "must supply a valid 'group_name'";
|
|
104
|
-
}
|
|
105
|
-
if (!to) {
|
|
106
|
-
to = groups[group_name].to;
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
to = typeof to === "string" ? [...groups[group_name].to, to] : [...groups[group_name].to, ...to];
|
|
110
|
-
}
|
|
111
|
-
cc.push(...(groups[group_name].cc || []));
|
|
112
|
-
}
|
|
113
|
-
/// set sendgrid account
|
|
114
|
-
sendgrid.setApiKey(sendgrid_api_key);
|
|
115
|
-
/// prepare message
|
|
116
|
-
const msg = body_html
|
|
117
|
-
? {
|
|
118
|
-
subject,
|
|
119
|
-
from: from || default_from,
|
|
120
|
-
to,
|
|
121
|
-
cc,
|
|
122
|
-
html: body_html,
|
|
123
|
-
attachments: attachments,
|
|
124
|
-
}
|
|
125
|
-
: {
|
|
126
|
-
subject,
|
|
127
|
-
from: from || default_from,
|
|
128
|
-
to,
|
|
129
|
-
cc,
|
|
130
|
-
text: body_plain_text,
|
|
131
|
-
attachments: attachments,
|
|
132
|
-
};
|
|
133
|
-
if (!attachments?.length) {
|
|
134
|
-
delete msg.attachments;
|
|
135
|
-
}
|
|
136
|
-
/// send email
|
|
137
|
-
const email_result = await sendgrid.send(msg);
|
|
138
|
-
if (email_result[0].statusCode !== 202) {
|
|
139
|
-
throw email_result[0].body;
|
|
140
|
-
}
|
|
141
|
-
/// add audit
|
|
142
|
-
await add_audit_record("send_email", entity_for_audit, { ...email_data, ...msg });
|
|
143
|
-
if (options?.debug) {
|
|
144
|
-
logger.log("email send successfully", { ...email_data, ...msg });
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
catch (error) {
|
|
148
|
-
logger.error("error sending email", error);
|
|
149
|
-
if (options?.debug) {
|
|
150
|
-
logger.error("email payload", { message: JSON.stringify(email_data) });
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
};
|