@develit-services/notification 0.0.18 → 0.0.20
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/@types.cjs +25 -0
- package/dist/@types.d.cts +70 -0
- package/dist/@types.d.mts +70 -0
- package/dist/@types.d.ts +70 -0
- package/dist/@types.mjs +14 -0
- package/dist/export/worker.cjs +11 -225
- package/dist/export/worker.d.cts +4 -196
- package/dist/export/worker.d.mts +4 -196
- package/dist/export/worker.d.ts +4 -196
- package/dist/export/worker.mjs +5 -214
- package/dist/export/wrangler.d.cts +1 -28
- package/dist/export/wrangler.d.mts +1 -28
- package/dist/export/wrangler.d.ts +1 -28
- package/dist/shared/notification.B2I1pTPy.d.cts +32 -0
- package/dist/shared/notification.B2I1pTPy.d.mts +32 -0
- package/dist/shared/notification.B2I1pTPy.d.ts +32 -0
- package/dist/shared/notification.B8IIgYsS.cjs +220 -0
- package/dist/shared/notification.Bc0Z2CT6.d.cts +199 -0
- package/dist/shared/notification.Bc0Z2CT6.d.mts +199 -0
- package/dist/shared/notification.Bc0Z2CT6.d.ts +199 -0
- package/dist/shared/notification.D1sgZDH0.mjs +205 -0
- package/package.json +6 -1
package/dist/export/worker.mjs
CHANGED
|
@@ -1,219 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import { z as z$2 } from 'zod/v4';
|
|
4
|
-
import twilio from 'twilio';
|
|
1
|
+
import { createInternalError, uuidv4, develitWorker, useResult, cloudflareQueue, action } from '@develit-io/backend-sdk';
|
|
2
|
+
import { E as EcomailConnector, T as TwilioConnector, s as sendEmailInputSchema, a as sendSmsInputSchema, b as sendSlackInputSchema } from '../shared/notification.D1sgZDH0.mjs';
|
|
5
3
|
import { s as schema } from '../shared/notification.C0X8Orrh.mjs';
|
|
6
4
|
import 'drizzle-orm';
|
|
7
5
|
import 'drizzle-orm/sqlite-core';
|
|
8
6
|
import { WorkerEntrypoint } from 'cloudflare:workers';
|
|
9
7
|
import { drizzle } from 'drizzle-orm/d1';
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
z.object({
|
|
14
|
-
email: z.string(),
|
|
15
|
-
name: z.union([z.string(), z.undefined()])
|
|
16
|
-
})
|
|
17
|
-
]);
|
|
18
|
-
const iEmailSchema = z.object({
|
|
19
|
-
to: z.union([iContactSchema, z.array(iContactSchema)]),
|
|
20
|
-
replyTo: z.union([iContactSchema, z.array(iContactSchema)]).optional(),
|
|
21
|
-
cc: z.union([iContactSchema, z.array(iContactSchema)]).optional(),
|
|
22
|
-
bcc: z.union([iContactSchema, z.array(iContactSchema)]).optional(),
|
|
23
|
-
from: iContactSchema.optional(),
|
|
24
|
-
subject: z.string(),
|
|
25
|
-
text: z.string().optional(),
|
|
26
|
-
html: z.string().optional(),
|
|
27
|
-
templateId: z.number().optional(),
|
|
28
|
-
templateVariables: z.record(z.string(), z.string()).optional()
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
class IEmailConnector {
|
|
32
|
-
constructor({
|
|
33
|
-
API_KEY,
|
|
34
|
-
SMTP_HOST,
|
|
35
|
-
SENDER
|
|
36
|
-
}) {
|
|
37
|
-
this.API_KEY = API_KEY;
|
|
38
|
-
this.SMTP_HOST = SMTP_HOST;
|
|
39
|
-
this.SENDER = SENDER;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
class EcomailConnector extends IEmailConnector {
|
|
44
|
-
static {
|
|
45
|
-
this.providerName = "ecomail";
|
|
46
|
-
}
|
|
47
|
-
constructor({
|
|
48
|
-
API_KEY,
|
|
49
|
-
SMTP_HOST,
|
|
50
|
-
SENDER
|
|
51
|
-
}) {
|
|
52
|
-
super({ API_KEY, SMTP_HOST, SENDER });
|
|
53
|
-
}
|
|
54
|
-
async sendEmail(email) {
|
|
55
|
-
console.log("ecomail - sendEmail");
|
|
56
|
-
if (email.templateVariables) {
|
|
57
|
-
for (const [key, value] of Object.entries(email.templateVariables)) {
|
|
58
|
-
if (typeof value === "string" && String(value).includes("localhost") && key in email.templateVariables) {
|
|
59
|
-
email.templateVariables[key] = String(value).replace("localhost", "origin");
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
console.log("ecomail - convertEmail");
|
|
64
|
-
const emEmail = this.convertEmail(email);
|
|
65
|
-
const uri = emEmail.message.template_id ? "https://api2.ecomailapp.cz/transactional/send-template" : "https://api2.ecomailapp.cz/transactional/send-message";
|
|
66
|
-
console.log("ecomail - start with fetch", {
|
|
67
|
-
uri,
|
|
68
|
-
emEmail,
|
|
69
|
-
apiKey: this.API_KEY
|
|
70
|
-
});
|
|
71
|
-
const [_, error] = await useFetch(uri, {
|
|
72
|
-
method: "POST",
|
|
73
|
-
headers: {
|
|
74
|
-
"Content-Type": "application/json",
|
|
75
|
-
key: this.API_KEY
|
|
76
|
-
},
|
|
77
|
-
body: JSON.stringify(emEmail)
|
|
78
|
-
});
|
|
79
|
-
console.log("ecomail - fetch done");
|
|
80
|
-
if (error) throw error;
|
|
81
|
-
}
|
|
82
|
-
convertEmail(email) {
|
|
83
|
-
const toContacts = this.convertContacts(email.to);
|
|
84
|
-
const ccContacts = email.cc ? this.convertContacts(email.cc) : [];
|
|
85
|
-
const bccContacts = email.bcc ? this.convertContacts(email.bcc) : [];
|
|
86
|
-
const replyTo = email.replyTo ? this.convertContacts(email.replyTo)[0] : void 0;
|
|
87
|
-
const from = this.convertContact(email.from || this.SENDER);
|
|
88
|
-
const subject = email.subject;
|
|
89
|
-
const textAttachments = email.text ? [{ name: "plain", type: "text/plain", content: email.text }] : [];
|
|
90
|
-
const htmlAttachments = email.html ? [{ type: "text/html", content: email.html, name: "email_body.html" }] : [];
|
|
91
|
-
const attachments = [...textAttachments, ...htmlAttachments];
|
|
92
|
-
const contacts = toContacts.flatMap((to) => {
|
|
93
|
-
const entries = [];
|
|
94
|
-
if (ccContacts.length > 0) {
|
|
95
|
-
ccContacts.forEach((cc) => {
|
|
96
|
-
entries.push({ email: cc?.email, name: cc?.name, cc: cc?.email });
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
if (bccContacts.length > 0) {
|
|
100
|
-
bccContacts.forEach((bcc) => {
|
|
101
|
-
entries.push({ email: bcc?.email, name: bcc?.name, bcc: bcc?.email });
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
return entries.length > 0 ? entries : [to];
|
|
105
|
-
});
|
|
106
|
-
return {
|
|
107
|
-
message: {
|
|
108
|
-
from_email: from?.email,
|
|
109
|
-
from_name: from.name || "",
|
|
110
|
-
subject,
|
|
111
|
-
attachments,
|
|
112
|
-
to: contacts,
|
|
113
|
-
text: email.text,
|
|
114
|
-
html: email.html,
|
|
115
|
-
reply_to: replyTo?.email,
|
|
116
|
-
template_id: email.templateId,
|
|
117
|
-
global_merge_vars: email.templateVariables ? Object.keys(email.templateVariables).map((key) => ({
|
|
118
|
-
name: key,
|
|
119
|
-
content: email.templateVariables[key]
|
|
120
|
-
})) : null
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
convertContacts(contacts) {
|
|
125
|
-
if (!contacts) {
|
|
126
|
-
return [];
|
|
127
|
-
}
|
|
128
|
-
const contactArray = Array.isArray(contacts) ? contacts : [contacts];
|
|
129
|
-
return contactArray.map(this.convertContact);
|
|
130
|
-
}
|
|
131
|
-
convertContact(contact) {
|
|
132
|
-
if (typeof contact === "string") {
|
|
133
|
-
return { email: contact, name: void 0 };
|
|
134
|
-
}
|
|
135
|
-
return { email: contact?.email, name: contact?.name };
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
const sendEmailInputSchema = z$1.object({
|
|
140
|
-
email: iEmailSchema,
|
|
141
|
-
metadata: z$1.object({
|
|
142
|
-
userAgent: z$1.string().optional(),
|
|
143
|
-
ip: z$1.ipv4().or(z$1.ipv6()).optional(),
|
|
144
|
-
initiator: z$1.object({
|
|
145
|
-
service: z$1.string(),
|
|
146
|
-
userId: z$1.string().optional()
|
|
147
|
-
})
|
|
148
|
-
})
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
const sendSlackInputSchema = z$2.object({
|
|
152
|
-
slack: z$2.object({
|
|
153
|
-
message: z$2.string()
|
|
154
|
-
}),
|
|
155
|
-
metadata: z$2.object({
|
|
156
|
-
userAgent: z$2.string().optional(),
|
|
157
|
-
ip: z$2.ipv4().or(z$2.ipv6()).optional(),
|
|
158
|
-
initiator: z$2.object({
|
|
159
|
-
service: z$2.string(),
|
|
160
|
-
userId: z$2.string().optional()
|
|
161
|
-
})
|
|
162
|
-
})
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
const sendSmsInputSchema = z.object({
|
|
166
|
-
sms: z.object({
|
|
167
|
-
message: z.string(),
|
|
168
|
-
to: z.string()
|
|
169
|
-
}),
|
|
170
|
-
metadata: z.object({
|
|
171
|
-
userAgent: z.string().optional(),
|
|
172
|
-
ip: z.ipv4().or(z.ipv6()).optional(),
|
|
173
|
-
initiator: z.object({
|
|
174
|
-
service: z.string(),
|
|
175
|
-
userId: z.string().optional()
|
|
176
|
-
})
|
|
177
|
-
})
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
class ISmsConnector {
|
|
181
|
-
constructor({
|
|
182
|
-
ACCOUNT_ID,
|
|
183
|
-
AUTH_TOKEN,
|
|
184
|
-
SERVICE_ID
|
|
185
|
-
}) {
|
|
186
|
-
this.ACCOUNT_ID = ACCOUNT_ID;
|
|
187
|
-
this.AUTH_TOKEN = AUTH_TOKEN;
|
|
188
|
-
this.SERVICE_ID = SERVICE_ID;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
class TwilioConnector extends ISmsConnector {
|
|
193
|
-
static {
|
|
194
|
-
this.providerName = "twilio";
|
|
195
|
-
}
|
|
196
|
-
constructor({
|
|
197
|
-
ACCOUNT_ID,
|
|
198
|
-
AUTH_TOKEN,
|
|
199
|
-
SERVICE_ID
|
|
200
|
-
}) {
|
|
201
|
-
super({ ACCOUNT_ID, AUTH_TOKEN, SERVICE_ID });
|
|
202
|
-
this.twilioClient = twilio(ACCOUNT_ID, AUTH_TOKEN);
|
|
203
|
-
}
|
|
204
|
-
async sendSms(sms) {
|
|
205
|
-
const message = await this.twilioClient.messages.create({
|
|
206
|
-
body: sms.message,
|
|
207
|
-
messagingServiceSid: this.SERVICE_ID,
|
|
208
|
-
to: sms.to
|
|
209
|
-
});
|
|
210
|
-
if (message.errorMessage)
|
|
211
|
-
return createInternalError(null, {
|
|
212
|
-
message: message.errorMessage,
|
|
213
|
-
status: message.errorCode
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
}
|
|
8
|
+
import 'zod';
|
|
9
|
+
import 'zod/v4';
|
|
10
|
+
import 'twilio';
|
|
217
11
|
|
|
218
12
|
const tables = schema;
|
|
219
13
|
|
|
@@ -407,9 +201,6 @@ class NotificationServiceBase extends develitWorker(
|
|
|
407
201
|
);
|
|
408
202
|
}
|
|
409
203
|
await this.emailConnector.sendEmail(email);
|
|
410
|
-
this.log({
|
|
411
|
-
message: `Email sent to ${email.to}`
|
|
412
|
-
});
|
|
413
204
|
const { command } = await createAuditLogCommand({
|
|
414
205
|
db: this.db,
|
|
415
206
|
auditLog: {
|
|
@@ -1,31 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
queue?: {
|
|
3
|
-
max_batch_size?: number;
|
|
4
|
-
max_batch_timeout?: number;
|
|
5
|
-
};
|
|
6
|
-
d1: {
|
|
7
|
-
id: string;
|
|
8
|
-
};
|
|
9
|
-
vars: {
|
|
10
|
-
EMAIL_PROVIDER: 'ecomail';
|
|
11
|
-
EMAIL_SENDER: {
|
|
12
|
-
name: string;
|
|
13
|
-
email: string;
|
|
14
|
-
};
|
|
15
|
-
EMAIL_SMTP_HOST: string;
|
|
16
|
-
SMS_PROVIDER: 'twilio';
|
|
17
|
-
SMS_ACCOUNT_ID: string;
|
|
18
|
-
SMS_SERVICE_ID: string | number;
|
|
19
|
-
SLACK_WEBHOOKS: [string];
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
interface NotificationServiceWranglerConfig {
|
|
23
|
-
name: string;
|
|
24
|
-
envs: {
|
|
25
|
-
local: NotificationServiceEnvironmentConfig;
|
|
26
|
-
[key: string]: NotificationServiceEnvironmentConfig;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
1
|
+
import { N as NotificationServiceWranglerConfig } from '../shared/notification.B2I1pTPy.cjs';
|
|
29
2
|
|
|
30
3
|
/** biome-ignore-all lint/suspicious/noExplicitAny: allow any */
|
|
31
4
|
|
|
@@ -1,31 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
queue?: {
|
|
3
|
-
max_batch_size?: number;
|
|
4
|
-
max_batch_timeout?: number;
|
|
5
|
-
};
|
|
6
|
-
d1: {
|
|
7
|
-
id: string;
|
|
8
|
-
};
|
|
9
|
-
vars: {
|
|
10
|
-
EMAIL_PROVIDER: 'ecomail';
|
|
11
|
-
EMAIL_SENDER: {
|
|
12
|
-
name: string;
|
|
13
|
-
email: string;
|
|
14
|
-
};
|
|
15
|
-
EMAIL_SMTP_HOST: string;
|
|
16
|
-
SMS_PROVIDER: 'twilio';
|
|
17
|
-
SMS_ACCOUNT_ID: string;
|
|
18
|
-
SMS_SERVICE_ID: string | number;
|
|
19
|
-
SLACK_WEBHOOKS: [string];
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
interface NotificationServiceWranglerConfig {
|
|
23
|
-
name: string;
|
|
24
|
-
envs: {
|
|
25
|
-
local: NotificationServiceEnvironmentConfig;
|
|
26
|
-
[key: string]: NotificationServiceEnvironmentConfig;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
1
|
+
import { N as NotificationServiceWranglerConfig } from '../shared/notification.B2I1pTPy.mjs';
|
|
29
2
|
|
|
30
3
|
/** biome-ignore-all lint/suspicious/noExplicitAny: allow any */
|
|
31
4
|
|
|
@@ -1,31 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
queue?: {
|
|
3
|
-
max_batch_size?: number;
|
|
4
|
-
max_batch_timeout?: number;
|
|
5
|
-
};
|
|
6
|
-
d1: {
|
|
7
|
-
id: string;
|
|
8
|
-
};
|
|
9
|
-
vars: {
|
|
10
|
-
EMAIL_PROVIDER: 'ecomail';
|
|
11
|
-
EMAIL_SENDER: {
|
|
12
|
-
name: string;
|
|
13
|
-
email: string;
|
|
14
|
-
};
|
|
15
|
-
EMAIL_SMTP_HOST: string;
|
|
16
|
-
SMS_PROVIDER: 'twilio';
|
|
17
|
-
SMS_ACCOUNT_ID: string;
|
|
18
|
-
SMS_SERVICE_ID: string | number;
|
|
19
|
-
SLACK_WEBHOOKS: [string];
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
interface NotificationServiceWranglerConfig {
|
|
23
|
-
name: string;
|
|
24
|
-
envs: {
|
|
25
|
-
local: NotificationServiceEnvironmentConfig;
|
|
26
|
-
[key: string]: NotificationServiceEnvironmentConfig;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
1
|
+
import { N as NotificationServiceWranglerConfig } from '../shared/notification.B2I1pTPy.js';
|
|
29
2
|
|
|
30
3
|
/** biome-ignore-all lint/suspicious/noExplicitAny: allow any */
|
|
31
4
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
interface NotificationServiceEnvironmentConfig {
|
|
2
|
+
queue?: {
|
|
3
|
+
max_batch_size?: number;
|
|
4
|
+
max_batch_timeout?: number;
|
|
5
|
+
};
|
|
6
|
+
d1: {
|
|
7
|
+
id: string;
|
|
8
|
+
};
|
|
9
|
+
vars: {
|
|
10
|
+
EMAIL_PROVIDER: 'ecomail';
|
|
11
|
+
EMAIL_SENDER: {
|
|
12
|
+
name: string;
|
|
13
|
+
email: string;
|
|
14
|
+
};
|
|
15
|
+
EMAIL_SMTP_HOST: string;
|
|
16
|
+
SMS_PROVIDER: 'twilio';
|
|
17
|
+
SMS_ACCOUNT_ID: string;
|
|
18
|
+
SMS_SERVICE_ID: string | number;
|
|
19
|
+
SLACK_WEBHOOKS: [string];
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
interface NotificationServiceWranglerConfig {
|
|
23
|
+
name: string;
|
|
24
|
+
envs: {
|
|
25
|
+
local: NotificationServiceEnvironmentConfig;
|
|
26
|
+
[key: string]: NotificationServiceEnvironmentConfig;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
interface NotificationServiceEnv extends NotificationEnv {
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type { NotificationServiceWranglerConfig as N, NotificationServiceEnvironmentConfig as a, NotificationServiceEnv as b };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
interface NotificationServiceEnvironmentConfig {
|
|
2
|
+
queue?: {
|
|
3
|
+
max_batch_size?: number;
|
|
4
|
+
max_batch_timeout?: number;
|
|
5
|
+
};
|
|
6
|
+
d1: {
|
|
7
|
+
id: string;
|
|
8
|
+
};
|
|
9
|
+
vars: {
|
|
10
|
+
EMAIL_PROVIDER: 'ecomail';
|
|
11
|
+
EMAIL_SENDER: {
|
|
12
|
+
name: string;
|
|
13
|
+
email: string;
|
|
14
|
+
};
|
|
15
|
+
EMAIL_SMTP_HOST: string;
|
|
16
|
+
SMS_PROVIDER: 'twilio';
|
|
17
|
+
SMS_ACCOUNT_ID: string;
|
|
18
|
+
SMS_SERVICE_ID: string | number;
|
|
19
|
+
SLACK_WEBHOOKS: [string];
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
interface NotificationServiceWranglerConfig {
|
|
23
|
+
name: string;
|
|
24
|
+
envs: {
|
|
25
|
+
local: NotificationServiceEnvironmentConfig;
|
|
26
|
+
[key: string]: NotificationServiceEnvironmentConfig;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
interface NotificationServiceEnv extends NotificationEnv {
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type { NotificationServiceWranglerConfig as N, NotificationServiceEnvironmentConfig as a, NotificationServiceEnv as b };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
interface NotificationServiceEnvironmentConfig {
|
|
2
|
+
queue?: {
|
|
3
|
+
max_batch_size?: number;
|
|
4
|
+
max_batch_timeout?: number;
|
|
5
|
+
};
|
|
6
|
+
d1: {
|
|
7
|
+
id: string;
|
|
8
|
+
};
|
|
9
|
+
vars: {
|
|
10
|
+
EMAIL_PROVIDER: 'ecomail';
|
|
11
|
+
EMAIL_SENDER: {
|
|
12
|
+
name: string;
|
|
13
|
+
email: string;
|
|
14
|
+
};
|
|
15
|
+
EMAIL_SMTP_HOST: string;
|
|
16
|
+
SMS_PROVIDER: 'twilio';
|
|
17
|
+
SMS_ACCOUNT_ID: string;
|
|
18
|
+
SMS_SERVICE_ID: string | number;
|
|
19
|
+
SLACK_WEBHOOKS: [string];
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
interface NotificationServiceWranglerConfig {
|
|
23
|
+
name: string;
|
|
24
|
+
envs: {
|
|
25
|
+
local: NotificationServiceEnvironmentConfig;
|
|
26
|
+
[key: string]: NotificationServiceEnvironmentConfig;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
interface NotificationServiceEnv extends NotificationEnv {
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type { NotificationServiceWranglerConfig as N, NotificationServiceEnvironmentConfig as a, NotificationServiceEnv as b };
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const z = require('zod');
|
|
4
|
+
const backendSdk = require('@develit-io/backend-sdk');
|
|
5
|
+
const v4 = require('zod/v4');
|
|
6
|
+
const twilio = require('twilio');
|
|
7
|
+
|
|
8
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
9
|
+
|
|
10
|
+
const z__default = /*#__PURE__*/_interopDefaultCompat(z);
|
|
11
|
+
const twilio__default = /*#__PURE__*/_interopDefaultCompat(twilio);
|
|
12
|
+
|
|
13
|
+
const iContactSchema = z.z.union([
|
|
14
|
+
z.z.string(),
|
|
15
|
+
z.z.object({
|
|
16
|
+
email: z.z.string(),
|
|
17
|
+
name: z.z.union([z.z.string(), z.z.undefined()])
|
|
18
|
+
})
|
|
19
|
+
]);
|
|
20
|
+
const iEmailSchema = z.z.object({
|
|
21
|
+
to: z.z.union([iContactSchema, z.z.array(iContactSchema)]),
|
|
22
|
+
replyTo: z.z.union([iContactSchema, z.z.array(iContactSchema)]).optional(),
|
|
23
|
+
cc: z.z.union([iContactSchema, z.z.array(iContactSchema)]).optional(),
|
|
24
|
+
bcc: z.z.union([iContactSchema, z.z.array(iContactSchema)]).optional(),
|
|
25
|
+
from: iContactSchema.optional(),
|
|
26
|
+
subject: z.z.string(),
|
|
27
|
+
text: z.z.string().optional(),
|
|
28
|
+
html: z.z.string().optional(),
|
|
29
|
+
templateId: z.z.number().optional(),
|
|
30
|
+
templateVariables: z.z.record(z.z.string(), z.z.string()).optional()
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
class IEmailConnector {
|
|
34
|
+
constructor({
|
|
35
|
+
API_KEY,
|
|
36
|
+
SMTP_HOST,
|
|
37
|
+
SENDER
|
|
38
|
+
}) {
|
|
39
|
+
this.API_KEY = API_KEY;
|
|
40
|
+
this.SMTP_HOST = SMTP_HOST;
|
|
41
|
+
this.SENDER = SENDER;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
class EcomailConnector extends IEmailConnector {
|
|
46
|
+
static {
|
|
47
|
+
this.providerName = "ecomail";
|
|
48
|
+
}
|
|
49
|
+
constructor({
|
|
50
|
+
API_KEY,
|
|
51
|
+
SMTP_HOST,
|
|
52
|
+
SENDER
|
|
53
|
+
}) {
|
|
54
|
+
super({ API_KEY, SMTP_HOST, SENDER });
|
|
55
|
+
}
|
|
56
|
+
async sendEmail(email) {
|
|
57
|
+
if (email.templateVariables) {
|
|
58
|
+
for (const [key, value] of Object.entries(email.templateVariables)) {
|
|
59
|
+
if (typeof value === "string" && String(value).includes("localhost") && key in email.templateVariables) {
|
|
60
|
+
email.templateVariables[key] = String(value).replace("localhost", "origin");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const emEmail = this.convertEmail(email);
|
|
65
|
+
const uri = emEmail.message.template_id ? "https://api2.ecomailapp.cz/transactional/send-template" : "https://api2.ecomailapp.cz/transactional/send-message";
|
|
66
|
+
const [_, error] = await backendSdk.useFetch(uri, {
|
|
67
|
+
method: "POST",
|
|
68
|
+
headers: {
|
|
69
|
+
"Content-Type": "application/json",
|
|
70
|
+
key: this.API_KEY
|
|
71
|
+
},
|
|
72
|
+
body: JSON.stringify(emEmail)
|
|
73
|
+
});
|
|
74
|
+
if (error) throw error;
|
|
75
|
+
}
|
|
76
|
+
convertEmail(email) {
|
|
77
|
+
const toContacts = this.convertContacts(email.to);
|
|
78
|
+
const ccContacts = email.cc ? this.convertContacts(email.cc) : [];
|
|
79
|
+
const bccContacts = email.bcc ? this.convertContacts(email.bcc) : [];
|
|
80
|
+
const replyTo = email.replyTo ? this.convertContacts(email.replyTo)[0] : void 0;
|
|
81
|
+
const from = this.convertContact(email.from || this.SENDER);
|
|
82
|
+
const subject = email.subject;
|
|
83
|
+
const textAttachments = email.text ? [{ name: "plain", type: "text/plain", content: email.text }] : [];
|
|
84
|
+
const htmlAttachments = email.html ? [{ type: "text/html", content: email.html, name: "email_body.html" }] : [];
|
|
85
|
+
const attachments = [...textAttachments, ...htmlAttachments];
|
|
86
|
+
const contacts = toContacts.flatMap((to) => {
|
|
87
|
+
const entries = [];
|
|
88
|
+
if (ccContacts.length > 0) {
|
|
89
|
+
ccContacts.forEach((cc) => {
|
|
90
|
+
entries.push({ email: cc?.email, name: cc?.name, cc: cc?.email });
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (bccContacts.length > 0) {
|
|
94
|
+
bccContacts.forEach((bcc) => {
|
|
95
|
+
entries.push({ email: bcc?.email, name: bcc?.name, bcc: bcc?.email });
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
return entries.length > 0 ? entries : [to];
|
|
99
|
+
});
|
|
100
|
+
return {
|
|
101
|
+
message: {
|
|
102
|
+
from_email: from?.email,
|
|
103
|
+
from_name: from.name || "",
|
|
104
|
+
subject,
|
|
105
|
+
attachments,
|
|
106
|
+
to: contacts,
|
|
107
|
+
text: email.text,
|
|
108
|
+
html: email.html,
|
|
109
|
+
reply_to: replyTo?.email,
|
|
110
|
+
template_id: email.templateId,
|
|
111
|
+
global_merge_vars: email.templateVariables ? Object.keys(email.templateVariables).map((key) => ({
|
|
112
|
+
name: key,
|
|
113
|
+
content: email.templateVariables[key]
|
|
114
|
+
})) : null
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
convertContacts(contacts) {
|
|
119
|
+
if (!contacts) {
|
|
120
|
+
return [];
|
|
121
|
+
}
|
|
122
|
+
const contactArray = Array.isArray(contacts) ? contacts : [contacts];
|
|
123
|
+
return contactArray.map(this.convertContact);
|
|
124
|
+
}
|
|
125
|
+
convertContact(contact) {
|
|
126
|
+
if (typeof contact === "string") {
|
|
127
|
+
return { email: contact, name: void 0 };
|
|
128
|
+
}
|
|
129
|
+
return { email: contact?.email, name: contact?.name };
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const sendEmailInputSchema = z__default.object({
|
|
134
|
+
email: iEmailSchema,
|
|
135
|
+
metadata: z__default.object({
|
|
136
|
+
userAgent: z__default.string().optional(),
|
|
137
|
+
ip: z__default.ipv4().or(z__default.ipv6()).optional(),
|
|
138
|
+
initiator: z__default.object({
|
|
139
|
+
service: z__default.string(),
|
|
140
|
+
userId: z__default.string().optional()
|
|
141
|
+
})
|
|
142
|
+
})
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const sendSlackInputSchema = v4.z.object({
|
|
146
|
+
slack: v4.z.object({
|
|
147
|
+
message: v4.z.string()
|
|
148
|
+
}),
|
|
149
|
+
metadata: v4.z.object({
|
|
150
|
+
userAgent: v4.z.string().optional(),
|
|
151
|
+
ip: v4.z.ipv4().or(v4.z.ipv6()).optional(),
|
|
152
|
+
initiator: v4.z.object({
|
|
153
|
+
service: v4.z.string(),
|
|
154
|
+
userId: v4.z.string().optional()
|
|
155
|
+
})
|
|
156
|
+
})
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
const sendSmsInputSchema = z.z.object({
|
|
160
|
+
sms: z.z.object({
|
|
161
|
+
message: z.z.string(),
|
|
162
|
+
to: z.z.string()
|
|
163
|
+
}),
|
|
164
|
+
metadata: z.z.object({
|
|
165
|
+
userAgent: z.z.string().optional(),
|
|
166
|
+
ip: z.z.ipv4().or(z.z.ipv6()).optional(),
|
|
167
|
+
initiator: z.z.object({
|
|
168
|
+
service: z.z.string(),
|
|
169
|
+
userId: z.z.string().optional()
|
|
170
|
+
})
|
|
171
|
+
})
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
class ISmsConnector {
|
|
175
|
+
constructor({
|
|
176
|
+
ACCOUNT_ID,
|
|
177
|
+
AUTH_TOKEN,
|
|
178
|
+
SERVICE_ID
|
|
179
|
+
}) {
|
|
180
|
+
this.ACCOUNT_ID = ACCOUNT_ID;
|
|
181
|
+
this.AUTH_TOKEN = AUTH_TOKEN;
|
|
182
|
+
this.SERVICE_ID = SERVICE_ID;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
class TwilioConnector extends ISmsConnector {
|
|
187
|
+
static {
|
|
188
|
+
this.providerName = "twilio";
|
|
189
|
+
}
|
|
190
|
+
constructor({
|
|
191
|
+
ACCOUNT_ID,
|
|
192
|
+
AUTH_TOKEN,
|
|
193
|
+
SERVICE_ID
|
|
194
|
+
}) {
|
|
195
|
+
super({ ACCOUNT_ID, AUTH_TOKEN, SERVICE_ID });
|
|
196
|
+
this.twilioClient = twilio__default(ACCOUNT_ID, AUTH_TOKEN);
|
|
197
|
+
}
|
|
198
|
+
async sendSms(sms) {
|
|
199
|
+
const message = await this.twilioClient.messages.create({
|
|
200
|
+
body: sms.message,
|
|
201
|
+
messagingServiceSid: this.SERVICE_ID,
|
|
202
|
+
to: sms.to
|
|
203
|
+
});
|
|
204
|
+
if (message.errorMessage)
|
|
205
|
+
return backendSdk.createInternalError(null, {
|
|
206
|
+
message: message.errorMessage,
|
|
207
|
+
status: message.errorCode
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
exports.EcomailConnector = EcomailConnector;
|
|
213
|
+
exports.IEmailConnector = IEmailConnector;
|
|
214
|
+
exports.ISmsConnector = ISmsConnector;
|
|
215
|
+
exports.TwilioConnector = TwilioConnector;
|
|
216
|
+
exports.iContactSchema = iContactSchema;
|
|
217
|
+
exports.iEmailSchema = iEmailSchema;
|
|
218
|
+
exports.sendEmailInputSchema = sendEmailInputSchema;
|
|
219
|
+
exports.sendSlackInputSchema = sendSlackInputSchema;
|
|
220
|
+
exports.sendSmsInputSchema = sendSmsInputSchema;
|