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

(fix) revise logic for saving attachments #270

Merged
merged 6 commits into from
May 20, 2024

Conversation

arodidev
Copy link
Contributor

Requirements

  • This PR has a title that briefly describes the work done including the ticket number. If there is a ticket, make sure your PR title includes a conventional commit label. See existing PR titles for inspiration.
  • My work conforms to the OpenMRS 3.0 Styleguide and design documentation.
  • My work includes tests or is validated by existing tests.

Summary

This PR seeks to ensure that no request is made to the attachments endpoint if no field bearing an attachment was filled at the point of submission.

Screenshots

Related Issue

Other

@arodidev arodidev requested a review from samuelmale May 13, 2024 21:19
if (EncounterFormManager.evaluatedAttachmentFields(fields)?.length) {
try {
const attachmentsResponse = await Promise.all(
EncounterFormManager.saveAttachments(fields, savedEncounter, abortController),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just make sure that if there are zero attachments this function returns an empty value for attachmentsResponse and implement the necessary filters internally?

Copy link
Contributor Author

@arodidev arodidev May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean something like this?

const evaluatedFields = EncounterFormManager.evaluatedAttachmentFields(fields);
if (evaluatedFields?.length) {
  try {
    const attachmentsResponse = await Promise.all(
      EncounterFormManager.saveAttachments(fields, savedEncounter, abortController),
    );
    if (attachmentsResponse?.length) {
      showSnackbar({
        title: t('attachmentsSaved', 'Attachment(s) saved successfully'),
        kind: 'success',
        isLowContrast: true,
      });
    }
  } catch (error) {
    const errorMessages = extractErrorMessagesFromResponse(error);
    return Promise.reject({
      title: t('errorSavingAttachments', 'Error saving attachment(s)'),
      subtitle: errorMessages.join(', '),
      kind: 'error',
      isLowContrast: false,
    });
  }
} else {
  return [];
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the logic can be handled in the EncounterFormManager.saveAttachments function,

  static saveAttachments(fields: FormField[], encounter: OpenmrsEncounter, abortController: AbortController) {
    const complexFields = fields?.filter((field) => field?.questionOptions.rendering === 'file');
    return complexFields.map((field) => {
      const patientUuid = typeof encounter?.patient === 'string' ? encounter?.patient : encounter?.patient?.uuid;
      return saveAttachment(
        patientUuid,
        field,
        field?.questionOptions.concept,
        new Date().toISOString(),
        encounter?.uuid,
        abortController,
      );
    });
  }

Somewhere in here, before return saveAttachment(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification @CynthiaKamau

@arodidev arodidev merged commit d401560 into openmrs:main May 20, 2024
4 checks passed
vasharma05 pushed a commit that referenced this pull request May 27, 2024
* reivse logic for saving attachments

* move logic to encounter form manager

* refactor

* cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants