How to create TEI, Enrollment & Events all in one mutation request?

Hi there,

I have just started my way in DHIS app development and have a questions on how can I create a TEI, enrollment and events all together like below in one mutation request by using dhis-runtime:

{
  "trackedEntityInstances": [
    {
      "created": "2024-06-24T20:01:53.782",
      "orgUnit": "ohhWSkJKVLf",
      "createdAtClient": "2024-06-24T20:01:53.782",
      "trackedEntityType": "gAkFSYjEBoP",
      "storedBy": "admin",
      "createdByUserInfo": {
        "uid": "M5zQapPyTZI",
        "firstName": "admin",
        "surname": "admin",
        "username": "admin"
      },
      "enrollments": [
        {
          "storedBy": "admin",
          "createdAtClient": "2024-06-24T20:01:53.955",
          "program": "vXMY3ACeWzL",
          "created": "2024-06-24T20:01:53.952",
          "orgUnit": "ohhWSkJKVLf",
          "trackedEntityType": "gAkFSYjEBoP",
          "enrollmentDate": "2024-06-24T00:00:00.000",
          "incidentDate": "2024-06-24T00:00:00.000",
          "status": "ACTIVE",
          "createdByUserInfo": {
            "uid": "M5zQapPyTZI",
            "firstName": "admin",
            "surname": "admin",
            "username": "admin"
          },
          "events": [
            {
              "storedBy": "admin",
              "dueDate": "2024-06-25T08:17:58.781",
              "program": "vXMY3ACeWzL",
              "programStage": "LknBcxH89wZ",
              "orgUnit": "ohhWSkJKVLf",
              "status": "ACTIVE",
              "eventDate": "2024-06-25T00:00:00.000",
              "attributeCategoryOptions": "xYerKDKCefk",
              "created": "2024-06-25T08:17:58.781",
              "attributeOptionCombo": "HllvX50cXC0",
              "createdByUserInfo": {
                "uid": "M5zQapPyTZI",
                "firstName": "admin",
                "surname": "admin",
                "username": "admin"
              },
              "dataValues": [
                {
                  "lastUpdated": "2024-06-25T08:18:09.089",
                  "created": "2024-06-25T08:18:09.089",
                  "dataElement": "BRYWi0UnxPL",
                  "value": "YNA_yes",
                  "providedElsewhere": false,
                  "createdByUserInfo": {
                    "uid": "M5zQapPyTZI",
                    "firstName": "admin",
                    "surname": "admin",
                    "username": "admin"
                  }
                },
                {
                  "lastUpdated": "2024-06-25T08:18:14.268",
                  "created": "2024-06-25T08:18:14.268",
                  "dataElement": "rklRSkweZ8E",
                  "value": "true",
                  "providedElsewhere": false,
                  "createdByUserInfo": {
                    "uid": "M5zQapPyTZI",
                    "firstName": "admin",
                    "surname": "admin",
                    "username": "admin"
                  }
                }
              ]
            },
            {
              "program": "vXMY3ACeWzL",
              "programStage": "EJYTLHjoVGH",
              "orgUnit": "ohhWSkJKVLf",
              "status": "COMPLETED",
              "eventDate": "2024-06-24",
              "completedDate": "2024-06-24",
              "dataValues": [
                {
                  "value": 1,
                  "dataElement": "Dcdgn4pvrcZ"
                },
                {
                  "value": 6,
                  "dataElement": "OkvIx4cjxkM"
                },
                {
                  "value": "true",
                  "dataElement": "lZ8xgz8bX3C"
                },
                {
                  "value": "some_text",
                  "dataElement": "ce6jIeCeMdK"
                },
                {
                  "value": "pl_NGO",
                  "dataElement": "n1zMMq13TKE"
                }
              ]
            }
          ],
          "attributes": [
            {
              "lastUpdated": "2024-06-24T20:01:53.803",
              "storedBy": "admin",
              "created": "2024-06-24T20:01:53.803",
              "valueType": "TEXT",
              "attribute": "kmzHA3cJtLx",
              "value": "АХUR115"
            },
            {
              "lastUpdated": "2024-06-24T20:01:53.787",
              "storedBy": "admin",
              "created": "2024-06-24T20:01:53.787",
              "valueType": "TEXT",
              "attribute": "H8beMnOxMzX",
              "value": "plhiv"
            }
          ]
        }
      ],
      "attributes": [
        {
          "lastUpdated": "2024-06-24T20:01:53.803",
          "storedBy": "admin",
          "created": "2024-06-24T20:01:53.803",
          "valueType": "TEXT",
          "attribute": "kmzHA3cJtLx",
          "value": "АХUR115"
        },
        {
          "lastUpdated": "2024-06-24T20:01:53.787",
          "storedBy": "admin",
          "created": "2024-06-24T20:01:53.787",
          "valueType": "TEXT",
          "attribute": "H8beMnOxMzX",
          "value": "plhiv"
        }
      ]
    }
  ]
}

In case if I used normal API point, I could create such nested TEI by posting all TEI to the /api/trackedEntityInstances API point. But I have no idea how to use useDataMutation hook and its mutation payload\request part, as couldn’t find clear guidance on the portal and documentations.

If it is not possible, then please guide me the steps which to be done to create such nested TEI with enrollments & events in other ways.

Thank you

Regards,
Ulanbek

That’s amazing @Ulanbek ! :clap:

Have you had the chance to view the web app workshop videos on the YouTube channel, specifically the following:

# Web App Development Workshop - Day 2 - Queries and mutations

# Web App Development Workshop - Day 2 - API useConfig

# Web App Development Workshop - Day 2 - Web App Q&A and Project 1

Hi @Gassim

Thank you for your reply. I have checked all development academy materials on github, and didn’t find anything, which could enlighten the questions what I have here…
Maybe @austin @Rene can help me?

Regards,
Ulanbek

1 Like

I have just started my way in DHIS app development

That’s great @Ulanbek. Welcome to our developer community!

If you are not already a member, you may want to join us on in the DHIS2 developer slack workspace: https://dhis2-dev-community.slack.com/

how can I create a TEI, enrollment and events all together like below in one mutation request by using dhis-runtime

The links that @Gassim shared are great for an introduction to understand how to perform mutations with app-runtime and it’s useful to look at them.

To create a new metadata object, you can use the useDataMutation hook (see documentation). For your case, I would particularly suggest you look at the link at the bottom of that page which shows an example of a CREATE mutation. You should be able to pass anything in the data argument that you would can normally send to the api directly, so you should be able to define the mutation with that data you shared in your example and it should work similar to a direct post to the api (e.g. from the command line with curl).

You may also want to install the Data Query Playground app (available from the App Hub) on your testing instance. You can use the app to try out mutations/queries before you put them into your app.

Let us know how it goes.

Thank you @tzemp and @Gassim for your reply.

In general is it possible to create a TEI as I showed above with the dhis2 runtime?

1 Like

In general is it possible to create a TEI as I showed above with the dhis2 runtime?

Yes @Ulanbek; that should be possible.

If you want to give it a try (e.g. by using the Data Query app on Login app | DHIS2), we could take a look at your code/query definition if you run into any problems.

1 Like

Hi @tzemp

I have managed to create nested TEI, Enrollment and events in one mutation request. Have another question:
Could you guide me a correct way of creating requests like: …/api/trackedEntityInstances/query.json?..

I do understand resource will be trackedEntityInstances, but what about query.son? Where it should be and what the object name will be?

Thank you

I have managed to create nested TEI, Enrollment and events in one mutation request.

Great :tada:

Could you guide me a correct way of creating requests like: …/api/trackedEntityInstances/query.json?

The resource is is flexible, so it should accept any string you want to add after ‘api’, so I think you can define a query for this like:

{
  resource: 'trackedEntityInstances/query.json'
    params: {
      ...yourParamsHere
    }
}

The example in the documentation (https://developers.dhis2.org/docs/app-runtime/hooks/useDataQuery#static-query) points to indicators.json for example (and also shows some sample parameters, but you can pass define any parameter that you could also put in a query to the api)

1 Like

Yes, I did it as you showed. But I was thinking maybe there is another more “correct” way how it can be done :slight_smile:
Thank you @tzemp

2 Likes