diff --git a/.agents/skills/hypercerts-api/references/endpoint-map.md b/.agents/skills/hypercerts-api/references/endpoint-map.md index 9455737..73ac422 100644 --- a/.agents/skills/hypercerts-api/references/endpoint-map.md +++ b/.agents/skills/hypercerts-api/references/endpoint-map.md @@ -26,7 +26,7 @@ Inspect the exact operation in the bundled OpenAPI contract before constructing | Load one actor's organization sidecar | `app.certified.actor.getOrganization` | `actor` is a DID. If no indexed sidecar exists, this single lookup declares `RecordNotFound`; unlike `getOrganizations`, it does not return a nullable organization per actor. | | Load organization context for known actors | `app.certified.actor.getOrganizations` | `actors` is a repeated DID array; results preserve request order and an absent organization is null per result. | | Browse/filter organizations | `app.certified.actor.listOrganizations` | Use for organization-type/visibility filters and pagination. | -| Find organizations by text | `app.certified.actor.searchOrganizations` | Searches associated profile text, with optional organization filters. | +| Find organizations by text | `app.certified.actor.searchOrganizations` | Searches organization legal name and associated profile text, with optional organization filters. | Use `getProfiles`/`getOrganizations` for known DID sets; use `list` or `search` for discovery. A missing optional profile inside an organization view is not the same as a missing organization record. Organization actor views from these four endpoints include `location` (the resolved, raw indexed location record, or null; `organization.record.location` stays the strong reference); use it instead of a second `getLocation` call. Organization views nested in other endpoints' author or actor views do not include it. diff --git a/.agents/skills/hypercerts-api/references/openapi.json b/.agents/skills/hypercerts-api/references/openapi.json index ec2860e..84e2e95 100644 --- a/.agents/skills/hypercerts-api/references/openapi.json +++ b/.agents/skills/hypercerts-api/references/openapi.json @@ -399,13 +399,13 @@ "get": { "operationId": "app_certified_actor_searchOrganizations", "tags": ["app.certified.actor"], - "description": "Searches organizations by associated profile text and optional filters. Filters combine with AND, and values within each array filter are ORed. Authentication is not required.", + "description": "Searches organizations by legal name, profile text and optional filters. Filters combine with AND, and values within each array filter are ORed. Authentication is not required.", "parameters": [ { "name": "search", "in": "query", "required": true, - "description": "Trimmed text is matched literally and case-insensitively as a substring in either displayName or description. Blank text is unrestricted; nonblank text requires an associated profile.", + "description": "Trimmed text is matched literally and case-insensitively as a substring in the organization's legalName or in the associated profile's displayName or description. Organizations without a profile match on legalName alone. Blank text is unrestricted.", "schema": { "type": "string" } }, { diff --git a/.changeset/search-organizations-legal-name.md b/.changeset/search-organizations-legal-name.md new file mode 100644 index 0000000..d7ca90c --- /dev/null +++ b/.changeset/search-organizations-legal-name.md @@ -0,0 +1,6 @@ +--- +'@hypercerts-org/happyview-modules': minor +'@hypercerts-org/api': patch +--- + +`app.certified.actor.searchOrganizations` now also searches an organization's legal name. Organizations without a profile can now be found by legal name; before, they never appeared in search results. diff --git a/docs/openapi.json b/docs/openapi.json index a8c49b7..19ca439 100644 --- a/docs/openapi.json +++ b/docs/openapi.json @@ -448,13 +448,13 @@ "tags": [ "app.certified.actor" ], - "description": "Searches organizations by associated profile text and optional filters. Filters combine with AND, and values within each array filter are ORed. Authentication is not required.", + "description": "Searches organizations by legal name, profile text and optional filters. Filters combine with AND, and values within each array filter are ORed. Authentication is not required.", "parameters": [ { "name": "search", "in": "query", "required": true, - "description": "Trimmed text is matched literally and case-insensitively as a substring in either displayName or description. Blank text is unrestricted; nonblank text requires an associated profile.", + "description": "Trimmed text is matched literally and case-insensitively as a substring in the organization's legalName or in the associated profile's displayName or description. Organizations without a profile match on legalName alone. Blank text is unrestricted.", "schema": { "type": "string" } diff --git a/docs/sources/lexicons/app.certified.actor.searchOrganizations.json b/docs/sources/lexicons/app.certified.actor.searchOrganizations.json index 391d11e..849507d 100644 --- a/docs/sources/lexicons/app.certified.actor.searchOrganizations.json +++ b/docs/sources/lexicons/app.certified.actor.searchOrganizations.json @@ -4,7 +4,7 @@ "defs": { "main": { "type": "query", - "description": "Searches organizations by associated profile text and optional filters. Filters combine with AND, and values within each array filter are ORed. Authentication is not required.", + "description": "Searches organizations by legal name, profile text and optional filters. Filters combine with AND, and values within each array filter are ORed. Authentication is not required.", "parameters": { "type": "params", "required": [ @@ -13,7 +13,7 @@ "properties": { "search": { "type": "string", - "description": "Trimmed text is matched literally and case-insensitively as a substring in either displayName or description. Blank text is unrestricted; nonblank text requires an associated profile." + "description": "Trimmed text is matched literally and case-insensitively as a substring in the organization's legalName or in the associated profile's displayName or description. Organizations without a profile match on legalName alone. Blank text is unrestricted." }, "actors": { "type": "array", diff --git a/happyview-modules/lexicons/app.certified.actor.searchOrganizations.json b/happyview-modules/lexicons/app.certified.actor.searchOrganizations.json index 41b61ea..e46e80b 100644 --- a/happyview-modules/lexicons/app.certified.actor.searchOrganizations.json +++ b/happyview-modules/lexicons/app.certified.actor.searchOrganizations.json @@ -4,14 +4,14 @@ "defs": { "main": { "type": "query", - "description": "Searches organizations by associated profile text and optional filters. Filters combine with AND, and values within each array filter are ORed. Authentication is not required.", + "description": "Searches organizations by legal name, profile text and optional filters. Filters combine with AND, and values within each array filter are ORed. Authentication is not required.", "parameters": { "type": "params", "required": ["search"], "properties": { "search": { "type": "string", - "description": "Trimmed text is matched literally and case-insensitively as a substring in either displayName or description. Blank text is unrestricted; nonblank text requires an associated profile." + "description": "Trimmed text is matched literally and case-insensitively as a substring in the organization's legalName or in the associated profile's displayName or description. Organizations without a profile match on legalName alone. Blank text is unrestricted." }, "actors": { "type": "array", diff --git a/happyview-modules/lua/endpoints/listOrganizations.lua b/happyview-modules/lua/endpoints/listOrganizations.lua index c7127db..9df3911 100644 --- a/happyview-modules/lua/endpoints/listOrganizations.lua +++ b/happyview-modules/lua/endpoints/listOrganizations.lua @@ -314,11 +314,12 @@ local function query_organizations(actors, organization_types, visibility, searc local search_parameter = "$" .. #values values[#values + 1] = PROFILE local profile_collection = "$" .. #values - where[#where + 1] = "EXISTS (SELECT 1 FROM happyview_records AS profile " .. + where[#where + 1] = "(strpos(lower(COALESCE(organization.record::jsonb->>'legalName', '')), lower(" .. + search_parameter .. ")) > 0 OR EXISTS (SELECT 1 FROM happyview_records AS profile " .. "WHERE profile.collection = " .. profile_collection .. " AND profile.did = organization.did " .. "AND profile.rkey = 'self' AND (strpos(lower(COALESCE(profile.record::jsonb->>'displayName', '')), lower(" .. search_parameter .. ")) > 0 OR strpos(lower(COALESCE(profile.record::jsonb->>'description', '')), lower(" .. - search_parameter .. ")) > 0))" + search_parameter .. ")) > 0)))" end if cursor then values[#values + 1] = cursor.t diff --git a/happyview-modules/lua/endpoints/searchOrganizations.lua b/happyview-modules/lua/endpoints/searchOrganizations.lua index c3973c5..ba5ef70 100644 --- a/happyview-modules/lua/endpoints/searchOrganizations.lua +++ b/happyview-modules/lua/endpoints/searchOrganizations.lua @@ -314,11 +314,12 @@ local function query_organizations(actors, organization_types, visibility, searc local search_parameter = "$" .. #values values[#values + 1] = PROFILE local profile_collection = "$" .. #values - where[#where + 1] = "EXISTS (SELECT 1 FROM happyview_records AS profile " .. + where[#where + 1] = "(strpos(lower(COALESCE(organization.record::jsonb->>'legalName', '')), lower(" .. + search_parameter .. ")) > 0 OR EXISTS (SELECT 1 FROM happyview_records AS profile " .. "WHERE profile.collection = " .. profile_collection .. " AND profile.did = organization.did " .. "AND profile.rkey = 'self' AND (strpos(lower(COALESCE(profile.record::jsonb->>'displayName', '')), lower(" .. search_parameter .. ")) > 0 OR strpos(lower(COALESCE(profile.record::jsonb->>'description', '')), lower(" .. - search_parameter .. ")) > 0))" + search_parameter .. ")) > 0)))" end if cursor then values[#values + 1] = cursor.t diff --git a/happyview-modules/lua/shared/organizationList.lua b/happyview-modules/lua/shared/organizationList.lua index c161520..4117d81 100644 --- a/happyview-modules/lua/shared/organizationList.lua +++ b/happyview-modules/lua/shared/organizationList.lua @@ -95,11 +95,12 @@ local function query_organizations(actors, organization_types, visibility, searc local search_parameter = "$" .. #values values[#values + 1] = PROFILE local profile_collection = "$" .. #values - where[#where + 1] = "EXISTS (SELECT 1 FROM happyview_records AS profile " .. + where[#where + 1] = "(strpos(lower(COALESCE(organization.record::jsonb->>'legalName', '')), lower(" .. + search_parameter .. ")) > 0 OR EXISTS (SELECT 1 FROM happyview_records AS profile " .. "WHERE profile.collection = " .. profile_collection .. " AND profile.did = organization.did " .. "AND profile.rkey = 'self' AND (strpos(lower(COALESCE(profile.record::jsonb->>'displayName', '')), lower(" .. search_parameter .. ")) > 0 OR strpos(lower(COALESCE(profile.record::jsonb->>'description', '')), lower(" .. - search_parameter .. ")) > 0))" + search_parameter .. ")) > 0)))" end if cursor then values[#values + 1] = cursor.t diff --git a/happyview-modules/tests/http/actor-organizations.http.test.js b/happyview-modules/tests/http/actor-organizations.http.test.js index 4e84a85..64136c3 100644 --- a/happyview-modules/tests/http/actor-organizations.http.test.js +++ b/happyview-modules/tests/http/actor-organizations.http.test.js @@ -195,7 +195,7 @@ test("listOrganizations combines exact type and visibility filters and leaves om ); }); -test("searchOrganizations matches full trimmed literal profile text and excludes organizations without profiles", async () => { +test("searchOrganizations matches full trimmed literal profile text", async () => { const result = await get("app.certified.actor.searchOrganizations", { search: " Forest %_ Commons ", actors: [unlistedOrganization.did], @@ -204,12 +204,35 @@ test("searchOrganizations matches full trimmed literal profile text and excludes assert.equal(result.actors[0].did, unlistedOrganization.did); assert.equal(result.actors[0].profile.record.displayName, "Forest %_ Commons"); assert.deepEqual(result.actors[0].organization.record, unlistedOrganization.record); +}); + +test("searchOrganizations matches legalName case-insensitively, including organizations without a profile", async () => { + // legalName is absent from the river profile's displayName and description. + const legalNameOnly = await get("app.certified.actor.searchOrganizations", { + search: "CATCHMENT trust", + actors: [actorFixtureDids.river], + }); + assert.deepEqual( + legalNameOnly.actors.map(({ did }) => did), + [actorFixtureDids.river], + ); + assert.ok(legalNameOnly.actors[0].profile); + + const profileless = await get("app.certified.actor.searchOrganizations", { + search: "weavers coop", + actors: [profilelessOrganization.did], + }); + assert.deepEqual( + profileless.actors.map(({ did }) => did), + [profilelessOrganization.did], + ); + assert.equal(profileless.actors[0].profile, null); - const absentProfile = await get("app.certified.actor.searchOrganizations", { + const noMatch = await get("app.certified.actor.searchOrganizations", { search: "community", actors: [profilelessOrganization.did], }); - assert.deepEqual(absentProfile.actors, []); + assert.deepEqual(noMatch.actors, []); }); test("listOrganizations paginates stably in both directions without skipping equal-timestamp records", async () => { diff --git a/happyview-modules/tests/http/fixtures/actors.fixture.js b/happyview-modules/tests/http/fixtures/actors.fixture.js index c0fbcd9..6d76098 100644 --- a/happyview-modules/tests/http/fixtures/actors.fixture.js +++ b/happyview-modules/tests/http/fixtures/actors.fixture.js @@ -106,6 +106,7 @@ const records = [ collection: organizationCollection, rkey: "self", record: { + legalName: "Chhu Catchment Trust", organizationType: ["nonprofit"], location: locationRefs.river, visibility: "public", @@ -117,6 +118,7 @@ const records = [ collection: organizationCollection, rkey: "self", record: { + legalName: "Bumthang Weavers Cooperative", organizationType: ["community"], createdAt, }, diff --git a/happyview-modules/tests/unit/tooling/organization.test.js b/happyview-modules/tests/unit/tooling/organization.test.js index 0080a8e..48e1713 100644 --- a/happyview-modules/tests/unit/tooling/organization.test.js +++ b/happyview-modules/tests/unit/tooling/organization.test.js @@ -470,7 +470,7 @@ assert(#calls == 2 and calls[2].values[1] == '${profileCollection}' and calls[2] }); }); -test("searchOrganizations binds the complete trimmed literal and excludes actors without a profile", () => { +test("searchOrganizations binds the complete trimmed literal and also matches legalName without requiring a profile", () => { const organization = organizationRow(actor, "search", { organizationType: ["nonprofit"], createdAt: "2025-01-01T00:00:00Z", @@ -490,7 +490,8 @@ test("searchOrganizations binds the complete trimmed literal and excludes actors assertions: ` assert(#result.actors == 1 and result.actors[1].did == '${actor}', 'actor result') assert(calls[1].sql:find('strpos(lower', 1, true), 'literal substring SQL') -assert(calls[1].sql:find('EXISTS', 1, true) and calls[1].sql:find('profile.did = organization.did', 1, true), 'profile existence predicate') +assert(calls[1].sql:find("organization.record::jsonb->>'legalName'", 1, true), 'legalName predicate') +assert(calls[1].sql:find(') > 0 OR EXISTS (SELECT 1', 1, true) and calls[1].sql:find('profile.did = organization.did', 1, true), 'profile alternative predicate') assert(not calls[1].sql:find('Forest', 1, true), 'search text must be a bound SQL value') assert(calls[1].sql:find('organization.did IN ($2)', 1, true), 'search retains its actors filter') assert(calls[1].values[2] == '${actor}' and calls[1].values[3] == 'nonprofit' and calls[1].values[4] == 'Forest %_ Restoration', table.concat(calls[1].values, '|')) diff --git a/packages/api/src/lexicons/app/certified/actor/searchOrganizations.defs.ts b/packages/api/src/lexicons/app/certified/actor/searchOrganizations.defs.ts index 211e249..0626cc4 100644 --- a/packages/api/src/lexicons/app/certified/actor/searchOrganizations.defs.ts +++ b/packages/api/src/lexicons/app/certified/actor/searchOrganizations.defs.ts @@ -20,7 +20,7 @@ export const $output = /*#__PURE__*/ l.payload("application/json", /*#__PURE__*/ export type $Output = l.InferPayload; export type $OutputBody = l.InferPayloadBody; -/** Searches organizations by associated profile text and optional filters. Filters combine with AND, and values within each array filter are ORed. Authentication is not required. */ +/** Searches organizations by legal name, profile text and optional filters. Filters combine with AND, and values within each array filter are ORed. Authentication is not required. */ const main = /*#__PURE__*/ l.query($nsid, $params, $output, ["InvalidRequest","OrganizationQueryFailed"]); export { main };