@kelpie/server 0.7.0 → 0.8.0
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/lib/consumerEmailDomains.d.ts +2 -0
- package/dist/lib/consumerEmailDomains.d.ts.map +1 -0
- package/dist/lib/consumerEmailDomains.js +62 -0
- package/dist/lib/consumerEmailDomains.js.map +1 -0
- package/dist/lib/email.d.ts +7 -0
- package/dist/lib/email.d.ts.map +1 -1
- package/dist/lib/email.js.map +1 -1
- package/dist/lib/emailContent.d.ts +33 -0
- package/dist/lib/emailContent.d.ts.map +1 -0
- package/dist/lib/emailContent.js +54 -0
- package/dist/lib/emailContent.js.map +1 -0
- package/dist/lib/emailDomainAutoLink.d.ts +27 -0
- package/dist/lib/emailDomainAutoLink.d.ts.map +1 -0
- package/dist/lib/emailDomainAutoLink.js +119 -0
- package/dist/lib/emailDomainAutoLink.js.map +1 -0
- package/dist/lib/ids.d.ts +2 -0
- package/dist/lib/ids.d.ts.map +1 -1
- package/dist/lib/ids.js +2 -0
- package/dist/lib/ids.js.map +1 -1
- package/dist/modules/attachedRecords.d.ts.map +1 -1
- package/dist/modules/attachedRecords.js +4 -2
- package/dist/modules/attachedRecords.js.map +1 -1
- package/dist/modules/auth/preferences.d.ts +11 -1
- package/dist/modules/auth/preferences.d.ts.map +1 -1
- package/dist/modules/auth/preferences.js +11 -0
- package/dist/modules/auth/preferences.js.map +1 -1
- package/dist/modules/auth/routes.d.ts.map +1 -1
- package/dist/modules/auth/routes.js +18 -0
- package/dist/modules/auth/routes.js.map +1 -1
- package/dist/modules/auth/schema.d.ts +20 -0
- package/dist/modules/auth/schema.d.ts.map +1 -1
- package/dist/modules/auth/schema.js +11 -1
- package/dist/modules/auth/schema.js.map +1 -1
- package/dist/modules/auth/service.d.ts.map +1 -1
- package/dist/modules/auth/service.js +27 -3
- package/dist/modules/auth/service.js.map +1 -1
- package/dist/modules/companies/repository.d.ts +2 -0
- package/dist/modules/companies/repository.d.ts.map +1 -1
- package/dist/modules/companies/repository.js +1 -0
- package/dist/modules/companies/repository.js.map +1 -1
- package/dist/modules/companies/routes.d.ts +2 -0
- package/dist/modules/companies/routes.d.ts.map +1 -1
- package/dist/modules/companies/routes.js +11 -0
- package/dist/modules/companies/routes.js.map +1 -1
- package/dist/modules/companies/schema.d.ts +17 -0
- package/dist/modules/companies/schema.d.ts.map +1 -1
- package/dist/modules/companies/schema.js +5 -1
- package/dist/modules/companies/schema.js.map +1 -1
- package/dist/modules/companies/service.d.ts +1 -0
- package/dist/modules/companies/service.d.ts.map +1 -1
- package/dist/modules/companies/service.js +22 -0
- package/dist/modules/companies/service.js.map +1 -1
- package/dist/modules/core.d.ts.map +1 -1
- package/dist/modules/core.js +11 -0
- package/dist/modules/core.js.map +1 -1
- package/dist/modules/email-domain-linker/index.d.ts +12 -0
- package/dist/modules/email-domain-linker/index.d.ts.map +1 -0
- package/dist/modules/email-domain-linker/index.js +34 -0
- package/dist/modules/email-domain-linker/index.js.map +1 -0
- package/dist/modules/email-domain-linker/routes.d.ts +14 -0
- package/dist/modules/email-domain-linker/routes.d.ts.map +1 -0
- package/dist/modules/email-domain-linker/routes.js +15 -0
- package/dist/modules/email-domain-linker/routes.js.map +1 -0
- package/dist/modules/email-domain-linker/service.d.ts +37 -0
- package/dist/modules/email-domain-linker/service.d.ts.map +1 -0
- package/dist/modules/email-domain-linker/service.js +63 -0
- package/dist/modules/email-domain-linker/service.js.map +1 -0
- package/dist/modules/eventCatalogs.d.ts +1 -0
- package/dist/modules/eventCatalogs.d.ts.map +1 -1
- package/dist/modules/eventCatalogs.js +1 -0
- package/dist/modules/eventCatalogs.js.map +1 -1
- package/dist/modules/lists/events.d.ts +42 -0
- package/dist/modules/lists/events.d.ts.map +1 -0
- package/dist/modules/lists/events.js +16 -0
- package/dist/modules/lists/events.js.map +1 -0
- package/dist/modules/lists/index.d.ts +10 -0
- package/dist/modules/lists/index.d.ts.map +1 -0
- package/dist/modules/lists/index.js +35 -0
- package/dist/modules/lists/index.js.map +1 -0
- package/dist/modules/lists/repository.d.ts +69 -0
- package/dist/modules/lists/repository.d.ts.map +1 -0
- package/dist/modules/lists/repository.js +165 -0
- package/dist/modules/lists/repository.js.map +1 -0
- package/dist/modules/lists/routes.d.ts +51 -0
- package/dist/modules/lists/routes.d.ts.map +1 -0
- package/dist/modules/lists/routes.js +160 -0
- package/dist/modules/lists/routes.js.map +1 -0
- package/dist/modules/lists/schema.d.ts +264 -0
- package/dist/modules/lists/schema.d.ts.map +1 -0
- package/dist/modules/lists/schema.js +73 -0
- package/dist/modules/lists/schema.js.map +1 -0
- package/dist/modules/lists/service.d.ts +70 -0
- package/dist/modules/lists/service.d.ts.map +1 -0
- package/dist/modules/lists/service.js +214 -0
- package/dist/modules/lists/service.js.map +1 -0
- package/dist/modules/lists/tools.d.ts +4 -0
- package/dist/modules/lists/tools.d.ts.map +1 -0
- package/dist/modules/lists/tools.js +86 -0
- package/dist/modules/lists/tools.js.map +1 -0
- package/dist/modules/people/repository.d.ts +11 -0
- package/dist/modules/people/repository.d.ts.map +1 -1
- package/dist/modules/people/repository.js +16 -0
- package/dist/modules/people/repository.js.map +1 -1
- package/dist/modules/people/service.d.ts.map +1 -1
- package/dist/modules/people/service.js +18 -0
- package/dist/modules/people/service.js.map +1 -1
- package/dist/modules/positions/routes.d.ts.map +1 -1
- package/dist/modules/positions/routes.js +4 -2
- package/dist/modules/positions/routes.js.map +1 -1
- package/dist/modules/sample-data/fixture.d.ts +139 -0
- package/dist/modules/sample-data/fixture.d.ts.map +1 -0
- package/dist/modules/sample-data/fixture.js +461 -0
- package/dist/modules/sample-data/fixture.js.map +1 -0
- package/dist/modules/sample-data/index.d.ts +11 -0
- package/dist/modules/sample-data/index.d.ts.map +1 -0
- package/dist/modules/sample-data/index.js +43 -0
- package/dist/modules/sample-data/index.js.map +1 -0
- package/dist/modules/sample-data/routes.d.ts +15 -0
- package/dist/modules/sample-data/routes.d.ts.map +1 -0
- package/dist/modules/sample-data/routes.js +24 -0
- package/dist/modules/sample-data/routes.js.map +1 -0
- package/dist/modules/sample-data/service.d.ts +38 -0
- package/dist/modules/sample-data/service.d.ts.map +1 -0
- package/dist/modules/sample-data/service.js +412 -0
- package/dist/modules/sample-data/service.js.map +1 -0
- package/dist/modules/sample-data/tools.d.ts +4 -0
- package/dist/modules/sample-data/tools.d.ts.map +1 -0
- package/dist/modules/sample-data/tools.js +22 -0
- package/dist/modules/sample-data/tools.js.map +1 -0
- package/dist/modules/smtp-email/index.d.ts +1 -0
- package/dist/modules/smtp-email/index.d.ts.map +1 -1
- package/dist/modules/smtp-email/index.js +1 -0
- package/dist/modules/smtp-email/index.js.map +1 -1
- package/dist/modules/workspace/service.d.ts.map +1 -1
- package/dist/modules/workspace/service.js +10 -1
- package/dist/modules/workspace/service.js.map +1 -1
- package/dist/schema/index.d.ts +1 -0
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/index.js +1 -0
- package/dist/schema/index.js.map +1 -1
- package/migrations/0021_certain_maggott.sql +30 -0
- package/migrations/0022_stormy_manta.sql +1 -0
- package/migrations/0023_funny_maddog.sql +1 -0
- package/migrations/meta/0021_snapshot.json +5389 -0
- package/migrations/meta/0022_snapshot.json +5396 -0
- package/migrations/meta/0023_snapshot.json +5403 -0
- package/migrations/meta/_journal.json +21 -0
- package/package.json +3 -2
- package/src/lib/consumerEmailDomains.ts +62 -0
- package/src/lib/email.ts +7 -0
- package/src/lib/emailContent.ts +91 -0
- package/src/lib/emailDomainAutoLink.ts +165 -0
- package/src/lib/ids.ts +2 -0
- package/src/modules/attachedRecords.ts +9 -2
- package/src/modules/auth/preferences.ts +13 -1
- package/src/modules/auth/routes.ts +22 -0
- package/src/modules/auth/schema.ts +12 -2
- package/src/modules/auth/service.ts +37 -3
- package/src/modules/companies/repository.ts +3 -0
- package/src/modules/companies/routes.ts +13 -0
- package/src/modules/companies/schema.ts +5 -1
- package/src/modules/companies/service.ts +25 -0
- package/src/modules/core.ts +11 -0
- package/src/modules/email-domain-linker/index.ts +40 -0
- package/src/modules/email-domain-linker/routes.ts +40 -0
- package/src/modules/email-domain-linker/service.ts +122 -0
- package/src/modules/eventCatalogs.ts +1 -0
- package/src/modules/lists/events.ts +41 -0
- package/src/modules/lists/index.ts +41 -0
- package/src/modules/lists/repository.ts +313 -0
- package/src/modules/lists/routes.ts +247 -0
- package/src/modules/lists/schema.ts +85 -0
- package/src/modules/lists/service.ts +404 -0
- package/src/modules/lists/tools.ts +128 -0
- package/src/modules/people/repository.ts +26 -0
- package/src/modules/people/service.ts +20 -0
- package/src/modules/positions/routes.ts +4 -2
- package/src/modules/sample-data/fixture.ts +627 -0
- package/src/modules/sample-data/index.ts +50 -0
- package/src/modules/sample-data/routes.ts +49 -0
- package/src/modules/sample-data/service.ts +590 -0
- package/src/modules/sample-data/tools.ts +32 -0
- package/src/modules/smtp-email/index.ts +8 -1
- package/src/modules/workspace/service.ts +13 -1
- package/src/schema/index.ts +1 -0
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { and, eq, ilike, inArray, sql } from 'drizzle-orm'
|
|
2
|
+
import type { SQL } from 'drizzle-orm'
|
|
3
|
+
|
|
4
|
+
import { keysetCondition, orderByWindow, textSort, timestampSort } from '../../lib/pagination.ts'
|
|
5
|
+
import type { ListWindow, SortableFields } from '../../lib/pagination.ts'
|
|
6
|
+
import { containsPattern } from '../../lib/search.ts'
|
|
7
|
+
import type { Queryable } from '../../runtime/transaction.ts'
|
|
8
|
+
import type { RecordTargetType } from '../recordTargets.ts'
|
|
9
|
+
import { listMembers, lists } from './schema.ts'
|
|
10
|
+
|
|
11
|
+
export type ListRecord = typeof lists.$inferSelect
|
|
12
|
+
export type ListColumns = typeof lists.$inferInsert
|
|
13
|
+
|
|
14
|
+
export type ListMemberRecord = typeof listMembers.$inferSelect
|
|
15
|
+
export type ListMemberColumns = typeof listMembers.$inferInsert
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The stored row with the rollup a UI index page needs. `member_count` is
|
|
19
|
+
* computed at read time rather than maintained on write so an unrelated bug
|
|
20
|
+
* cannot leave the count out of step with the actual rows.
|
|
21
|
+
*/
|
|
22
|
+
export type ListWithCount = ListRecord & { readonly memberCount: number }
|
|
23
|
+
|
|
24
|
+
export const LIST_SORTS: SortableFields<ListWithCount> = {
|
|
25
|
+
name: textSort(lists.name, (list) => list.name),
|
|
26
|
+
created_at: timestampSort(lists.createdAt, (list) => list.createdAt),
|
|
27
|
+
updated_at: timestampSort(lists.updatedAt, (list) => list.updatedAt),
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const DEFAULT_LIST_SORT = '-updated_at'
|
|
31
|
+
|
|
32
|
+
export interface ListFilters {
|
|
33
|
+
readonly term?: string | undefined
|
|
34
|
+
readonly targetType?: RecordTargetType | undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function conditionsFor(workspaceId: string, filters: ListFilters): (SQL | undefined)[] {
|
|
38
|
+
const term = filters.term
|
|
39
|
+
const targetType = filters.targetType
|
|
40
|
+
|
|
41
|
+
return [
|
|
42
|
+
eq(lists.workspaceId, workspaceId),
|
|
43
|
+
term === undefined ? undefined : ilike(lists.name, containsPattern(term)),
|
|
44
|
+
targetType === undefined ? undefined : eq(lists.targetType, targetType),
|
|
45
|
+
]
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @returns Up to `window.fetchLimit` rows: one more than the page, so the
|
|
50
|
+
* caller can tell there is a next one. Each row carries the count of members
|
|
51
|
+
* attached to it.
|
|
52
|
+
*/
|
|
53
|
+
export async function listLists(
|
|
54
|
+
db: Queryable,
|
|
55
|
+
workspaceId: string,
|
|
56
|
+
filters: ListFilters,
|
|
57
|
+
window: ListWindow<ListWithCount>,
|
|
58
|
+
): Promise<ListWithCount[]> {
|
|
59
|
+
const rows = await db
|
|
60
|
+
.select()
|
|
61
|
+
.from(lists)
|
|
62
|
+
.where(and(...conditionsFor(workspaceId, filters), keysetCondition(window, lists.id)))
|
|
63
|
+
.orderBy(...orderByWindow(window, lists.id))
|
|
64
|
+
.limit(window.fetchLimit)
|
|
65
|
+
|
|
66
|
+
const counts = await countMembersFor(db, rows.map((row) => row.id))
|
|
67
|
+
|
|
68
|
+
return rows.map((row) => ({ ...row, memberCount: counts.get(row.id) ?? 0 }))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function countMembersFor(
|
|
72
|
+
db: Queryable,
|
|
73
|
+
listIds: readonly string[],
|
|
74
|
+
): Promise<ReadonlyMap<string, number>> {
|
|
75
|
+
if (listIds.length === 0) {
|
|
76
|
+
return new Map()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const rows = await db
|
|
80
|
+
.select({ listId: listMembers.listId, count: sql<number>`count(*)::int` })
|
|
81
|
+
.from(listMembers)
|
|
82
|
+
.where(inArray(listMembers.listId, [...listIds]))
|
|
83
|
+
.groupBy(listMembers.listId)
|
|
84
|
+
|
|
85
|
+
return new Map(rows.map((row) => [row.listId, Number(row.count)]))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function findList(
|
|
89
|
+
db: Queryable,
|
|
90
|
+
workspaceId: string,
|
|
91
|
+
id: string,
|
|
92
|
+
): Promise<ListWithCount | undefined> {
|
|
93
|
+
const [found] = await db
|
|
94
|
+
.select()
|
|
95
|
+
.from(lists)
|
|
96
|
+
.where(and(eq(lists.workspaceId, workspaceId), eq(lists.id, id)))
|
|
97
|
+
.limit(1)
|
|
98
|
+
|
|
99
|
+
if (found === undefined) {
|
|
100
|
+
return undefined
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return { ...found, memberCount: await countMembers(db, id) }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function countMembers(db: Queryable, listId: string): Promise<number> {
|
|
107
|
+
const [row] = await db
|
|
108
|
+
.select({ count: sql<number>`count(*)::int` })
|
|
109
|
+
.from(listMembers)
|
|
110
|
+
.where(eq(listMembers.listId, listId))
|
|
111
|
+
|
|
112
|
+
return Number(row?.count ?? 0)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export async function insertList(db: Queryable, values: ListColumns): Promise<ListRecord> {
|
|
116
|
+
const [created] = await db.insert(lists).values(values).returning()
|
|
117
|
+
|
|
118
|
+
if (created === undefined) {
|
|
119
|
+
throw new Error(`Inserting list ${values.id} returned no row`)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return created
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export async function updateList(
|
|
126
|
+
db: Queryable,
|
|
127
|
+
workspaceId: string,
|
|
128
|
+
id: string,
|
|
129
|
+
changes: Partial<ListColumns>,
|
|
130
|
+
): Promise<ListRecord | undefined> {
|
|
131
|
+
const [updated] = await db
|
|
132
|
+
.update(lists)
|
|
133
|
+
.set(changes)
|
|
134
|
+
.where(and(eq(lists.workspaceId, workspaceId), eq(lists.id, id)))
|
|
135
|
+
.returning()
|
|
136
|
+
|
|
137
|
+
return updated
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export async function deleteList(db: Queryable, workspaceId: string, id: string): Promise<number> {
|
|
141
|
+
const deleted = await db
|
|
142
|
+
.delete(lists)
|
|
143
|
+
.where(and(eq(lists.workspaceId, workspaceId), eq(lists.id, id)))
|
|
144
|
+
.returning({ id: lists.id })
|
|
145
|
+
|
|
146
|
+
return deleted.length
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Members: no cursor pagination yet. A list's members render on one page today. */
|
|
150
|
+
|
|
151
|
+
export const LIST_MEMBER_SORTS: SortableFields<ListMemberRecord> = {
|
|
152
|
+
added_at: timestampSort(listMembers.addedAt, (row) => row.addedAt),
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export const DEFAULT_LIST_MEMBER_SORT = '-added_at'
|
|
156
|
+
|
|
157
|
+
export interface ListMemberFilters {
|
|
158
|
+
readonly listId: string
|
|
159
|
+
readonly targetType?: RecordTargetType | undefined
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function memberConditionsFor(
|
|
163
|
+
workspaceId: string,
|
|
164
|
+
filters: ListMemberFilters,
|
|
165
|
+
): (SQL | undefined)[] {
|
|
166
|
+
const targetType = filters.targetType
|
|
167
|
+
|
|
168
|
+
return [
|
|
169
|
+
eq(listMembers.workspaceId, workspaceId),
|
|
170
|
+
eq(listMembers.listId, filters.listId),
|
|
171
|
+
targetType === undefined ? undefined : eq(listMembers.targetType, targetType),
|
|
172
|
+
]
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function listListMembers(
|
|
176
|
+
db: Queryable,
|
|
177
|
+
workspaceId: string,
|
|
178
|
+
filters: ListMemberFilters,
|
|
179
|
+
window: ListWindow<ListMemberRecord>,
|
|
180
|
+
): Promise<ListMemberRecord[]> {
|
|
181
|
+
return db
|
|
182
|
+
.select()
|
|
183
|
+
.from(listMembers)
|
|
184
|
+
.where(and(...memberConditionsFor(workspaceId, filters), keysetCondition(window, listMembers.id)))
|
|
185
|
+
.orderBy(...orderByWindow(window, listMembers.id))
|
|
186
|
+
.limit(window.fetchLimit)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export async function findListMember(
|
|
190
|
+
db: Queryable,
|
|
191
|
+
workspaceId: string,
|
|
192
|
+
listId: string,
|
|
193
|
+
id: string,
|
|
194
|
+
): Promise<ListMemberRecord | undefined> {
|
|
195
|
+
const [found] = await db
|
|
196
|
+
.select()
|
|
197
|
+
.from(listMembers)
|
|
198
|
+
.where(
|
|
199
|
+
and(
|
|
200
|
+
eq(listMembers.workspaceId, workspaceId),
|
|
201
|
+
eq(listMembers.listId, listId),
|
|
202
|
+
eq(listMembers.id, id),
|
|
203
|
+
),
|
|
204
|
+
)
|
|
205
|
+
.limit(1)
|
|
206
|
+
|
|
207
|
+
return found
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export async function insertListMember(
|
|
211
|
+
db: Queryable,
|
|
212
|
+
values: ListMemberColumns,
|
|
213
|
+
): Promise<ListMemberRecord> {
|
|
214
|
+
const [created] = await db.insert(listMembers).values(values).returning()
|
|
215
|
+
|
|
216
|
+
if (created === undefined) {
|
|
217
|
+
throw new Error(`Inserting list_member ${values.id} returned no row`)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return created
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export async function deleteListMember(
|
|
224
|
+
db: Queryable,
|
|
225
|
+
workspaceId: string,
|
|
226
|
+
listId: string,
|
|
227
|
+
id: string,
|
|
228
|
+
): Promise<number> {
|
|
229
|
+
const deleted = await db
|
|
230
|
+
.delete(listMembers)
|
|
231
|
+
.where(
|
|
232
|
+
and(
|
|
233
|
+
eq(listMembers.workspaceId, workspaceId),
|
|
234
|
+
eq(listMembers.listId, listId),
|
|
235
|
+
eq(listMembers.id, id),
|
|
236
|
+
),
|
|
237
|
+
)
|
|
238
|
+
.returning({ id: listMembers.id })
|
|
239
|
+
|
|
240
|
+
return deleted.length
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** A membership row enriched with the list it points at. */
|
|
244
|
+
export interface MembershipWithList {
|
|
245
|
+
readonly id: string
|
|
246
|
+
readonly listId: string
|
|
247
|
+
readonly listName: string
|
|
248
|
+
readonly listTargetType: string
|
|
249
|
+
readonly targetType: string
|
|
250
|
+
readonly targetId: string
|
|
251
|
+
readonly addedAt: Date
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* "Which lists is this record on?"
|
|
256
|
+
*
|
|
257
|
+
* Ordered by list name so a record's Lists tab reads alphabetically and the
|
|
258
|
+
* order does not shift as memberships are added.
|
|
259
|
+
*/
|
|
260
|
+
export async function listMembershipsForTarget(
|
|
261
|
+
db: Queryable,
|
|
262
|
+
workspaceId: string,
|
|
263
|
+
targetType: string,
|
|
264
|
+
targetId: string,
|
|
265
|
+
): Promise<MembershipWithList[]> {
|
|
266
|
+
return db
|
|
267
|
+
.select({
|
|
268
|
+
id: listMembers.id,
|
|
269
|
+
listId: listMembers.listId,
|
|
270
|
+
listName: lists.name,
|
|
271
|
+
listTargetType: lists.targetType,
|
|
272
|
+
targetType: listMembers.targetType,
|
|
273
|
+
targetId: listMembers.targetId,
|
|
274
|
+
addedAt: listMembers.addedAt,
|
|
275
|
+
})
|
|
276
|
+
.from(listMembers)
|
|
277
|
+
.innerJoin(lists, eq(listMembers.listId, lists.id))
|
|
278
|
+
.where(
|
|
279
|
+
and(
|
|
280
|
+
eq(listMembers.workspaceId, workspaceId),
|
|
281
|
+
eq(listMembers.targetType, targetType),
|
|
282
|
+
eq(listMembers.targetId, targetId),
|
|
283
|
+
),
|
|
284
|
+
)
|
|
285
|
+
.orderBy(lists.name)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Removes every list membership for one target.
|
|
290
|
+
*
|
|
291
|
+
* The target is polymorphic and carries no foreign key of its own, so nothing
|
|
292
|
+
* in the database deletes these when the target goes. The service that deletes
|
|
293
|
+
* the target calls this inside the same transaction.
|
|
294
|
+
*/
|
|
295
|
+
export async function deleteMembershipsForTarget(
|
|
296
|
+
db: Queryable,
|
|
297
|
+
workspaceId: string,
|
|
298
|
+
targetType: string,
|
|
299
|
+
targetId: string,
|
|
300
|
+
): Promise<number> {
|
|
301
|
+
const deleted = await db
|
|
302
|
+
.delete(listMembers)
|
|
303
|
+
.where(
|
|
304
|
+
and(
|
|
305
|
+
eq(listMembers.workspaceId, workspaceId),
|
|
306
|
+
eq(listMembers.targetType, targetType),
|
|
307
|
+
eq(listMembers.targetId, targetId),
|
|
308
|
+
),
|
|
309
|
+
)
|
|
310
|
+
.returning({ id: listMembers.id })
|
|
311
|
+
|
|
312
|
+
return deleted.length
|
|
313
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import type { Context, Hono } from 'hono'
|
|
2
|
+
import { z } from 'zod'
|
|
3
|
+
|
|
4
|
+
import { AppError } from '../../lib/errors.ts'
|
|
5
|
+
import { pageBody, readJsonBody, readListParameters } from '../../lib/http.ts'
|
|
6
|
+
import type { Actor } from '../auth/actor.ts'
|
|
7
|
+
import { resolveActorFrom } from '../auth/credentials.ts'
|
|
8
|
+
import type { CredentialDependencies } from '../auth/credentials.ts'
|
|
9
|
+
import { isRecordTargetType } from '../recordTargets.ts'
|
|
10
|
+
import type { RecordTargetType } from '../recordTargets.ts'
|
|
11
|
+
import { RECORD_TARGET_TYPES } from './schema.ts'
|
|
12
|
+
import type {
|
|
13
|
+
AddListMemberInput,
|
|
14
|
+
CreateListInput,
|
|
15
|
+
ListMemberView,
|
|
16
|
+
ListMembershipView,
|
|
17
|
+
ListView,
|
|
18
|
+
ListsService,
|
|
19
|
+
UpdateListInput,
|
|
20
|
+
} from './service.ts'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Wire shapes for `/v1/lists`.
|
|
24
|
+
*
|
|
25
|
+
* `target_type` is required on create and absent from update: a list's type is
|
|
26
|
+
* fixed for its lifetime. Bodies are strict, so a caller sending `target_type`
|
|
27
|
+
* on PATCH hears about it as a 422 rather than silently ignored.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
export const createBody = z.strictObject({
|
|
31
|
+
name: z.string().min(1),
|
|
32
|
+
target_type: z.enum(RECORD_TARGET_TYPES),
|
|
33
|
+
description: z.string().nullable().default(null),
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
export const updateBody = z
|
|
37
|
+
.strictObject({
|
|
38
|
+
name: z.string().min(1),
|
|
39
|
+
description: z.string().nullable(),
|
|
40
|
+
})
|
|
41
|
+
.partial()
|
|
42
|
+
|
|
43
|
+
export const addMemberBody = z.strictObject({
|
|
44
|
+
target_type: z.enum(RECORD_TARGET_TYPES),
|
|
45
|
+
target_id: z.string().min(1),
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
export interface ListsRoutesDependencies extends CredentialDependencies {
|
|
49
|
+
readonly service: ListsService
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function toCreateInput(body: z.infer<typeof createBody>): CreateListInput {
|
|
53
|
+
return {
|
|
54
|
+
name: body.name,
|
|
55
|
+
targetType: body.target_type,
|
|
56
|
+
description: body.description,
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function toUpdateInput(body: z.infer<typeof updateBody>): UpdateListInput {
|
|
61
|
+
return {
|
|
62
|
+
...(body.name === undefined ? {} : { name: body.name }),
|
|
63
|
+
...(body.description === undefined ? {} : { description: body.description }),
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function toAddMemberInput(body: z.infer<typeof addMemberBody>): AddListMemberInput {
|
|
68
|
+
return {
|
|
69
|
+
targetType: body.target_type,
|
|
70
|
+
targetId: body.target_id,
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function listResponse(list: ListView): Record<string, unknown> {
|
|
75
|
+
return {
|
|
76
|
+
id: list.id,
|
|
77
|
+
name: list.name,
|
|
78
|
+
description: list.description,
|
|
79
|
+
target_type: list.targetType,
|
|
80
|
+
member_count: list.memberCount,
|
|
81
|
+
created_at: list.createdAt.toISOString(),
|
|
82
|
+
updated_at: list.updatedAt.toISOString(),
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function listMembershipResponse(
|
|
87
|
+
membership: ListMembershipView,
|
|
88
|
+
): Record<string, unknown> {
|
|
89
|
+
return {
|
|
90
|
+
id: membership.id,
|
|
91
|
+
list_id: membership.listId,
|
|
92
|
+
list_name: membership.listName,
|
|
93
|
+
list_target_type: membership.listTargetType,
|
|
94
|
+
target_type: membership.targetType,
|
|
95
|
+
target_id: membership.targetId,
|
|
96
|
+
added_at: membership.addedAt.toISOString(),
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function listMemberResponse(member: ListMemberView): Record<string, unknown> {
|
|
101
|
+
return {
|
|
102
|
+
id: member.id,
|
|
103
|
+
list_id: member.listId,
|
|
104
|
+
target_type: member.targetType,
|
|
105
|
+
target_id: member.targetId,
|
|
106
|
+
target_name: member.targetName,
|
|
107
|
+
added_at: member.addedAt.toISOString(),
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function readTargetTypeFilter(context: Context): RecordTargetType | undefined {
|
|
112
|
+
const raw = context.req.query('target_type')
|
|
113
|
+
|
|
114
|
+
if (raw === undefined) {
|
|
115
|
+
return undefined
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!isRecordTargetType(raw)) {
|
|
119
|
+
throw AppError.validationFailed('That is not a record type a list holds', [
|
|
120
|
+
{ field: 'target_type', message: `Unknown target type "${raw}"` },
|
|
121
|
+
])
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return raw
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function readMembershipTarget(context: Context): {
|
|
128
|
+
targetType: RecordTargetType
|
|
129
|
+
targetId: string
|
|
130
|
+
} {
|
|
131
|
+
const rawType = context.req.query('target_type')
|
|
132
|
+
const targetId = context.req.query('target_id')
|
|
133
|
+
|
|
134
|
+
if (rawType === undefined || targetId === undefined || targetId.length === 0) {
|
|
135
|
+
throw AppError.validationFailed(
|
|
136
|
+
'A memberships list always names the record it is for',
|
|
137
|
+
[
|
|
138
|
+
{ field: 'target_type', message: 'Required' },
|
|
139
|
+
{ field: 'target_id', message: 'Required' },
|
|
140
|
+
],
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (!isRecordTargetType(rawType)) {
|
|
145
|
+
throw AppError.validationFailed('That is not a record type a list holds', [
|
|
146
|
+
{ field: 'target_type', message: `Unknown target type "${rawType}"` },
|
|
147
|
+
])
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return { targetType: rawType, targetId }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function mountListsRoutes(router: Hono, dependencies: ListsRoutesDependencies): void {
|
|
154
|
+
const requireActor = (context: Context): Promise<Actor> =>
|
|
155
|
+
resolveActorFrom(dependencies, context)
|
|
156
|
+
|
|
157
|
+
router.get('/list-memberships', async (context) => {
|
|
158
|
+
const { targetType, targetId } = readMembershipTarget(context)
|
|
159
|
+
const memberships = await dependencies.service.membershipsFor(
|
|
160
|
+
await requireActor(context),
|
|
161
|
+
targetType,
|
|
162
|
+
targetId,
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
// Not paged. A record has a small number of memberships in practice; the
|
|
166
|
+
// `next_cursor: null` is here so the standard list envelope reader accepts
|
|
167
|
+
// this response without a special case.
|
|
168
|
+
return context.json({
|
|
169
|
+
data: memberships.map(listMembershipResponse),
|
|
170
|
+
next_cursor: null,
|
|
171
|
+
})
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
router.get('/lists', async (context) => {
|
|
175
|
+
const page = await dependencies.service.list(
|
|
176
|
+
await requireActor(context),
|
|
177
|
+
{
|
|
178
|
+
term: context.req.query('q'),
|
|
179
|
+
targetType: readTargetTypeFilter(context),
|
|
180
|
+
},
|
|
181
|
+
readListParameters(context),
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
return context.json(pageBody(page, listResponse))
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
router.post('/lists', async (context) => {
|
|
188
|
+
const body = await readJsonBody(context, createBody)
|
|
189
|
+
const list = await dependencies.service.create(await requireActor(context), toCreateInput(body))
|
|
190
|
+
|
|
191
|
+
return context.json(listResponse(list), 201)
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
router.get('/lists/:id', async (context) => {
|
|
195
|
+
const list = await dependencies.service.get(await requireActor(context), context.req.param('id'))
|
|
196
|
+
|
|
197
|
+
return context.json(listResponse(list))
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
router.patch('/lists/:id', async (context) => {
|
|
201
|
+
const body = await readJsonBody(context, updateBody)
|
|
202
|
+
const list = await dependencies.service.update(
|
|
203
|
+
await requireActor(context),
|
|
204
|
+
context.req.param('id'),
|
|
205
|
+
toUpdateInput(body),
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
return context.json(listResponse(list))
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
router.delete('/lists/:id', async (context) => {
|
|
212
|
+
await dependencies.service.remove(await requireActor(context), context.req.param('id'))
|
|
213
|
+
|
|
214
|
+
return context.body(null, 204)
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
router.get('/lists/:id/members', async (context) => {
|
|
218
|
+
const page = await dependencies.service.listMembers(
|
|
219
|
+
await requireActor(context),
|
|
220
|
+
{ listId: context.req.param('id') },
|
|
221
|
+
readListParameters(context),
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
return context.json(pageBody(page, listMemberResponse))
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
router.post('/lists/:id/members', async (context) => {
|
|
228
|
+
const body = await readJsonBody(context, addMemberBody)
|
|
229
|
+
const member = await dependencies.service.addMember(
|
|
230
|
+
await requireActor(context),
|
|
231
|
+
context.req.param('id'),
|
|
232
|
+
toAddMemberInput(body),
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
return context.json(listMemberResponse(member), 201)
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
router.delete('/lists/:id/members/:memberId', async (context) => {
|
|
239
|
+
await dependencies.service.removeMember(
|
|
240
|
+
await requireActor(context),
|
|
241
|
+
context.req.param('id'),
|
|
242
|
+
context.req.param('memberId'),
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
return context.body(null, 204)
|
|
246
|
+
})
|
|
247
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { RECORD_TARGET_TYPES } from '@kelpie/schemas'
|
|
2
|
+
import { foreignKey, index, pgTable, text, timestamp, unique } from 'drizzle-orm/pg-core'
|
|
3
|
+
|
|
4
|
+
import { checkOneOf, createdAt, primaryId, updatedAt } from '../../lib/columns.ts'
|
|
5
|
+
import { workspaces } from '../workspace/schema.ts'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The target types come from `@kelpie/schemas`, so this check constraint, the
|
|
9
|
+
* route's Zod enum, and the browser's decoder are one list rather than three
|
|
10
|
+
* copies. Re-exported because `routes.ts` reads them from here, and the tables
|
|
11
|
+
* they constrain are the reason they matter.
|
|
12
|
+
*/
|
|
13
|
+
export { RECORD_TARGET_TYPES } from '@kelpie/schemas'
|
|
14
|
+
export type { RecordTargetType } from '@kelpie/schemas'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A named collection of records of one type.
|
|
18
|
+
*
|
|
19
|
+
* The `target_type` is chosen at creation and never changes. A `list_members`
|
|
20
|
+
* row's `(list_id, target_type)` foreign-keys back into the composite unique on
|
|
21
|
+
* this table, so adding a person to a company list is refused by the database
|
|
22
|
+
* rather than trusted to the service.
|
|
23
|
+
*
|
|
24
|
+
* `(workspace_id, name)` is unique so the sidebar never shows two lists with the
|
|
25
|
+
* same label. Renaming to an in-use name returns 409.
|
|
26
|
+
*/
|
|
27
|
+
export const lists = pgTable(
|
|
28
|
+
'lists',
|
|
29
|
+
{
|
|
30
|
+
id: primaryId(),
|
|
31
|
+
workspaceId: text('workspace_id')
|
|
32
|
+
.notNull()
|
|
33
|
+
.references(() => workspaces.id, { onDelete: 'cascade' }),
|
|
34
|
+
name: text('name').notNull(),
|
|
35
|
+
description: text('description'),
|
|
36
|
+
targetType: text('target_type').notNull(),
|
|
37
|
+
createdAt: createdAt(),
|
|
38
|
+
updatedAt: updatedAt(),
|
|
39
|
+
},
|
|
40
|
+
(table) => [
|
|
41
|
+
index('lists_workspace_idx').on(table.workspaceId),
|
|
42
|
+
unique('lists_workspace_name_key').on(table.workspaceId, table.name),
|
|
43
|
+
// The referent of the composite FK from `list_members`. Postgres requires a
|
|
44
|
+
// unique key over the referenced columns; this exists solely to satisfy it.
|
|
45
|
+
unique('lists_id_target_type_key').on(table.id, table.targetType),
|
|
46
|
+
checkOneOf('lists_target_type_check', table.targetType, RECORD_TARGET_TYPES),
|
|
47
|
+
],
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A record's membership in a list.
|
|
52
|
+
*
|
|
53
|
+
* `(list_id, target_type)` foreign-keys back to the list's own unique key, so
|
|
54
|
+
* the member's type must equal its parent list's. The target itself is
|
|
55
|
+
* polymorphic and carries no foreign key of its own; the owning service checks
|
|
56
|
+
* existence with `missingTargets` and removes the row when the target goes
|
|
57
|
+
* (`attachedRecords.ts`).
|
|
58
|
+
*/
|
|
59
|
+
export const listMembers = pgTable(
|
|
60
|
+
'list_members',
|
|
61
|
+
{
|
|
62
|
+
id: primaryId(),
|
|
63
|
+
workspaceId: text('workspace_id')
|
|
64
|
+
.notNull()
|
|
65
|
+
.references(() => workspaces.id, { onDelete: 'cascade' }),
|
|
66
|
+
listId: text('list_id').notNull(),
|
|
67
|
+
targetType: text('target_type').notNull(),
|
|
68
|
+
targetId: text('target_id').notNull(),
|
|
69
|
+
// A membership is not edited, only added or removed, so no `updated_at`.
|
|
70
|
+
addedAt: timestamp('added_at', { withTimezone: true, mode: 'date' })
|
|
71
|
+
.notNull()
|
|
72
|
+
.defaultNow(),
|
|
73
|
+
},
|
|
74
|
+
(table) => [
|
|
75
|
+
index('list_members_list_idx').on(table.workspaceId, table.listId),
|
|
76
|
+
index('list_members_target_idx').on(table.workspaceId, table.targetType, table.targetId),
|
|
77
|
+
unique('list_members_list_target_key').on(table.listId, table.targetId),
|
|
78
|
+
foreignKey({
|
|
79
|
+
name: 'list_members_list_target_type_fk',
|
|
80
|
+
columns: [table.listId, table.targetType],
|
|
81
|
+
foreignColumns: [lists.id, lists.targetType],
|
|
82
|
+
}).onDelete('cascade'),
|
|
83
|
+
checkOneOf('list_members_target_type_check', table.targetType, RECORD_TARGET_TYPES),
|
|
84
|
+
],
|
|
85
|
+
)
|