Avoid duplicate Patient on resource create

906 Views Asked by At

When sending this request multiple times, I get multiple Patients created.

{
  "resourceType": "Patient",
  "identifier": [
    {
      "use": "usual",
      "system": "urn:oid:2.16.840.1.113883.19.5",
      "value": "12345"
    }
  ],
  "active": true,
  "name": [
    {
      "family": "Levin",
      "given": [
        "Omri"
      ]
    }
  ],
  "gender": "male",
  "birthDate": "1980-09-24"
}

My assumption is that when I provide the identifier, it would reject the 2nd call, as that PAtient already exists. But seems that is not the case? How do I avoid these duplications?

(Tried it both on Azure and on Google cloud - both with the same result)

2

There are 2 best solutions below

2
On BEST ANSWER

I had to dig deeper into the docs. And the answer is here - https://www.hl7.org/fhir/http.html#ccreate

In my case adding the header If-None-Exist: identifier=urn:oid:2.16.840.1.113883.19.5|12345 did the trick on Azure. On Google cloud, however, if trying to use V1 of the API we get an error for adding that header:

{
    "issue": [
        {
            "code": "value",
            "details": {
                "text": "invalid_query"
            },
            "diagnostics": "conditional operations are not supported in API version v1",
            "severity": "error"
        }
    ],
    "resourceType": "OperationOutcome"
}

To overcome that we need to use v1beta1. So URL should change

- https://healthcare.googleapis.com/v1/projects/...
+ https://healthcare.googleapis.com/v1beta1/projects/...

Read more about the "v1" and "new v1beta1" difference here

0
On

Altarnatively you could use conditional update/create (PUT with search/match criteria) instead of a POST i.e. PUT Patient?identifier=urn:oid:2.16.840.1.113883.19.5|12345 with the same payload you have in your question.

Or you can POST a Bundle of one or more resources, also using PUT with search criteria :

    {
  "resourceType": "Bundle",
  "id": "bundle-transaction",
  "meta": {
    "lastUpdated": "2014-08-18T01:43:30Z"
  },
  "type": "transaction",
  "entry": [
    {
      "fullUrl": "urn:uuid:61ebe359-bfdc-4613-8bf2-c5e300945f0a",
      "resource": {
      "resourceType": "Patient",
      "identifier": [
        {
          "use": "usual",
          "system": "urn:oid:2.16.840.1.113883.19.5",
          "value": "12345"
        }
      ],
      "active": true,
      "name": [
        {
          "family": "Levin",
          "given": [
        "Omri"
          ]
        }
      ],
      "gender": "male",
  "birthDate": "1980-09-24"
      },
      "request": {
        "method": "PUT",
        "url": "Patient?identifier=urn:oid:2.16.840.1.113883.19.5|12345"
      }
    }
  ]
}

When using a Bundle, If no Patient resource with the same identifier already exists then the OperatioOutcome will return a 201 Created otherwise you will get a 200 OK and the resource will be updated, creating a new version of it.

You can read more about bundles here https://www.hl7.org/fhir/bundle.html