Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(soup): query items associated with CRM records (#7279)

teo7e9838cce8fa
diff --git a/.sqlx/query-a3ac43b571a925e4bb9f341bcdd24a1b2c17d58c04b718484c659b76f1e0c8c3.json b/.sqlx/query-a3ac43b571a925e4bb9f341bcdd24a1b2c17d58c04b718484c659b76f1e0c8c3.json
deleted file mode 100644
index b98d5e9ecf..0000000000
--- a/.sqlx/query-a3ac43b571a925e4bb9f341bcdd24a1b2c17d58c04b718484c659b76f1e0c8c3.json
+++ /dev/null
@@ -1,140 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH user_source_ids AS (\n SELECT cp.channel_id::text AS source_id\n FROM comms_channel_participants cp\n WHERE cp.user_id = $1 AND cp.left_at IS NULL\n UNION ALL\n SELECT t.team_id::text AS source_id\n FROM team_user t\n WHERE t.user_id = $1\n UNION ALL\n SELECT $1 AS source_id\n ),\n visible_calls AS (\n SELECT\n c.id AS call_id,\n c.channel_id,\n c.room_name,\n c.created_by,\n c.created_at AS started_at,\n NULL::timestamptz AS ended_at,\n NULL::bigint AS duration_ms,\n c.egress_id,\n c.recording_key,\n c.preview_url,\n c.recording_started_at,\n (SELECT title FROM call_meetings WHERE id = c.meeting_id) AS custom_name,\n NULL::text AS summary,\n c.share_with_team,\n sp.team_share_access_level,\n true AS is_active,\n CASE\n WHEN EXISTS (\n SELECT 1 FROM call_participants cp\n WHERE cp.call_id = c.id AND cp.user_id = $1\n ) THEN 'ATTENDED'::text\n WHEN EXISTS (\n SELECT 1 FROM comms_channel_participants ccp\n WHERE ccp.channel_id = c.channel_id\n AND ccp.user_id = $1\n AND ccp.left_at IS NULL\n ) THEN 'MISSED'::text\n ELSE 'UNATTENDED'::text\n END AS status\n FROM calls c\n JOIN \"SharePermission\" sp ON sp.id = c.share_permission_id\n WHERE EXISTS (\n SELECT 1 FROM entity_access ea\n JOIN user_source_ids u ON u.source_id = ea.source_id\n WHERE ea.entity_id = c.id\n AND ea.entity_type = 'call'\n AND (c.channel_id IS NOT NULL OR (ea.source_type = 'user' AND ea.source_id = $1))\n )\n AND ($3::bool IS FALSE OR c.channel_id = ANY($4))\n AND ($5::bool IS FALSE OR c.id = ANY($6))\n AND ($9::bool IS FALSE OR (\n ($11::bool IS FALSE AND EXISTS (\n SELECT 1 FROM entity_properties ep\n WHERE ep.entity_id = c.id::text\n AND ep.entity_type = 'CALL_RECORD'\n AND jsonb_typeof(ep.values -> 'value') = 'array'\n AND jsonb_exists_any(ep.values -> 'value', $10::text[])\n ))\n OR ($11::bool IS TRUE AND $10::text[] <@ (\n SELECT COALESCE(array_agg(elem), ARRAY[]::text[])\n FROM (\n SELECT values FROM entity_properties\n WHERE entity_id = c.id::text\n AND entity_type = 'CALL_RECORD'\n AND jsonb_typeof(values -> 'value') = 'array'\n ) ep\n CROSS JOIN LATERAL jsonb_array_elements_text(ep.values -> 'value') AS elem\n ))\n ))\n UNION ALL\n SELECT\n cr.id AS call_id,\n cr.channel_id,\n cr.room_name,\n cr.created_by,\n cr.started_at,\n cr.ended_at,\n cr.duration_ms,\n cr.egress_id,\n cr.recording_key,\n cr.preview_url,\n cr.recording_started_at,\n cr.custom_name,\n cr.summary,\n (sp.team_share_access_level IS NOT NULL) AS share_with_team,\n sp.team_share_access_level,\n false AS is_active,\n CASE\n WHEN EXISTS (\n SELECT 1 FROM call_record_participants crp\n WHERE crp.call_record_id = cr.id AND crp.user_id = $1\n ) THEN 'ATTENDED'::text\n WHEN EXISTS (\n SELECT 1 FROM comms_channel_participants ccp\n WHERE ccp.channel_id = cr.channel_id\n AND ccp.user_id = $1\n AND ccp.left_at IS NULL\n ) THEN 'MISSED'::text\n ELSE 'UNATTENDED'::text\n END AS status\n FROM call_records cr\n JOIN \"SharePermission\" sp ON sp.id = cr.share_permission_id\n WHERE EXISTS (\n SELECT 1 FROM entity_access ea\n JOIN user_source_ids u ON u.source_id = ea.source_id\n WHERE ea.entity_id = cr.id\n AND ea.entity_type = 'call'\n AND (cr.channel_id IS NOT NULL OR (ea.source_type = 'user' AND ea.source_id = $1))\n )\n AND ($3::bool IS FALSE OR cr.channel_id = ANY($4))\n AND ($5::bool IS FALSE OR cr.id = ANY($6))\n AND ($9::bool IS FALSE OR (\n ($11::bool IS FALSE AND EXISTS (\n SELECT 1 FROM entity_properties ep\n WHERE ep.entity_id = cr.id::text\n AND ep.entity_type = 'CALL_RECORD'\n AND jsonb_typeof(ep.values -> 'value') = 'array'\n AND jsonb_exists_any(ep.values -> 'value', $10::text[])\n ))\n OR ($11::bool IS TRUE AND $10::text[] <@ (\n SELECT COALESCE(array_agg(elem), ARRAY[]::text[])\n FROM (\n SELECT values FROM entity_properties\n WHERE entity_id = cr.id::text\n AND entity_type = 'CALL_RECORD'\n AND jsonb_typeof(values -> 'value') = 'array'\n ) ep\n CROSS JOIN LATERAL jsonb_array_elements_text(ep.values -> 'value') AS elem\n ))\n ))\n )\n SELECT\n call_id as \"call_id!\",\n channel_id as \"channel_id?\",\n room_name as \"room_name!\",\n created_by as \"created_by!\",\n started_at as \"started_at!\",\n ended_at,\n duration_ms,\n egress_id,\n recording_key,\n preview_url,\n recording_started_at,\n custom_name,\n summary,\n share_with_team as \"share_with_team!\",\n team_share_access_level as \"team_share_access_level?: AccessLevel\",\n is_active as \"is_active!\",\n status as \"status!\"\n FROM visible_calls\n WHERE ($7::bool IS FALSE OR status = ANY($8::text[]))\n ORDER BY \"started_at!\" DESC\n LIMIT $2\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "call_id!",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "channel_id?",
- "type_info": "Uuid"
- },
- {
- "ordinal": 2,
- "name": "room_name!",
- "type_info": "Text"
- },
- {
- "ordinal": 3,
- "name": "created_by!",
- "type_info": "Text"
- },
- {
- "ordinal": 4,
- "name": "started_at!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 5,
- "name": "ended_at",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 6,
- "name": "duration_ms",
- "type_info": "Int8"
- },
- {
- "ordinal": 7,
- "name": "egress_id",
- "type_info": "Text"
- },
- {
- "ordinal": 8,
- "name": "recording_key",
- "type_info": "Text"
- },
- {
- "ordinal": 9,
- "name": "preview_url",
- "type_info": "Text"
- },
- {
- "ordinal": 10,
- "name": "recording_started_at",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 11,
- "name": "custom_name",
- "type_info": "Text"
- },
- {
- "ordinal": 12,
- "name": "summary",
- "type_info": "Text"
- },
- {
- "ordinal": 13,
- "name": "share_with_team!",
- "type_info": "Bool"
- },
- {
- "ordinal": 14,
- "name": "team_share_access_level?: AccessLevel",
- "type_info": {
- "Custom": {
- "name": "\"AccessLevel\"",
- "kind": {
- "Enum": [
- "view",
- "comment",
- "edit",
- "owner"
- ]
- }
- }
- }
- },
- {
- "ordinal": 15,
- "name": "is_active!",
- "type_info": "Bool"
- },
- {
- "ordinal": 16,
- "name": "status!",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Text",
- "Int8",
- "Bool",
- "UuidArray",
- "Bool",
- "UuidArray",
- "Bool",
- "TextArray",
- "Bool",
- "TextArray",
- "Bool"
- ]
- },
- "nullable": [
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null
- ]
- },
- "hash": "a3ac43b571a925e4bb9f341bcdd24a1b2c17d58c04b718484c659b76f1e0c8c3"
-}
diff --git a/.sqlx/query-e1ebe683de73f15ff6895e08b71c93ef2134b40434bab28c7e497869165cb76a.json b/.sqlx/query-e1ebe683de73f15ff6895e08b71c93ef2134b40434bab28c7e497869165cb76a.json
new file mode 100644
index 0000000000..dad6bb5ddd
--- /dev/null
+++ b/.sqlx/query-e1ebe683de73f15ff6895e08b71c93ef2134b40434bab28c7e497869165cb76a.json
@@ -0,0 +1,142 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH user_source_ids AS (\n SELECT cp.channel_id::text AS source_id\n FROM comms_channel_participants cp\n WHERE cp.user_id = $1 AND cp.left_at IS NULL\n UNION ALL\n SELECT t.team_id::text AS source_id\n FROM team_user t\n WHERE t.user_id = $1\n UNION ALL\n SELECT $1 AS source_id\n ),\n visible_calls AS (\n SELECT\n c.id AS call_id,\n c.channel_id,\n c.room_name,\n c.created_by,\n c.created_at AS started_at,\n NULL::timestamptz AS ended_at,\n NULL::bigint AS duration_ms,\n c.egress_id,\n c.recording_key,\n c.preview_url,\n c.recording_started_at,\n (SELECT title FROM call_meetings WHERE id = c.meeting_id) AS custom_name,\n NULL::text AS summary,\n c.share_with_team,\n sp.team_share_access_level,\n true AS is_active,\n CASE\n WHEN EXISTS (\n SELECT 1 FROM call_participants cp\n WHERE cp.call_id = c.id AND cp.user_id = $1\n ) THEN 'ATTENDED'::text\n WHEN EXISTS (\n SELECT 1 FROM comms_channel_participants ccp\n WHERE ccp.channel_id = c.channel_id\n AND ccp.user_id = $1\n AND ccp.left_at IS NULL\n ) THEN 'MISSED'::text\n ELSE 'UNATTENDED'::text\n END AS status\n FROM calls c\n JOIN \"SharePermission\" sp ON sp.id = c.share_permission_id\n WHERE EXISTS (\n SELECT 1 FROM entity_access ea\n JOIN user_source_ids u ON u.source_id = ea.source_id\n WHERE ea.entity_id = c.id\n AND ea.entity_type = 'call'\n AND (c.channel_id IS NOT NULL OR (ea.source_type = 'user' AND ea.source_id = $1))\n )\n AND ($3::bool IS FALSE OR c.channel_id = ANY($4))\n AND ($5::bool IS FALSE OR c.id = ANY($6))\n -- Property conditions in conjunctive normal form: every\n -- clause needs a term the call satisfies. Tags (no\n -- definition) match any property value; entity references\n -- match their definition's value.\n AND NOT EXISTS (\n SELECT 1\n FROM generate_series(0, $9::int - 1) AS clause(idx)\n WHERE NOT EXISTS (\n SELECT 1\n FROM UNNEST($10::int[], $11::bool[], $12::uuid[], $13::text[])\n AS term(clause_idx, negated, definition_id, value)\n WHERE term.clause_idx = clause.idx\n AND term.negated <> EXISTS (\n SELECT 1 FROM entity_properties ep\n WHERE ep.entity_id = c.id::text\n AND ep.entity_type = 'CALL_RECORD'\n AND jsonb_typeof(ep.values -> 'value') = 'array'\n AND CASE\n WHEN term.definition_id IS NULL\n THEN jsonb_exists(ep.values -> 'value', term.value)\n ELSE ep.property_definition_id = term.definition_id\n AND ep.values -> 'value' @> jsonb_build_array(\n jsonb_build_object('entity_id', term.value)\n )\n END\n )\n )\n )\n UNION ALL\n SELECT\n cr.id AS call_id,\n cr.channel_id,\n cr.room_name,\n cr.created_by,\n cr.started_at,\n cr.ended_at,\n cr.duration_ms,\n cr.egress_id,\n cr.recording_key,\n cr.preview_url,\n cr.recording_started_at,\n cr.custom_name,\n cr.summary,\n (sp.team_share_access_level IS NOT NULL) AS share_with_team,\n sp.team_share_access_level,\n false AS is_active,\n CASE\n WHEN EXISTS (\n SELECT 1 FROM call_record_participants crp\n WHERE crp.call_record_id = cr.id AND crp.user_id = $1\n ) THEN 'ATTENDED'::text\n WHEN EXISTS (\n SELECT 1 FROM comms_channel_participants ccp\n WHERE ccp.channel_id = cr.channel_id\n AND ccp.user_id = $1\n AND ccp.left_at IS NULL\n ) THEN 'MISSED'::text\n ELSE 'UNATTENDED'::text\n END AS status\n FROM call_records cr\n JOIN \"SharePermission\" sp ON sp.id = cr.share_permission_id\n WHERE EXISTS (\n SELECT 1 FROM entity_access ea\n JOIN user_source_ids u ON u.source_id = ea.source_id\n WHERE ea.entity_id = cr.id\n AND ea.entity_type = 'call'\n AND (cr.channel_id IS NOT NULL OR (ea.source_type = 'user' AND ea.source_id = $1))\n )\n AND ($3::bool IS FALSE OR cr.channel_id = ANY($4))\n AND ($5::bool IS FALSE OR cr.id = ANY($6))\n -- Property conditions in conjunctive normal form: every\n -- clause needs a term the call satisfies. Tags (no\n -- definition) match any property value; entity references\n -- match their definition's value.\n AND NOT EXISTS (\n SELECT 1\n FROM generate_series(0, $9::int - 1) AS clause(idx)\n WHERE NOT EXISTS (\n SELECT 1\n FROM UNNEST($10::int[], $11::bool[], $12::uuid[], $13::text[])\n AS term(clause_idx, negated, definition_id, value)\n WHERE term.clause_idx = clause.idx\n AND term.negated <> EXISTS (\n SELECT 1 FROM entity_properties ep\n WHERE ep.entity_id = cr.id::text\n AND ep.entity_type = 'CALL_RECORD'\n AND jsonb_typeof(ep.values -> 'value') = 'array'\n AND CASE\n WHEN term.definition_id IS NULL\n THEN jsonb_exists(ep.values -> 'value', term.value)\n ELSE ep.property_definition_id = term.definition_id\n AND ep.values -> 'value' @> jsonb_build_array(\n jsonb_build_object('entity_id', term.value)\n )\n END\n )\n )\n )\n )\n SELECT\n call_id as \"call_id!\",\n channel_id as \"channel_id?\",\n room_name as \"room_name!\",\n created_by as \"created_by!\",\n started_at as \"started_at!\",\n ended_at,\n duration_ms,\n egress_id,\n recording_key,\n preview_url,\n recording_started_at,\n custom_name,\n summary,\n share_with_team as \"share_with_team!\",\n team_share_access_level as \"team_share_access_level?: AccessLevel\",\n is_active as \"is_active!\",\n status as \"status!\"\n FROM visible_calls\n WHERE ($7::bool IS FALSE OR status = ANY($8::text[]))\n ORDER BY \"started_at!\" DESC\n LIMIT $2\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "call_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "channel_id?",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "room_name!",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "created_by!",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "started_at!",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 5,
+ "name": "ended_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 6,
+ "name": "duration_ms",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 7,
+ "name": "egress_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 8,
+ "name": "recording_key",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 9,
+ "name": "preview_url",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 10,
+ "name": "recording_started_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 11,
+ "name": "custom_name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 12,
+ "name": "summary",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 13,
+ "name": "share_with_team!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 14,
+ "name": "team_share_access_level?: AccessLevel",
+ "type_info": {
+ "Custom": {
+ "name": "\"AccessLevel\"",
+ "kind": {
+ "Enum": [
+ "view",
+ "comment",
+ "edit",
+ "owner"
+ ]
+ }
+ }
+ }
+ },
+ {
+ "ordinal": 15,
+ "name": "is_active!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 16,
+ "name": "status!",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Int8",
+ "Bool",
+ "UuidArray",
+ "Bool",
+ "UuidArray",
+ "Bool",
+ "TextArray",
+ "Int4",
+ "Int4Array",
+ "BoolArray",
+ "UuidArray",
+ "TextArray"
+ ]
+ },
+ "nullable": [
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null
+ ]
+ },
+ "hash": "e1ebe683de73f15ff6895e08b71c93ef2134b40434bab28c7e497869165cb76a"
+}
diff --git a/apps/web/src/lib/queries/soup/graphql/ast.test.ts b/apps/web/src/lib/queries/soup/graphql/ast.test.ts
index 1b23a59915..090b084903 100644
--- a/apps/web/src/lib/queries/soup/graphql/ast.test.ts
+++ b/apps/web/src/lib/queries/soup/graphql/ast.test.ts
@@ -361,6 +361,51 @@ describe('makeGraphqlSoupInput', () => {
});
});
+ it('translates CRM association and email attachment literals', () => {
+ const companyId = '0198a1b2-c3d4-7e5f-8061-728394a5b700';
+ expect(
+ makeGraphqlSoupInput({
+ params: { limit: 100, sort_method: 'updated_at' },
+ body: {
+ df: {
+ '|': [
+ {
+ l: {
+ prop: {
+ pd: '00000001-0000-0000-0000-00000000000c',
+ v: { er: companyId },
+ },
+ },
+ },
+ { l: { eap: { Domain: 'acme.com' } } },
+ ],
+ },
+ } as never,
+ })
+ ).toMatchObject({
+ initial: {
+ filters: {
+ documentFilter: {
+ or: {
+ left: {
+ literal: {
+ property: {
+ propertyDefinitionId:
+ '00000001-0000-0000-0000-00000000000c',
+ value: { entityRef: companyId },
+ },
+ },
+ },
+ right: {
+ literal: { emailAttachmentParticipant: { domain: 'acme.com' } },
+ },
+ },
+ },
+ },
+ },
+ });
+ });
+
it('throws for unsupported calendar literals so callers can fall back', () => {
expect(() =>
makeGraphqlSoupInput({
diff --git a/apps/web/src/lib/queries/soup/graphql/ast.ts b/apps/web/src/lib/queries/soup/graphql/ast.ts
index 55fa024a69..24a4185cd5 100644
--- a/apps/web/src/lib/queries/soup/graphql/ast.ts
+++ b/apps/web/src/lib/queries/soup/graphql/ast.ts
@@ -299,6 +299,10 @@ function mapDocumentLiteral(literal: unknown): GraphqlDocumentLiteralInput {
return { createdAt: mapDateLiteral(value) };
case 'ua':
return { updatedAt: mapDateLiteral(value) };
+ case 'prop':
+ return { property: mapPropertiesLiteral(value) };
+ case 'eap':
+ return { emailAttachmentParticipant: mapEmailValue(value) };
default:
unsupported(`document literal ${field}`);
}
diff --git a/apps/web/src/lib/service-clients/service-storage/graphql/generated/graphql.ts b/apps/web/src/lib/service-clients/service-storage/graphql/generated/graphql.ts
index d0441854ce..ebb3c4d45b 100644
--- a/apps/web/src/lib/service-clients/service-storage/graphql/generated/graphql.ts
+++ b/apps/web/src/lib/service-clients/service-storage/graphql/generated/graphql.ts
@@ -462,31 +462,35 @@ export type GraphqlDocumentExpr =
/** GraphQL input representing the document literal. */
export type GraphqlDocumentLiteral =
{ /** The created at option. */
- createdAt: GraphqlDateLiteral; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; subType?: never; updatedAt?: never; }
- | { createdAt?: never; /** The file assoc option. */
- fileAssoc: string; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; subType?: never; updatedAt?: never; }
- | { createdAt?: never; fileAssoc?: never; /** The file type option. */
- fileType: string; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; subType?: never; updatedAt?: never; }
- | { createdAt?: never; fileAssoc?: never; fileType?: never; /** The id option. */
- id: string | number; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; subType?: never; updatedAt?: never; }
- | { createdAt?: never; fileAssoc?: never; fileType?: never; id?: never; /** The importance option. */
- importance: boolean; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; subType?: never; updatedAt?: never; }
- | { createdAt?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; /** The include cbm atm nc option. */
- includeCbmAtmNc: boolean; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; subType?: never; updatedAt?: never; }
- | { createdAt?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; /** The is email attachment option. */
- isEmailAttachment: boolean; notificationState?: never; owner?: never; projectId?: never; subType?: never; updatedAt?: never; }
- | { createdAt?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; /** Exact notification state for the requester. */
- notificationState: NotificationState; owner?: never; projectId?: never; subType?: never; updatedAt?: never; }
- | { createdAt?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; /**
+ createdAt: GraphqlDateLiteral; emailAttachmentParticipant?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; property?: never; subType?: never; updatedAt?: never; }
+ | { createdAt?: never; /** Uploaded from an email attachment sent by, or to, a matching address. */
+ emailAttachmentParticipant: GraphqlEmailValue; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; property?: never; subType?: never; updatedAt?: never; }
+ | { createdAt?: never; emailAttachmentParticipant?: never; /** The file assoc option. */
+ fileAssoc: string; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; property?: never; subType?: never; updatedAt?: never; }
+ | { createdAt?: never; emailAttachmentParticipant?: never; fileAssoc?: never; /** The file type option. */
+ fileType: string; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; property?: never; subType?: never; updatedAt?: never; }
+ | { createdAt?: never; emailAttachmentParticipant?: never; fileAssoc?: never; fileType?: never; /** The id option. */
+ id: string | number; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; property?: never; subType?: never; updatedAt?: never; }
+ | { createdAt?: never; emailAttachmentParticipant?: never; fileAssoc?: never; fileType?: never; id?: never; /** The importance option. */
+ importance: boolean; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; property?: never; subType?: never; updatedAt?: never; }
+ | { createdAt?: never; emailAttachmentParticipant?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; /** The include cbm atm nc option. */
+ includeCbmAtmNc: boolean; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; property?: never; subType?: never; updatedAt?: never; }
+ | { createdAt?: never; emailAttachmentParticipant?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; /** The is email attachment option. */
+ isEmailAttachment: boolean; notificationState?: never; owner?: never; projectId?: never; property?: never; subType?: never; updatedAt?: never; }
+ | { createdAt?: never; emailAttachmentParticipant?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; /** Exact notification state for the requester. */
+ notificationState: NotificationState; owner?: never; projectId?: never; property?: never; subType?: never; updatedAt?: never; }
+ | { createdAt?: never; emailAttachmentParticipant?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; /**
* The owner principal option — a user (`macro|<email>`), a bot
* (`bot|<uuid>`), or a team (a bare hyphenated uuid).
*/
- owner: string; projectId?: never; subType?: never; updatedAt?: never; }
- | { createdAt?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; /** The project id option. */
- projectId: string | number; subType?: never; updatedAt?: never; }
- | { createdAt?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; /** The sub type option. */
+ owner: string; projectId?: never; property?: never; subType?: never; updatedAt?: never; }
+ | { createdAt?: never; emailAttachmentParticipant?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; /** The project id option. */
+ projectId: string | number; property?: never; subType?: never; updatedAt?: never; }
+ | { createdAt?: never; emailAttachmentParticipant?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; /** An entity-property condition on the document or task. */
+ property: GraphqlFilterPropertiesLiteral; subType?: never; updatedAt?: never; }
+ | { createdAt?: never; emailAttachmentParticipant?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; property?: never; /** The sub type option. */
subType: GraphqlDocumentSubType; updatedAt?: never; }
- | { createdAt?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; subType?: never; /** The updated at option. */
+ | { createdAt?: never; emailAttachmentParticipant?: never; fileAssoc?: never; fileType?: never; id?: never; importance?: never; includeCbmAtmNc?: never; isEmailAttachment?: never; notificationState?: never; owner?: never; projectId?: never; property?: never; subType?: never; /** The updated at option. */
updatedAt: GraphqlDateLiteral; };
/** GraphQL input representing the document sub type. */
diff --git a/crates/call/src/outbound/pg_call_repo.rs b/crates/call/src/outbound/pg_call_repo.rs
index 4890d630ef..03e6959f17 100644
--- a/crates/call/src/outbound/pg_call_repo.rs
+++ b/crates/call/src/outbound/pg_call_repo.rs
@@ -4,6 +4,7 @@ mod edit;
mod lifecycle;
mod meetings;
mod preparations;
+mod property_clauses;
mod team_share;
#[cfg(test)]
@@ -19,7 +20,7 @@ use entity_access::domain::models::AccessLevel;
use filter_ast::Expr;
use item_filters::{
CallStatus,
- ast::{LiteralTree, call::CallLiteral, properties::PropertyMatchValue},
+ ast::{LiteralTree, call::CallLiteral},
};
use macro_user_id::{cowlike::CowLike, user_id::MacroUserIdStr};
use models_permissions::share_permission::team_share::TeamShareFacts;
@@ -37,6 +38,7 @@ use crate::domain::models::{
TranscriptSegmentRequest, WithCallId,
};
use crate::domain::ports::CallRepository;
+use property_clauses::{clause_params, property_clauses};
/// Name of the partial unique index enforcing one active call per user.
const ACTIVE_CALL_UNIQUE_INDEX: &str = "call_participants_one_active_per_user";
@@ -71,7 +73,7 @@ fn collect_channel_ids(expr: &Expr<CallLiteral>, ids: &mut Vec<Uuid>) {
Expr::Literal(CallLiteral::Attended(_)) => {}
// Speaker is transcript-segment-only; soup's call list ignores it.
Expr::Literal(CallLiteral::Speaker(_)) => {}
- // Tag/property conditions are handled by `extract_tag_option_ids`.
+ // Tag/property conditions are handled by `property_clauses`.
Expr::Literal(CallLiteral::Property(_)) => {}
Expr::And(a, b) | Expr::Or(a, b) => {
collect_channel_ids(a, ids);
@@ -91,61 +93,6 @@ fn extract_call_ids(filter: &LiteralTree<CallLiteral>) -> Vec<Uuid> {
ids
}
-/// Extract tag option ids (select-option UUIDs) from `CallLiteral::Property`
-/// literals. Tags are def-less: a call matches if any of its property values
-/// contains one of these option ids, mirroring the search-service tag filter.
-/// Structure (AND/OR/NOT) is flattened — soup only folds in a positive OR of
-/// tag literals, so a flat "any of" match is exact.
-fn extract_tag_option_ids(filter: &LiteralTree<CallLiteral>) -> Vec<String> {
- let Some(expr) = filter else {
- return Vec::new();
- };
- let mut ids = Vec::new();
- collect_tag_option_ids(expr, &mut ids);
- ids
-}
-
-fn collect_tag_option_ids(expr: &Expr<CallLiteral>, ids: &mut Vec<String>) {
- match expr {
- Expr::Literal(CallLiteral::Property(lit)) => {
- if let PropertyMatchValue::SelectOption(option_id) = &lit.value {
- ids.push(option_id.to_string());
- }
- }
- Expr::Literal(_) => {}
- Expr::And(a, b) | Expr::Or(a, b) => {
- collect_tag_option_ids(a, ids);
- collect_tag_option_ids(b, ids);
- }
- Expr::Not(inner) => collect_tag_option_ids(inner, ids),
- }
-}
-
-/// Whether the tag filter requires ALL of its options rather than ANY. The
-/// tag filter is folded in as an OR of tag literals for ANY and an AND of tag
-/// literals for ALL (matching the search index's `match_all_tags`), so an
-/// `And` joining two tag-bearing branches marks ALL. A single option reads as
-/// ANY, which is equivalent.
-fn tag_filter_requires_all(filter: &LiteralTree<CallLiteral>) -> bool {
- fn contains_tag(expr: &Expr<CallLiteral>) -> bool {
- match expr {
- Expr::Literal(CallLiteral::Property(_)) => true,
- Expr::Literal(_) => false,
- Expr::And(a, b) | Expr::Or(a, b) => contains_tag(a) || contains_tag(b),
- Expr::Not(inner) => contains_tag(inner),
- }
- }
- fn walk(expr: &Expr<CallLiteral>) -> bool {
- match expr {
- Expr::And(a, b) => (contains_tag(a) && contains_tag(b)) || walk(a) || walk(b),
- Expr::Or(a, b) => walk(a) || walk(b),
- Expr::Not(inner) => walk(inner),
- Expr::Literal(_) => false,
- }
- }
- filter.as_ref().is_some_and(|expr| walk(expr))
-}
-
fn collect_call_ids(expr: &Expr<CallLiteral>, ids: &mut Vec<Uuid>) {
match expr {
Expr::Literal(CallLiteral::CallId(id)) => ids.push(*id),
@@ -1618,9 +1565,7 @@ impl CallRepository for PgCallRepo {
.map(call_status_sql_value)
.map(str::to_string)
.collect();
- let tag_option_ids = extract_tag_option_ids(filter);
- let has_tag_filter = !tag_option_ids.is_empty();
- let match_all_tags = tag_filter_requires_all(filter);
+ let property = clause_params(&property_clauses(filter));
let rows = sqlx::query!(
r#"
@@ -1677,25 +1622,34 @@ impl CallRepository for PgCallRepo {
)
AND ($3::bool IS FALSE OR c.channel_id = ANY($4))
AND ($5::bool IS FALSE OR c.id = ANY($6))
- AND ($9::bool IS FALSE OR (
- ($11::bool IS FALSE AND EXISTS (
- SELECT 1 FROM entity_properties ep
- WHERE ep.entity_id = c.id::text
- AND ep.entity_type = 'CALL_RECORD'
- AND jsonb_typeof(ep.values -> 'value') = 'array'
- AND jsonb_exists_any(ep.values -> 'value', $10::text[])
- ))
- OR ($11::bool IS TRUE AND $10::text[] <@ (
- SELECT COALESCE(array_agg(elem), ARRAY[]::text[])
- FROM (
- SELECT values FROM entity_properties
- WHERE entity_id = c.id::text
- AND entity_type = 'CALL_RECORD'
- AND jsonb_typeof(values -> 'value') = 'array'
- ) ep
- CROSS JOIN LATERAL jsonb_array_elements_text(ep.values -> 'value') AS elem
- ))
- ))
+ -- Property conditions in conjunctive normal form: every
+ -- clause needs a term the call satisfies. Tags (no
+ -- definition) match any property value; entity references
+ -- match their definition's value.
+ AND NOT EXISTS (
+ SELECT 1
+ FROM generate_series(0, $9::int - 1) AS clause(idx)
+ WHERE NOT EXISTS (
+ SELECT 1
+ FROM UNNEST($10::int[], $11::bool[], $12::uuid[], $13::text[])
+ AS term(clause_idx, negated, definition_id, value)
+ WHERE term.clause_idx = clause.idx
+ AND term.negated <> EXISTS (
+ SELECT 1 FROM entity_properties ep
+ WHERE ep.entity_id = c.id::text
+ AND ep.entity_type = 'CALL_RECORD'
+ AND jsonb_typeof(ep.values -> 'value') = 'array'
+ AND CASE
+ WHEN term.definition_id IS NULL
+ THEN jsonb_exists(ep.values -> 'value', term.value)
+ ELSE ep.property_definition_id = term.definition_id
+ AND ep.values -> 'value' @> jsonb_build_array(
+ jsonb_build_object('entity_id', term.value)
+ )
+ END
+ )
+ )
+ )
UNION ALL
SELECT
cr.id AS call_id,
@@ -1738,25 +1692,34 @@ impl CallRepository for PgCallRepo {
)
AND ($3::bool IS FALSE OR cr.channel_id = ANY($4))
AND ($5::bool IS FALSE OR cr.id = ANY($6))
- AND ($9::bool IS FALSE OR (
- ($11::bool IS FALSE AND EXISTS (
- SELECT 1 FROM entity_properties ep
- WHERE ep.entity_id = cr.id::text
- AND ep.entity_type = 'CALL_RECORD'
- AND jsonb_typeof(ep.values -> 'value') = 'array'
- AND jsonb_exists_any(ep.values -> 'value', $10::text[])
- ))
- OR ($11::bool IS TRUE AND $10::text[] <@ (
- SELECT COALESCE(array_agg(elem), ARRAY[]::text[])
- FROM (
- SELECT values FROM entity_properties
- WHERE entity_id = cr.id::text
- AND entity_type = 'CALL_RECORD'
- AND jsonb_typeof(values -> 'value') = 'array'
- ) ep
- CROSS JOIN LATERAL jsonb_array_elements_text(ep.values -> 'value') AS elem
- ))
- ))
+ -- Property conditions in conjunctive normal form: every
+ -- clause needs a term the call satisfies. Tags (no
+ -- definition) match any property value; entity references
+ -- match their definition's value.
+ AND NOT EXISTS (
+ SELECT 1
+ FROM generate_series(0, $9::int - 1) AS clause(idx)
+ WHERE NOT EXISTS (
+ SELECT 1
+ FROM UNNEST($10::int[], $11::bool[], $12::uuid[], $13::text[])
+ AS term(clause_idx, negated, definition_id, value)
+ WHERE term.clause_idx = clause.idx
+ AND term.negated <> EXISTS (
+ SELECT 1 FROM entity_properties ep
+ WHERE ep.entity_id = cr.id::text
+ AND ep.entity_type = 'CALL_RECORD'
+ AND jsonb_typeof(ep.values -> 'value') = 'array'
+ AND CASE
+ WHEN term.definition_id IS NULL
+ THEN jsonb_exists(ep.values -> 'value', term.value)
+ ELSE ep.property_definition_id = term.definition_id
+ AND ep.values -> 'value' @> jsonb_build_array(
+ jsonb_build_object('entity_id', term.value)
+ )
+ END
+ )
+ )
+ )
)
SELECT
call_id as "call_id!",
@@ -1789,9 +1752,11 @@ impl CallRepository for PgCallRepo {
&call_ids,
has_status_filter,
&status_filter_values,
- has_tag_filter,
- &tag_option_ids,
- match_all_tags,
+ property.clause_count,
+ &property.clause_indices,
+ &property.negated,
+ &property.definition_ids as &[Option<Uuid>],
+ &property.values,
)
.fetch_all(&self.pool)
.await?;
diff --git a/crates/call/src/outbound/pg_call_repo/property_clauses.rs b/crates/call/src/outbound/pg_call_repo/property_clauses.rs
new file mode 100644
index 0000000000..6841bd0528
--- /dev/null
+++ b/crates/call/src/outbound/pg_call_repo/property_clauses.rs
@@ -0,0 +1,163 @@
+//! The property conditions of a call filter, in conjunctive normal form.
+//!
+//! Soup folds a properties filter (tags, entity references) into the call
+//! filter with its `AND`/`OR`/`NOT` structure intact. The call query is static
+//! SQL, so the conditions are flattened into clauses it can evaluate exactly:
+//! a call matches when every clause has at least one term it satisfies.
+
+use filter_ast::Expr;
+use item_filters::ast::{LiteralTree, call::CallLiteral, properties::PropertyMatchValue};
+use uuid::Uuid;
+
+#[cfg(test)]
+mod test;
+
+/// Clauses beyond which a filter is too large to expand; the property
+/// conditions are then dropped with a warning rather than expanded.
+const MAX_CLAUSES: usize = 256;
+
+/// One property condition.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub(super) enum PropertyCondition {
+ /// Any of the call's property values contains this tag option. Tags are
+ /// matched without their definition, like the search service does.
+ Tag(String),
+ /// The call's `definition` property references `entity_id`.
+ EntityRef { definition: Uuid, entity_id: String },
+}
+
+/// A condition, or its negation.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub(super) struct PropertyTerm {
+ pub condition: PropertyCondition,
+ pub negated: bool,
+}
+
+/// Property conditions as bound to the call query: one row per term, with
+/// the clause it belongs to.
+#[derive(Debug, Default, PartialEq, Eq)]
+pub(super) struct PropertyClauseParams {
+ pub clause_count: i32,
+ pub clause_indices: Vec<i32>,
+ pub negated: Vec<bool>,
+ /// `None` for tags.
+ pub definition_ids: Vec<Option<Uuid>>,
+ pub values: Vec<String>,
+}
+
+/// A filter's property part. Non-property literals are evaluated by the
+/// other call filters, so here they are unconstrained (`Any`).
+enum Formula {
+ Any,
+ Term(PropertyTerm),
+ And(Box<Formula>, Box<Formula>),
+ Or(Box<Formula>, Box<Formula>),
+}
+
+/// The property conditions of `filter` in conjunctive normal form.
+pub(super) fn property_clauses(filter: &LiteralTree<CallLiteral>) -> Vec<Vec<PropertyTerm>> {
+ let Some(expr) = filter else {
+ return Vec::new();
+ };
+ match clauses(&formula(expr, false)) {
+ Some(clauses) => clauses,
+ None => {
+ tracing::warn!(
+ max = MAX_CLAUSES,
+ "call property filter is too large to evaluate; ignoring its property conditions"
+ );
+ Vec::new()
+ }
+ }
+}
+
+/// Flatten clauses into the call query's parameters.
+pub(super) fn clause_params(clauses: &[Vec<PropertyTerm>]) -> PropertyClauseParams {
+ let mut params = PropertyClauseParams {
+ clause_count: i32::try_from(clauses.len()).unwrap_or(i32::MAX),
+ ..PropertyClauseParams::default()
+ };
+ for (index, clause) in clauses.iter().enumerate() {
+ for term in clause {
+ params
+ .clause_indices
+ .push(i32::try_from(index).unwrap_or(i32::MAX));
+ params.negated.push(term.negated);
+ let (definition, value) = match &term.condition {
+ PropertyCondition::Tag(option_id) => (None, option_id.clone()),
+ PropertyCondition::EntityRef {
+ definition,
+ entity_id,
+ } => (Some(*definition), entity_id.clone()),
+ };
+ params.definition_ids.push(definition);
+ params.values.push(value);
+ }
+ }
+ params
+}
+
+/// The property formula of `expr`, with negations pushed onto terms.
+fn formula(expr: &Expr<CallLiteral>, negated: bool) -> Formula {
+ match expr {
+ Expr::Literal(CallLiteral::Property(literal)) => {
+ let condition = match &literal.value {
+ PropertyMatchValue::SelectOption(option_id) => {
+ PropertyCondition::Tag(option_id.to_string())
+ }
+ PropertyMatchValue::EntityRef(entity_id) => PropertyCondition::EntityRef {
+ definition: literal.property_definition_id,
+ entity_id: entity_id.to_string(),
+ },
+ };
+ Formula::Term(PropertyTerm { condition, negated })
+ }
+ Expr::Literal(_) => Formula::Any,
+ Expr::Not(inner) => formula(inner, !negated),
+ // De Morgan: a negated AND is an OR of negations, and vice versa.
+ Expr::And(a, b) if !negated => and(formula(a, false), formula(b, false)),
+ Expr::Or(a, b) if negated => and(formula(a, true), formula(b, true)),
+ Expr::Or(a, b) | Expr::And(a, b) => or(formula(a, negated), formula(b, negated)),
+ }
+}
+
+fn and(a: Formula, b: Formula) -> Formula {
+ match (a, b) {
+ (Formula::Any, other) | (other, Formula::Any) => other,
+ (a, b) => Formula::And(Box::new(a), Box::new(b)),
+ }
+}
+
+fn or(a: Formula, b: Formula) -> Formula {
+ match (a, b) {
+ (Formula::Any, _) | (_, Formula::Any) => Formula::Any,
+ (a, b) => Formula::Or(Box::new(a), Box::new(b)),
+ }
+}
+
+/// Conjunctive normal form, or `None` when it exceeds [`MAX_CLAUSES`].
+fn clauses(formula: &Formula) -> Option<Vec<Vec<PropertyTerm>>> {
+ match formula {
+ Formula::Any => Some(Vec::new()),
+ Formula::Term(term) => Some(vec![vec![term.clone()]]),
+ Formula::And(a, b) => {
+ let mut all = clauses(a)?;
+ all.extend(clauses(b)?);
+ (all.len() <= MAX_CLAUSES).then_some(all)
+ }
+ Formula::Or(a, b) => {
+ let (a, b) = (clauses(a)?, clauses(b)?);
+ if a.len().saturating_mul(b.len()) > MAX_CLAUSES {
+ return None;
+ }
+ Some(
+ a.iter()
+ .flat_map(|left| {
+ b.iter()
+ .map(move |right| left.iter().chain(right).cloned().collect())
+ })
+ .collect(),
+ )
+ }
+ }
+}
diff --git a/crates/call/src/outbound/pg_call_repo/property_clauses/test.rs b/crates/call/src/outbound/pg_call_repo/property_clauses/test.rs
new file mode 100644
index 0000000000..d5145a532d
--- /dev/null
+++ b/crates/call/src/outbound/pg_call_repo/property_clauses/test.rs
@@ -0,0 +1,153 @@
+use filter_ast::Expr;
+use item_filters::ast::{
+ call::CallLiteral,
+ properties::{EntityRefId, PropertiesLiteral, PropertyMatchValue},
+};
+use uuid::Uuid;
+
+use super::*;
+
+const COMPANIES: Uuid = Uuid::from_u128(0x00000001_0000_0000_0000_00000000000c);
+const TAGS: Uuid = Uuid::from_u128(0x0198a1b2_c3d4_7e5f_8061_728394a5b7ff);
+
+fn company(id: &str) -> Expr<CallLiteral> {
+ Expr::Literal(CallLiteral::Property(PropertiesLiteral {
+ property_definition_id: COMPANIES,
+ entity_type: None,
+ value: PropertyMatchValue::EntityRef(EntityRefId::new(id.to_string()).unwrap()),
+ }))
+}
+
+fn tag(option: u128) -> Expr<CallLiteral> {
+ Expr::Literal(CallLiteral::Property(PropertiesLiteral {
+ property_definition_id: TAGS,
+ entity_type: None,
+ value: PropertyMatchValue::SelectOption(Uuid::from_u128(option)),
+ }))
+}
+
+fn has(id: &str) -> PropertyTerm {
+ PropertyTerm {
+ condition: PropertyCondition::EntityRef {
+ definition: COMPANIES,
+ entity_id: id.to_string(),
+ },
+ negated: false,
+ }
+}
+
+fn lacks(id: &str) -> PropertyTerm {
+ PropertyTerm {
+ negated: true,
+ ..has(id)
+ }
+}
+
+fn tagged(option: u128) -> PropertyTerm {
+ PropertyTerm {
+ condition: PropertyCondition::Tag(Uuid::from_u128(option).to_string()),
+ negated: false,
+ }
+}
+
+fn clauses_of(expr: Expr<CallLiteral>) -> Vec<Vec<PropertyTerm>> {
+ property_clauses(&Some(std::sync::Arc::new(expr)))
+}
+
+#[test]
+fn keeps_and_or_and_not_structure() {
+ assert_eq!(clauses_of(company("a")), vec![vec![has("a")]]);
+ assert_eq!(
+ clauses_of(Expr::or(company("a"), company("b"))),
+ vec![vec![has("a"), has("b")]]
+ );
+ assert_eq!(
+ clauses_of(Expr::and(company("a"), company("b"))),
+ vec![vec![has("a")], vec![has("b")]]
+ );
+ assert_eq!(
+ clauses_of(Expr::is_not(company("a"))),
+ vec![vec![lacks("a")]]
+ );
+ // De Morgan: NOT (a AND b) = NOT a OR NOT b.
+ assert_eq!(
+ clauses_of(Expr::is_not(Expr::and(company("a"), company("b")))),
+ vec![vec![lacks("a"), lacks("b")]]
+ );
+ // (a AND b) OR c distributes into (a OR c) AND (b OR c).
+ assert_eq!(
+ clauses_of(Expr::or(
+ Expr::and(company("a"), company("b")),
+ company("c")
+ )),
+ vec![vec![has("a"), has("c")], vec![has("b"), has("c")]]
+ );
+}
+
+#[test]
+fn tags_keep_any_and_all_semantics() {
+ // ANY: options ORed together.
+ assert_eq!(
+ clauses_of(Expr::or(tag(1), tag(2))),
+ vec![vec![tagged(1), tagged(2)]]
+ );
+ // ALL: options ANDed together, even next to a channel filter.
+ let channel = Expr::Literal(CallLiteral::ChannelId(Uuid::from_u128(9)));
+ assert_eq!(
+ clauses_of(Expr::and(channel, Expr::and(tag(1), tag(2)))),
+ vec![vec![tagged(1)], vec![tagged(2)]]
+ );
+}
+
+#[test]
+fn a_tag_or_a_reference_is_one_clause() {
+ assert_eq!(
+ clauses_of(Expr::or(tag(1), company("a"))),
+ vec![vec![tagged(1), has("a")]]
+ );
+}
+
+#[test]
+fn other_call_literals_leave_properties_unconstrained() {
+ let channel = || Expr::Literal(CallLiteral::ChannelId(Uuid::nil()));
+ // ANDed in, as soup folds properties into the call filter.
+ assert_eq!(
+ clauses_of(Expr::and(channel(), company("a"))),
+ vec![vec![has("a")]]
+ );
+ assert!(clauses_of(Expr::or(channel(), company("a"))).is_empty());
+ assert!(clauses_of(channel()).is_empty());
+ assert!(property_clauses(&None).is_empty());
+}
+
+#[test]
+fn flattens_clauses_into_query_parameters() {
+ let params = clause_params(&[vec![tagged(1), has("a")], vec![lacks("b")]]);
+
+ assert_eq!(
+ params,
+ PropertyClauseParams {
+ clause_count: 2,
+ clause_indices: vec![0, 0, 1],
+ negated: vec![false, false, true],
+ definition_ids: vec![None, Some(COMPANIES), Some(COMPANIES)],
+ values: vec![
+ Uuid::from_u128(1).to_string(),
+ "a".to_string(),
+ "b".to_string()
+ ],
+ }
+ );
+}
+
+#[test]
+fn oversized_filters_drop_their_property_conditions() {
+ // 9 nested ORs of two-term ANDs expand to 2^9 = 512 clauses.
+ let expr = (0..9).fold(company("seed"), |acc, i| {
+ Expr::or(
+ acc,
+ Expr::and(company(&format!("x{i}")), company(&format!("y{i}"))),
+ )
+ });
+ assert!(clauses_of(expr).is_empty());
+}
diff --git a/crates/call/src/outbound/pg_call_repo/test.rs b/crates/call/src/outbound/pg_call_repo/test.rs
index 7e32e2a230..d53cbc1eac 100644
--- a/crates/call/src/outbound/pg_call_repo/test.rs
+++ b/crates/call/src/outbound/pg_call_repo/test.rs
@@ -40,49 +40,6 @@ fn tag_property_literal(option_id: Uuid) -> CallLiteral {
})
}
-#[test]
-fn extract_tag_option_ids_collects_select_options_across_or() {
- let opt1 = Uuid::from_u128(1);
- let opt2 = Uuid::from_u128(2);
- let filter: LiteralTree<CallLiteral> = Some(Arc::new(Expr::or(
- Expr::Literal(tag_property_literal(opt1)),
- Expr::Literal(tag_property_literal(opt2)),
- )));
-
- let mut ids = super::extract_tag_option_ids(&filter);
- ids.sort();
- let mut expected = vec![opt1.to_string(), opt2.to_string()];
- expected.sort();
- assert_eq!(ids, expected);
-}
-
-#[test]
-fn extract_tag_option_ids_empty_for_non_property_filter() {
- assert!(super::extract_tag_option_ids(&status_filter(CallStatus::Attended)).is_empty());
- assert!(super::extract_tag_option_ids(&None).is_empty());
-}
-
-#[test]
-fn tag_filter_requires_all_distinguishes_and_from_or() {
- let a = Expr::Literal(tag_property_literal(Uuid::from_u128(1)));
- let b = Expr::Literal(tag_property_literal(Uuid::from_u128(2)));
- // ANY: options ORed together.
- let any: LiteralTree<CallLiteral> = Some(Arc::new(Expr::or(a.clone(), b.clone())));
- assert!(!super::tag_filter_requires_all(&any));
- // ALL: options ANDed together, even when combined with a channel filter.
- let all: LiteralTree<CallLiteral> = Some(Arc::new(Expr::and(
- Expr::Literal(CallLiteral::ChannelId(Uuid::from_u128(9))),
- Expr::and(a, b),
- )));
- assert!(super::tag_filter_requires_all(&all));
- // A single option (or no filter) reads as ANY.
- let single: LiteralTree<CallLiteral> = Some(Arc::new(Expr::Literal(tag_property_literal(
- Uuid::from_u128(1),
- ))));
- assert!(!super::tag_filter_requires_all(&single));
- assert!(!super::tag_filter_requires_all(&None));
-}
-
fn not_status_filter(status: CallStatus) -> LiteralTree<CallLiteral> {
let expr = Expr::Literal(CallLiteral::Status(status));
Some(Arc::new(Expr::is_not(expr)))
@@ -1927,6 +1884,114 @@ async fn get_call_records_by_user_call_ids_filter_narrows_results(
Ok(())
}
+fn entity_ref_property_literal(definition_id: Uuid, entity_id: Uuid) -> CallLiteral {
+ use item_filters::ast::properties::{EntityRefId, PropertiesLiteral, PropertyMatchValue};
+ CallLiteral::Property(PropertiesLiteral {
+ property_definition_id: definition_id,
+ entity_type: None,
+ value: PropertyMatchValue::EntityRef(EntityRefId::new(entity_id.to_string()).unwrap()),
+ })
+}
+
+#[sqlx::test(
+ fixtures(path = "../../../fixtures", scripts("call_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn get_call_records_by_user_entity_ref_filter_matches_referencing_calls(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let repo = repo(pool.clone());
+ let companies = Uuid::from_u128(0x00000001_0000_0000_0000_00000000000c);
+ let acme = Uuid::from_u128(0xac);
+ let globex = Uuid::from_u128(0x61);
+ sqlx::query(
+ r#"INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)
+ VALUES ($1, $2, 'CALL_RECORD', $3, $4)"#,
+ )
+ .bind(Uuid::now_v7())
+ .bind(CALL_ARCHIVED.to_string())
+ .bind(companies)
+ .bind(serde_json::json!({
+ "type": "EntityReference",
+ "value": [{ "entity_type": "COMPANY", "entity_id": acme.to_string() }]
+ }))
+ .execute(&pool)
+ .await?;
+
+ let filter: LiteralTree<CallLiteral> = Some(Arc::new(Expr::Literal(
+ entity_ref_property_literal(companies, acme),
+ )));
+ let records = repo
+ .get_call_records_by_user(USER_A.deref().copied(), 10, &filter)
+ .await?;
+ assert_eq!(
+ records.iter().map(|r| r.call_id).collect::<Vec<_>>(),
+ vec![CALL_ARCHIVED]
+ );
+
+ // Any referenced entity matches; another definition or entity does not.
+ let filter: LiteralTree<CallLiteral> = Some(Arc::new(Expr::or(
+ Expr::Literal(entity_ref_property_literal(companies, globex)),
+ Expr::Literal(entity_ref_property_literal(companies, acme)),
+ )));
+ let records = repo
+ .get_call_records_by_user(USER_A.deref().copied(), 10, &filter)
+ .await?;
+ assert_eq!(records.len(), 1);
+
+ for literal in [
+ entity_ref_property_literal(companies, globex),
+ entity_ref_property_literal(Uuid::from_u128(0x13), acme),
+ ] {
+ let filter: LiteralTree<CallLiteral> = Some(Arc::new(Expr::Literal(literal)));
+ let records = repo
+ .get_call_records_by_user(USER_A.deref().copied(), 10, &filter)
+ .await?;
+ assert!(records.is_empty(), "unexpected match: {records:?}");
+ }
+
+ let matching = |filter: Expr<CallLiteral>| {
+ let repo = &repo;
+ async move {
+ repo.get_call_records_by_user(USER_A.deref().copied(), 10, &Some(Arc::new(filter)))
+ .await
+ .map(|records| records.iter().map(|r| r.call_id).collect::<Vec<_>>())
+ }
+ };
+ let acme_ref = || Expr::Literal(entity_ref_property_literal(companies, acme));
+ let globex_ref = || Expr::Literal(entity_ref_property_literal(companies, globex));
+ let missing_tag = || Expr::Literal(tag_property_literal(Uuid::from_u128(0x7a6)));
+
+ // AND requires both references.
+ assert!(
+ matching(Expr::and(acme_ref(), globex_ref()))
+ .await?
+ .is_empty()
+ );
+ // NOT excludes the referencing call and keeps the others.
+ assert!(
+ !matching(Expr::is_not(acme_ref()))
+ .await?
+ .contains(&CALL_ARCHIVED)
+ );
+ assert!(
+ matching(Expr::is_not(globex_ref()))
+ .await?
+ .contains(&CALL_ARCHIVED)
+ );
+ // A tag OR a reference matches on either.
+ assert_eq!(
+ matching(Expr::or(missing_tag(), acme_ref())).await?,
+ vec![CALL_ARCHIVED]
+ );
+ assert!(
+ matching(Expr::and(missing_tag(), acme_ref()))
+ .await?
+ .is_empty()
+ );
+ Ok(())
+}
+
#[sqlx::test(
fixtures(path = "../../../fixtures", scripts("call_repo")),
migrator = "MACRO_DB_MIGRATIONS"
diff --git a/crates/frecency/src/outbound/postgres/dynamic.rs b/crates/frecency/src/outbound/postgres/dynamic.rs
index ef720a83b3..795ce0ebe8 100644
--- a/crates/frecency/src/outbound/postgres/dynamic.rs
+++ b/crates/frecency/src/outbound/postgres/dynamic.rs
@@ -3,8 +3,13 @@
use crate::domain::models::{AggregateFrecency, AggregateId, FrecencyData, TimestampWeight};
use filter_ast::Expr;
use item_filters::ast::{
- EntityFilterAst, chat::ChatLiteral, date::DateLiteral, document::DocumentLiteral,
+ EntityFilterAst,
+ chat::ChatLiteral,
+ date::DateLiteral,
+ document::DocumentLiteral,
+ email::Email,
project::ProjectLiteral,
+ properties::{PropertiesLiteral, PropertyMatchValue},
};
use macro_user_id::{cowlike::CowLike, user_id::MacroUserIdStr};
use model_entity::EntityType;
@@ -206,6 +211,12 @@ fn build_document_filter(ast: Option<&Expr<DocumentLiteral>>) -> String {
filter_ast::ExprFrame::Literal(DocumentLiteral::UpdatedAt(DateLiteral::LessThanOrEqual(dt))) => {
format!(r#"entity_id IN (SELECT id FROM "Document" WHERE "updatedAt" <= '{}'::timestamptz AND "deletedAt" IS NULL)"#, dt.to_rfc3339())
}
+ filter_ast::ExprFrame::Literal(DocumentLiteral::Property(literal)) => {
+ property_literal_predicate(&literal)
+ }
+ filter_ast::ExprFrame::Literal(DocumentLiteral::EmailAttachmentParticipant(email)) => {
+ email_attachment_participant_predicate(&email)
+ }
});
if formatting.is_empty() {
String::new()
@@ -214,6 +225,77 @@ fn build_document_filter(ast: Option<&Expr<DocumentLiteral>>) -> String {
}
}
+/// A single-quoted SQL string literal with embedded quotes doubled.
+fn sql_string_literal(value: &str) -> String {
+ format!("'{}'", value.replace('\'', "''"))
+}
+
+/// An EXISTS check that the aggregated document has a property value
+/// matching `literal`.
+fn property_literal_predicate(literal: &PropertiesLiteral) -> String {
+ let PropertiesLiteral {
+ property_definition_id,
+ entity_type,
+ value,
+ } = literal;
+ let value_predicate = match value {
+ PropertyMatchValue::SelectOption(option_id) => {
+ format!("ep_prop.values->'value' ? '{option_id}'")
+ }
+ PropertyMatchValue::EntityRef(entity_id) => format!(
+ "ep_prop.values->'value' @> jsonb_build_array(jsonb_build_object('entity_id', '{entity_id}'))"
+ ),
+ };
+ let entity_type_clause = match entity_type {
+ Some(et) => format!("AND ep_prop.entity_type = '{et}'"),
+ None => String::new(),
+ };
+ format!(
+ r#"EXISTS (
+ SELECT 1 FROM entity_properties ep_prop
+ WHERE ep_prop.entity_id = fa.entity_id
+ {entity_type_clause}
+ AND ep_prop.property_definition_id = '{property_definition_id}'
+ AND {value_predicate}
+ )"#
+ )
+}
+
+/// Matches documents uploaded from an email attachment whose message was sent
+/// by, or addressed to, an address matching `email`.
+fn email_attachment_participant_predicate(email: &Email) -> String {
+ let address_predicate = match email {
+ Email::Complete(address) => format!(
+ "LOWER(c.email_address) = {}",
+ sql_string_literal(&address.0.as_ref().to_lowercase())
+ ),
+ Email::Domain(domain) => format!(
+ "LOWER(SPLIT_PART(c.email_address, '@', 2)) = {}",
+ sql_string_literal(&domain.to_lowercase())
+ ),
+ Email::Partial(fragment) => format!(
+ "STRPOS(LOWER(c.email_address), {}) > 0",
+ sql_string_literal(&fragment.to_lowercase())
+ ),
+ };
+ format!(
+ r#"EXISTS (
+ SELECT 1
+ FROM document_email de
+ JOIN email_attachments ea ON ea.id = de.email_attachment_id
+ JOIN email_messages m ON m.id = ea.message_id
+ JOIN LATERAL (
+ SELECT m.from_contact_id AS contact_id
+ UNION ALL
+ SELECT r.contact_id FROM email_message_recipients r WHERE r.message_id = m.id
+ ) participant ON TRUE
+ JOIN email_contacts c ON c.id = participant.contact_id
+ WHERE de.document_id = fa.entity_id
+ AND {address_predicate}
+ )"#
+ )
+}
+
fn build_chat_filter(ast: Option<&Expr<ChatLiteral>>) -> String {
let Some(expr) = ast else {
return String::new();
diff --git a/crates/frecency/src/outbound/postgres/dynamic_tests.rs b/crates/frecency/src/outbound/postgres/dynamic_tests.rs
index bad68be6cf..5b6a8b9260 100644
--- a/crates/frecency/src/outbound/postgres/dynamic_tests.rs
+++ b/crates/frecency/src/outbound/postgres/dynamic_tests.rs
@@ -1279,3 +1279,119 @@ async fn test_dynamic_filter_document_date_updated_at_lt(pool: PgPool) {
assert_eq!(results.len(), 1);
assert_eq!(results[0].id.entity.entity_id, doc_early.to_string());
}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn test_dynamic_filter_document_crm_literals(pool: PgPool) {
+ use item_filters::ast::{
+ email::Email,
+ properties::{EntityRefId, PropertiesLiteral, PropertyMatchValue},
+ };
+
+ let storage = FrecencyPgStorage::new(pool.clone());
+ let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+ let associated = Uuid::new_v4();
+ let attachment = Uuid::new_v4();
+ let unrelated = Uuid::new_v4();
+ let company = Uuid::new_v4();
+ for id in [associated, attachment, unrelated] {
+ storage
+ .set_aggregate(AggregateFrecency {
+ id: AggregateId {
+ entity: EntityType::Document.with_entity_string(id.to_string()),
+ user_id: user_id.clone(),
+ },
+ data: FrecencyData {
+ event_count: 1,
+ frecency_score: 10.0,
+ first_event: Utc::now(),
+ recent_events: VecDeque::new(),
+ },
+ })
+ .await
+ .unwrap();
+ }
+
+ let mut tx = pool.begin().await.unwrap();
+ sqlx::query("SET LOCAL session_replication_role = 'replica'")
+ .execute(&mut *tx)
+ .await
+ .unwrap();
+ sqlx::query(
+ r#"INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)
+ VALUES ($1, $2, 'DOCUMENT', '00000001-0000-0000-0000-00000000000c', $3)"#,
+ )
+ .bind(Uuid::new_v4())
+ .bind(associated.to_string())
+ .bind(serde_json::json!({
+ "type": "EntityReference",
+ "value": [{ "entity_type": "COMPANY", "entity_id": company.to_string() }]
+ }))
+ .execute(&mut *tx)
+ .await
+ .unwrap();
+ let (contact, message, attachment_row) = (Uuid::new_v4(), Uuid::new_v4(), Uuid::new_v4());
+ sqlx::query(
+ "INSERT INTO email_contacts (id, link_id, email_address) VALUES ($1, $2, '[email protected]')",
+ )
+ .bind(contact)
+ .bind(Uuid::new_v4())
+ .execute(&mut *tx)
+ .await
+ .unwrap();
+ sqlx::query("INSERT INTO email_messages (id, thread_id, link_id, from_contact_id) VALUES ($1, $2, $3, $4)")
+ .bind(message)
+ .bind(Uuid::new_v4())
+ .bind(Uuid::new_v4())
+ .bind(contact)
+ .execute(&mut *tx)
+ .await
+ .unwrap();
+ sqlx::query("INSERT INTO email_attachments (id, message_id) VALUES ($1, $2)")
+ .bind(attachment_row)
+ .bind(message)
+ .execute(&mut *tx)
+ .await
+ .unwrap();
+ sqlx::query("INSERT INTO document_email (document_id, email_attachment_id) VALUES ($1, $2)")
+ .bind(attachment.to_string())
+ .bind(attachment_row)
+ .execute(&mut *tx)
+ .await
+ .unwrap();
+ tx.commit().await.unwrap();
+
+ let filter = EntityFilterAst {
+ document_filter: Some(Arc::new(Expr::or(
+ Expr::Literal(DocumentLiteral::Property(PropertiesLiteral {
+ property_definition_id: Uuid::parse_str("00000001-0000-0000-0000-00000000000c")
+ .unwrap(),
+ entity_type: None,
+ value: PropertyMatchValue::EntityRef(
+ EntityRefId::new(company.to_string()).unwrap(),
+ ),
+ })),
+ Expr::Literal(DocumentLiteral::EmailAttachmentParticipant(Email::Domain(
+ "acme.com".to_string(),
+ ))),
+ ))),
+ ..EntityFilterAst::default()
+ };
+ let results = storage
+ .get_top_entities(FrecencyPageRequest {
+ user_id: user_id.copied(),
+ from_score: None,
+ limit: 10,
+ filters: Some(filter),
+ })
+ .await
+ .unwrap();
+
+ let mut ids: Vec<String> = results
+ .iter()
+ .map(|r| r.id.entity.entity_id.as_ref().to_string())
+ .collect();
+ ids.sort();
+ let mut expected = vec![associated.to_string(), attachment.to_string()];
+ expected.sort();
+ assert_eq!(ids, expected);
+}
diff --git a/crates/graphql_soup_filter_input/src/lib.rs b/crates/graphql_soup_filter_input/src/lib.rs
index 89ec394801..a805ae806a 100644
--- a/crates/graphql_soup_filter_input/src/lib.rs
+++ b/crates/graphql_soup_filter_input/src/lib.rs
@@ -242,9 +242,10 @@ struct GraphqlFilterPropertiesLiteral {
value: GraphqlFilterPropertyMatchValue,
}
-impl IntoFilterExpr<PropertiesLiteral> for GraphqlFilterPropertiesLiteral {
- fn into_expr(self) -> InputResult<Expr<PropertiesLiteral>> {
- Ok(Expr::val(PropertiesLiteral {
+impl GraphqlFilterPropertiesLiteral {
+ /// Convert this input into the domain literal.
+ fn into_literal(self) -> InputResult<PropertiesLiteral> {
+ Ok(PropertiesLiteral {
property_definition_id: parse_id(self.property_definition_id, "propertyDefinitionId")?,
entity_type: self
.entity_type
@@ -254,7 +255,13 @@ impl IntoFilterExpr<PropertiesLiteral> for GraphqlFilterPropertiesLiteral {
InputError::new(format!("unsupported entityType {entity_type:?}"))
})?,
value: self.value.into_ast()?,
- }))
+ })
+ }
+}
+
+impl IntoFilterExpr<PropertiesLiteral> for GraphqlFilterPropertiesLiteral {
+ fn into_expr(self) -> InputResult<Expr<PropertiesLiteral>> {
+ self.into_literal().map(Expr::val)
}
}
@@ -650,6 +657,10 @@ enum GraphqlDocumentLiteral {
CreatedAt(GraphqlDateLiteral),
/// The updated at option.
UpdatedAt(GraphqlDateLiteral),
+ /// An entity-property condition on the document or task.
+ Property(GraphqlFilterPropertiesLiteral),
+ /// Uploaded from an email attachment sent by, or to, a matching address.
+ EmailAttachmentParticipant(GraphqlEmailValue),
}
impl IntoFilterExpr<DocumentLiteral> for GraphqlDocumentLiteral {
@@ -678,6 +689,10 @@ impl IntoFilterExpr<DocumentLiteral> for GraphqlDocumentLiteral {
Self::IsEmailAttachment(value) => DocumentLiteral::IsEmailAttachment(value),
Self::CreatedAt(date) => DocumentLiteral::CreatedAt(date.into_ast()?),
Self::UpdatedAt(date) => DocumentLiteral::UpdatedAt(date.into_ast()?),
+ Self::Property(property) => DocumentLiteral::Property(property.into_literal()?),
+ Self::EmailAttachmentParticipant(value) => {
+ DocumentLiteral::EmailAttachmentParticipant(value.into_ast()?)
+ }
};
Ok(Expr::val(literal))
}
diff --git a/crates/graphql_soup_filter_input/src/test.rs b/crates/graphql_soup_filter_input/src/test.rs
index 93ae4feb32..8105cec39a 100644
--- a/crates/graphql_soup_filter_input/src/test.rs
+++ b/crates/graphql_soup_filter_input/src/test.rs
@@ -207,3 +207,41 @@ fn channel_thread_has_replies_materializes_for_browser_and_server() {
);
}
}
+
+#[test]
+fn crm_document_literals_materialize_for_browser_and_server() {
+ let company_id = "0198a1b2-c3d4-7e5f-8061-728394a5b700";
+ let value = json!({
+ "documentFilter": {"or": {
+ "left": {"literal": {"property": {
+ "propertyDefinitionId": "00000001-0000-0000-0000-00000000000c",
+ "value": {"entityRef": company_id}
+ }}},
+ "right": {"literal": {"emailAttachmentParticipant": {"domain": "acme.com"}}}
+ }}
+ });
+ let ast = materialize_graphql_filter(value.clone()).unwrap();
+ let input: GraphqlEntityFilterAst = serde_json::from_value(value).unwrap();
+ assert_eq!(
+ serde_json::to_value(&ast).unwrap(),
+ serde_json::to_value(input.into_ast().unwrap()).unwrap()
+ );
+
+ let Some(Expr::Or(left, right)) = ast.document_filter.as_deref() else {
+ panic!("expected an OR of the two CRM literals")
+ };
+ assert!(matches!(
+ left.as_ref(),
+ Expr::Literal(DocumentLiteral::Property(PropertiesLiteral {
+ entity_type: None,
+ value: PropertyMatchValue::EntityRef(id),
+ ..
+ })) if id.to_string() == company_id
+ ));
+ assert!(matches!(
+ right.as_ref(),
+ Expr::Literal(DocumentLiteral::EmailAttachmentParticipant(
+ item_filters::ast::email::Email::Domain(domain)
+ )) if domain == "acme.com"
+ ));
+}
diff --git a/crates/item_filter_index/src/test.rs b/crates/item_filter_index/src/test.rs
index ba6275edd8..b8dc0cd351 100644
--- a/crates/item_filter_index/src/test.rs
+++ b/crates/item_filter_index/src/test.rs
@@ -114,6 +114,36 @@ fn compiles_complete_supported_forest_after_eligibility() {
));
}
+#[test]
+fn crm_document_literals_always_fall_back_to_the_server() {
+ use item_filters::ast::{
+ email::Email,
+ properties::{EntityRefId, PropertiesLiteral, PropertyMatchValue},
+ };
+ for literal in [
+ DocumentLiteral::Property(PropertiesLiteral {
+ property_definition_id: uuid::Uuid::from_u128(0xc),
+ entity_type: None,
+ value: PropertyMatchValue::EntityRef(EntityRefId::new("company".to_string()).unwrap()),
+ }),
+ DocumentLiteral::EmailAttachmentParticipant(Email::Domain("acme.com".to_string())),
+ ] {
+ let mut ast = excluded_deferred_partitions();
+ ast.document_filter = Some(Arc::new(Expr::val(literal)));
+ for outcome in [
+ compile_soup_flat_v1(&ast, request()).unwrap(),
+ compile_soup_flat_v2(&ast, request()).unwrap(),
+ compile_soup_flat_v3(&ast, request()).unwrap(),
+ compile_soup_flat_v4(&ast, request()).unwrap(),
+ ] {
+ assert_eq!(
+ outcome,
+ LocalCompileOutcome::Unsupported(UnsupportedReason::Literal("document"))
+ );
+ }
+ }
+}
+
#[test]
fn production_documents_membership_literals_require_and_compile_in_v2() {
for literal in [
diff --git a/crates/item_filters/src/ast/document.rs b/crates/item_filters/src/ast/document.rs
index c5fa954c53..c0e03fdbcd 100644
--- a/crates/item_filters/src/ast/document.rs
+++ b/crates/item_filters/src/ast/document.rs
@@ -1,6 +1,6 @@
use crate::{
DocumentFilters,
- ast::{ExpandErr, date::DateLiteral},
+ ast::{ExpandErr, date::DateLiteral, email::Email, properties::PropertiesLiteral},
};
use document_sub_type::DocumentSubType;
use either::Either;
@@ -56,6 +56,15 @@ pub enum DocumentLiteral {
/// this node value filters by document updatedAt timestamp
#[serde(rename = "ua")]
UpdatedAt(DateLiteral),
+ /// an entity-property condition on the document or task, e.g. its
+ /// Companies property referencing a CRM company. Unlike the top-level
+ /// properties filter, it composes with the other document literals.
+ #[serde(rename = "prop")]
+ Property(PropertiesLiteral),
+ /// the document was uploaded from an email attachment whose message was
+ /// sent by, or addressed to, a matching email address or domain
+ #[serde(rename = "eap")]
+ EmailAttachmentParticipant(Email),
}
fn prefix(s: &str) -> IResult<&str, &str> {
diff --git a/crates/item_filters/src/ast/tests.rs b/crates/item_filters/src/ast/tests.rs
index ec289b6a21..a3a7f038a6 100644
--- a/crates/item_filters/src/ast/tests.rs
+++ b/crates/item_filters/src/ast/tests.rs
@@ -1654,3 +1654,39 @@ fn initiatives_expand_with_properties_without_enabling_legacy_queries() {
assert!(ast.initiative_filter.is_some());
assert!(ast.properties_filter.is_some());
}
+
+#[test]
+fn crm_document_literals_round_trip_through_the_rest_ast() {
+ let company_id = "0198a1b2-c3d4-7e5f-8061-728394a5b700";
+ let tree = json!({
+ "|": [
+ { "l": { "prop": {
+ "pd": "00000001-0000-0000-0000-00000000000c",
+ "v": { "er": company_id }
+ } } },
+ { "l": { "eap": { "Domain": "acme.com" } } }
+ ]
+ });
+ let expr: Expr<document::DocumentLiteral> = serde_json::from_value(tree.clone()).unwrap();
+
+ let Expr::Or(left, right) = &expr else {
+ panic!("expected an OR, got {expr:?}")
+ };
+ assert_matches!(
+ left.as_ref(),
+ Expr::Literal(document::DocumentLiteral::Property(
+ properties::PropertiesLiteral {
+ entity_type: None,
+ value: properties::PropertyMatchValue::EntityRef(_),
+ ..
+ }
+ ))
+ );
+ assert_matches!(
+ right.as_ref(),
+ Expr::Literal(document::DocumentLiteral::EmailAttachmentParticipant(
+ email::Email::Domain(domain)
+ )) if domain == "acme.com"
+ );
+ assert_eq!(serde_json::to_value(&expr).unwrap(), tree);
+}
diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs
index db378e81ae..709dccc29d 100644
--- a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs
+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs
@@ -24,6 +24,7 @@ use item_filters::ast::{
chat::ChatLiteral,
date::DateLiteral,
document::DocumentLiteral,
+ email::Email,
project::ProjectLiteral,
properties::{PropertiesLiteral, PropertyEntityType, PropertyMatchValue},
};
@@ -898,6 +899,12 @@ pub(in crate::outbound::pg_soup_repo) fn build_document_filter(
filter_ast::ExprFrame::Literal(DocumentLiteral::UpdatedAt(lit)) => {
date_predicate(r#"d."updatedAt""#, &lit)
}
+ filter_ast::ExprFrame::Literal(DocumentLiteral::Property(lit)) => {
+ property_literal_predicate(&lit, "d.id")
+ }
+ filter_ast::ExprFrame::Literal(DocumentLiteral::EmailAttachmentParticipant(email)) => {
+ email_attachment_participant_predicate(&email, "d.id")
+ }
});
if formatting.is_empty() {
String::new()
@@ -906,6 +913,41 @@ pub(in crate::outbound::pg_soup_repo) fn build_document_filter(
}
}
+/// Matches documents uploaded from an email attachment whose message was sent
+/// by, or addressed to, an address matching `email`.
+fn email_attachment_participant_predicate(email: &Email, document_id_sql: &str) -> String {
+ let address_predicate = match email {
+ Email::Complete(address) => format!(
+ "LOWER(c.email_address) = {}",
+ sql_string_literal(&address.0.as_ref().to_lowercase())
+ ),
+ Email::Domain(domain) => format!(
+ "LOWER(SPLIT_PART(c.email_address, '@', 2)) = {}",
+ sql_string_literal(&domain.to_lowercase())
+ ),
+ Email::Partial(fragment) => format!(
+ "STRPOS(LOWER(c.email_address), {}) > 0",
+ sql_string_literal(&fragment.to_lowercase())
+ ),
+ };
+ format!(
+ r#"EXISTS (
+ SELECT 1
+ FROM document_email de
+ JOIN email_attachments ea ON ea.id = de.email_attachment_id
+ JOIN email_messages m ON m.id = ea.message_id
+ JOIN LATERAL (
+ SELECT m.from_contact_id AS contact_id
+ UNION ALL
+ SELECT r.contact_id FROM email_message_recipients r WHERE r.message_id = m.id
+ ) participant ON TRUE
+ JOIN email_contacts c ON c.id = participant.contact_id
+ WHERE de.document_id = {document_id_sql}
+ AND {address_predicate}
+ )"#
+ )
+}
+
/// A single-quoted SQL string literal with embedded quotes doubled. Used for
/// every caller-supplied string these bind-free builders inline: the calendar
/// filter's statuses and attendee/organizer emails, and the owner principals,
@@ -1067,34 +1109,8 @@ pub(in crate::outbound::pg_soup_repo) fn build_properties_filter(
filter_ast::ExprFrame::And(a, b) => format!("({a} AND {b})"),
filter_ast::ExprFrame::Or(a, b) => format!("({a} OR {b})"),
filter_ast::ExprFrame::Not(a) => format!("(NOT {a})"),
- filter_ast::ExprFrame::Literal(PropertiesLiteral {
- property_definition_id,
- entity_type,
- value,
- }) => {
- let value_predicate = match value {
- PropertyMatchValue::SelectOption(option_id) => {
- format!("ep_prop.values->'value' ? '{option_id}'")
- }
- PropertyMatchValue::EntityRef(entity_id) => {
- format!(
- "ep_prop.values->'value' @> jsonb_build_array(jsonb_build_object('entity_id', '{entity_id}'))"
- )
- }
- };
- let entity_type_clause = match entity_type {
- Some(et) => format!("AND ep_prop.entity_type = '{et}'"),
- None => String::new(),
- };
- format!(
- r#"EXISTS (
- SELECT 1 FROM entity_properties ep_prop
- WHERE ep_prop.entity_id = {entity_id_sql}
- {entity_type_clause}
- AND ep_prop.property_definition_id = '{property_definition_id}'
- AND {value_predicate}
- )"#
- )
+ filter_ast::ExprFrame::Literal(literal) => {
+ property_literal_predicate(&literal, entity_id_sql)
}
});
if formatting.is_empty() {
@@ -1104,6 +1120,39 @@ pub(in crate::outbound::pg_soup_repo) fn build_properties_filter(
}
}
+/// An EXISTS check that the entity identified by `entity_id_sql` has a
+/// property value matching `literal`.
+fn property_literal_predicate(literal: &PropertiesLiteral, entity_id_sql: &str) -> String {
+ let PropertiesLiteral {
+ property_definition_id,
+ entity_type,
+ value,
+ } = literal;
+ let value_predicate = match value {
+ PropertyMatchValue::SelectOption(option_id) => {
+ format!("ep_prop.values->'value' ? '{option_id}'")
+ }
+ PropertyMatchValue::EntityRef(entity_id) => {
+ format!(
+ "ep_prop.values->'value' @> jsonb_build_array(jsonb_build_object('entity_id', '{entity_id}'))"
+ )
+ }
+ };
+ let entity_type_clause = match entity_type {
+ Some(et) => format!("AND ep_prop.entity_type = '{et}'"),
+ None => String::new(),
+ };
+ format!(
+ r#"EXISTS (
+ SELECT 1 FROM entity_properties ep_prop
+ WHERE ep_prop.entity_id = {entity_id_sql}
+ {entity_type_clause}
+ AND ep_prop.property_definition_id = '{property_definition_id}'
+ AND {value_predicate}
+ )"#
+ )
+}
+
pub(in crate::outbound::pg_soup_repo) fn document_filter_needs_task_property_joins(
ast: Option<&Expr<DocumentLiteral>>,
) -> bool {
diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/tests.rs b/crates/soup/src/outbound/pg_soup_repo/expanded/tests.rs
index 99e4f0ce83..8a5e9cc42a 100644
--- a/crates/soup/src/outbound/pg_soup_repo/expanded/tests.rs
+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/tests.rs
@@ -6906,3 +6906,159 @@ async fn test_optimized_notification_filter_matches_unoptimized_equivalent(
Ok(())
}
+
+const DOC_A: &str = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
+const DOC_B: &str = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb";
+const DOC_D: &str = "dddddddd-dddd-dddd-dddd-dddddddddddd";
+
+/// The fixture's attachments hang off messages that do not exist. Give doc A's
+/// attachment a message from the user to [email protected] and doc B's a message
+/// from [email protected], and point doc D's Companies property at `company`.
+async fn seed_crm_document_associations(db: &PgPool, company: Uuid) -> anyhow::Result<()> {
+ let mut tx = db.begin().await?;
+ sqlx::query("SET LOCAL session_replication_role = 'replica'")
+ .execute(&mut *tx)
+ .await?;
+ let link = Uuid::now_v7();
+ let (me, vendor, other) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ for (id, address) in [
+ (vendor, "[email protected]"),
+ (other, "[email protected]"),
+ ] {
+ sqlx::query("INSERT INTO email_contacts (id, link_id, email_address) VALUES ($1, $2, $3)")
+ .bind(id)
+ .bind(link)
+ .bind(address)
+ .execute(&mut *tx)
+ .await?;
+ }
+ let to_acme = Uuid::parse_str("ea000001-0000-0000-0000-000000000099")?;
+ let from_globex = Uuid::now_v7();
+ for (message, sender) in [(to_acme, me), (from_globex, other)] {
+ sqlx::query(
+ "INSERT INTO email_messages (id, thread_id, link_id, from_contact_id) VALUES ($1, $2, $3, $4)",
+ )
+ .bind(message)
+ .bind(Uuid::now_v7())
+ .bind(link)
+ .bind(sender)
+ .execute(&mut *tx)
+ .await?;
+ }
+ sqlx::query(
+ "INSERT INTO email_message_recipients (message_id, contact_id, recipient_type) VALUES ($1, $2, 'TO')",
+ )
+ .bind(to_acme)
+ .bind(vendor)
+ .execute(&mut *tx)
+ .await?;
+ sqlx::query(
+ "UPDATE email_attachments SET message_id = $1 WHERE id = 'ea000001-0000-0000-0000-000000000002'",
+ )
+ .bind(from_globex)
+ .execute(&mut *tx)
+ .await?;
+ sqlx::query(
+ r#"INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)
+ VALUES ($1, $2, 'DOCUMENT', $3, $4)"#,
+ )
+ .bind(Uuid::now_v7())
+ .bind(DOC_D)
+ .bind(SystemPropertyKey::COMPANIES_UUID)
+ .bind(serde_json::json!({
+ "type": "EntityReference",
+ "value": [{ "entity_type": "COMPANY", "entity_id": company.to_string() }]
+ }))
+ .execute(&mut *tx)
+ .await?;
+ tx.commit().await?;
+ Ok(())
+}
+
+async fn matching_document_ids(db: &PgPool, filter: Expr<DocumentLiteral>) -> HashSet<String> {
+ let (items, ..) = run_and_count(
+ db,
+ EntityFilterAst {
+ document_filter: Some(Arc::new(filter)),
+ ..mock_empty_ast()
+ },
+ )
+ .await
+ .unwrap();
+ items
+ .into_iter()
+ .filter_map(|item| match item {
+ SoupItem::Document(doc) => Some(doc.id.to_string()),
+ _ => None,
+ })
+ .collect()
+}
+
+fn ids(values: &[&str]) -> HashSet<String> {
+ values.iter().map(ToString::to_string).collect()
+}
+
+#[sqlx::test(
+ fixtures(
+ path = "../../../../../macro_db_client/fixtures",
+ scripts("entity_filter_tests")
+ ),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn crm_document_literals_match_attachments_and_associations(
+ db: PgPool,
+) -> anyhow::Result<()> {
+ use item_filters::ast::{
+ email::Email,
+ properties::{EntityRefId, PropertiesLiteral, PropertyMatchValue},
+ };
+ use macro_user_id::email::EmailStr;
+
+ let company = Uuid::now_v7();
+ seed_crm_document_associations(&db, company).await?;
+ let participant =
+ |email: Email| Expr::Literal(DocumentLiteral::EmailAttachmentParticipant(email));
+ let associated = Expr::Literal(DocumentLiteral::Property(PropertiesLiteral {
+ property_definition_id: SystemPropertyKey::COMPANIES_UUID,
+ entity_type: None,
+ value: PropertyMatchValue::EntityRef(EntityRefId::new(company.to_string())?),
+ }));
+
+ // Recipients count as participants, and domains match case-insensitively.
+ assert_eq!(
+ matching_document_ids(&db, participant(Email::Domain("ACME.com".to_string()))).await,
+ ids(&[DOC_A])
+ );
+ // So do senders, by exact address.
+ let globex = EmailStr::parse_from_str("[email protected]")?.into_owned();
+ assert_eq!(
+ matching_document_ids(&db, participant(Email::Complete(globex))).await,
+ ids(&[DOC_B])
+ );
+ assert_eq!(
+ matching_document_ids(&db, participant(Email::Partial("vendor@".to_string()))).await,
+ ids(&[DOC_A])
+ );
+ // A company's files: attachments with its people OR documents associated with it.
+ assert_eq!(
+ matching_document_ids(
+ &db,
+ Expr::or(
+ participant(Email::Domain("acme.com".to_string())),
+ associated.clone()
+ )
+ )
+ .await,
+ ids(&[DOC_A, DOC_D])
+ );
+ // Negation keeps every other document.
+ let not_acme = matching_document_ids(
+ &db,
+ Expr::is_not(participant(Email::Domain("acme.com".to_string()))),
+ )
+ .await;
+ assert!(!not_acme.contains(DOC_A));
+ assert!(not_acme.contains(DOC_B) && not_acme.contains(DOC_D));
+ Ok(())
+}
diff --git a/static_assets/schema.graphql b/static_assets/schema.graphql
index db51ce9c3a..380ae8b6c1 100644
--- a/static_assets/schema.graphql
+++ b/static_assets/schema.graphql
@@ -1880,6 +1880,14 @@ input GraphqlDocumentLiteral @oneOf {
The updated at option.
"""
updatedAt: GraphqlDateLiteral
+ """
+ An entity-property condition on the document or task.
+ """
+ property: GraphqlFilterPropertiesLiteral
+ """
+ Uploaded from an email attachment sent by, or to, a matching address.
+ """
+ emailAttachmentParticipant: GraphqlEmailValue
}
"""