Skip to content

Refactor of GetDigitalSpecimenComplete service and setup of data mapper to UI#244

Merged
MelanieTheLion merged 9 commits into
mainfrom
feature/refactor-ds-data-service
Apr 21, 2026
Merged

Refactor of GetDigitalSpecimenComplete service and setup of data mapper to UI#244
MelanieTheLion merged 9 commits into
mainfrom
feature/refactor-ds-data-service

Conversation

@MelanieTheLion
Copy link
Copy Markdown
Collaborator

In this PR:

  • Rewrote service getDigitalSpecimenComplete to use tanstack and execute dataMapper for digital specimen
  • Added data mapper function to map data to UI in one general place, instead of multiple places in the app

This service and data mapper is not used yet, but is tested. Implementation to UI will follow this PR due to complexity.

@MelanieTheLion MelanieTheLion self-assigned this Apr 3, 2026
* @param ds The digital specimen object
* @returns Either the accepted identification or, if there is none, the first identification it can find
*/
const getAcceptedIdentification = (ds: any) => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This can return undefined when identifications is falsy, is this expected? Because the @returns suggests it will always return an identification

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I adjusted the function to also return a null in case of no identifications whatsoever. Probably a bit of an edge case, but my mapper works also if no identifications are found.

mappedFields[fieldKey] = {
label: config.label,
value: config.resolve(ds, acceptedIdentification),
isHtml: !!config.isHtml,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I know !! is a quite a common trick, but it is not explicit, !Boolean(config.isHtml), makes it more clear that it converts it to an boolean and invert it

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Changed it to Boolean(config.isHtml). Turned out to be the right one :)

return Object.entries(DIGITAL_SPECIMEN_SCHEMA_MAP as SchemaMap).reduce((accumulator, [groupKey, fields]) => {
const mappedFields: Record<string, UIProperty> = {};

Object.entries(fields).forEach(([fieldKey, config]) => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You could make your code a bit more compact and immutable, but this is optional and opinionated.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries#object_transformations

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I like this approach! I addressed it as well as I could. Let me know what you think.


accumulator[groupKey as keyof DigitalSpecimenUIModel] = mappedFields;
return accumulator;
}, {} as DigitalSpecimenUIModel);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

.reduce accepts a generic what it should return, this is more typesafe

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

What it should return added.

Copy link
Copy Markdown
Contributor

@southeo southeo left a comment

Choose a reason for hiding this comment

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

🐝

Comment thread src/services/digitalSpecimenService/getDigitalSpecimenComplete.ts Outdated
Comment thread src/services/digitalSpecimenService/getDigitalSpecimenComplete.ts
console.error('Error fetching the digital specimen:', error);

/* Rethrow error for useQuery */
throw error;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why catch here just to log it? why not just add the logging on line 28?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

In the future, this will be caught by some form of error notification system I still have to implement. The error in the catch can be any kind of error, 500s, 404s. The error on line 28 is only thrown if the status is OK (200, 201), but the response that we get does not match what we expect at all.
It is an edge case, because if it is OK, your response would probably (assumption) be what we expect.

The catch takes care of my error codes. So separate error instances.

Comment thread src/utils/DataMappers/schemas/DigitalSpecimenSchema.ts Outdated
resolve: (_, { acceptedIdentification }) => acceptedIdentification?.["dwc:specificEpithet"]
},
nomenClaturalCode: {
label: 'Nomen/Clatural Code',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this a typo?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Haha yes. Sometimes I don't know what I am talking about

Comment thread src/utils/DataMappers/types/dataMapperTypes.ts Outdated
@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@southeo southeo left a comment

Choose a reason for hiding this comment

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

🎏

CVE-2026-33636 No newline at end of file
CVE-2026-33636

# lodash-es is pinned as a dependency on a vulnerable version in formik and cannot be upgraded by us Apr-3-2026
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

are you sure this cannot be changed with overrides ? Or are all the earlier packages vulnerable and new versions not compatible with formik ?
https://docs.npmjs.com/cli/v8/configuring-npm/package-json#overrides

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think I could. I have a follow up user story to address all these trivyignores, because the list is getting long and I'd like to clean them up.

@MelanieTheLion MelanieTheLion merged commit 1941cef into main Apr 21, 2026
4 checks passed
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