@develit-services/notification 0.0.2
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/@types/consts/audit-log.consts.ts +7 -0
- package/@types/consts/index.ts +1 -0
- package/@types/database/audit-log.types.ts +7 -0
- package/@types/database/index.ts +1 -0
- package/@types/email/IEmail.connector.ts +21 -0
- package/@types/email/IEmail.types.ts +25 -0
- package/@types/email/ecomail/ecomail.connector.ts +140 -0
- package/@types/email/ecomail/ecomail.types.ts +27 -0
- package/@types/email/ecomail/index.ts +2 -0
- package/@types/email/index.ts +3 -0
- package/@types/index.ts +9 -0
- package/@types/io/index.ts +3 -0
- package/@types/io/sendEmail.ts +18 -0
- package/@types/io/sendSlack.ts +19 -0
- package/@types/io/sendSms.ts +20 -0
- package/@types/pushNotification/IPushNotification.ts +1 -0
- package/@types/pushNotification/index.ts +1 -0
- package/@types/queue.ts +24 -0
- package/@types/service.ts +30 -0
- package/@types/slack/ISlack.types.ts +3 -0
- package/@types/slack/index.ts +1 -0
- package/@types/slack/slack.connector.ts +27 -0
- package/@types/sms/ISms.connector.ts +22 -0
- package/@types/sms/ISms.types.ts +4 -0
- package/@types/sms/index.ts +3 -0
- package/@types/sms/twilio/index.ts +1 -0
- package/@types/sms/twilio/twilio.connector.ts +36 -0
- package/CHANGELOG.md +28 -0
- package/build.config.ts +28 -0
- package/drizzle.config.ts +3 -0
- package/package.json +40 -0
- package/scripts/generate-wrangler.ts +13 -0
- package/src/database/drizzle.ts +6 -0
- package/src/database/migrations/0000_funny_beast.sql +12 -0
- package/src/database/migrations/meta/0000_snapshot.json +101 -0
- package/src/database/migrations/meta/_journal.json +13 -0
- package/src/database/schema/audit-log.schema.ts +13 -0
- package/src/database/schema/index.ts +1 -0
- package/src/defineNotificationService.ts +321 -0
- package/src/defineNotificationWrangler.ts +78 -0
- package/src/index.ts +9 -0
- package/src/utils/connectors.ts +1 -0
- package/src/utils/database/command/create-audit-log.command.ts +34 -0
- package/src/utils/database/command/index.ts +1 -0
- package/src/utils/database/index.ts +1 -0
- package/src/utils/email.ts +25 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/sms.ts +28 -0
- package/test/env.d.ts +7 -0
- package/test/integration/sendEmail.test.ts +88 -0
- package/test/setup/migrations.ts +3 -0
- package/test/unit/connectors/ecomail.connector.ts +715 -0
- package/test/unit/email.test.ts +55 -0
- package/tsconfig.json +3 -0
- package/vitest.config.ts +31 -0
- package/worker-configuration.d.ts +25 -0
- package/wrangler.jsonc +199 -0
- package/wrangler.ts +106 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "6",
|
|
3
|
+
"dialect": "sqlite",
|
|
4
|
+
"id": "a975fc44-2981-453f-abe4-53763432eb34",
|
|
5
|
+
"prevId": "00000000-0000-0000-0000-000000000000",
|
|
6
|
+
"tables": {
|
|
7
|
+
"audit_log": {
|
|
8
|
+
"name": "audit_log",
|
|
9
|
+
"columns": {
|
|
10
|
+
"id": {
|
|
11
|
+
"name": "id",
|
|
12
|
+
"type": "text",
|
|
13
|
+
"primaryKey": true,
|
|
14
|
+
"notNull": true,
|
|
15
|
+
"autoincrement": false
|
|
16
|
+
},
|
|
17
|
+
"created_at": {
|
|
18
|
+
"name": "created_at",
|
|
19
|
+
"type": "integer",
|
|
20
|
+
"primaryKey": false,
|
|
21
|
+
"notNull": false,
|
|
22
|
+
"autoincrement": false,
|
|
23
|
+
"default": "(unixepoch('subsec') * 1000)"
|
|
24
|
+
},
|
|
25
|
+
"updated_at": {
|
|
26
|
+
"name": "updated_at",
|
|
27
|
+
"type": "integer",
|
|
28
|
+
"primaryKey": false,
|
|
29
|
+
"notNull": false,
|
|
30
|
+
"autoincrement": false,
|
|
31
|
+
"default": "(unixepoch('subsec') * 1000)"
|
|
32
|
+
},
|
|
33
|
+
"deleted_at": {
|
|
34
|
+
"name": "deleted_at",
|
|
35
|
+
"type": "integer",
|
|
36
|
+
"primaryKey": false,
|
|
37
|
+
"notNull": false,
|
|
38
|
+
"autoincrement": false,
|
|
39
|
+
"default": "null"
|
|
40
|
+
},
|
|
41
|
+
"event": {
|
|
42
|
+
"name": "event",
|
|
43
|
+
"type": "text",
|
|
44
|
+
"primaryKey": false,
|
|
45
|
+
"notNull": true,
|
|
46
|
+
"autoincrement": false
|
|
47
|
+
},
|
|
48
|
+
"ip": {
|
|
49
|
+
"name": "ip",
|
|
50
|
+
"type": "text",
|
|
51
|
+
"primaryKey": false,
|
|
52
|
+
"notNull": false,
|
|
53
|
+
"autoincrement": false
|
|
54
|
+
},
|
|
55
|
+
"user_agent": {
|
|
56
|
+
"name": "user_agent",
|
|
57
|
+
"type": "text",
|
|
58
|
+
"primaryKey": false,
|
|
59
|
+
"notNull": false,
|
|
60
|
+
"autoincrement": false
|
|
61
|
+
},
|
|
62
|
+
"description": {
|
|
63
|
+
"name": "description",
|
|
64
|
+
"type": "text",
|
|
65
|
+
"primaryKey": false,
|
|
66
|
+
"notNull": false,
|
|
67
|
+
"autoincrement": false
|
|
68
|
+
},
|
|
69
|
+
"initiator_service": {
|
|
70
|
+
"name": "initiator_service",
|
|
71
|
+
"type": "text",
|
|
72
|
+
"primaryKey": false,
|
|
73
|
+
"notNull": true,
|
|
74
|
+
"autoincrement": false
|
|
75
|
+
},
|
|
76
|
+
"initiator_user_id": {
|
|
77
|
+
"name": "initiator_user_id",
|
|
78
|
+
"type": "text",
|
|
79
|
+
"primaryKey": false,
|
|
80
|
+
"notNull": false,
|
|
81
|
+
"autoincrement": false
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
"indexes": {},
|
|
85
|
+
"foreignKeys": {},
|
|
86
|
+
"compositePrimaryKeys": {},
|
|
87
|
+
"uniqueConstraints": {},
|
|
88
|
+
"checkConstraints": {}
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
"views": {},
|
|
92
|
+
"enums": {},
|
|
93
|
+
"_meta": {
|
|
94
|
+
"schemas": {},
|
|
95
|
+
"tables": {},
|
|
96
|
+
"columns": {}
|
|
97
|
+
},
|
|
98
|
+
"internal": {
|
|
99
|
+
"indexes": {}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { base } from '@develit-io/backend-sdk'
|
|
2
|
+
import type { AuditLogEventType } from '@services/notification/@types'
|
|
3
|
+
import { sqliteTable, text } from 'drizzle-orm/sqlite-core'
|
|
4
|
+
|
|
5
|
+
export const auditLog = sqliteTable('audit_log', {
|
|
6
|
+
...base,
|
|
7
|
+
event: text('event').$type<AuditLogEventType>().notNull(),
|
|
8
|
+
ip: text('ip'),
|
|
9
|
+
userAgent: text('user_agent'),
|
|
10
|
+
description: text('description'),
|
|
11
|
+
initiatorService: text('initiator_service').notNull(),
|
|
12
|
+
initiatorUserId: text('initiator_user_id'),
|
|
13
|
+
})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './audit-log.schema'
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type IRPCResponse,
|
|
3
|
+
action,
|
|
4
|
+
cloudflareQueue,
|
|
5
|
+
createInternalError,
|
|
6
|
+
develitWorker,
|
|
7
|
+
useResult,
|
|
8
|
+
uuidv4,
|
|
9
|
+
} from '@develit-io/backend-sdk'
|
|
10
|
+
import {
|
|
11
|
+
type IEmailConnector,
|
|
12
|
+
type ISmsConnector,
|
|
13
|
+
type NotificationQueueMessage,
|
|
14
|
+
type SendEmailInput,
|
|
15
|
+
type SendEmailOutput,
|
|
16
|
+
type SendSlackInput,
|
|
17
|
+
type SendSlackOutput,
|
|
18
|
+
type SendSmsInput,
|
|
19
|
+
type SendSmsOutput,
|
|
20
|
+
sendEmailInputSchema,
|
|
21
|
+
sendSlackInputSchema,
|
|
22
|
+
sendSmsInputSchema,
|
|
23
|
+
} from '@develit-services/notification/@types'
|
|
24
|
+
import { tables } from '@develit-services/notification/src/database/drizzle'
|
|
25
|
+
import {
|
|
26
|
+
createAuditLogCommand,
|
|
27
|
+
initiateEmailConnector,
|
|
28
|
+
initiateSmsConnector,
|
|
29
|
+
} from '@develit-services/notification/src/utils'
|
|
30
|
+
import { WorkerEntrypoint } from 'cloudflare:workers'
|
|
31
|
+
import { type DrizzleD1Database, drizzle } from 'drizzle-orm/d1'
|
|
32
|
+
import { SlackConnector } from '../@types/slack/slack.connector'
|
|
33
|
+
|
|
34
|
+
export class NotificationServiceBase extends develitWorker(
|
|
35
|
+
WorkerEntrypoint<NotificationEnv>,
|
|
36
|
+
) {
|
|
37
|
+
readonly name: string
|
|
38
|
+
|
|
39
|
+
readonly slackConnector = new SlackConnector(this.env.SLACK_WEBHOOKS)
|
|
40
|
+
protected emailConnector!: IEmailConnector
|
|
41
|
+
protected smsConnector!: ISmsConnector
|
|
42
|
+
|
|
43
|
+
readonly db: DrizzleD1Database<typeof tables>
|
|
44
|
+
|
|
45
|
+
constructor(ctx: ExecutionContext, env: NotificationEnv) {
|
|
46
|
+
super(ctx, env)
|
|
47
|
+
this.name = 'notification-service'
|
|
48
|
+
this.db = drizzle(this.env.NOTIFICATION_D1, { schema: tables })
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@cloudflareQueue({ baseDelay: 60 })
|
|
52
|
+
async queue(batch: MessageBatch<NotificationQueueMessage>): Promise<void> {
|
|
53
|
+
for (const message of batch.messages) {
|
|
54
|
+
this.logInput({ message })
|
|
55
|
+
|
|
56
|
+
let notificationAction
|
|
57
|
+
|
|
58
|
+
const { type, metadata, payload } = message.body
|
|
59
|
+
|
|
60
|
+
if (type === 'email') {
|
|
61
|
+
const [emailConnector, error] = await useResult(
|
|
62
|
+
initiateEmailConnector(
|
|
63
|
+
this.env.EMAIL_PROVIDER,
|
|
64
|
+
this.env.EMAIL_API_KEY,
|
|
65
|
+
this.env.EMAIL_SMTP_HOST,
|
|
66
|
+
this.env.EMAIL_SENDER,
|
|
67
|
+
),
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
if (error) {
|
|
71
|
+
this.logError({ error })
|
|
72
|
+
message.retry()
|
|
73
|
+
continue
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
this.emailConnector = emailConnector!
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (type === 'sms') {
|
|
80
|
+
const [smsConnector, error] = await useResult(
|
|
81
|
+
initiateSmsConnector(
|
|
82
|
+
this.env.SMS_PROVIDER,
|
|
83
|
+
this.env.SMS_ACCOUNT_ID,
|
|
84
|
+
this.env.SMS_AUTH_TOKEN,
|
|
85
|
+
this.env.SMS_SERVICE_ID,
|
|
86
|
+
),
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
if (error) {
|
|
90
|
+
this.logError({ error })
|
|
91
|
+
message.retry()
|
|
92
|
+
continue
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
this.smsConnector = smsConnector!
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
switch (type) {
|
|
99
|
+
case 'email':
|
|
100
|
+
notificationAction = async () =>
|
|
101
|
+
this._sendEmail({
|
|
102
|
+
email: payload.email!,
|
|
103
|
+
metadata,
|
|
104
|
+
})
|
|
105
|
+
break
|
|
106
|
+
case 'sms':
|
|
107
|
+
notificationAction = async () =>
|
|
108
|
+
this._sendSms({
|
|
109
|
+
sms: payload.sms!,
|
|
110
|
+
metadata,
|
|
111
|
+
})
|
|
112
|
+
break
|
|
113
|
+
case 'pushNotification':
|
|
114
|
+
notificationAction = async () => this._sendPushNotification()
|
|
115
|
+
break
|
|
116
|
+
case 'slack':
|
|
117
|
+
notificationAction = async () =>
|
|
118
|
+
this.sendSlackNotification({
|
|
119
|
+
slack: message.body.payload.slack!,
|
|
120
|
+
metadata,
|
|
121
|
+
})
|
|
122
|
+
break
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const { error, message: errorMessage } = await notificationAction()
|
|
126
|
+
|
|
127
|
+
if (error) {
|
|
128
|
+
this.logError({ error: errorMessage })
|
|
129
|
+
message.retry()
|
|
130
|
+
continue
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
message.ack()
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
@action('private-send-email')
|
|
138
|
+
private async _sendEmail(
|
|
139
|
+
input: SendEmailInput,
|
|
140
|
+
): Promise<IRPCResponse<SendEmailOutput>> {
|
|
141
|
+
return this.handleAction(
|
|
142
|
+
{ data: input, schema: sendEmailInputSchema },
|
|
143
|
+
{ successMessage: 'Email sent.' },
|
|
144
|
+
async (params) => {
|
|
145
|
+
const {
|
|
146
|
+
email,
|
|
147
|
+
metadata: {
|
|
148
|
+
ip,
|
|
149
|
+
userAgent,
|
|
150
|
+
initiator: { service, userId },
|
|
151
|
+
},
|
|
152
|
+
} = params!
|
|
153
|
+
|
|
154
|
+
if (!this.emailConnector)
|
|
155
|
+
this.emailConnector = await initiateEmailConnector(
|
|
156
|
+
this.env.EMAIL_PROVIDER,
|
|
157
|
+
this.env.EMAIL_API_KEY,
|
|
158
|
+
this.env.EMAIL_SMTP_HOST,
|
|
159
|
+
this.env.EMAIL_SENDER,
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
const response = await this.emailConnector.sendEmail(email)
|
|
163
|
+
|
|
164
|
+
if (!response?.ok) {
|
|
165
|
+
throw createInternalError(null, {
|
|
166
|
+
message: `Could not send email: ${JSON.stringify(await response?.json())}`,
|
|
167
|
+
status: 500,
|
|
168
|
+
})
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const { command } = await createAuditLogCommand({
|
|
172
|
+
db: this.db,
|
|
173
|
+
auditLog: {
|
|
174
|
+
id: uuidv4(),
|
|
175
|
+
event: 'EMAIL',
|
|
176
|
+
ip,
|
|
177
|
+
initiatorService: service,
|
|
178
|
+
initiatorUserId: userId,
|
|
179
|
+
userAgent,
|
|
180
|
+
description: JSON.stringify(input),
|
|
181
|
+
},
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
await this.db.batch([command])
|
|
185
|
+
|
|
186
|
+
return {}
|
|
187
|
+
},
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
@action('private-send-sms')
|
|
192
|
+
private async _sendSms(
|
|
193
|
+
input: SendSmsInput,
|
|
194
|
+
): Promise<IRPCResponse<SendSmsOutput>> {
|
|
195
|
+
return this.handleAction(
|
|
196
|
+
{ data: input, schema: sendSmsInputSchema },
|
|
197
|
+
{ successMessage: 'Sms sent.' },
|
|
198
|
+
async (params) => {
|
|
199
|
+
const {
|
|
200
|
+
sms: { message, to },
|
|
201
|
+
metadata: {
|
|
202
|
+
ip,
|
|
203
|
+
userAgent,
|
|
204
|
+
initiator: { service, userId },
|
|
205
|
+
},
|
|
206
|
+
} = params!
|
|
207
|
+
|
|
208
|
+
await this.smsConnector.sendSms({ message, to })
|
|
209
|
+
|
|
210
|
+
const { command } = await createAuditLogCommand({
|
|
211
|
+
db: this.db,
|
|
212
|
+
auditLog: {
|
|
213
|
+
id: uuidv4(),
|
|
214
|
+
event: 'SMS',
|
|
215
|
+
ip,
|
|
216
|
+
userAgent,
|
|
217
|
+
initiatorService: service,
|
|
218
|
+
initiatorUserId: userId,
|
|
219
|
+
description: JSON.stringify(input),
|
|
220
|
+
},
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
await this.db.batch([command])
|
|
224
|
+
|
|
225
|
+
return {}
|
|
226
|
+
},
|
|
227
|
+
)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
@action('public-send-email')
|
|
231
|
+
async sendEmail(
|
|
232
|
+
input: SendEmailInput,
|
|
233
|
+
): Promise<IRPCResponse<SendEmailOutput>> {
|
|
234
|
+
return this.handleAction(
|
|
235
|
+
{ data: input, schema: sendEmailInputSchema },
|
|
236
|
+
{ successMessage: 'Email sent.' },
|
|
237
|
+
async (params) => {
|
|
238
|
+
const { email, metadata } = params!
|
|
239
|
+
|
|
240
|
+
await this.pushToQueue<NotificationQueueMessage>(
|
|
241
|
+
this.env.NOTIFICATIONS_QUEUE,
|
|
242
|
+
{
|
|
243
|
+
type: 'email',
|
|
244
|
+
payload: {
|
|
245
|
+
email,
|
|
246
|
+
},
|
|
247
|
+
metadata,
|
|
248
|
+
},
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
return {}
|
|
252
|
+
},
|
|
253
|
+
)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
@action('public-send-sms')
|
|
257
|
+
async sendSms(input: SendSmsInput): Promise<IRPCResponse<SendSmsOutput>> {
|
|
258
|
+
return this.handleAction(
|
|
259
|
+
{ data: input, schema: sendSmsInputSchema },
|
|
260
|
+
{ successMessage: 'SMS sent.' },
|
|
261
|
+
async (params) => {
|
|
262
|
+
const {
|
|
263
|
+
sms: { message, to },
|
|
264
|
+
metadata,
|
|
265
|
+
} = params!
|
|
266
|
+
|
|
267
|
+
await this.pushToQueue<NotificationQueueMessage>(
|
|
268
|
+
this.env.NOTIFICATIONS_QUEUE,
|
|
269
|
+
{
|
|
270
|
+
type: 'sms',
|
|
271
|
+
payload: {
|
|
272
|
+
sms: {
|
|
273
|
+
message,
|
|
274
|
+
to,
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
metadata,
|
|
278
|
+
},
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
return {}
|
|
282
|
+
},
|
|
283
|
+
)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
@action('send-push-notification')
|
|
287
|
+
private async _sendPushNotification(): Promise<IRPCResponse<{}>> {
|
|
288
|
+
this.logInput({})
|
|
289
|
+
|
|
290
|
+
this.logError({ error: 'Method not implemented.' })
|
|
291
|
+
throw new Error('Method not implemented.')
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
@action('send-slack-notification')
|
|
295
|
+
async sendSlackNotification(
|
|
296
|
+
input: SendSlackInput,
|
|
297
|
+
): Promise<IRPCResponse<SendSlackOutput>> {
|
|
298
|
+
return this.handleAction(
|
|
299
|
+
{
|
|
300
|
+
data: input,
|
|
301
|
+
schema: sendSlackInputSchema,
|
|
302
|
+
},
|
|
303
|
+
{ successMessage: 'Slack sent.' },
|
|
304
|
+
async (params) => {
|
|
305
|
+
const { slack } = params!
|
|
306
|
+
|
|
307
|
+
await this.slackConnector.sendNotificationToAllSlack(slack.message)
|
|
308
|
+
|
|
309
|
+
return {}
|
|
310
|
+
},
|
|
311
|
+
)
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export function defineNotificationService() {
|
|
316
|
+
return class NotificationService extends NotificationServiceBase {
|
|
317
|
+
constructor(ctx: ExecutionContext, env: NotificationEnv) {
|
|
318
|
+
super(ctx, env)
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// defineNotificationServiceWrangler.ts
|
|
2
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: allow any */
|
|
3
|
+
import type { NotificationServiceWranglerConfig } from '../@types'
|
|
4
|
+
|
|
5
|
+
export function defineNotificationServiceWrangler(
|
|
6
|
+
config: NotificationServiceWranglerConfig,
|
|
7
|
+
) {
|
|
8
|
+
const { name, envs } = config
|
|
9
|
+
|
|
10
|
+
const base = {
|
|
11
|
+
name,
|
|
12
|
+
main: './src/index.ts',
|
|
13
|
+
compatibility_date: '2025-06-04',
|
|
14
|
+
compatibility_flags: ['nodejs_compat'],
|
|
15
|
+
d1_databases: [
|
|
16
|
+
{
|
|
17
|
+
binding: 'NOTIFICATION_D1',
|
|
18
|
+
database_name: name,
|
|
19
|
+
database_id: envs.local.d1.id,
|
|
20
|
+
migrations_dir: './src/database/migrations',
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
queues: {
|
|
24
|
+
producers: [
|
|
25
|
+
{
|
|
26
|
+
binding: 'NOTIFICATIONS_QUEUE',
|
|
27
|
+
queue: `${name}`,
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
consumers: [
|
|
31
|
+
{
|
|
32
|
+
queue: `${name}`,
|
|
33
|
+
max_batch_size: 1,
|
|
34
|
+
max_batch_timeout: 5,
|
|
35
|
+
dead_letter_queue: `${name}-dlq`,
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
env: {} as Record<string, any>,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (const [envName, envCfg] of Object.entries(envs).filter(
|
|
43
|
+
([key]) => key !== 'local',
|
|
44
|
+
)) {
|
|
45
|
+
base.env[envName] = {
|
|
46
|
+
vars: {
|
|
47
|
+
...envCfg.vars,
|
|
48
|
+
ENVIRONMENT: envName,
|
|
49
|
+
},
|
|
50
|
+
d1_databases: [
|
|
51
|
+
{
|
|
52
|
+
binding: 'NOTIFICATION_D1',
|
|
53
|
+
database_name: `${name}-${envName}`,
|
|
54
|
+
database_id: envCfg.d1.id,
|
|
55
|
+
migrations_dir: './src/database/migrations',
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
queues: {
|
|
59
|
+
producers: [
|
|
60
|
+
{
|
|
61
|
+
binding: 'NOTIFICATIONS_QUEUE',
|
|
62
|
+
queue: `${name}-${envName}`,
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
consumers: [
|
|
66
|
+
{
|
|
67
|
+
queue: `${name}-${envName}`,
|
|
68
|
+
max_batch_size: envCfg.queue?.max_batch_size ?? 1,
|
|
69
|
+
max_batch_timeout: envCfg.queue?.max_batch_timeout ?? 5,
|
|
70
|
+
dead_letter_queue: `${name}-dlq-${envName}`,
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return base
|
|
78
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { defineNotificationService } from './defineNotificationService'
|
|
2
|
+
import type { WorkerEntrypoint } from 'cloudflare:workers'
|
|
3
|
+
|
|
4
|
+
const NotificationService = defineNotificationService()
|
|
5
|
+
|
|
6
|
+
export default NotificationService as new (
|
|
7
|
+
ctx: ExecutionContext,
|
|
8
|
+
env: NotificationEnv,
|
|
9
|
+
) => WorkerEntrypoint<NotificationEnv>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const initiateConnector = () => {}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { uuidv4 } from '@develit-io/backend-sdk'
|
|
2
|
+
import type { AuditLogInsertType } from '@services/notification/@types'
|
|
3
|
+
import { tables } from '@services/notification/src/database/drizzle'
|
|
4
|
+
import type { DrizzleD1Database } from 'drizzle-orm/d1'
|
|
5
|
+
|
|
6
|
+
export const createAuditLogCommand = async ({
|
|
7
|
+
db,
|
|
8
|
+
auditLog: {
|
|
9
|
+
event,
|
|
10
|
+
description,
|
|
11
|
+
initiatorService,
|
|
12
|
+
initiatorUserId,
|
|
13
|
+
ip,
|
|
14
|
+
userAgent,
|
|
15
|
+
},
|
|
16
|
+
}: {
|
|
17
|
+
db: DrizzleD1Database<typeof tables>
|
|
18
|
+
auditLog: AuditLogInsertType
|
|
19
|
+
}) => {
|
|
20
|
+
const command = db.insert(tables.auditLog).values({
|
|
21
|
+
id: uuidv4(),
|
|
22
|
+
createdAt: new Date(),
|
|
23
|
+
event,
|
|
24
|
+
description,
|
|
25
|
+
ip,
|
|
26
|
+
userAgent,
|
|
27
|
+
initiatorService,
|
|
28
|
+
initiatorUserId,
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
command,
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './create-audit-log.command'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './command'
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createInternalError } from '@develit-io/backend-sdk'
|
|
2
|
+
import {
|
|
3
|
+
EcomailConnector,
|
|
4
|
+
type IContact,
|
|
5
|
+
type IEmailConnector,
|
|
6
|
+
} from '@services/notification/@types'
|
|
7
|
+
|
|
8
|
+
export const initiateEmailConnector = async (
|
|
9
|
+
provider: string,
|
|
10
|
+
apiKey: string,
|
|
11
|
+
smtpHost: string,
|
|
12
|
+
sender: IContact,
|
|
13
|
+
): Promise<IEmailConnector> => {
|
|
14
|
+
const connector = [EcomailConnector].find(
|
|
15
|
+
(conn) => conn.providerName === provider,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
if (!connector)
|
|
19
|
+
throw createInternalError(null, {
|
|
20
|
+
message: 'Unsupported email provider',
|
|
21
|
+
status: 404,
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
return new connector({ API_KEY: apiKey, SMTP_HOST: smtpHost, SENDER: sender })
|
|
25
|
+
}
|
package/src/utils/sms.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { createInternalError } from '@develit-io/backend-sdk'
|
|
2
|
+
import {
|
|
3
|
+
type ISmsConnector,
|
|
4
|
+
TwilioConnector,
|
|
5
|
+
} from '@services/notification/@types/sms'
|
|
6
|
+
|
|
7
|
+
export const initiateSmsConnector = async (
|
|
8
|
+
provider: string,
|
|
9
|
+
accountId: string,
|
|
10
|
+
authToken: string,
|
|
11
|
+
serviceId: string,
|
|
12
|
+
): Promise<ISmsConnector> => {
|
|
13
|
+
const connector = [TwilioConnector].find(
|
|
14
|
+
(conn) => conn.providerName === provider,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
if (!connector)
|
|
18
|
+
throw createInternalError(null, {
|
|
19
|
+
message: 'Unsupported sms provider',
|
|
20
|
+
status: 404,
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
return new connector({
|
|
24
|
+
ACCOUNT_ID: accountId,
|
|
25
|
+
AUTH_TOKEN: authToken,
|
|
26
|
+
SERVICE_ID: serviceId,
|
|
27
|
+
})
|
|
28
|
+
}
|