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

Document API / document merging #27

Closed
3 tasks done
clemens-msupply opened this issue May 23, 2022 · 0 comments
Closed
3 tasks done

Document API / document merging #27

clemens-msupply opened this issue May 23, 2022 · 0 comments
Labels

Comments

@clemens-msupply
Copy link
Collaborator

clemens-msupply commented May 23, 2022

Integrate the document api (#19) to:

  • fetch documents (e.g. needed to view full patient details including none standard fields)
  • update document (e.g. needed to create patients)
  • implement document updates that require auto merge

(document api is currently in PR

Example to add a new document and update it:

  // get the graphql api
  const api = getApi();

  // pick one store
  const stores = (await api.getStores()).stores.nodes;
  const store0 = stores[0];
  console.log(`Store: ${store0.id}, code: ${store0.code}`);

  // insert first version of the document
  const u0 = await api.updateDocument({
    storeId: store0.id,
    input: {
      author: "me",
      type: "testtype",
      data: {
        version: 1,
        txt: "hello",
      },
      name: docName,
      parents: [],
      schemaId: undefined,
      timestamp: new Date().toISOString(),
    },
  });
  console.log(u0);

  // fetch the latest version of the document
  const filter = {
    name: {
      equalTo: docName,
    },
  };
  const d1 = await api.getDocuments({
    storeId: store0.id,
    filter,
  });
  console.log(d1);

  // modify the last version and set parents correctly
  const u1 = await api.updateDocument({
    storeId: store0.id,
    input: {
      author: "me",
      type: "testtype",
      data: {
        version: 2,
        txt: "hello update",
        more: "more",
      },
      name: docName,
      parents: [d1.documents.nodes[0].id],
      schemaId: undefined,
      timestamp: new Date().toISOString(),
    },
  });
  console.log(u1);
  const exhaustiveCheck = (_: never) => {};

  // example error handling
  let docId: string = "";
  switch (u1.updateDocument.__typename) {
    case "DocumentNode":
      docId = u1.updateDocument.id;
      break;
    case "UpdateDocumentError": {
      const error = u1.updateDocument.error;
      switch (error.__typename) {
        case "MergeRequiredError":
          // TODO: merge and resubmit
          throw Error("unhandled");
        default:
        //exhaustiveCheck(error);
      }
      throw Error("unhandled");
    }
    default:
      exhaustiveCheck(u1.updateDocument);
  }

  // do another update
  const u2 = await api.updateDocument({
    storeId: store0.id,
    input: {
      author: "me",
      type: "testtype",
      data: {
        version: 3,
        txt: "hello update 2",
        more: "more...",
      },
      name: docName,
      parents: [docId],
      schemaId: undefined,
      timestamp: new Date().toISOString(),
    },
  });
  console.log(u2);

Merge concurrently edited documents

The API may detect concurrent edits which requires a merge of both versions.
In this case the API returns a MergeRequiredError.
The MergeRequiredError error contains a proposed auto-merged document.
If the FE is happy the auto-merged version can be used to update the document.
This operation will succeeds as long as there wasn't a further concurrent edit.

@clemens-msupply clemens-msupply changed the title FE: document API / document merging Document API / document merging May 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant