@develit-services/notification 0.0.19 → 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 -214
- 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 -203
- 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,211 +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
|
-
if (email.templateVariables) {
|
|
56
|
-
for (const [key, value] of Object.entries(email.templateVariables)) {
|
|
57
|
-
if (typeof value === "string" && String(value).includes("localhost") && key in email.templateVariables) {
|
|
58
|
-
email.templateVariables[key] = String(value).replace("localhost", "origin");
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
const emEmail = this.convertEmail(email);
|
|
63
|
-
const uri = emEmail.message.template_id ? "https://api2.ecomailapp.cz/transactional/send-template" : "https://api2.ecomailapp.cz/transactional/send-message";
|
|
64
|
-
const [_, error] = await useFetch(uri, {
|
|
65
|
-
method: "POST",
|
|
66
|
-
headers: {
|
|
67
|
-
"Content-Type": "application/json",
|
|
68
|
-
key: this.API_KEY
|
|
69
|
-
},
|
|
70
|
-
body: JSON.stringify(emEmail)
|
|
71
|
-
});
|
|
72
|
-
if (error) throw error;
|
|
73
|
-
}
|
|
74
|
-
convertEmail(email) {
|
|
75
|
-
const toContacts = this.convertContacts(email.to);
|
|
76
|
-
const ccContacts = email.cc ? this.convertContacts(email.cc) : [];
|
|
77
|
-
const bccContacts = email.bcc ? this.convertContacts(email.bcc) : [];
|
|
78
|
-
const replyTo = email.replyTo ? this.convertContacts(email.replyTo)[0] : void 0;
|
|
79
|
-
const from = this.convertContact(email.from || this.SENDER);
|
|
80
|
-
const subject = email.subject;
|
|
81
|
-
const textAttachments = email.text ? [{ name: "plain", type: "text/plain", content: email.text }] : [];
|
|
82
|
-
const htmlAttachments = email.html ? [{ type: "text/html", content: email.html, name: "email_body.html" }] : [];
|
|
83
|
-
const attachments = [...textAttachments, ...htmlAttachments];
|
|
84
|
-
const contacts = toContacts.flatMap((to) => {
|
|
85
|
-
const entries = [];
|
|
86
|
-
if (ccContacts.length > 0) {
|
|
87
|
-
ccContacts.forEach((cc) => {
|
|
88
|
-
entries.push({ email: cc?.email, name: cc?.name, cc: cc?.email });
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
if (bccContacts.length > 0) {
|
|
92
|
-
bccContacts.forEach((bcc) => {
|
|
93
|
-
entries.push({ email: bcc?.email, name: bcc?.name, bcc: bcc?.email });
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
return entries.length > 0 ? entries : [to];
|
|
97
|
-
});
|
|
98
|
-
return {
|
|
99
|
-
message: {
|
|
100
|
-
from_email: from?.email,
|
|
101
|
-
from_name: from.name || "",
|
|
102
|
-
subject,
|
|
103
|
-
attachments,
|
|
104
|
-
to: contacts,
|
|
105
|
-
text: email.text,
|
|
106
|
-
html: email.html,
|
|
107
|
-
reply_to: replyTo?.email,
|
|
108
|
-
template_id: email.templateId,
|
|
109
|
-
global_merge_vars: email.templateVariables ? Object.keys(email.templateVariables).map((key) => ({
|
|
110
|
-
name: key,
|
|
111
|
-
content: email.templateVariables[key]
|
|
112
|
-
})) : null
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
convertContacts(contacts) {
|
|
117
|
-
if (!contacts) {
|
|
118
|
-
return [];
|
|
119
|
-
}
|
|
120
|
-
const contactArray = Array.isArray(contacts) ? contacts : [contacts];
|
|
121
|
-
return contactArray.map(this.convertContact);
|
|
122
|
-
}
|
|
123
|
-
convertContact(contact) {
|
|
124
|
-
if (typeof contact === "string") {
|
|
125
|
-
return { email: contact, name: void 0 };
|
|
126
|
-
}
|
|
127
|
-
return { email: contact?.email, name: contact?.name };
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const sendEmailInputSchema = z$1.object({
|
|
132
|
-
email: iEmailSchema,
|
|
133
|
-
metadata: z$1.object({
|
|
134
|
-
userAgent: z$1.string().optional(),
|
|
135
|
-
ip: z$1.ipv4().or(z$1.ipv6()).optional(),
|
|
136
|
-
initiator: z$1.object({
|
|
137
|
-
service: z$1.string(),
|
|
138
|
-
userId: z$1.string().optional()
|
|
139
|
-
})
|
|
140
|
-
})
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
const sendSlackInputSchema = z$2.object({
|
|
144
|
-
slack: z$2.object({
|
|
145
|
-
message: z$2.string()
|
|
146
|
-
}),
|
|
147
|
-
metadata: z$2.object({
|
|
148
|
-
userAgent: z$2.string().optional(),
|
|
149
|
-
ip: z$2.ipv4().or(z$2.ipv6()).optional(),
|
|
150
|
-
initiator: z$2.object({
|
|
151
|
-
service: z$2.string(),
|
|
152
|
-
userId: z$2.string().optional()
|
|
153
|
-
})
|
|
154
|
-
})
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
const sendSmsInputSchema = z.object({
|
|
158
|
-
sms: z.object({
|
|
159
|
-
message: z.string(),
|
|
160
|
-
to: z.string()
|
|
161
|
-
}),
|
|
162
|
-
metadata: z.object({
|
|
163
|
-
userAgent: z.string().optional(),
|
|
164
|
-
ip: z.ipv4().or(z.ipv6()).optional(),
|
|
165
|
-
initiator: z.object({
|
|
166
|
-
service: z.string(),
|
|
167
|
-
userId: z.string().optional()
|
|
168
|
-
})
|
|
169
|
-
})
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
class ISmsConnector {
|
|
173
|
-
constructor({
|
|
174
|
-
ACCOUNT_ID,
|
|
175
|
-
AUTH_TOKEN,
|
|
176
|
-
SERVICE_ID
|
|
177
|
-
}) {
|
|
178
|
-
this.ACCOUNT_ID = ACCOUNT_ID;
|
|
179
|
-
this.AUTH_TOKEN = AUTH_TOKEN;
|
|
180
|
-
this.SERVICE_ID = SERVICE_ID;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
class TwilioConnector extends ISmsConnector {
|
|
185
|
-
static {
|
|
186
|
-
this.providerName = "twilio";
|
|
187
|
-
}
|
|
188
|
-
constructor({
|
|
189
|
-
ACCOUNT_ID,
|
|
190
|
-
AUTH_TOKEN,
|
|
191
|
-
SERVICE_ID
|
|
192
|
-
}) {
|
|
193
|
-
super({ ACCOUNT_ID, AUTH_TOKEN, SERVICE_ID });
|
|
194
|
-
this.twilioClient = twilio(ACCOUNT_ID, AUTH_TOKEN);
|
|
195
|
-
}
|
|
196
|
-
async sendSms(sms) {
|
|
197
|
-
const message = await this.twilioClient.messages.create({
|
|
198
|
-
body: sms.message,
|
|
199
|
-
messagingServiceSid: this.SERVICE_ID,
|
|
200
|
-
to: sms.to
|
|
201
|
-
});
|
|
202
|
-
if (message.errorMessage)
|
|
203
|
-
return createInternalError(null, {
|
|
204
|
-
message: message.errorMessage,
|
|
205
|
-
status: message.errorCode
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
}
|
|
8
|
+
import 'zod';
|
|
9
|
+
import 'zod/v4';
|
|
10
|
+
import 'twilio';
|
|
209
11
|
|
|
210
12
|
const tables = schema;
|
|
211
13
|
|
|
@@ -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;
|