@headless-lms/adapter-db 0.2.0 → 0.3.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/index.d.ts +7 -6
- package/dist/index.js +7 -6
- package/dist/repositories/admin.d.ts +23 -0
- package/dist/repositories/admin.js +393 -0
- package/dist/repositories/admin.js.map +1 -0
- package/dist/repositories/assets.d.ts +6 -2
- package/dist/repositories/assets.js +50 -9
- package/dist/repositories/assets.js.map +1 -1
- package/dist/repositories/automations.d.ts +6 -2
- package/dist/repositories/automations.js +45 -2
- package/dist/repositories/automations.js.map +1 -1
- package/dist/repositories/chats.d.ts +55 -0
- package/dist/repositories/chats.js +454 -0
- package/dist/repositories/chats.js.map +1 -0
- package/dist/repositories/content.d.ts +24 -1
- package/dist/repositories/content.js +293 -9
- package/dist/repositories/content.js.map +1 -1
- package/dist/repositories/discussion.d.ts +10 -3
- package/dist/repositories/discussion.js +70 -15
- package/dist/repositories/discussion.js.map +1 -1
- package/dist/repositories/entitlements.d.ts +29 -9
- package/dist/repositories/entitlements.js +207 -17
- package/dist/repositories/entitlements.js.map +1 -1
- package/dist/repositories/grant-covers.d.ts +20 -0
- package/dist/repositories/grant-covers.js +55 -0
- package/dist/repositories/grant-covers.js.map +1 -0
- package/dist/repositories/identity.d.ts +4 -2
- package/dist/repositories/identity.js +32 -1
- package/dist/repositories/identity.js.map +1 -1
- package/dist/repositories/member.d.ts +16 -0
- package/dist/repositories/member.js +63 -0
- package/dist/repositories/member.js.map +1 -0
- package/dist/repositories/members.js +2 -2
- package/dist/repositories/members.js.map +1 -1
- package/dist/repositories/notifications.d.ts +23 -0
- package/dist/repositories/notifications.js +70 -0
- package/dist/repositories/notifications.js.map +1 -0
- package/dist/repositories/org-user-profile.d.ts +214 -1
- package/dist/repositories/org-user-profile.js +23 -2
- package/dist/repositories/org-user-profile.js.map +1 -1
- package/dist/repositories/organizations.d.ts +3 -2
- package/dist/repositories/organizations.js +20 -8
- package/dist/repositories/organizations.js.map +1 -1
- package/dist/repositories/pg-errors.js +3 -1
- package/dist/repositories/pg-errors.js.map +1 -1
- package/dist/repositories/posts.d.ts +39 -0
- package/dist/repositories/posts.js +255 -0
- package/dist/repositories/posts.js.map +1 -0
- package/dist/schema/assets.d.ts +19 -2
- package/dist/schema/assets.js +10 -4
- package/dist/schema/assets.js.map +1 -1
- package/dist/schema/automations.d.ts +38 -0
- package/dist/schema/automations.js +5 -1
- package/dist/schema/automations.js.map +1 -1
- package/dist/schema/better-auth.d.ts +1613 -238
- package/dist/schema/better-auth.js +107 -22
- package/dist/schema/better-auth.js.map +1 -1
- package/dist/schema/content.d.ts +642 -5
- package/dist/schema/content.js +119 -10
- package/dist/schema/content.js.map +1 -1
- package/dist/schema/discussion.d.ts +2045 -55
- package/dist/schema/discussion.js +426 -11
- package/dist/schema/discussion.js.map +1 -1
- package/dist/schema/entitlements.d.ts +432 -1
- package/dist/schema/entitlements.js +109 -4
- package/dist/schema/entitlements.js.map +1 -1
- package/dist/schema/identity.d.ts +517 -1
- package/dist/schema/identity.js +118 -2
- package/dist/schema/identity.js.map +1 -1
- package/dist/schema/index.d.ts +6 -6
- package/dist/schema/index.js +30 -7
- package/dist/schema/organizations.d.ts +4 -4
- package/dist/schema/organizations.js +2 -0
- package/dist/schema/organizations.js.map +1 -1
- package/drizzle/0003_community.sql +340 -0
- package/drizzle/0004_assets_sources.sql +1 -0
- package/drizzle/0005_notices_and_seed_index.sql +12 -0
- package/drizzle/0006_community_review_fixes.sql +24 -0
- package/drizzle/0007_notifications.sql +37 -0
- package/drizzle/0008_content_access.sql +13 -0
- package/drizzle/0009_community_indexes.sql +56 -0
- package/drizzle/0010_mcp_access.sql +114 -0
- package/drizzle/meta/0003_snapshot.json +7061 -0
- package/drizzle/meta/0004_snapshot.json +7068 -0
- package/drizzle/meta/0005_snapshot.json +7180 -0
- package/drizzle/meta/0006_snapshot.json +7298 -0
- package/drizzle/meta/0007_snapshot.json +7620 -0
- package/drizzle/meta/0008_snapshot.json +7666 -0
- package/drizzle/meta/0009_snapshot.json +8185 -0
- package/drizzle/meta/0010_snapshot.json +8813 -0
- package/drizzle/meta/_journal.json +57 -1
- package/package.json +2 -2
- package/dist/repositories/course-analytics.d.ts +0 -15
- package/dist/repositories/course-analytics.js +0 -137
- package/dist/repositories/course-analytics.js.map +0 -1
- package/dist/repositories/dashboard.d.ts +0 -14
- package/dist/repositories/dashboard.js +0 -57
- package/dist/repositories/dashboard.js.map +0 -1
- package/dist/repositories/learn.d.ts +0 -19
- package/dist/repositories/learn.js +0 -56
- package/dist/repositories/learn.js.map +0 -1
- package/dist/repositories/students.d.ts +0 -16
- package/dist/repositories/students.js +0 -160
- package/dist/repositories/students.js.map +0 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { assets } from "../schema/assets.js";
|
|
2
|
-
import { translateDbErrors } from "./pg-errors.js";
|
|
3
|
-
import { and, asc, desc, eq, ilike, sql } from "drizzle-orm";
|
|
2
|
+
import { pgErrorFields, translateDbErrors } from "./pg-errors.js";
|
|
3
|
+
import { and, asc, desc, eq, ilike, inArray, lt, sql } from "drizzle-orm";
|
|
4
|
+
import { ConflictError } from "@headless-lms/core/shared/errors";
|
|
4
5
|
import { noopLogger } from "@headless-lms/core/shared/logger";
|
|
5
6
|
//#region src/repositories/assets.ts
|
|
6
7
|
function toAsset(row) {
|
|
@@ -9,6 +10,7 @@ function toAsset(row) {
|
|
|
9
10
|
id: row.id,
|
|
10
11
|
key: row.key,
|
|
11
12
|
kind: row.kind,
|
|
13
|
+
source: row.source,
|
|
12
14
|
filename: row.filename,
|
|
13
15
|
contentType: row.contentType,
|
|
14
16
|
size: row.size,
|
|
@@ -23,6 +25,21 @@ const SORT_COLUMNS = {
|
|
|
23
25
|
size: assets.size,
|
|
24
26
|
createdAt: assets.createdAt
|
|
25
27
|
};
|
|
28
|
+
function referenced(orgId, assetId) {
|
|
29
|
+
return sql`(
|
|
30
|
+
exists (select 1 from user_profiles p where p.image_asset_org_id = ${orgId} and p.image_asset_id = ${assetId})
|
|
31
|
+
or exists (select 1 from posts p where p.org_id = ${orgId} and p.cover_asset_id = ${assetId})
|
|
32
|
+
or exists (select 1 from message_attachments ma where ma.org_id = ${orgId} and ma.asset_id = ${assetId})
|
|
33
|
+
or exists (select 1 from activity_assets aa where aa.org_id = ${orgId} and aa.asset_id = ${assetId})
|
|
34
|
+
or exists (select 1 from download_assets da where da.org_id = ${orgId} and da.asset_id = ${assetId})
|
|
35
|
+
or exists (select 1 from courses c where c.org_id = ${orgId} and c.thumbnail_asset_id = ${assetId})
|
|
36
|
+
or exists (select 1 from downloads d where d.org_id = ${orgId} and d.thumbnail_asset_id = ${assetId})
|
|
37
|
+
or exists (select 1 from communities c where c.org_id = ${orgId} and c.thumbnail_asset_id = ${assetId})
|
|
38
|
+
)`;
|
|
39
|
+
}
|
|
40
|
+
function unreferencedMemberUploads(orgId, before) {
|
|
41
|
+
return and(eq(assets.orgId, orgId), eq(assets.source, "member"), lt(assets.createdAt, before), sql`not ${referenced(sql`${assets.orgId}`, sql`${assets.id}`)}`);
|
|
42
|
+
}
|
|
26
43
|
var DrizzleAssetsRepository = class {
|
|
27
44
|
db;
|
|
28
45
|
logger;
|
|
@@ -37,6 +54,7 @@ var DrizzleAssetsRepository = class {
|
|
|
37
54
|
orgId: asset.orgId,
|
|
38
55
|
key: asset.key,
|
|
39
56
|
kind: asset.kind,
|
|
57
|
+
source: asset.source,
|
|
40
58
|
filename: asset.filename,
|
|
41
59
|
contentType: asset.contentType,
|
|
42
60
|
size: asset.size,
|
|
@@ -50,6 +68,7 @@ var DrizzleAssetsRepository = class {
|
|
|
50
68
|
async list(orgId, query) {
|
|
51
69
|
const filters = [eq(assets.orgId, orgId)];
|
|
52
70
|
if (query.kind) filters.push(eq(assets.kind, query.kind));
|
|
71
|
+
if (query.source) filters.push(eq(assets.source, query.source));
|
|
53
72
|
const q = query.search?.trim();
|
|
54
73
|
if (q) filters.push(ilike(assets.filename, `%${q}%`));
|
|
55
74
|
const where = and(...filters);
|
|
@@ -74,16 +93,38 @@ var DrizzleAssetsRepository = class {
|
|
|
74
93
|
const [row] = await this.db.select().from(assets).where(and(eq(assets.orgId, orgId), eq(assets.id, id))).limit(1);
|
|
75
94
|
return row ? toAsset(row) : null;
|
|
76
95
|
}
|
|
77
|
-
async
|
|
96
|
+
async findByIds(orgId, ids) {
|
|
97
|
+
if (ids.length === 0) return [];
|
|
98
|
+
return (await this.db.select().from(assets).where(and(eq(assets.orgId, orgId), inArray(assets.id, ids)))).map(toAsset);
|
|
99
|
+
}
|
|
100
|
+
async markReady(orgId, id, patch) {
|
|
78
101
|
const [row] = await this.db.update(assets).set({
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
102
|
+
key: patch.key,
|
|
103
|
+
kind: patch.kind,
|
|
104
|
+
size: patch.size,
|
|
105
|
+
contentType: patch.contentType,
|
|
106
|
+
status: "ready"
|
|
107
|
+
}).where(and(eq(assets.orgId, orgId), eq(assets.id, id), eq(assets.status, "pending"))).returning();
|
|
83
108
|
return row ? toAsset(row) : null;
|
|
84
109
|
}
|
|
85
|
-
async delete(id) {
|
|
86
|
-
|
|
110
|
+
async delete(orgId, id) {
|
|
111
|
+
try {
|
|
112
|
+
return (await this.db.delete(assets).where(and(eq(assets.orgId, orgId), eq(assets.id, id))).returning({ id: assets.id })).length > 0;
|
|
113
|
+
} catch (err) {
|
|
114
|
+
if (pgErrorFields(err)?.code === "23503") throw Object.assign(new ConflictError("This file is still in use"), { cause: err });
|
|
115
|
+
throw err;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
async hasUnreferencedMemberUploads(orgId, before) {
|
|
119
|
+
const [row] = await this.db.select({ id: assets.id }).from(assets).where(unreferencedMemberUploads(orgId, before)).limit(1);
|
|
120
|
+
return row !== void 0;
|
|
121
|
+
}
|
|
122
|
+
async listUnreferencedMemberUploads(orgId, before) {
|
|
123
|
+
return (await this.db.select().from(assets).where(unreferencedMemberUploads(orgId, before)).orderBy(asc(assets.createdAt))).map(toAsset);
|
|
124
|
+
}
|
|
125
|
+
async heldKeys(orgId, keys) {
|
|
126
|
+
if (keys.length === 0) return [];
|
|
127
|
+
return (await this.db.select({ key: assets.key }).from(assets).where(and(eq(assets.orgId, orgId), inArray(assets.key, keys)))).map((r) => r.key);
|
|
87
128
|
}
|
|
88
129
|
};
|
|
89
130
|
translateDbErrors(DrizzleAssetsRepository);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assets.js","names":[],"sources":["../../src/repositories/assets.ts"],"sourcesContent":["// assets — Drizzle repository (implements the core outbound port).\nimport { and, eq, ilike, sql, asc, desc, type SQL } from \"drizzle-orm\";\nimport type { AssetsRepository } from \"@headless-lms/core/assets\";\nimport type {\n Asset,\n AssetKind,\n AssetStatus,\n AssetsQuery,\n Logger,\n Page,\n} from \"@headless-lms/core/types\";\nimport { assets } from \"../schema/assets.js\";\nimport { noopLogger } from \"@headless-lms/core/shared/logger\";\nimport type { DbExecutor } from \"../client.js\";\nimport { translateDbErrors } from \"./pg-errors.js\";\n\ntype Row = typeof assets.$inferSelect;\n\nfunction toAsset(row: Row): Asset {\n return {\n orgId: row.orgId,\n id: row.id,\n key: row.key,\n kind: row.kind as AssetKind,\n filename: row.filename,\n contentType: row.contentType,\n size: row.size,\n status: row.status as AssetStatus,\n uploadedBy: row.uploadedBy,\n createdAt: row.createdAt,\n updatedAt: row.updatedAt,\n };\n}\n\n// Whitelisted sort columns; anything else falls back to newest-first.\nconst SORT_COLUMNS = {\n filename: assets.filename,\n size: assets.size,\n createdAt: assets.createdAt,\n} as const;\n\nexport class DrizzleAssetsRepository implements AssetsRepository {\n constructor(\n private readonly db: DbExecutor,\n private readonly logger: Logger = noopLogger,\n ) {}\n\n async insert(orgId: string, asset: Asset): Promise<Asset> {\n if (asset.orgId !== orgId) {\n throw new Error(\"asset org mismatch\");\n }\n const [row] = await this.db\n .insert(assets)\n .values({\n id: asset.id,\n orgId: asset.orgId,\n key: asset.key,\n kind: asset.kind,\n filename: asset.filename,\n contentType: asset.contentType,\n size: asset.size,\n status: asset.status,\n uploadedBy: asset.uploadedBy,\n createdAt: new Date(asset.createdAt),\n })\n .returning();\n if (!row) {\n throw new Error(\"failed to insert asset\");\n }\n return toAsset(row);\n }\n\n async list(orgId: string, query: AssetsQuery): Promise<Page<Asset>> {\n const filters: (SQL | undefined)[] = [eq(assets.orgId, orgId)];\n if (query.kind) {\n filters.push(eq(assets.kind, query.kind));\n }\n const q = query.search?.trim();\n if (q) {\n filters.push(ilike(assets.filename, `%${q}%`));\n }\n const where = and(...filters);\n\n const orderBy = (() => {\n if (!query.sort) {\n return desc(assets.createdAt);\n }\n const descending = query.sort.startsWith(\"-\");\n const field = descending ? query.sort.slice(1) : query.sort;\n const col = SORT_COLUMNS[field as keyof typeof SORT_COLUMNS] ?? assets.createdAt;\n return descending ? desc(col) : asc(col);\n })();\n\n const offset = (query.page - 1) * query.pageSize;\n const rows = await this.db\n .select()\n .from(assets)\n .where(where)\n .orderBy(orderBy)\n .limit(query.pageSize)\n .offset(offset);\n\n const counted = await this.db\n .select({ value: sql<number>`count(*)::int` })\n .from(assets)\n .where(where);\n\n return {\n rows: rows.map(toAsset),\n total: counted[0]?.value ?? 0,\n page: query.page,\n pageSize: query.pageSize,\n };\n }\n\n async findById(orgId: string, id: string): Promise<Asset | null> {\n const [row] = await this.db\n .select()\n .from(assets)\n .where(and(eq(assets.orgId, orgId), eq(assets.id, id)))\n .limit(1);\n return row ? toAsset(row) : null;\n }\n\n async update(\n id: string,\n patch: Partial<Pick<Asset, \"size\" | \"contentType\" | \"status\">>,\n ): Promise<Asset | null> {\n const [row] = await this.db\n .update(assets)\n .set({\n ...(patch.size !== undefined ? { size: patch.size } : {}),\n ...(patch.contentType !== undefined ? { contentType: patch.contentType } : {}),\n ...(patch.status !== undefined ? { status: patch.status } : {}),\n })\n .where(eq(assets.id, id))\n .returning();\n return row ? toAsset(row) : null;\n }\n\n async delete(id: string): Promise<boolean> {\n const deleted = await this.db\n .delete(assets)\n .where(eq(assets.id, id))\n .returning({ id: assets.id });\n return deleted.length > 0;\n }\n}\ntranslateDbErrors(DrizzleAssetsRepository);\n"],"mappings":";;;;;AAkBA,SAAS,QAAQ,KAAiB;CAChC,OAAO;EACL,OAAO,IAAI;EACX,IAAI,IAAI;EACR,KAAK,IAAI;EACT,MAAM,IAAI;EACV,UAAU,IAAI;EACd,aAAa,IAAI;EACjB,MAAM,IAAI;EACV,QAAQ,IAAI;EACZ,YAAY,IAAI;EAChB,WAAW,IAAI;EACf,WAAW,IAAI;CACjB;AACF;AAGA,MAAM,eAAe;CACnB,UAAU,OAAO;CACjB,MAAM,OAAO;CACb,WAAW,OAAO;AACpB;AAEA,IAAa,0BAAb,MAAiE;CAE5C;CACA;CAFnB,YACE,IACA,SAAkC,YAClC;EAFiB,KAAA,KAAA;EACA,KAAA,SAAA;CAChB;CAEH,MAAM,OAAO,OAAe,OAA8B;EACxD,IAAI,MAAM,UAAU,OAClB,MAAM,IAAI,MAAM,oBAAoB;EAEtC,MAAM,CAAC,OAAO,MAAM,KAAK,GACtB,OAAO,MAAM,CAAC,CACd,OAAO;GACN,IAAI,MAAM;GACV,OAAO,MAAM;GACb,KAAK,MAAM;GACX,MAAM,MAAM;GACZ,UAAU,MAAM;GAChB,aAAa,MAAM;GACnB,MAAM,MAAM;GACZ,QAAQ,MAAM;GACd,YAAY,MAAM;GAClB,WAAW,IAAI,KAAK,MAAM,SAAS;EACrC,CAAC,CAAC,CACD,UAAU;EACb,IAAI,CAAC,KACH,MAAM,IAAI,MAAM,wBAAwB;EAE1C,OAAO,QAAQ,GAAG;CACpB;CAEA,MAAM,KAAK,OAAe,OAA0C;EAClE,MAAM,UAA+B,CAAC,GAAG,OAAO,OAAO,KAAK,CAAC;EAC7D,IAAI,MAAM,MACR,QAAQ,KAAK,GAAG,OAAO,MAAM,MAAM,IAAI,CAAC;EAE1C,MAAM,IAAI,MAAM,QAAQ,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AAI,MAAM,SAAS,KAAA,IAAY,EAAE,MAAM,MAAM,KAAK,IAAI,CAAC;GACvD,GAAI,MAAM,gBAAgB,KAAA,IAAY,EAAE,aAAa,MAAM,YAAY,IAAI,CAAC;GAC5E,GAAI,MAAM,WAAW,KAAA,IAAY,EAAE,QAAQ,MAAM,OAAO,IAAI,CAAC;EAC/D,CAAC,CAAC,CACD,MAAM,GAAG,OAAO,IAAI,EAAE,CAAC,CAAC,CACxB,UAAU;EACb,OAAO,MAAM,QAAQ,GAAG,IAAI;CAC9B;CAEA,MAAM,OAAO,IAA8B;EAKzC,QAAO,MAJe,KAAK,GACxB,OAAO,MAAM,CAAC,CACd,MAAM,GAAG,OAAO,IAAI,EAAE,CAAC,CAAC,CACxB,UAAU,EAAE,IAAI,OAAO,GAAG,CAAC,EAAA,CACf,SAAS;CAC1B;AACF;AACA,kBAAkB,uBAAuB"}
|
|
1
|
+
{"version":3,"file":"assets.js","names":[],"sources":["../../src/repositories/assets.ts"],"sourcesContent":["import { and, eq, ilike, inArray, lt, sql, asc, desc, type SQL } from \"drizzle-orm\";\nimport type { AssetsRepository } from \"@headless-lms/core/assets\";\nimport type {\n Asset,\n AssetKind,\n AssetSource,\n AssetStatus,\n AssetsQuery,\n Logger,\n Page,\n} from \"@headless-lms/core/types\";\nimport { assets } from \"../schema/assets.js\";\nimport { noopLogger } from \"@headless-lms/core/shared/logger\";\nimport type { DbExecutor } from \"../client.js\";\nimport { ConflictError } from \"@headless-lms/core/shared/errors\";\nimport { pgErrorFields, translateDbErrors } from \"./pg-errors.js\";\n\ntype Row = typeof assets.$inferSelect;\n\nfunction toAsset(row: Row): Asset {\n return {\n orgId: row.orgId,\n id: row.id,\n key: row.key,\n kind: row.kind as AssetKind,\n source: row.source as AssetSource,\n filename: row.filename,\n contentType: row.contentType,\n size: row.size,\n status: row.status as AssetStatus,\n uploadedBy: row.uploadedBy,\n createdAt: row.createdAt,\n updatedAt: row.updatedAt,\n };\n}\n\nconst SORT_COLUMNS = {\n filename: assets.filename,\n size: assets.size,\n createdAt: assets.createdAt,\n} as const;\n\nfunction referenced(orgId: SQL, assetId: SQL): SQL {\n return sql`(\n exists (select 1 from user_profiles p where p.image_asset_org_id = ${orgId} and p.image_asset_id = ${assetId})\n or exists (select 1 from posts p where p.org_id = ${orgId} and p.cover_asset_id = ${assetId})\n or exists (select 1 from message_attachments ma where ma.org_id = ${orgId} and ma.asset_id = ${assetId})\n or exists (select 1 from activity_assets aa where aa.org_id = ${orgId} and aa.asset_id = ${assetId})\n or exists (select 1 from download_assets da where da.org_id = ${orgId} and da.asset_id = ${assetId})\n or exists (select 1 from courses c where c.org_id = ${orgId} and c.thumbnail_asset_id = ${assetId})\n or exists (select 1 from downloads d where d.org_id = ${orgId} and d.thumbnail_asset_id = ${assetId})\n or exists (select 1 from communities c where c.org_id = ${orgId} and c.thumbnail_asset_id = ${assetId})\n )`;\n}\n\nfunction unreferencedMemberUploads(orgId: string, before: Date): SQL | undefined {\n return and(\n eq(assets.orgId, orgId),\n eq(assets.source, \"member\"),\n lt(assets.createdAt, before),\n sql`not ${referenced(sql`${assets.orgId}`, sql`${assets.id}`)}`,\n );\n}\n\nexport class DrizzleAssetsRepository implements AssetsRepository {\n constructor(\n private readonly db: DbExecutor,\n private readonly logger: Logger = noopLogger,\n ) {}\n\n async insert(orgId: string, asset: Asset): Promise<Asset> {\n if (asset.orgId !== orgId) {\n throw new Error(\"asset org mismatch\");\n }\n const [row] = await this.db\n .insert(assets)\n .values({\n id: asset.id,\n orgId: asset.orgId,\n key: asset.key,\n kind: asset.kind,\n source: asset.source,\n filename: asset.filename,\n contentType: asset.contentType,\n size: asset.size,\n status: asset.status,\n uploadedBy: asset.uploadedBy,\n createdAt: new Date(asset.createdAt),\n })\n .returning();\n if (!row) {\n throw new Error(\"failed to insert asset\");\n }\n return toAsset(row);\n }\n\n async list(orgId: string, query: AssetsQuery): Promise<Page<Asset>> {\n const filters: (SQL | undefined)[] = [eq(assets.orgId, orgId)];\n if (query.kind) {\n filters.push(eq(assets.kind, query.kind));\n }\n if (query.source) {\n filters.push(eq(assets.source, query.source));\n }\n const q = query.search?.trim();\n if (q) {\n filters.push(ilike(assets.filename, `%${q}%`));\n }\n const where = and(...filters);\n\n const orderBy = (() => {\n if (!query.sort) {\n return desc(assets.createdAt);\n }\n const descending = query.sort.startsWith(\"-\");\n const field = descending ? query.sort.slice(1) : query.sort;\n const col = SORT_COLUMNS[field as keyof typeof SORT_COLUMNS] ?? assets.createdAt;\n return descending ? desc(col) : asc(col);\n })();\n\n const offset = (query.page - 1) * query.pageSize;\n const rows = await this.db\n .select()\n .from(assets)\n .where(where)\n .orderBy(orderBy)\n .limit(query.pageSize)\n .offset(offset);\n\n const counted = await this.db\n .select({ value: sql<number>`count(*)::int` })\n .from(assets)\n .where(where);\n\n return {\n rows: rows.map(toAsset),\n total: counted[0]?.value ?? 0,\n page: query.page,\n pageSize: query.pageSize,\n };\n }\n\n async findById(orgId: string, id: string): Promise<Asset | null> {\n const [row] = await this.db\n .select()\n .from(assets)\n .where(and(eq(assets.orgId, orgId), eq(assets.id, id)))\n .limit(1);\n return row ? toAsset(row) : null;\n }\n\n async findByIds(orgId: string, ids: string[]): Promise<Asset[]> {\n if (ids.length === 0) {\n return [];\n }\n const rows = await this.db\n .select()\n .from(assets)\n .where(and(eq(assets.orgId, orgId), inArray(assets.id, ids)));\n return rows.map(toAsset);\n }\n\n async markReady(\n orgId: string,\n id: string,\n patch: Pick<Asset, \"key\" | \"kind\" | \"size\" | \"contentType\">,\n ): Promise<Asset | null> {\n const [row] = await this.db\n .update(assets)\n .set({\n key: patch.key,\n kind: patch.kind,\n size: patch.size,\n contentType: patch.contentType,\n status: \"ready\",\n })\n .where(and(eq(assets.orgId, orgId), eq(assets.id, id), eq(assets.status, \"pending\")))\n .returning();\n return row ? toAsset(row) : null;\n }\n\n async delete(orgId: string, id: string): Promise<boolean> {\n try {\n const deleted = await this.db\n .delete(assets)\n .where(and(eq(assets.orgId, orgId), eq(assets.id, id)))\n .returning({ id: assets.id });\n return deleted.length > 0;\n } catch (err) {\n if (pgErrorFields(err)?.code === \"23503\") {\n throw Object.assign(new ConflictError(\"This file is still in use\"), { cause: err });\n }\n throw err;\n }\n }\n\n async hasUnreferencedMemberUploads(orgId: string, before: Date): Promise<boolean> {\n const [row] = await this.db\n .select({ id: assets.id })\n .from(assets)\n .where(unreferencedMemberUploads(orgId, before))\n .limit(1);\n return row !== undefined;\n }\n\n async listUnreferencedMemberUploads(orgId: string, before: Date): Promise<Asset[]> {\n const rows = await this.db\n .select()\n .from(assets)\n .where(unreferencedMemberUploads(orgId, before))\n .orderBy(asc(assets.createdAt));\n return rows.map(toAsset);\n }\n\n async heldKeys(orgId: string, keys: string[]): Promise<string[]> {\n if (keys.length === 0) {\n return [];\n }\n const rows = await this.db\n .select({ key: assets.key })\n .from(assets)\n .where(and(eq(assets.orgId, orgId), inArray(assets.key, keys)));\n return rows.map((r) => r.key);\n }\n}\ntranslateDbErrors(DrizzleAssetsRepository);\n"],"mappings":";;;;;;AAmBA,SAAS,QAAQ,KAAiB;CAChC,OAAO;EACL,OAAO,IAAI;EACX,IAAI,IAAI;EACR,KAAK,IAAI;EACT,MAAM,IAAI;EACV,QAAQ,IAAI;EACZ,UAAU,IAAI;EACd,aAAa,IAAI;EACjB,MAAM,IAAI;EACV,QAAQ,IAAI;EACZ,YAAY,IAAI;EAChB,WAAW,IAAI;EACf,WAAW,IAAI;CACjB;AACF;AAEA,MAAM,eAAe;CACnB,UAAU,OAAO;CACjB,MAAM,OAAO;CACb,WAAW,OAAO;AACpB;AAEA,SAAS,WAAW,OAAY,SAAmB;CACjD,OAAO,GAAG;yEAC6D,MAAM,0BAA0B,QAAQ;wDACzD,MAAM,0BAA0B,QAAQ;wEACxB,MAAM,qBAAqB,QAAQ;oEACvC,MAAM,qBAAqB,QAAQ;oEACnC,MAAM,qBAAqB,QAAQ;0DAC7C,MAAM,8BAA8B,QAAQ;4DAC1C,MAAM,8BAA8B,QAAQ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,CAAC,EAAA,CACpB,IAAI,OAAO;CACzB;CAEA,MAAM,SAAS,OAAe,MAAmC;EAC/D,IAAI,KAAK,WAAW,GAClB,OAAO,CAAC;EAMV,QAAO,MAJY,KAAK,GACrB,OAAO,EAAE,KAAK,OAAO,IAAI,CAAC,CAAC,CAC3B,KAAK,MAAM,CAAC,CACZ,MAAM,IAAI,GAAG,OAAO,OAAO,KAAK,GAAG,QAAQ,OAAO,KAAK,IAAI,CAAC,CAAC,EAAA,CACpD,KAAK,MAAM,EAAE,GAAG;CAC9B;AACF;AACA,kBAAkB,uBAAuB"}
|
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
import { DbExecutor } from "../client.js";
|
|
2
2
|
import { Logger } from "@headless-lms/core/shared/ports";
|
|
3
|
-
import {
|
|
3
|
+
import { ScheduledAutomation } from "@headless-lms/core/types";
|
|
4
|
+
import { Automation, AutomationKind, AutomationRun, AutomationRunsQuery, AutomationRunsRepository, AutomationsRepository, NewAutomation, NewAutomationRun, Page as Page$1, UpdateAutomationInput } from "@headless-lms/core/automations";
|
|
4
5
|
//#region src/repositories/automations.d.ts
|
|
5
6
|
declare class DrizzleAutomationsRepository implements AutomationsRepository {
|
|
6
7
|
private readonly db;
|
|
7
8
|
private readonly logger;
|
|
8
9
|
constructor(db: DbExecutor, logger?: Logger);
|
|
9
10
|
insert(orgId: string, input: NewAutomation): Promise<Automation>;
|
|
11
|
+
insertSeed(orgId: string, input: NewAutomation): Promise<Automation | null>;
|
|
10
12
|
update(orgId: string, id: string, patch: UpdateAutomationInput): Promise<Automation | null>;
|
|
11
13
|
delete(orgId: string, id: string): Promise<Automation | null>;
|
|
12
14
|
findById(orgId: string, id: string): Promise<Automation | null>;
|
|
13
15
|
listByOrg(orgId: string, kind?: AutomationKind): Promise<Automation[]>;
|
|
14
16
|
listByTrigger(orgId: string, trigger: string): Promise<Automation[]>;
|
|
17
|
+
listSchedules(): Promise<ScheduledAutomation[]>;
|
|
15
18
|
}
|
|
16
19
|
declare class DrizzleAutomationRunsRepository implements AutomationRunsRepository {
|
|
17
20
|
private readonly db;
|
|
@@ -25,7 +28,8 @@ declare class DrizzleAutomationRunsRepository implements AutomationRunsRepositor
|
|
|
25
28
|
actionResults: AutomationRun["actionResults"];
|
|
26
29
|
finishedAt: Date;
|
|
27
30
|
}): Promise<AutomationRun | null>;
|
|
28
|
-
list(orgId: string, automationId: string, query: AutomationRunsQuery): Promise<Page<AutomationRun>>;
|
|
31
|
+
list(orgId: string, automationId: string, query: AutomationRunsQuery): Promise<Page$1<AutomationRun>>;
|
|
32
|
+
recentRecipients(orgId: string, automationId: string, chatId: string, since: Date): Promise<string[]>;
|
|
29
33
|
}
|
|
30
34
|
//#endregion
|
|
31
35
|
export { DrizzleAutomationRunsRepository, DrizzleAutomationsRepository };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { automationRuns, automations } from "../schema/automations.js";
|
|
2
2
|
import "../schema/index.js";
|
|
3
3
|
import { translateDbErrors } from "./pg-errors.js";
|
|
4
|
-
import { and, arrayContains, asc, desc, eq, ne, sql } from "drizzle-orm";
|
|
4
|
+
import { and, arrayContains, asc, desc, eq, gte, ne, sql } from "drizzle-orm";
|
|
5
5
|
import { noopLogger } from "@headless-lms/core/shared/logger";
|
|
6
6
|
//#region src/repositories/automations.ts
|
|
7
7
|
const automationSelection = {
|
|
@@ -13,6 +13,8 @@ const automationSelection = {
|
|
|
13
13
|
triggers: automations.triggers,
|
|
14
14
|
actions: automations.actions,
|
|
15
15
|
enabled: automations.enabled,
|
|
16
|
+
filter: automations.filter,
|
|
17
|
+
source: automations.source,
|
|
16
18
|
createdAt: automations.createdAt,
|
|
17
19
|
updatedAt: automations.updatedAt
|
|
18
20
|
};
|
|
@@ -26,6 +28,8 @@ function toAutomation(row) {
|
|
|
26
28
|
triggers: row.triggers,
|
|
27
29
|
actions: row.actions,
|
|
28
30
|
enabled: row.enabled,
|
|
31
|
+
filter: row.filter,
|
|
32
|
+
source: row.source,
|
|
29
33
|
createdAt: row.createdAt,
|
|
30
34
|
updatedAt: row.updatedAt
|
|
31
35
|
};
|
|
@@ -44,13 +48,28 @@ var DrizzleAutomationsRepository = class {
|
|
|
44
48
|
kind: input.kind,
|
|
45
49
|
description: input.description,
|
|
46
50
|
triggers: input.triggers,
|
|
47
|
-
actions: input.actions
|
|
51
|
+
actions: input.actions,
|
|
52
|
+
filter: input.filter ?? null,
|
|
53
|
+
source: input.source ?? "user"
|
|
48
54
|
}).returning({ id: automations.id });
|
|
49
55
|
if (!inserted) throw new Error("failed to insert automation");
|
|
50
56
|
const created = await this.findById(orgId, inserted.id);
|
|
51
57
|
if (!created) throw new Error("failed to load created automation");
|
|
52
58
|
return created;
|
|
53
59
|
}
|
|
60
|
+
async insertSeed(orgId, input) {
|
|
61
|
+
const [inserted] = await this.db.insert(automations).values({
|
|
62
|
+
orgId,
|
|
63
|
+
name: input.name,
|
|
64
|
+
kind: input.kind,
|
|
65
|
+
description: input.description,
|
|
66
|
+
triggers: input.triggers,
|
|
67
|
+
actions: input.actions,
|
|
68
|
+
filter: input.filter ?? null,
|
|
69
|
+
source: "system"
|
|
70
|
+
}).onConflictDoNothing().returning({ id: automations.id });
|
|
71
|
+
return inserted ? this.findById(orgId, inserted.id) : null;
|
|
72
|
+
}
|
|
54
73
|
async update(orgId, id, patch) {
|
|
55
74
|
const set = { updatedAt: /* @__PURE__ */ new Date() };
|
|
56
75
|
if (patch.name !== void 0) set.name = patch.name;
|
|
@@ -58,6 +77,7 @@ var DrizzleAutomationsRepository = class {
|
|
|
58
77
|
if (patch.triggers !== void 0) set.triggers = patch.triggers;
|
|
59
78
|
if (patch.actions !== void 0) set.actions = patch.actions;
|
|
60
79
|
if (patch.enabled !== void 0) set.enabled = patch.enabled;
|
|
80
|
+
if (patch.filter !== void 0) set.filter = patch.filter;
|
|
61
81
|
const [updated] = await this.db.update(automations).set(set).where(and(eq(automations.orgId, orgId), eq(automations.id, id))).returning({ id: automations.id });
|
|
62
82
|
if (!updated) return null;
|
|
63
83
|
return this.findById(orgId, id);
|
|
@@ -78,6 +98,23 @@ var DrizzleAutomationsRepository = class {
|
|
|
78
98
|
async listByTrigger(orgId, trigger) {
|
|
79
99
|
return (await this.db.select(automationSelection).from(automations).where(and(eq(automations.orgId, orgId), arrayContains(automations.triggers, [trigger]))).orderBy(desc(automations.createdAt))).map(toAutomation);
|
|
80
100
|
}
|
|
101
|
+
async listSchedules() {
|
|
102
|
+
const rows = await this.db.select({
|
|
103
|
+
orgId: automations.orgId,
|
|
104
|
+
id: automations.id,
|
|
105
|
+
triggers: automations.triggers
|
|
106
|
+
}).from(automations).where(and(eq(automations.enabled, true), sql`exists (select 1 from unnest(${automations.triggers}) as t(trigger) where t.trigger like 'schedule:%')`));
|
|
107
|
+
const schedules = [];
|
|
108
|
+
for (const row of rows) for (const trigger of row.triggers) {
|
|
109
|
+
if (!trigger.startsWith("schedule:")) continue;
|
|
110
|
+
schedules.push({
|
|
111
|
+
orgId: row.orgId,
|
|
112
|
+
automationId: row.id,
|
|
113
|
+
cron: trigger.slice(9)
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return schedules;
|
|
117
|
+
}
|
|
81
118
|
};
|
|
82
119
|
const runSelection = {
|
|
83
120
|
id: automationRuns.id,
|
|
@@ -181,6 +218,12 @@ var DrizzleAutomationRunsRepository = class {
|
|
|
181
218
|
pageSize: query.pageSize
|
|
182
219
|
};
|
|
183
220
|
}
|
|
221
|
+
async recentRecipients(orgId, automationId, chatId, since) {
|
|
222
|
+
const rows = await this.db.select({ actionResults: automationRuns.actionResults }).from(automationRuns).where(and(eq(automationRuns.orgId, orgId), eq(automationRuns.automationId, automationId), eq(automationRuns.status, "completed"), gte(automationRuns.finishedAt, since), sql`${automationRuns.event}->'data'->>'chatId' = ${chatId}`));
|
|
223
|
+
const ids = [];
|
|
224
|
+
for (const row of rows) for (const result of row.actionResults) if (result.recipients) ids.push(...result.recipients);
|
|
225
|
+
return [...new Set(ids)];
|
|
226
|
+
}
|
|
184
227
|
};
|
|
185
228
|
translateDbErrors(DrizzleAutomationsRepository);
|
|
186
229
|
translateDbErrors(DrizzleAutomationRunsRepository);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"automations.js","names":[],"sources":["../../src/repositories/automations.ts"],"sourcesContent":["// automations — Drizzle repositories (implement the core outbound ports).\n// Org-scoped: every method takes the domain `organizations.id` and constrains\n// its queries to that tenant. `Automation`/`AutomationRun` carry no `orgId`\n// column of their own in the domain type (mirrors the `Entitlement` shape) —\n// `AutomationRun` is the one exception, since it's the run's own `orgId`\n// field the service reads back off `handle`'s event.\nimport { and, arrayContains, asc, desc, eq, ne, sql, type SQL } from \"drizzle-orm\";\nimport type { DbExecutor } from \"../client.js\";\nimport type {\n Automation,\n AutomationKind,\n AutomationRun,\n AutomationRunsQuery,\n AutomationRunsRepository,\n AutomationsRepository,\n NewAutomation,\n NewAutomationRun,\n Page,\n UpdateAutomationInput,\n} from \"@headless-lms/core/automations\";\nimport { automations, automationRuns } from \"../schema/index.js\";\nimport type { Logger } from \"@headless-lms/core/shared/ports\";\nimport { noopLogger } from \"@headless-lms/core/shared/logger\";\nimport { translateDbErrors } from \"./pg-errors.js\";\n\nconst automationSelection = {\n orgId: automations.orgId,\n id: automations.id,\n name: automations.name,\n kind: automations.kind,\n description: automations.description,\n triggers: automations.triggers,\n actions: automations.actions,\n enabled: automations.enabled,\n createdAt: automations.createdAt,\n updatedAt: automations.updatedAt,\n};\n\ninterface AutomationRow {\n orgId: string;\n id: string;\n name: string;\n kind: AutomationKind;\n description: string | null;\n triggers: string[];\n actions: Automation[\"actions\"];\n enabled: boolean;\n createdAt: Date;\n updatedAt: Date;\n}\n\nfunction toAutomation(row: AutomationRow): Automation {\n return {\n orgId: row.orgId,\n id: row.id,\n name: row.name,\n kind: row.kind,\n description: row.description,\n triggers: row.triggers,\n actions: row.actions,\n enabled: row.enabled,\n createdAt: row.createdAt,\n updatedAt: row.updatedAt,\n };\n}\n\nexport class DrizzleAutomationsRepository implements AutomationsRepository {\n constructor(\n private readonly db: DbExecutor,\n private readonly logger: Logger = noopLogger,\n ) {}\n\n async insert(orgId: string, input: NewAutomation): Promise<Automation> {\n const [inserted] = await this.db\n .insert(automations)\n .values({\n orgId,\n name: input.name,\n kind: input.kind,\n description: input.description,\n triggers: input.triggers,\n actions: input.actions,\n })\n .returning({ id: automations.id });\n if (!inserted) {\n throw new Error(\"failed to insert automation\");\n }\n const created = await this.findById(orgId, inserted.id);\n if (!created) {\n throw new Error(\"failed to load created automation\");\n }\n return created;\n }\n\n async update(\n orgId: string,\n id: string,\n patch: UpdateAutomationInput,\n ): Promise<Automation | null> {\n const set: Record<string, unknown> = { updatedAt: new Date() };\n if (patch.name !== undefined) {\n set.name = patch.name;\n }\n if (patch.description !== undefined) {\n set.description = patch.description;\n }\n if (patch.triggers !== undefined) {\n set.triggers = patch.triggers;\n }\n if (patch.actions !== undefined) {\n set.actions = patch.actions;\n }\n if (patch.enabled !== undefined) {\n set.enabled = patch.enabled;\n }\n\n const [updated] = await this.db\n .update(automations)\n .set(set)\n .where(and(eq(automations.orgId, orgId), eq(automations.id, id)))\n .returning({ id: automations.id });\n if (!updated) {\n return null;\n }\n return this.findById(orgId, id);\n }\n\n async delete(orgId: string, id: string): Promise<Automation | null> {\n const [deleted] = await this.db\n .delete(automations)\n .where(and(eq(automations.orgId, orgId), eq(automations.id, id)))\n .returning(automationSelection);\n return deleted ? toAutomation(deleted) : null;\n }\n\n async findById(orgId: string, id: string): Promise<Automation | null> {\n const [row] = await this.db\n .select(automationSelection)\n .from(automations)\n .where(and(eq(automations.orgId, orgId), eq(automations.id, id)))\n .limit(1);\n return row ? toAutomation(row) : null;\n }\n\n async listByOrg(orgId: string, kind?: AutomationKind): Promise<Automation[]> {\n const conditions: SQL[] = [eq(automations.orgId, orgId)];\n if (kind) {\n conditions.push(eq(automations.kind, kind));\n }\n const rows = await this.db\n .select(automationSelection)\n .from(automations)\n .where(and(...conditions))\n .orderBy(desc(automations.createdAt));\n return rows.map(toAutomation);\n }\n\n async listByTrigger(orgId: string, trigger: string): Promise<Automation[]> {\n const rows = await this.db\n .select(automationSelection)\n .from(automations)\n .where(and(eq(automations.orgId, orgId), arrayContains(automations.triggers, [trigger])))\n .orderBy(desc(automations.createdAt));\n return rows.map(toAutomation);\n }\n}\n\nconst runSelection = {\n id: automationRuns.id,\n orgId: automationRuns.orgId,\n automationId: automationRuns.automationId,\n trigger: automationRuns.trigger,\n eventId: automationRuns.eventId,\n event: automationRuns.event,\n status: automationRuns.status,\n actionResults: automationRuns.actionResults,\n startedAt: automationRuns.startedAt,\n finishedAt: automationRuns.finishedAt,\n createdAt: automationRuns.createdAt,\n updatedAt: automationRuns.updatedAt,\n};\n\ninterface RunRow {\n id: string;\n orgId: string;\n automationId: string;\n trigger: string;\n eventId: string;\n event: AutomationRun[\"event\"];\n status: AutomationRun[\"status\"];\n actionResults: AutomationRun[\"actionResults\"];\n startedAt: Date;\n finishedAt: Date | null;\n createdAt: Date;\n updatedAt: Date;\n}\n\nfunction toAutomationRun(row: RunRow): AutomationRun {\n return {\n id: row.id,\n orgId: row.orgId,\n automationId: row.automationId,\n trigger: row.trigger,\n eventId: row.eventId,\n event: row.event,\n status: row.status,\n actionResults: row.actionResults,\n startedAt: row.startedAt,\n finishedAt: row.finishedAt,\n createdAt: row.createdAt,\n updatedAt: row.updatedAt,\n };\n}\n\n// Sortable columns by the client-facing field name. `-field` = desc.\nconst sortColumns = {\n startedAt: automationRuns.startedAt,\n finishedAt: automationRuns.finishedAt,\n status: automationRuns.status,\n} as const;\n\nexport class DrizzleAutomationRunsRepository implements AutomationRunsRepository {\n constructor(\n private readonly db: DbExecutor,\n private readonly logger: Logger = noopLogger,\n ) {}\n\n async insert(orgId: string, run: NewAutomationRun): Promise<AutomationRun | null> {\n const [inserted] = await this.db\n .insert(automationRuns)\n .values({\n orgId,\n automationId: run.automationId,\n trigger: run.trigger,\n eventId: run.event.id,\n event: run.event,\n status: run.status,\n actionResults: run.actionResults,\n startedAt: run.startedAt,\n finishedAt: run.finishedAt,\n })\n // A redelivered trigger event hits the unique (org, automation, event) index; no row comes back.\n .onConflictDoNothing({\n target: [automationRuns.orgId, automationRuns.automationId, automationRuns.eventId],\n })\n .returning(runSelection);\n return inserted ? toAutomationRun(inserted) : null;\n }\n\n async findById(orgId: string, id: string): Promise<AutomationRun | null> {\n const [row] = await this.db\n .select(runSelection)\n .from(automationRuns)\n .where(and(eq(automationRuns.orgId, orgId), eq(automationRuns.id, id)))\n .limit(1);\n return row ? toAutomationRun(row) : null;\n }\n\n async restart(orgId: string, id: string, startedAt: Date): Promise<AutomationRun | null> {\n const [restarted] = await this.db\n .update(automationRuns)\n .set({ status: \"running\", actionResults: [], startedAt, finishedAt: null })\n .where(\n and(\n eq(automationRuns.orgId, orgId),\n eq(automationRuns.id, id),\n ne(automationRuns.status, \"running\"),\n ),\n )\n .returning(runSelection);\n return restarted ? toAutomationRun(restarted) : null;\n }\n\n async recordOutcome(\n orgId: string,\n id: string,\n outcome: {\n status: AutomationRun[\"status\"];\n actionResults: AutomationRun[\"actionResults\"];\n finishedAt: Date;\n },\n ): Promise<AutomationRun | null> {\n const [updated] = await this.db\n .update(automationRuns)\n .set({\n status: outcome.status,\n actionResults: outcome.actionResults,\n finishedAt: outcome.finishedAt,\n })\n .where(and(eq(automationRuns.orgId, orgId), eq(automationRuns.id, id)))\n .returning(runSelection);\n return updated ? toAutomationRun(updated) : null;\n }\n\n async list(\n orgId: string,\n automationId: string,\n query: AutomationRunsQuery,\n ): Promise<Page<AutomationRun>> {\n const conditions: SQL[] = [\n eq(automationRuns.orgId, orgId),\n eq(automationRuns.automationId, automationId),\n ];\n if (query.status) {\n conditions.push(eq(automationRuns.status, query.status));\n }\n const where = and(...conditions);\n\n // Sort: `-field` for descending; default to most-recently started first.\n let orderBy: SQL;\n if (query.sort) {\n const isDesc = query.sort.startsWith(\"-\");\n const field = (isDesc ? query.sort.slice(1) : query.sort) as keyof typeof sortColumns;\n const col = sortColumns[field] ?? automationRuns.startedAt;\n orderBy = isDesc ? desc(col) : asc(col);\n } else {\n orderBy = desc(automationRuns.startedAt);\n }\n\n const offset = (query.page - 1) * query.pageSize;\n\n const rows = await this.db\n .select(runSelection)\n .from(automationRuns)\n .where(where)\n .orderBy(orderBy)\n .limit(query.pageSize)\n .offset(offset);\n\n const [{ total } = { total: 0 }] = await this.db\n .select({ total: sql<number>`cast(count(*) as int)` })\n .from(automationRuns)\n .where(where);\n\n return {\n rows: rows.map(toAutomationRun),\n total,\n page: query.page,\n pageSize: query.pageSize,\n };\n }\n}\ntranslateDbErrors(DrizzleAutomationsRepository);\ntranslateDbErrors(DrizzleAutomationRunsRepository);\n"],"mappings":";;;;;;AAyBA,MAAM,sBAAsB;CAC1B,OAAO,YAAY;CACnB,IAAI,YAAY;CAChB,MAAM,YAAY;CAClB,MAAM,YAAY;CAClB,aAAa,YAAY;CACzB,UAAU,YAAY;CACtB,SAAS,YAAY;CACrB,SAAS,YAAY;CACrB,WAAW,YAAY;CACvB,WAAW,YAAY;AACzB;AAeA,SAAS,aAAa,KAAgC;CACpD,OAAO;EACL,OAAO,IAAI;EACX,IAAI,IAAI;EACR,MAAM,IAAI;EACV,MAAM,IAAI;EACV,aAAa,IAAI;EACjB,UAAU,IAAI;EACd,SAAS,IAAI;EACb,SAAS,IAAI;EACb,WAAW,IAAI;EACf,WAAW,IAAI;CACjB;AACF;AAEA,IAAa,+BAAb,MAA2E;CAEtD;CACA;CAFnB,YACE,IACA,SAAkC,YAClC;EAFiB,KAAA,KAAA;EACA,KAAA,SAAA;CAChB;CAEH,MAAM,OAAO,OAAe,OAA2C;EACrE,MAAM,CAAC,YAAY,MAAM,KAAK,GAC3B,OAAO,WAAW,CAAC,CACnB,OAAO;GACN;GACA,MAAM,MAAM;GACZ,MAAM,MAAM;GACZ,aAAa,MAAM;GACnB,UAAU,MAAM;GAChB,SAAS,MAAM;EACjB,CAAC,CAAC,CACD,UAAU,EAAE,IAAI,YAA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,KAAK;EAEd,OAAO;GACL,MAAM,KAAK,IAAI,eAAe;GAC9B;GACA,MAAM,MAAM;GACZ,UAAU,MAAM;EAClB;CACF;AACF;AACA,kBAAkB,4BAA4B;AAC9C,kBAAkB,+BAA+B"}
|
|
1
|
+
{"version":3,"file":"automations.js","names":[],"sources":["../../src/repositories/automations.ts"],"sourcesContent":["// automations — Drizzle repositories (implement the core outbound ports).\n// Org-scoped: every method takes the domain `organizations.id` and constrains\n// its queries to that tenant. `Automation`/`AutomationRun` carry no `orgId`\n// column of their own in the domain type (mirrors the `Entitlement` shape) —\n// `AutomationRun` is the one exception, since it's the run's own `orgId`\n// field the service reads back off `handle`'s event.\nimport { and, arrayContains, asc, desc, eq, gte, ne, sql, type SQL } from \"drizzle-orm\";\nimport type { DbExecutor } from \"../client.js\";\nimport type {\n Automation,\n AutomationKind,\n AutomationRun,\n AutomationRunsQuery,\n AutomationRunsRepository,\n AutomationsRepository,\n NewAutomation,\n NewAutomationRun,\n Page,\n UpdateAutomationInput,\n} from \"@headless-lms/core/automations\";\nimport type { ScheduledAutomation } from \"@headless-lms/core/types\";\nimport { automations, automationRuns } from \"../schema/index.js\";\nimport type { Logger } from \"@headless-lms/core/shared/ports\";\nimport { noopLogger } from \"@headless-lms/core/shared/logger\";\nimport { translateDbErrors } from \"./pg-errors.js\";\n\nconst automationSelection = {\n orgId: automations.orgId,\n id: automations.id,\n name: automations.name,\n kind: automations.kind,\n description: automations.description,\n triggers: automations.triggers,\n actions: automations.actions,\n enabled: automations.enabled,\n filter: automations.filter,\n source: automations.source,\n createdAt: automations.createdAt,\n updatedAt: automations.updatedAt,\n};\n\ninterface AutomationRow {\n orgId: string;\n id: string;\n name: string;\n kind: AutomationKind;\n description: string | null;\n triggers: string[];\n actions: Automation[\"actions\"];\n enabled: boolean;\n filter: Automation[\"filter\"];\n source: Automation[\"source\"];\n createdAt: Date;\n updatedAt: Date;\n}\n\nfunction toAutomation(row: AutomationRow): Automation {\n return {\n orgId: row.orgId,\n id: row.id,\n name: row.name,\n kind: row.kind,\n description: row.description,\n triggers: row.triggers,\n actions: row.actions,\n enabled: row.enabled,\n filter: row.filter,\n source: row.source,\n createdAt: row.createdAt,\n updatedAt: row.updatedAt,\n };\n}\n\nexport class DrizzleAutomationsRepository implements AutomationsRepository {\n constructor(\n private readonly db: DbExecutor,\n private readonly logger: Logger = noopLogger,\n ) {}\n\n async insert(orgId: string, input: NewAutomation): Promise<Automation> {\n const [inserted] = await this.db\n .insert(automations)\n .values({\n orgId,\n name: input.name,\n kind: input.kind,\n description: input.description,\n triggers: input.triggers,\n actions: input.actions,\n filter: input.filter ?? null,\n source: input.source ?? \"user\",\n })\n .returning({ id: automations.id });\n if (!inserted) {\n throw new Error(\"failed to insert automation\");\n }\n const created = await this.findById(orgId, inserted.id);\n if (!created) {\n throw new Error(\"failed to load created automation\");\n }\n return created;\n }\n\n async insertSeed(orgId: string, input: NewAutomation): Promise<Automation | null> {\n const [inserted] = await this.db\n .insert(automations)\n .values({\n orgId,\n name: input.name,\n kind: input.kind,\n description: input.description,\n triggers: input.triggers,\n actions: input.actions,\n filter: input.filter ?? null,\n source: \"system\",\n })\n .onConflictDoNothing()\n .returning({ id: automations.id });\n return inserted ? this.findById(orgId, inserted.id) : null;\n }\n\n async update(\n orgId: string,\n id: string,\n patch: UpdateAutomationInput,\n ): Promise<Automation | null> {\n const set: Record<string, unknown> = { updatedAt: new Date() };\n if (patch.name !== undefined) {\n set.name = patch.name;\n }\n if (patch.description !== undefined) {\n set.description = patch.description;\n }\n if (patch.triggers !== undefined) {\n set.triggers = patch.triggers;\n }\n if (patch.actions !== undefined) {\n set.actions = patch.actions;\n }\n if (patch.enabled !== undefined) {\n set.enabled = patch.enabled;\n }\n if (patch.filter !== undefined) {\n set.filter = patch.filter;\n }\n\n const [updated] = await this.db\n .update(automations)\n .set(set)\n .where(and(eq(automations.orgId, orgId), eq(automations.id, id)))\n .returning({ id: automations.id });\n if (!updated) {\n return null;\n }\n return this.findById(orgId, id);\n }\n\n async delete(orgId: string, id: string): Promise<Automation | null> {\n const [deleted] = await this.db\n .delete(automations)\n .where(and(eq(automations.orgId, orgId), eq(automations.id, id)))\n .returning(automationSelection);\n return deleted ? toAutomation(deleted) : null;\n }\n\n async findById(orgId: string, id: string): Promise<Automation | null> {\n const [row] = await this.db\n .select(automationSelection)\n .from(automations)\n .where(and(eq(automations.orgId, orgId), eq(automations.id, id)))\n .limit(1);\n return row ? toAutomation(row) : null;\n }\n\n async listByOrg(orgId: string, kind?: AutomationKind): Promise<Automation[]> {\n const conditions: SQL[] = [eq(automations.orgId, orgId)];\n if (kind) {\n conditions.push(eq(automations.kind, kind));\n }\n const rows = await this.db\n .select(automationSelection)\n .from(automations)\n .where(and(...conditions))\n .orderBy(desc(automations.createdAt));\n return rows.map(toAutomation);\n }\n\n async listByTrigger(orgId: string, trigger: string): Promise<Automation[]> {\n const rows = await this.db\n .select(automationSelection)\n .from(automations)\n .where(and(eq(automations.orgId, orgId), arrayContains(automations.triggers, [trigger])))\n .orderBy(desc(automations.createdAt));\n return rows.map(toAutomation);\n }\n\n async listSchedules(): Promise<ScheduledAutomation[]> {\n const rows = await this.db\n .select({\n orgId: automations.orgId,\n id: automations.id,\n triggers: automations.triggers,\n })\n .from(automations)\n .where(\n and(\n eq(automations.enabled, true),\n sql`exists (select 1 from unnest(${automations.triggers}) as t(trigger) where t.trigger like 'schedule:%')`,\n ),\n );\n const schedules: ScheduledAutomation[] = [];\n for (const row of rows) {\n for (const trigger of row.triggers) {\n if (!trigger.startsWith(\"schedule:\")) {\n continue;\n }\n schedules.push({\n orgId: row.orgId,\n automationId: row.id,\n cron: trigger.slice(\"schedule:\".length),\n });\n }\n }\n return schedules;\n }\n}\n\nconst runSelection = {\n id: automationRuns.id,\n orgId: automationRuns.orgId,\n automationId: automationRuns.automationId,\n trigger: automationRuns.trigger,\n eventId: automationRuns.eventId,\n event: automationRuns.event,\n status: automationRuns.status,\n actionResults: automationRuns.actionResults,\n startedAt: automationRuns.startedAt,\n finishedAt: automationRuns.finishedAt,\n createdAt: automationRuns.createdAt,\n updatedAt: automationRuns.updatedAt,\n};\n\ninterface RunRow {\n id: string;\n orgId: string;\n automationId: string;\n trigger: string;\n eventId: string;\n event: AutomationRun[\"event\"];\n status: AutomationRun[\"status\"];\n actionResults: AutomationRun[\"actionResults\"];\n startedAt: Date;\n finishedAt: Date | null;\n createdAt: Date;\n updatedAt: Date;\n}\n\nfunction toAutomationRun(row: RunRow): AutomationRun {\n return {\n id: row.id,\n orgId: row.orgId,\n automationId: row.automationId,\n trigger: row.trigger,\n eventId: row.eventId,\n event: row.event,\n status: row.status,\n actionResults: row.actionResults,\n startedAt: row.startedAt,\n finishedAt: row.finishedAt,\n createdAt: row.createdAt,\n updatedAt: row.updatedAt,\n };\n}\n\n// Sortable columns by the client-facing field name. `-field` = desc.\nconst sortColumns = {\n startedAt: automationRuns.startedAt,\n finishedAt: automationRuns.finishedAt,\n status: automationRuns.status,\n} as const;\n\nexport class DrizzleAutomationRunsRepository implements AutomationRunsRepository {\n constructor(\n private readonly db: DbExecutor,\n private readonly logger: Logger = noopLogger,\n ) {}\n\n async insert(orgId: string, run: NewAutomationRun): Promise<AutomationRun | null> {\n const [inserted] = await this.db\n .insert(automationRuns)\n .values({\n orgId,\n automationId: run.automationId,\n trigger: run.trigger,\n eventId: run.event.id,\n event: run.event,\n status: run.status,\n actionResults: run.actionResults,\n startedAt: run.startedAt,\n finishedAt: run.finishedAt,\n })\n // A redelivered trigger event hits the unique (org, automation, event) index; no row comes back.\n .onConflictDoNothing({\n target: [automationRuns.orgId, automationRuns.automationId, automationRuns.eventId],\n })\n .returning(runSelection);\n return inserted ? toAutomationRun(inserted) : null;\n }\n\n async findById(orgId: string, id: string): Promise<AutomationRun | null> {\n const [row] = await this.db\n .select(runSelection)\n .from(automationRuns)\n .where(and(eq(automationRuns.orgId, orgId), eq(automationRuns.id, id)))\n .limit(1);\n return row ? toAutomationRun(row) : null;\n }\n\n async restart(orgId: string, id: string, startedAt: Date): Promise<AutomationRun | null> {\n const [restarted] = await this.db\n .update(automationRuns)\n .set({ status: \"running\", actionResults: [], startedAt, finishedAt: null })\n .where(\n and(\n eq(automationRuns.orgId, orgId),\n eq(automationRuns.id, id),\n ne(automationRuns.status, \"running\"),\n ),\n )\n .returning(runSelection);\n return restarted ? toAutomationRun(restarted) : null;\n }\n\n async recordOutcome(\n orgId: string,\n id: string,\n outcome: {\n status: AutomationRun[\"status\"];\n actionResults: AutomationRun[\"actionResults\"];\n finishedAt: Date;\n },\n ): Promise<AutomationRun | null> {\n const [updated] = await this.db\n .update(automationRuns)\n .set({\n status: outcome.status,\n actionResults: outcome.actionResults,\n finishedAt: outcome.finishedAt,\n })\n .where(and(eq(automationRuns.orgId, orgId), eq(automationRuns.id, id)))\n .returning(runSelection);\n return updated ? toAutomationRun(updated) : null;\n }\n\n async list(\n orgId: string,\n automationId: string,\n query: AutomationRunsQuery,\n ): Promise<Page<AutomationRun>> {\n const conditions: SQL[] = [\n eq(automationRuns.orgId, orgId),\n eq(automationRuns.automationId, automationId),\n ];\n if (query.status) {\n conditions.push(eq(automationRuns.status, query.status));\n }\n const where = and(...conditions);\n\n // Sort: `-field` for descending; default to most-recently started first.\n let orderBy: SQL;\n if (query.sort) {\n const isDesc = query.sort.startsWith(\"-\");\n const field = (isDesc ? query.sort.slice(1) : query.sort) as keyof typeof sortColumns;\n const col = sortColumns[field] ?? automationRuns.startedAt;\n orderBy = isDesc ? desc(col) : asc(col);\n } else {\n orderBy = desc(automationRuns.startedAt);\n }\n\n const offset = (query.page - 1) * query.pageSize;\n\n const rows = await this.db\n .select(runSelection)\n .from(automationRuns)\n .where(where)\n .orderBy(orderBy)\n .limit(query.pageSize)\n .offset(offset);\n\n const [{ total } = { total: 0 }] = await this.db\n .select({ total: sql<number>`cast(count(*) as int)` })\n .from(automationRuns)\n .where(where);\n\n return {\n rows: rows.map(toAutomationRun),\n total,\n page: query.page,\n pageSize: query.pageSize,\n };\n }\n\n async recentRecipients(\n orgId: string,\n automationId: string,\n chatId: string,\n since: Date,\n ): Promise<string[]> {\n const rows = await this.db\n .select({\n actionResults: automationRuns.actionResults,\n })\n .from(automationRuns)\n .where(\n and(\n eq(automationRuns.orgId, orgId),\n eq(automationRuns.automationId, automationId),\n eq(automationRuns.status, \"completed\"),\n gte(automationRuns.finishedAt, since),\n sql`${automationRuns.event}->'data'->>'chatId' = ${chatId}`,\n ),\n );\n const ids: string[] = [];\n for (const row of rows) {\n for (const result of row.actionResults) {\n if (result.recipients) {\n ids.push(...result.recipients);\n }\n }\n }\n return [...new Set(ids)];\n }\n}\ntranslateDbErrors(DrizzleAutomationsRepository);\ntranslateDbErrors(DrizzleAutomationRunsRepository);\n"],"mappings":";;;;;;AA0BA,MAAM,sBAAsB;CAC1B,OAAO,YAAY;CACnB,IAAI,YAAY;CAChB,MAAM,YAAY;CAClB,MAAM,YAAY;CAClB,aAAa,YAAY;CACzB,UAAU,YAAY;CACtB,SAAS,YAAY;CACrB,SAAS,YAAY;CACrB,QAAQ,YAAY;CACpB,QAAQ,YAAY;CACpB,WAAW,YAAY;CACvB,WAAW,YAAY;AACzB;AAiBA,SAAS,aAAa,KAAgC;CACpD,OAAO;EACL,OAAO,IAAI;EACX,IAAI,IAAI;EACR,MAAM,IAAI;EACV,MAAM,IAAI;EACV,aAAa,IAAI;EACjB,UAAU,IAAI;EACd,SAAS,IAAI;EACb,SAAS,IAAI;EACb,QAAQ,IAAI;EACZ,QAAQ,IAAI;EACZ,WAAW,IAAI;EACf,WAAW,IAAI;CACjB;AACF;AAEA,IAAa,+BAAb,MAA2E;CAEtD;CACA;CAFnB,YACE,IACA,SAAkC,YAClC;EAFiB,KAAA,KAAA;EACA,KAAA,SAAA;CAChB;CAEH,MAAM,OAAO,OAAe,OAA2C;EACrE,MAAM,CAAC,YAAY,MAAM,KAAK,GAC3B,OAAO,WAAW,CAAC,CACnB,OAAO;GACN;GACA,MAAM,MAAM;GACZ,MAAM,MAAM;GACZ,aAA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rB,OAAO,EACN,eAAe,eAAe,cAChC,CAAC,CAAC,CACD,KAAK,cAAc,CAAC,CACpB,MACC,IACE,GAAG,eAAe,OAAO,KAAK,GAC9B,GAAG,eAAe,cAAc,YAAY,GAC5C,GAAG,eAAe,QAAQ,WAAW,GACrC,IAAI,eAAe,YAAY,KAAK,GACpC,GAAG,GAAG,eAAe,MAAM,wBAAwB,QACrD,CACF;EACF,MAAM,MAAgB,CAAC;EACvB,KAAK,MAAM,OAAO,MAChB,KAAK,MAAM,UAAU,IAAI,eACvB,IAAI,OAAO,YACT,IAAI,KAAK,GAAG,OAAO,UAAU;EAInC,OAAO,CAAC,GAAG,IAAI,IAAI,GAAG,CAAC;CACzB;AACF;AACA,kBAAkB,4BAA4B;AAC9C,kBAAkB,+BAA+B"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
2
|
+
import { Chat, ChatUser, Logger, Message, MessageFlag, MessageFlagResolution, Page, ReactionEmoji } from "@headless-lms/core/types";
|
|
3
|
+
import { ChatListItem, ChatRepository, FeedQuery, MessageReactions, PageQuery } from "@headless-lms/core/discussion";
|
|
4
|
+
//#region src/repositories/chats.d.ts
|
|
5
|
+
declare class DrizzleChatRepository implements ChatRepository {
|
|
6
|
+
private readonly db;
|
|
7
|
+
private readonly logger;
|
|
8
|
+
constructor(db: NodePgDatabase, logger?: Logger);
|
|
9
|
+
lockDirectPair(orgId: string, communityId: string, orgUserId: string, otherOrgUserId: string): Promise<void>;
|
|
10
|
+
findDirect(orgId: string, communityId: string, orgUserId: string, otherOrgUserId: string): Promise<Chat | null>;
|
|
11
|
+
insertChat(orgId: string, chat: Chat): Promise<Chat>;
|
|
12
|
+
updateChat(orgId: string, id: string, patch: Partial<Pick<Chat, "status" | "lockedAt" | "lockedBy">>): Promise<Chat | null>;
|
|
13
|
+
findChat(orgId: string, id: string): Promise<Chat | null>;
|
|
14
|
+
listForUser(orgId: string, communityId: string, orgUserId: string, query: PageQuery): Promise<Page<ChatListItem>>;
|
|
15
|
+
participants(orgId: string, chatId: string): Promise<ChatUser[]>;
|
|
16
|
+
join(orgId: string, user: ChatUser): Promise<void>;
|
|
17
|
+
markLeft(orgId: string, chatId: string, orgUserId: string, leftAt: Date): Promise<void>;
|
|
18
|
+
setRead(orgId: string, chatId: string, orgUserId: string, messageId: string): Promise<void>;
|
|
19
|
+
setMuted(orgId: string, chatId: string, orgUserId: string, muted: boolean, mutedUntil: Date | null): Promise<void>;
|
|
20
|
+
insertMessage(orgId: string, message: Message): Promise<Message>;
|
|
21
|
+
updateMessage(orgId: string, id: string, patch: Partial<Pick<Message, "body" | "editedAt" | "deletedAt">>): Promise<Message | null>;
|
|
22
|
+
findMessage(orgId: string, id: string): Promise<Message | null>;
|
|
23
|
+
listMessages(orgId: string, chatId: string, query: Pick<FeedQuery, "page" | "pageSize">): Promise<Page<Message>>;
|
|
24
|
+
attach(orgId: string, communityId: string, messageId: string, assetIds: string[]): Promise<void>;
|
|
25
|
+
attachmentsOf(orgId: string, messageIds: string[]): Promise<Record<string, string[]>>;
|
|
26
|
+
setReaction(orgId: string, communityId: string, messageId: string, orgUserId: string, emoji: ReactionEmoji | null): Promise<void>;
|
|
27
|
+
reactionsOf(orgId: string, messageIds: string[], viewerId: string): Promise<Record<string, MessageReactions>>;
|
|
28
|
+
block(orgId: string, communityId: string, blockerOrgUserId: string, blockedOrgUserId: string): Promise<void>;
|
|
29
|
+
unblock(orgId: string, communityId: string, blockerOrgUserId: string, blockedOrgUserId: string): Promise<void>;
|
|
30
|
+
isBlocked(orgId: string, communityId: string, a: string, b: string): Promise<boolean>;
|
|
31
|
+
anyBlockedAmong(orgId: string, communityId: string, orgUserIds: string[]): Promise<boolean>;
|
|
32
|
+
blockedWith(orgId: string, communityId: string, orgUserId: string, otherIds: string[]): Promise<string[]>;
|
|
33
|
+
insertFlag(orgId: string, flag: MessageFlag): Promise<MessageFlag | null>;
|
|
34
|
+
findFlag(orgId: string, messageId: string, orgUserId: string): Promise<MessageFlag | null>;
|
|
35
|
+
listOpenFlags(orgId: string, communityId: string, limit?: number): Promise<MessageFlag[]>;
|
|
36
|
+
messagesByIds(orgId: string, ids: string[]): Promise<Message[]>;
|
|
37
|
+
chatSummaries(orgId: string, chatIds: string[]): Promise<{
|
|
38
|
+
chat: Chat;
|
|
39
|
+
participantIds: string[];
|
|
40
|
+
}[]>;
|
|
41
|
+
resolveOpenFlagsForChat(orgId: string, chatId: string, resolved: {
|
|
42
|
+
resolvedAt: Date;
|
|
43
|
+
resolvedBy: string;
|
|
44
|
+
resolution: MessageFlagResolution;
|
|
45
|
+
}): Promise<void>;
|
|
46
|
+
resolveFlags(orgId: string, messageId: string, resolved: {
|
|
47
|
+
resolvedAt: Date;
|
|
48
|
+
resolvedBy: string;
|
|
49
|
+
resolution: MessageFlagResolution;
|
|
50
|
+
}): Promise<void>;
|
|
51
|
+
private touch;
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
export { DrizzleChatRepository };
|
|
55
|
+
//# sourceMappingURL=chats.d.ts.map
|