Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MockClient#search on _tag not working for only some resources #4582

Open
dillonstreator opened this issue May 22, 2024 · 1 comment
Open
Assignees

Comments

@dillonstreator
Copy link
Contributor

I'm attempting to use the _tag search on token with system filter and this works properly with the MedplumClient but the MockClient is unable to find the resource(s).

Version: @medplum/[email protected]

import { createReference } from '@medplum/core';
import { MockClient } from '@medplum/mock';

const medplum = new MockClient({ debug: true });

const patient = await medplum.createResource({
  resourceType: 'Patient',
  meta: {
    tag: [
      {
        system: 'http:https://medplum.com/example',
        code: 'somevalue',
      },
    ],
  },
});

await medplum.createResource({
  resourceType: 'Practitioner',
  meta: {
    tag: [
      {
        system: 'http:https://medplum.com/example',
        code: 'somevalue',
      },
    ],
  },
});

await medplum.createResource({
  resourceType: 'CareTeam',
  meta: {
    tag: [
      {
        system: 'http:https://medplum.com/example',
        code: 'somevalue',
      },
    ],
  },
});

await medplum.createResource({
  resourceType: 'ClinicalImpression',
  status: 'completed',
  subject: createReference(patient),
  meta: {
    tag: [
      {
        system: 'http:https://medplum.com/example',
        code: 'somevalue',
      },
    ],
  },
});

const patientSearchResult = await medplum.searchOne('Patient', '_tag=http:https://medplum.com/example|somevalue');
console.log(patientSearchResult); // returns patient

const practitionerSearchResult = await medplum.searchOne('Practitioner', '_tag=http:https://medplum.com/example|somevalue');
console.log(practitionerSearchResult); // returns practitioner

const careTeamSearchResult = await medplum.searchOne('CareTeam', '_tag=http:https://medplum.com/example|somevalue');
console.log(careTeamSearchResult); // undefined

const clinicalImpressionSearchResult = await medplum.searchOne(
  'ClinicalImpression',
  '_tag=http:https://medplum.com/example|somevalue',
);
console.log(clinicalImpressionSearchResult); // undefined

Creation & read order do not matter - CareTeam & ClinicalImpression always return undefined.

Here is the debug output:

ockClient POST fhir/R4/Patient
MockClient {
  "resourceType": "Patient",
  "meta": {
    "tag": [
      {
        "system": "http:https://medplum.com/example",
        "code": "somevalue"
      }
    ],
    "versionId": "7a7d3bf8-91a9-4b85-bca0-d4cd90b7174d",
    "lastUpdated": "2024-05-22T21:15:52.832Z"
  },
  "id": "c05e7c96-81ae-439b-b0c3-4df10179ceee"
}
MockClient POST fhir/R4/Practitioner
MockClient {
  "resourceType": "Practitioner",
  "meta": {
    "tag": [
      {
        "system": "http:https://medplum.com/example",
        "code": "somevalue"
      }
    ],
    "versionId": "46a01ecb-f22b-4125-ab5b-4c291f437603",
    "lastUpdated": "2024-05-22T21:15:52.832Z"
  },
  "id": "bd3f21c1-b201-4cfb-82e2-8eb5dc2ec6de"
}
MockClient POST fhir/R4/CareTeam
MockClient {
  "resourceType": "CareTeam",
  "meta": {
    "tag": [
      {
        "system": "http:https://medplum.com/example",
        "code": "somevalue"
      }
    ],
    "versionId": "7ba98555-310a-4f80-a4be-8f3795afd3d5",
    "lastUpdated": "2024-05-22T21:15:52.832Z"
  },
  "id": "77a4fd29-b148-4c4e-af86-cbc9fde73ba0"
}
MockClient POST fhir/R4/ClinicalImpression
MockClient {
  "resourceType": "ClinicalImpression",
  "status": "completed",
  "subject": {
    "reference": "Patient/c05e7c96-81ae-439b-b0c3-4df10179ceee"
  },
  "meta": {
    "tag": [
      {
        "system": "http:https://medplum.com/example",
        "code": "somevalue"
      }
    ],
    "versionId": "20c59701-4112-47cc-b328-8963aa89c1a1",
    "lastUpdated": "2024-05-22T21:15:52.832Z"
  },
  "id": "fac8336c-a089-4e68-8acf-3ce0ea643dd6"
}
MockClient GET fhir/R4/Patient?_count=1&_tag=http%3A%2F%2Fmedplum.com%2Fexample%7Csomevalue
MockClient {
  "resourceType": "Bundle",
  "type": "searchset",
  "entry": [
    {
      "resource": {
        "resourceType": "Patient",
        "meta": {
          "tag": [
            {
              "system": "http:https://medplum.com/example",
              "code": "somevalue"
            }
          ],
          "versionId": "7a7d3bf8-91a9-4b85-bca0-d4cd90b7174d",
          "lastUpdated": "2024-05-22T21:15:52.832Z"
        },
        "id": "c05e7c96-81ae-439b-b0c3-4df10179ceee"
      }
    }
  ],
  "total": 1
}
{
  resourceType: "Patient",
  meta: {
    tag: [
      [Object ...]
    ],
    versionId: "7a7d3bf8-91a9-4b85-bca0-d4cd90b7174d",
    lastUpdated: "2024-05-22T21:15:52.832Z",
  },
  id: "c05e7c96-81ae-439b-b0c3-4df10179ceee",
}
MockClient GET fhir/R4/Practitioner?_count=1&_tag=http%3A%2F%2Fmedplum.com%2Fexample%7Csomevalue
MockClient {
  "resourceType": "Bundle",
  "type": "searchset",
  "entry": [
    {
      "resource": {
        "resourceType": "Practitioner",
        "meta": {
          "tag": [
            {
              "system": "http:https://medplum.com/example",
              "code": "somevalue"
            }
          ],
          "versionId": "46a01ecb-f22b-4125-ab5b-4c291f437603",
          "lastUpdated": "2024-05-22T21:15:52.832Z"
        },
        "id": "bd3f21c1-b201-4cfb-82e2-8eb5dc2ec6de"
      }
    }
  ],
  "total": 1
}
{
  resourceType: "Practitioner",
  meta: {
    tag: [
      [Object ...]
    ],
    versionId: "46a01ecb-f22b-4125-ab5b-4c291f437603",
    lastUpdated: "2024-05-22T21:15:52.832Z",
  },
  id: "bd3f21c1-b201-4cfb-82e2-8eb5dc2ec6de",
}
MockClient GET fhir/R4/CareTeam?_count=1&_tag=http%3A%2F%2Fmedplum.com%2Fexample%7Csomevalue
MockClient {
  "resourceType": "Bundle",
  "type": "searchset",
  "entry": [],
  "total": 0
}
undefined
MockClient GET fhir/R4/ClinicalImpression?_count=1&_tag=http%3A%2F%2Fmedplum.com%2Fexample%7Csomevalue
MockClient {
  "resourceType": "Bundle",
  "type": "searchset",
  "entry": [],
  "total": 0
}
undefined
@dillonstreator dillonstreator changed the title MockClient#search on _tag not working for only some resources MockClient#search on _tag not working for only some resources May 22, 2024
@ThatOneBro ThatOneBro self-assigned this May 29, 2024
@ThatOneBro ThatOneBro added this to the May 31st, 2024 milestone May 29, 2024
@reshmakh reshmakh modified the milestones: May 31st, 2024, June 30, 2024 Jun 2, 2024
@ThatOneBro
Copy link
Member

Hi @dillonstreator, sorry for the late response. Currently not all StructureDefinitions and SearchParameterss are indexed by default in MockClient. This is mostly done to cut down on Storybook load time and generally keep MockClient lightweight since adding all StructureDefinitions upfront can add a lot of extra load time in the browser and generally make browser bundles super large.

We are looking at merging this PR (#4391) to make this more clear when you run into a missing SearchParameter for a resource type in MockClient

We are just trying to figure out the implications of matchesSearchRequest() throwing in production settings since this will change behavior to throw when an unknown SearchParameter is encountered which may cause some places where invalid search parameters might be silently ignored to suddenly throw after the change. That doesn't seem to be the case from our cursory research, but we are kind of slow walking it for now to make sure.

Regardless here is the solution for your immediate problem if you are still stuck:

If you're using MockClient in jest you can add this:

import {
  indexSearchParameterBundle,
  indexStructureDefinitionBundle,
} from '@medplum/core';
import { SEARCH_PARAMETER_BUNDLE_FILES, readJson } from '@medplum/definitions';
import { Bundle, SearchParameter } from '@medplum/fhirtypes';

describe('Example tests', () => {
  beforeAll(() => {
    indexStructureDefinitionBundle(readJson('fhir/r4/profiles-types.json') as Bundle);
    indexStructureDefinitionBundle(readJson('fhir/r4/profiles-resources.json') as Bundle);
    for (const filename of SEARCH_PARAMETER_BUNDLE_FILES) {
      indexSearchParameterBundle(readJson(filename) as Bundle<SearchParameter>);
    }
  });
  
  
});

Sorry for any confusion; we know this is a rough edge for sure but it's on our radar. Let us know if you need any more help in this regard or if this unblocks you 😄

@reshmakh reshmakh modified the milestones: June 30, 2024, July 31, 2024 Jul 1, 2024
@rahul1 rahul1 modified the milestones: July 31, 2024, August 31, 2024 Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ⏰ Paused
Development

No branches or pull requests

4 participants