-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.ts
More file actions
399 lines (343 loc) · 10.2 KB
/
Copy pathschema.ts
File metadata and controls
399 lines (343 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import { z } from 'zod';
export const subscriberEvents = [
'buy-name-notice',
'join-network-notice',
'leave-network-notice',
'epoch-created-notice',
'epoch-distribution-notice',
'update-gateway-settings-notice',
'save-observations-notice',
'arns-name-expiration-notice',
// 'updated-demand-factor-notice',
// 'returned-name-notice',
// 'upgrade-name-notice',
// 'delegate-stake-notice',
] as const;
export const processEventSubscriptionSchema = z.object({
eventType: z.string(),
addresses: z.array(z.string()),
});
export type ProcessEventSubscription = z.infer<
typeof processEventSubscriptionSchema
>;
export const subscriberEventSchema = z.enum(subscriberEvents);
export type SubscriberEvent = (typeof subscriberEvents)[number];
// Subscriber schema
const subscriberSchema = z.object({
id: z.number(),
email: z.string().email(),
verified: z.boolean().default(false),
createdAt: z.date(),
updatedAt: z.date(),
premium: z.boolean().default(false),
});
export type Subscriber = z.infer<typeof subscriberSchema>;
// New subscriber schema (for insertion)
const newSubscriberSchema = subscriberSchema.omit({
id: true,
verified: true,
createdAt: true,
updatedAt: true,
});
export type NewSubscriber = z.infer<typeof newSubscriberSchema>;
// Process schema
const processSchema = z.object({
id: z.number(),
process_id: z.string(),
name: z.string(),
created_at: z.date(),
updated_at: z.date(),
});
export type Process = z.infer<typeof processSchema>;
// subscriber to a process schema
const subscribeToProcessSchema = z.object({
id: z.number(),
subscriber_id: z.number(),
process_id: z.string(),
event_type: z.string(),
addresses: z.string(),
});
export type SubscribeToProcess = z.infer<typeof subscribeToProcessSchema>;
const newSubscribeToProcessSchema = subscribeToProcessSchema.omit({
id: true,
});
export type NewSubscribeToProcess = z.infer<typeof newSubscribeToProcessSchema>;
// Alert schema
const eventSchema = z.object({
id: z.number(),
processId: z.string(),
eventType: z.string(),
eventData: z.object({
tags: z.array(
z.object({
name: z.string(),
value: z.string(),
}),
),
data: z.any(),
target: z.string(),
from: z.string().nullable(),
id: z.string(),
}),
blockHeight: z.number().nullable(),
nonce: z.number(),
emailsSent: z.boolean(),
createdAt: z.date(),
processedAt: z.date().nullable(),
});
export type Event = z.infer<typeof eventSchema>;
const dbEventSchema = z.object({
id: z.number(),
event_type: z.string(),
event_data: z.string(),
nonce: z.number(),
emails_sent: z.boolean().default(false),
created_at: z.date(),
processed_at: z.date().nullable(),
block_height: z.number().nullable(),
process_id: z.string(),
});
export type DBEvent = z.infer<typeof dbEventSchema>;
// new event schema (for insertion)
const newEventSchema = eventSchema.omit({
id: true,
createdAt: true,
emailsSent: true,
processedAt: true,
});
export type NewEvent = z.infer<typeof newEventSchema>;
const webhookEventSchema = z.object({
data: z.object({
id: z.string(),
tags: z.array(
z.object({
name: z.string(),
value: z.string(),
}),
),
data_hash: z.string(),
data_offset: z.number(),
data_size: z.number(),
filter: z.string(),
target: z.string(),
}),
event: z.string(),
});
export type WebhookEvent = z.infer<typeof webhookEventSchema>;
// New alert schema (for insertion)
const aoRawEventSchema = z.object({
Id: z.string(),
Messages: z.array(
z.object({
Tags: z.array(
z.object({
name: z.string(),
value: z.string(),
}),
),
Data: z.string(),
}),
),
Output: z.any(),
Error: z.any().nullable(),
});
export type RawEvent = z.infer<typeof aoRawEventSchema>;
const gqlEventSchema = z.object({
id: z.string(),
recipient: z.string(),
tags: z.array(
z.object({
name: z.string(),
value: z.string(),
}),
),
data: z.string(),
block: z.object({
height: z.number().nullable(),
}),
});
export type GQLEvent = z.infer<typeof gqlEventSchema>;
// Webhook types
export const webhookTypes = ['custom', 'discord', 'slack'] as const;
export type WebhookType = (typeof webhookTypes)[number];
// Webhook schema
const webhookSchema = z.object({
id: z.number(),
subscriber_id: z.number(),
url: z.string().url(),
description: z.string().nullable(),
type: z.enum(webhookTypes).default('custom'),
active: z.boolean().default(true),
authorization: z.string().nullable(),
last_status: z.enum(['success', 'failed']).nullable(),
last_error: z.string().nullable(),
last_triggered_at: z.string().nullable(),
created_at: z.date(),
updated_at: z.date(),
});
export type Webhook = z.infer<typeof webhookSchema>;
// New webhook schema (for insertion)
const newWebhookSchema = webhookSchema.omit({
id: true,
last_status: true,
last_error: true,
last_triggered_at: true,
created_at: true,
updated_at: true,
});
export type NewWebhook = z.infer<typeof newWebhookSchema>;
// DB webhook schema (for database rows)
const dbWebhookSchema = z.object({
id: z.number(),
subscriber_id: z.number(),
url: z.string(),
description: z.string().nullable(),
type: z.string(),
active: z.union([z.boolean(), z.number()]), // SQLite stores as 0/1
authorization: z.string().nullable(),
last_status: z.string().nullable(),
last_error: z.string().nullable(),
last_triggered_at: z.string().nullable(),
created_at: z.date(),
updated_at: z.date(),
});
export type DBWebhook = z.infer<typeof dbWebhookSchema>;
// Webhook event link schema (join table for webhook <-> event_type)
const webhookEventLinkSchema = z.object({
id: z.number(),
webhook_id: z.number(),
event_type: z.string(),
created_at: z.date(),
});
export type WebhookEventLink = z.infer<typeof webhookEventLinkSchema>;
// New webhook event link schema (for insertion)
const newWebhookEventLinkSchema = webhookEventLinkSchema.omit({
id: true,
created_at: true,
});
export type NewWebhookEventLink = z.infer<typeof newWebhookEventLinkSchema>;
// ArNS Name schema (leased names for expiration tracking)
const arnsNameSchema = z.object({
id: z.number(),
name: z.string(),
process_id: z.string(),
owner: z.string(),
root_tx_id: z.string().nullable(),
end_timestamp: z.number(),
start_timestamp: z.number(),
last_synced_at: z.date(),
created_at: z.date(),
updated_at: z.date(),
});
export type ArNSName = z.infer<typeof arnsNameSchema>;
// New ArNS name schema (for insertion/update)
const newArnsNameSchema = arnsNameSchema.omit({
id: true,
last_synced_at: true,
created_at: true,
updated_at: true,
});
export type NewArNSName = z.infer<typeof newArnsNameSchema>;
// ArNS expiration notification schema (tracks sent notifications)
export const arnsNotificationTypes = [
'grace_period_start',
'grace_period_ending',
] as const;
export type ArNSNotificationType = (typeof arnsNotificationTypes)[number];
const arnsExpirationNotificationSchema = z.object({
id: z.number(),
name: z.string(),
notification_type: z.enum(arnsNotificationTypes),
end_timestamp: z.number(),
sent_at: z.date(),
});
export type ArNSExpirationNotification = z.infer<
typeof arnsExpirationNotificationSchema
>;
// ArNS name subscription schema (subscribe to specific names)
const arnsNameSubscriptionSchema = z.object({
id: z.number(),
subscriber_id: z.number(),
name: z.string(),
created_at: z.date(),
});
export type ArNSNameSubscription = z.infer<typeof arnsNameSubscriptionSchema>;
// Gateway Monitor schemas
export const gatewayMonitorStatuses = [
'unknown',
'healthy',
'unhealthy',
] as const;
export type GatewayMonitorStatus = (typeof gatewayMonitorStatuses)[number];
const gatewayMonitorSchema = z.object({
id: z.number(),
subscriber_id: z.number(),
fqdn: z.string(),
enabled: z.boolean().default(true),
check_interval_minutes: z.number().default(5),
failure_threshold: z.number().default(3),
current_status: z.enum(gatewayMonitorStatuses).default('unknown'),
consecutive_failures: z.number().default(0),
last_check_at: z.date().nullable(),
last_alert_sent_at: z.date().nullable(),
last_recovery_sent_at: z.date().nullable(),
notify_email: z.boolean().default(true),
created_at: z.date(),
updated_at: z.date(),
});
export type GatewayMonitor = z.infer<typeof gatewayMonitorSchema>;
const newGatewayMonitorSchema = gatewayMonitorSchema.omit({
id: true,
current_status: true,
consecutive_failures: true,
last_check_at: true,
last_alert_sent_at: true,
last_recovery_sent_at: true,
created_at: true,
updated_at: true,
});
export type NewGatewayMonitor = z.infer<typeof newGatewayMonitorSchema>;
// Gateway healthcheck history schema
export const healthcheckStatuses = ['success', 'failed'] as const;
export type HealthcheckStatus = (typeof healthcheckStatuses)[number];
const gatewayHealthcheckHistorySchema = z.object({
id: z.number(),
monitor_id: z.number(),
status: z.enum(healthcheckStatuses),
response_time_ms: z.number().nullable(),
status_code: z.number().nullable(),
error_message: z.string().nullable(),
checked_at: z.date(),
});
export type GatewayHealthcheckHistory = z.infer<
typeof gatewayHealthcheckHistorySchema
>;
// Gateway monitor webhook link schema
const gatewayMonitorWebhookSchema = z.object({
id: z.number(),
monitor_id: z.number(),
webhook_id: z.number(),
notify_on_down: z.boolean().default(true),
notify_on_recovery: z.boolean().default(true),
created_at: z.date(),
});
export type GatewayMonitorWebhook = z.infer<typeof gatewayMonitorWebhookSchema>;
// Export schemas for validation
export const schemas = {
subscriber: subscriberSchema,
newSubscriber: newSubscriberSchema,
event: eventSchema,
rawEvent: aoRawEventSchema,
webhookEvent: webhookEventSchema,
gqlEvent: gqlEventSchema,
webhook: webhookSchema,
newWebhook: newWebhookSchema,
arnsName: arnsNameSchema,
newArnsName: newArnsNameSchema,
arnsExpirationNotification: arnsExpirationNotificationSchema,
arnsNameSubscription: arnsNameSubscriptionSchema,
gatewayMonitor: gatewayMonitorSchema,
newGatewayMonitor: newGatewayMonitorSchema,
gatewayHealthcheckHistory: gatewayHealthcheckHistorySchema,
gatewayMonitorWebhook: gatewayMonitorWebhookSchema,
};