notifkit 0.1.15 → 0.1.16
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/dashboard/dist/assets/index-C-DJYu0_.css +1 -0
- package/dashboard/dist/assets/index-CzFZcEon.js +318 -0
- package/dashboard/dist/index.html +2 -2
- package/dist/{bootstrap-BOmT69OH.mjs → bootstrap-BMDRPjgp.mjs} +1 -1
- package/dist/{bulk-ZPiOJRx-.mjs → bulk-CJ5QXiPa.mjs} +1 -1
- package/dist/index.d.mts +71 -27
- package/dist/index.mjs +2 -2
- package/dist/{main-CCqcFDDe.mjs → main-6_bXQBDH.mjs} +21 -41
- package/dist/{main-BdZvBgV7.mjs → main-8nP3qmCT.mjs} +22 -48
- package/dist/{main-C0m9-Eos.mjs → main-B82bsFoy.mjs} +183 -334
- package/dist/{main-CTjaZ7ZD.mjs → main-CpaHF1Zw.mjs} +13 -15
- package/dist/{main-JEXBLP2A.mjs → main-DULD8-th.mjs} +8 -6
- package/dist/{main-C6S3EsJ3.mjs → main-ZW5RaiOK.mjs} +77 -64
- package/dist/{main-g33jpia4.mjs → main-g1KEcXXp.mjs} +676 -582
- package/dist/{main-24J4U8yP.mjs → main-mGIT8jv4.mjs} +4 -4
- package/dist/schedule-or-dispatch-Cd7GLfc3.mjs +52 -0
- package/dist/{src-9LXITcNi.mjs → src-BE_EUpZ6.mjs} +93 -93
- package/package.json +1 -1
- package/scripts/create-project.mjs +6 -2
- package/src/client.ts +3 -3
- package/src/config/index.ts +1 -0
- package/src/contracts/common.ts +2 -0
- package/src/contracts/envelope.ts +1 -1
- package/src/contracts/events/notification-created.ts +2 -0
- package/src/contracts/events/notification-delivered.ts +1 -1
- package/src/contracts/events/notification-failed.ts +1 -1
- package/src/contracts/events/notification-requested.ts +3 -2
- package/src/contracts/helpers.ts +1 -2
- package/src/contracts/index.ts +1 -0
- package/src/contracts/registry.ts +1 -0
- package/src/contracts/sdk.ts +8 -37
- package/src/db/schema.ts +8 -13
- package/src/idempotency/index.ts +6 -8
- package/src/metrics/index.ts +2 -0
- package/src/rate-limiter/api-rate-limiter.ts +208 -0
- package/src/rate-limiter/index.ts +3 -411
- package/src/rate-limiter/project-settings-cache.ts +99 -0
- package/src/rate-limiter/user-throttle.ts +100 -0
- package/src/redis/index.ts +10 -3
- package/src/repositories/admin-user.ts +83 -0
- package/src/repositories/contact.ts +359 -0
- package/src/repositories/index.ts +9 -1416
- package/src/repositories/preference.ts +35 -0
- package/src/repositories/project.ts +110 -0
- package/src/repositories/segment.ts +16 -0
- package/src/repositories/template.ts +72 -0
- package/src/repositories/types.ts +49 -0
- package/src/repositories/user.ts +644 -0
- package/src/repositories/workflow.ts +64 -0
- package/src/services/ai/main.ts +29 -71
- package/src/services/api/admin-static.ts +4 -1
- package/src/services/api/handlers/auth.ts +109 -0
- package/src/services/api/handlers/campaigns.ts +191 -0
- package/src/services/api/handlers/events.ts +99 -0
- package/src/services/api/handlers/notifications.ts +356 -0
- package/src/services/api/handlers/projects.ts +116 -0
- package/src/services/api/handlers/shared.ts +262 -0
- package/src/services/api/handlers/suppressions.ts +95 -0
- package/src/services/api/handlers/system.ts +314 -0
- package/src/services/api/handlers/templates.ts +97 -0
- package/src/services/api/handlers/unsubscribe.ts +215 -0
- package/src/services/api/handlers/users.ts +262 -0
- package/src/services/api/handlers/workflows.ts +150 -0
- package/src/services/api/handlers.ts +25 -2190
- package/src/services/api/health.ts +133 -0
- package/src/services/api/http.ts +12 -0
- package/src/services/api/main.ts +32 -322
- package/src/services/api/router.ts +1 -1
- package/src/services/api/webhooks.ts +172 -0
- package/src/services/delivery/main.ts +256 -498
- package/src/services/engine/main.ts +31 -247
- package/src/services/engine/quiet-hours.ts +151 -0
- package/src/services/engine/schedule-or-dispatch.ts +104 -0
- package/src/services/enricher/main.ts +9 -21
- package/src/services/events/main.ts +2 -2
- package/src/services/scheduler/main.ts +16 -25
- package/src/services/workflow/duration.ts +24 -0
- package/src/services/workflow/main.ts +6 -164
- package/src/services/workflow/timers.ts +157 -0
- package/src/shared/events.ts +3 -2
- package/src/templates/render.ts +1 -0
- package/src/workers/index.ts +7 -4
- package/src/workflows/sdk.ts +1 -0
- package/dashboard/dist/assets/index-ggpJKZqZ.js +0 -315
- package/dashboard/dist/assets/index-s-SXGIwv.css +0 -1
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { eq, or, sql as drizzleSql } from "drizzle-orm";
|
|
2
|
+
import type { Db } from "@/index.js";
|
|
3
|
+
import { adminUsers } from "@/db/schema.js";
|
|
4
|
+
|
|
5
|
+
export interface AdminUserRecord {
|
|
6
|
+
id: string;
|
|
7
|
+
email: string;
|
|
8
|
+
username: string | null;
|
|
9
|
+
passwordHash: string;
|
|
10
|
+
role: string;
|
|
11
|
+
createdAt: Date;
|
|
12
|
+
updatedAt: Date;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class AdminUserRepository {
|
|
16
|
+
constructor(private readonly db: Db) {}
|
|
17
|
+
|
|
18
|
+
async findByEmailOrUsername(identifier: string): Promise<AdminUserRecord | null> {
|
|
19
|
+
const normalized = identifier.trim().toLowerCase();
|
|
20
|
+
const rows = await this.db
|
|
21
|
+
.select()
|
|
22
|
+
.from(adminUsers)
|
|
23
|
+
.where(or(eq(adminUsers.email, normalized), eq(adminUsers.username, identifier.trim())))
|
|
24
|
+
.limit(1);
|
|
25
|
+
return (rows[0] as AdminUserRecord) ?? null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async findById(id: string): Promise<AdminUserRecord | null> {
|
|
29
|
+
const rows = await this.db.select().from(adminUsers).where(eq(adminUsers.id, id)).limit(1);
|
|
30
|
+
return (rows[0] as AdminUserRecord) ?? null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async create(data: {
|
|
34
|
+
email: string;
|
|
35
|
+
username?: string | null;
|
|
36
|
+
passwordHash: string;
|
|
37
|
+
role?: string;
|
|
38
|
+
}): Promise<AdminUserRecord> {
|
|
39
|
+
const rows = await this.db
|
|
40
|
+
.insert(adminUsers)
|
|
41
|
+
.values({
|
|
42
|
+
email: data.email.trim().toLowerCase(),
|
|
43
|
+
username: data.username ? data.username.trim() : null,
|
|
44
|
+
passwordHash: data.passwordHash,
|
|
45
|
+
role: data.role || "admin",
|
|
46
|
+
})
|
|
47
|
+
.returning();
|
|
48
|
+
return rows[0] as AdminUserRecord;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @deprecated Unused; removed in the next minor version. */
|
|
52
|
+
async updatePassword(id: string, passwordHash: string): Promise<boolean> {
|
|
53
|
+
const rows = await this.db
|
|
54
|
+
.update(adminUsers)
|
|
55
|
+
.set({ passwordHash, updatedAt: new Date() })
|
|
56
|
+
.where(eq(adminUsers.id, id))
|
|
57
|
+
.returning();
|
|
58
|
+
return rows.length > 0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @deprecated Unused; removed in the next minor version. */
|
|
62
|
+
async list(): Promise<Omit<AdminUserRecord, "passwordHash">[]> {
|
|
63
|
+
const rows = await this.db
|
|
64
|
+
.select({
|
|
65
|
+
id: adminUsers.id,
|
|
66
|
+
email: adminUsers.email,
|
|
67
|
+
username: adminUsers.username,
|
|
68
|
+
role: adminUsers.role,
|
|
69
|
+
createdAt: adminUsers.createdAt,
|
|
70
|
+
updatedAt: adminUsers.updatedAt,
|
|
71
|
+
})
|
|
72
|
+
.from(adminUsers)
|
|
73
|
+
.orderBy(adminUsers.createdAt);
|
|
74
|
+
return rows as Omit<AdminUserRecord, "passwordHash">[];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async count(): Promise<number> {
|
|
78
|
+
const rows = await this.db
|
|
79
|
+
.select({ count: drizzleSql<number>`count(*)::int` })
|
|
80
|
+
.from(adminUsers);
|
|
81
|
+
return rows[0]?.count ?? 0;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import { eq, and, sql as drizzleSql, inArray } from "drizzle-orm";
|
|
2
|
+
import { LRUCache } from "@/shared/index.js";
|
|
3
|
+
import type { Db } from "@/index.js";
|
|
4
|
+
import type { Preferences, ContactChannel } from "@/contracts/index.js";
|
|
5
|
+
import { users, userContacts, contactTopicPreferences } from "@/db/schema.js";
|
|
6
|
+
import type { UserContact } from "./types.js";
|
|
7
|
+
|
|
8
|
+
export class ContactRepository {
|
|
9
|
+
private readonly activeContactsCache = new LRUCache<string, UserContact[]>(5000, 30_000);
|
|
10
|
+
|
|
11
|
+
constructor(private readonly db: Db) {}
|
|
12
|
+
|
|
13
|
+
/** A repository that runs its queries inside `tx`. */
|
|
14
|
+
withTx(tx: Db): ContactRepository {
|
|
15
|
+
return new ContactRepository(tx);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async findByUserId(projectId: string, userId: string): Promise<UserContact[]> {
|
|
19
|
+
const internalUserIdRows = await this.db
|
|
20
|
+
.select({ id: users.id })
|
|
21
|
+
.from(users)
|
|
22
|
+
.where(and(eq(users.externalId, userId), eq(users.projectId, projectId)));
|
|
23
|
+
const internalId = internalUserIdRows[0]?.id;
|
|
24
|
+
if (!internalId) return [];
|
|
25
|
+
|
|
26
|
+
const rows = await this.db
|
|
27
|
+
.select()
|
|
28
|
+
.from(userContacts)
|
|
29
|
+
.where(eq(userContacts.userId, internalId));
|
|
30
|
+
if (rows.length === 0) return [];
|
|
31
|
+
|
|
32
|
+
const topicRows = await this.db
|
|
33
|
+
.select()
|
|
34
|
+
.from(contactTopicPreferences)
|
|
35
|
+
.where(
|
|
36
|
+
inArray(
|
|
37
|
+
contactTopicPreferences.contactId,
|
|
38
|
+
rows.map((r) => r.id),
|
|
39
|
+
),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const topicsByContact = new Map<string, Record<string, boolean>>();
|
|
43
|
+
for (const t of topicRows) {
|
|
44
|
+
if (!topicsByContact.has(t.contactId)) topicsByContact.set(t.contactId, {});
|
|
45
|
+
topicsByContact.get(t.contactId)![t.topic] = t.enabled;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return rows.map((r) => ({
|
|
49
|
+
id: r.id,
|
|
50
|
+
userId,
|
|
51
|
+
channel: r.channel as ContactChannel,
|
|
52
|
+
target: r.target,
|
|
53
|
+
preferences: { topics: topicsByContact.get(r.id) ?? {} },
|
|
54
|
+
active: r.enabled,
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Resolve active addressable contacts for a batch without an N+1 query. */
|
|
59
|
+
async findActiveByUserIds(
|
|
60
|
+
projectId: string,
|
|
61
|
+
userIds: string[],
|
|
62
|
+
): Promise<Map<string, UserContact[]>> {
|
|
63
|
+
const byUser = new Map<string, UserContact[]>();
|
|
64
|
+
if (userIds.length === 0) return byUser;
|
|
65
|
+
|
|
66
|
+
const missingIds: string[] = [];
|
|
67
|
+
for (const userId of userIds) {
|
|
68
|
+
const cacheKey = `${projectId}:${userId}`;
|
|
69
|
+
const cached = this.activeContactsCache.get(cacheKey);
|
|
70
|
+
if (cached !== undefined) {
|
|
71
|
+
if (cached.length > 0) {
|
|
72
|
+
byUser.set(userId, cached);
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
missingIds.push(userId);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (missingIds.length === 0) {
|
|
80
|
+
return byUser;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const rows = await this.db
|
|
84
|
+
.select({
|
|
85
|
+
userId: users.externalId,
|
|
86
|
+
id: userContacts.id,
|
|
87
|
+
channel: userContacts.channel,
|
|
88
|
+
target: userContacts.target,
|
|
89
|
+
enabled: userContacts.enabled,
|
|
90
|
+
})
|
|
91
|
+
.from(users)
|
|
92
|
+
.innerJoin(userContacts, eq(users.id, userContacts.userId))
|
|
93
|
+
.where(
|
|
94
|
+
and(
|
|
95
|
+
eq(users.projectId, projectId),
|
|
96
|
+
inArray(users.externalId, missingIds),
|
|
97
|
+
eq(userContacts.enabled, true),
|
|
98
|
+
),
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
// Per-address topic opt-outs (a user who wants marketing at home but not at
|
|
102
|
+
// work). Without them the pipeline cannot honour an opt-out it accepted.
|
|
103
|
+
const topicsByContact = new Map<string, Record<string, boolean>>();
|
|
104
|
+
if (rows.length > 0) {
|
|
105
|
+
const topicRows = await this.db
|
|
106
|
+
.select()
|
|
107
|
+
.from(contactTopicPreferences)
|
|
108
|
+
.where(
|
|
109
|
+
inArray(
|
|
110
|
+
contactTopicPreferences.contactId,
|
|
111
|
+
rows.map((r) => r.id),
|
|
112
|
+
),
|
|
113
|
+
);
|
|
114
|
+
for (const t of topicRows) {
|
|
115
|
+
if (!topicsByContact.has(t.contactId)) topicsByContact.set(t.contactId, {});
|
|
116
|
+
topicsByContact.get(t.contactId)![t.topic] = t.enabled;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const fetchedByUser = new Map<string, UserContact[]>();
|
|
121
|
+
for (const id of missingIds) {
|
|
122
|
+
fetchedByUser.set(id, []);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
for (const row of rows) {
|
|
126
|
+
const contacts = fetchedByUser.get(row.userId) ?? [];
|
|
127
|
+
const topics = topicsByContact.get(row.id);
|
|
128
|
+
contacts.push({
|
|
129
|
+
id: row.id,
|
|
130
|
+
userId: row.userId,
|
|
131
|
+
channel: row.channel as ContactChannel,
|
|
132
|
+
target: row.target,
|
|
133
|
+
preferences: topics ? { topics } : {},
|
|
134
|
+
active: row.enabled,
|
|
135
|
+
});
|
|
136
|
+
fetchedByUser.set(row.userId, contacts);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
for (const id of missingIds) {
|
|
140
|
+
const contacts = fetchedByUser.get(id) ?? [];
|
|
141
|
+
this.activeContactsCache.set(`${projectId}:${id}`, contacts);
|
|
142
|
+
if (contacts.length > 0) {
|
|
143
|
+
byUser.set(id, contacts);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return byUser;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async upsert(
|
|
151
|
+
projectId: string,
|
|
152
|
+
userId: string,
|
|
153
|
+
channel: ContactChannel,
|
|
154
|
+
target: string,
|
|
155
|
+
preferences: Preferences = {},
|
|
156
|
+
): Promise<void> {
|
|
157
|
+
const internalUserIdRows = await this.db
|
|
158
|
+
.select({ id: users.id })
|
|
159
|
+
.from(users)
|
|
160
|
+
.where(and(eq(users.externalId, userId), eq(users.projectId, projectId)));
|
|
161
|
+
const internalId = internalUserIdRows[0]?.id;
|
|
162
|
+
if (!internalId) return;
|
|
163
|
+
|
|
164
|
+
const inserted = await this.db
|
|
165
|
+
.insert(userContacts)
|
|
166
|
+
.values({
|
|
167
|
+
userId: internalId,
|
|
168
|
+
channel: channel as any,
|
|
169
|
+
target,
|
|
170
|
+
enabled: true,
|
|
171
|
+
})
|
|
172
|
+
.onConflictDoUpdate({
|
|
173
|
+
target: [userContacts.userId, userContacts.channel, userContacts.target],
|
|
174
|
+
// Re-adding a contact re-enables it; this is also how a device token
|
|
175
|
+
// deactivated by an invalid-token response comes back.
|
|
176
|
+
set: { enabled: true },
|
|
177
|
+
})
|
|
178
|
+
.returning({ id: userContacts.id });
|
|
179
|
+
|
|
180
|
+
const contactId = inserted[0]?.id;
|
|
181
|
+
if (!contactId) return;
|
|
182
|
+
|
|
183
|
+
// Contact-level topic preferences were previously accepted by the API and
|
|
184
|
+
// silently discarded.
|
|
185
|
+
const topics = Object.entries(preferences.topics ?? {});
|
|
186
|
+
if (topics.length > 0) {
|
|
187
|
+
await this.db
|
|
188
|
+
.insert(contactTopicPreferences)
|
|
189
|
+
.values(topics.map(([topic, enabled]) => ({ contactId, topic, enabled })))
|
|
190
|
+
.onConflictDoUpdate({
|
|
191
|
+
target: [contactTopicPreferences.contactId, contactTopicPreferences.topic],
|
|
192
|
+
set: { enabled: drizzleSql`excluded.enabled` },
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
this.activeContactsCache.delete(`${projectId}:${userId}`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async upsertMany(
|
|
199
|
+
projectId: string,
|
|
200
|
+
contactsList: Array<{
|
|
201
|
+
userId: string;
|
|
202
|
+
channel: ContactChannel;
|
|
203
|
+
target: string;
|
|
204
|
+
preferences?: Preferences;
|
|
205
|
+
}>,
|
|
206
|
+
): Promise<void> {
|
|
207
|
+
if (contactsList.length === 0) return;
|
|
208
|
+
|
|
209
|
+
const internalUserIdRows = await this.db
|
|
210
|
+
.select({ id: users.id, externalId: users.externalId })
|
|
211
|
+
.from(users)
|
|
212
|
+
.where(
|
|
213
|
+
and(
|
|
214
|
+
inArray(
|
|
215
|
+
users.externalId,
|
|
216
|
+
contactsList.map((c) => c.userId),
|
|
217
|
+
),
|
|
218
|
+
eq(users.projectId, projectId),
|
|
219
|
+
),
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
const idMap = new Map(internalUserIdRows.map((r) => [r.externalId, r.id]));
|
|
223
|
+
|
|
224
|
+
// The same address can arrive twice — as `email` and again in `contacts`
|
|
225
|
+
// — and ON CONFLICT DO UPDATE rejects a statement that touches one row
|
|
226
|
+
// twice. Keep one entry per address, merging their topic preferences.
|
|
227
|
+
const byAddress = new Map<string, (typeof contactsList)[number]>();
|
|
228
|
+
for (const c of contactsList) {
|
|
229
|
+
if (!idMap.has(c.userId)) continue;
|
|
230
|
+
const key = `${c.userId}\u0000${c.channel}\u0000${c.target}`;
|
|
231
|
+
const seen = byAddress.get(key);
|
|
232
|
+
byAddress.set(
|
|
233
|
+
key,
|
|
234
|
+
seen
|
|
235
|
+
? {
|
|
236
|
+
...c,
|
|
237
|
+
preferences: {
|
|
238
|
+
...seen.preferences,
|
|
239
|
+
...c.preferences,
|
|
240
|
+
topics: { ...seen.preferences?.topics, ...c.preferences?.topics },
|
|
241
|
+
},
|
|
242
|
+
}
|
|
243
|
+
: c,
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
const validContacts = [...byAddress.values()];
|
|
247
|
+
if (validContacts.length === 0) return;
|
|
248
|
+
|
|
249
|
+
const inserted = await this.db
|
|
250
|
+
.insert(userContacts)
|
|
251
|
+
.values(
|
|
252
|
+
validContacts.map((c) => ({
|
|
253
|
+
userId: idMap.get(c.userId)!,
|
|
254
|
+
channel: c.channel as any,
|
|
255
|
+
target: c.target,
|
|
256
|
+
enabled: true,
|
|
257
|
+
})),
|
|
258
|
+
)
|
|
259
|
+
.onConflictDoUpdate({
|
|
260
|
+
target: [userContacts.userId, userContacts.channel, userContacts.target],
|
|
261
|
+
set: { enabled: true },
|
|
262
|
+
})
|
|
263
|
+
.returning({
|
|
264
|
+
id: userContacts.id,
|
|
265
|
+
userId: userContacts.userId,
|
|
266
|
+
channel: userContacts.channel,
|
|
267
|
+
target: userContacts.target,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
const contactIdMap = new Map();
|
|
271
|
+
for (const row of inserted) {
|
|
272
|
+
contactIdMap.set(`${row.userId}:${row.channel}:${row.target}`, row.id);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const topicInserts: any[] = [];
|
|
276
|
+
for (const c of validContacts) {
|
|
277
|
+
const internalId = idMap.get(c.userId)!;
|
|
278
|
+
const contactId = contactIdMap.get(`${internalId}:${c.channel}:${c.target}`);
|
|
279
|
+
if (!contactId || !c.preferences?.topics) continue;
|
|
280
|
+
|
|
281
|
+
for (const [topic, enabled] of Object.entries(c.preferences.topics)) {
|
|
282
|
+
topicInserts.push({ contactId, topic, enabled });
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (topicInserts.length > 0) {
|
|
287
|
+
await this.db
|
|
288
|
+
.insert(contactTopicPreferences)
|
|
289
|
+
.values(topicInserts)
|
|
290
|
+
.onConflictDoUpdate({
|
|
291
|
+
target: [contactTopicPreferences.contactId, contactTopicPreferences.topic],
|
|
292
|
+
set: { enabled: drizzleSql`excluded.enabled` },
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
for (const c of validContacts) {
|
|
296
|
+
this.activeContactsCache.delete(`${projectId}:${c.userId}`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Mark a contact unusable without destroying it — used when a provider
|
|
302
|
+
* reports an invalid push token. Deleting the row would lose the user's
|
|
303
|
+
* device permanently on what is often a transient provider response.
|
|
304
|
+
*/
|
|
305
|
+
async deactivate(
|
|
306
|
+
projectId: string,
|
|
307
|
+
userId: string,
|
|
308
|
+
channel: ContactChannel,
|
|
309
|
+
target: string,
|
|
310
|
+
): Promise<boolean> {
|
|
311
|
+
this.activeContactsCache.delete(`${projectId}:${userId}`);
|
|
312
|
+
const internalUserIdRows = await this.db
|
|
313
|
+
.select({ id: users.id })
|
|
314
|
+
.from(users)
|
|
315
|
+
.where(and(eq(users.externalId, userId), eq(users.projectId, projectId)));
|
|
316
|
+
const internalId = internalUserIdRows[0]?.id;
|
|
317
|
+
if (!internalId) return false;
|
|
318
|
+
|
|
319
|
+
const result = await this.db
|
|
320
|
+
.update(userContacts)
|
|
321
|
+
.set({ enabled: false })
|
|
322
|
+
.where(
|
|
323
|
+
and(
|
|
324
|
+
eq(userContacts.userId, internalId),
|
|
325
|
+
eq(userContacts.channel, channel as any),
|
|
326
|
+
eq(userContacts.target, target),
|
|
327
|
+
),
|
|
328
|
+
)
|
|
329
|
+
.returning();
|
|
330
|
+
return result.length > 0;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async delete(
|
|
334
|
+
projectId: string,
|
|
335
|
+
userId: string,
|
|
336
|
+
channel: ContactChannel,
|
|
337
|
+
target: string,
|
|
338
|
+
): Promise<boolean> {
|
|
339
|
+
this.activeContactsCache.delete(`${projectId}:${userId}`);
|
|
340
|
+
const internalUserIdRows = await this.db
|
|
341
|
+
.select({ id: users.id })
|
|
342
|
+
.from(users)
|
|
343
|
+
.where(and(eq(users.externalId, userId), eq(users.projectId, projectId)));
|
|
344
|
+
const internalId = internalUserIdRows[0]?.id;
|
|
345
|
+
if (!internalId) return false;
|
|
346
|
+
|
|
347
|
+
const result = await this.db
|
|
348
|
+
.delete(userContacts)
|
|
349
|
+
.where(
|
|
350
|
+
and(
|
|
351
|
+
eq(userContacts.userId, internalId),
|
|
352
|
+
eq(userContacts.channel, channel as any),
|
|
353
|
+
eq(userContacts.target, target),
|
|
354
|
+
),
|
|
355
|
+
)
|
|
356
|
+
.returning();
|
|
357
|
+
return result.length > 0;
|
|
358
|
+
}
|
|
359
|
+
}
|