Skip to content

Commit f31833c

Browse files
authored
feat: prepare organization enrichment and cache tables (CM-685) (#3422)
1 parent a60ab3a commit f31833c

5 files changed

Lines changed: 69 additions & 1 deletion

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
drop table "organizationEnrichments";
2+
drop table "organizationEnrichmentCache";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
create table "organizationEnrichments"(
2+
"organizationId" uuid primary key,
3+
"lastTriedAt" timestamp with time zone,
4+
"lastUpdatedAt" timestamp with time zone
5+
);
6+
7+
create table "organizationEnrichmentCache" (
8+
"organizationId" uuid not null references organizations(id) on delete no action on update no action,
9+
"data" jsonb not null,
10+
"source" text not null,
11+
"createdAt" timestamp with time zone not null,
12+
"updatedAt" timestamp with time zone not null,
13+
primary key ("organizationId", source)
14+
);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { IOrganizationEnrichmentCache, OrganizationEnrichmentSource } from '@crowd/types'
2+
3+
import { QueryExecutor } from '../../queryExecutor'
4+
5+
export async function insertOrganizationEnrichmentCache<T>(
6+
qx: QueryExecutor,
7+
organizationId: string,
8+
data: T,
9+
source: OrganizationEnrichmentSource,
10+
): Promise<void> {
11+
await qx.result(
12+
`insert into "organizationEnrichmentCache" ("organizationId", "data", "source", "createdAt", "updatedAt")
13+
values ($(organizationId), $(data), $(source), now(), now())
14+
on conflict ("organizationId", "source") do update set "data" = $(data), "updatedAt" = now()`,
15+
{ organizationId, data, source },
16+
)
17+
}
18+
19+
export async function findOrganizationEnrichmentCache<T>(
20+
qx: QueryExecutor,
21+
organizationId: string,
22+
source: OrganizationEnrichmentSource,
23+
): Promise<IOrganizationEnrichmentCache<T> | null> {
24+
return qx.selectOneOrNone(
25+
`select * from "organizationEnrichmentCache" where "organizationId" = $(organizationId) and "source" = $(source)`,
26+
{ organizationId, source },
27+
)
28+
}
29+
30+
export async function updateOrganizationEnrichmentCache<T>(
31+
qx: QueryExecutor,
32+
organizationId: string,
33+
data: T,
34+
source: OrganizationEnrichmentSource,
35+
): Promise<void> {
36+
await qx.result(
37+
`update "organizationEnrichmentCache" set "data" = $(data), "updatedAt" = now() where "organizationId" = $(organizationId) and "source" = $(source)`,
38+
{ organizationId, data, source },
39+
)
40+
}

services/libs/types/src/enrichment.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MemberEnrichmentSource } from './enums'
1+
import { MemberEnrichmentSource, OrganizationEnrichmentSource } from './enums'
22
import { IMemberIdentity, IMemberReach } from './members'
33
import { IOrganizationIdentity } from './organizations'
44

@@ -55,3 +55,11 @@ export interface IMemberOriginalData {
5555
// memberOrganizations table data
5656
organizations: IMemberOrganizationData[]
5757
}
58+
59+
export interface IOrganizationEnrichmentCache<T> {
60+
createdAt: string
61+
updatedAt: string
62+
organizationId: string
63+
data: T
64+
source: OrganizationEnrichmentSource
65+
}

services/libs/types/src/enums/enrichment.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ export enum MemberEnrichmentMaterializedView {
1717
ENTITY_UPDATES_ANALYSIS = 'memberEnrichmentMonitoringEntityUpdates',
1818
LLM_USAGE_ANALYSIS = 'memberEnrichmentMonitoringLLMQueries',
1919
}
20+
21+
export enum OrganizationEnrichmentSource {
22+
INTERNAL_API = 'internal-api',
23+
}

0 commit comments

Comments
 (0)