Skip to content

Commit

Permalink
refactor: reverse the order of getDimensionsWithSuffix
Browse files Browse the repository at this point in the history
  • Loading branch information
martinkrulltott committed Feb 26, 2024
1 parent 76a938b commit 0f23372
Showing 1 changed file with 47 additions and 51 deletions.
98 changes: 47 additions & 51 deletions src/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,62 +94,58 @@ export const getDimensionsWithSuffix = ({
})

if (
[OUTPUT_TYPE_ENROLLMENT, OUTPUT_TYPE_TRACKED_ENTITY].includes(inputType)
![OUTPUT_TYPE_ENROLLMENT, OUTPUT_TYPE_TRACKED_ENTITY].includes(
inputType
)
) {
const dimensionsWithSuffix = dimensions.map((dimension) => {
if (
[DIMENSION_TYPE_DATA_ELEMENT, DIMENSION_TYPE_PERIOD].includes(
dimension.dimensionType || dimension.dimensionItemType
)
) {
const duplicates = dimensions.filter(
(d) =>
d.dimensionId === dimension.dimensionId &&
d !== dimension &&
((dimension.programId && d.programId) ||
(dimension.programStageId && d.programStageId))
)
return dimensions
}

if (duplicates.length > 0) {
const sameProgramId = duplicates.find(
(dup) => dup.programId === dimension.programId
)
const thirdPartyDuplicates = duplicates
.filter((dup) => dup.programId !== dimension.programId)
.find((dpid) =>
duplicates.find(
(dup) =>
dup.programStageId !==
dpid.programStageId &&
dup.programId === dpid.programId
)
return dimensions.map((dimension) => {
if (
[DIMENSION_TYPE_DATA_ELEMENT, DIMENSION_TYPE_PERIOD].includes(
dimension.dimensionType || dimension.dimensionItemType
)
) {
const duplicates = dimensions.filter(
(d) =>
d.dimensionId === dimension.dimensionId &&
d !== dimension &&
((dimension.programId && d.programId) ||
(dimension.programStageId && d.programStageId))
)

if (duplicates.length > 0) {
const sameProgramId = duplicates.find(
(dup) => dup.programId === dimension.programId
)
const thirdPartyDuplicates = duplicates
.filter((dup) => dup.programId !== dimension.programId)
.find((dpid) =>
duplicates.find(
(dup) =>
dup.programStageId !== dpid.programStageId &&
dup.programId === dpid.programId
)
)

if (sameProgramId || thirdPartyDuplicates) {
dimension.suffix =
metadata[dimension.programStageId].name
} else {
dimension.suffix = metadata[dimension.programId].name
}
if (sameProgramId || thirdPartyDuplicates) {
dimension.suffix = metadata[dimension.programStageId].name
} else {
dimension.suffix = metadata[dimension.programId].name
}
} else if (
// always suffix ou and statuses for TE
inputType === OUTPUT_TYPE_TRACKED_ENTITY &&
[
DIMENSION_TYPE_ORGANISATION_UNIT,
DIMENSION_TYPE_STATUS,
].includes(
dimension.dimensionType || dimension.dimensionItemType
) &&
dimension.programId
) {
dimension.suffix = metadata[dimension.programId].name
}
} else if (
// always suffix ou and statuses for TE
inputType === OUTPUT_TYPE_TRACKED_ENTITY &&
[DIMENSION_TYPE_ORGANISATION_UNIT, DIMENSION_TYPE_STATUS].includes(
dimension.dimensionType || dimension.dimensionItemType
) &&
dimension.programId
) {
dimension.suffix = metadata[dimension.programId].name
}

return dimension
})
return dimensionsWithSuffix
}

return dimensions
return dimension
})
}

0 comments on commit 0f23372

Please sign in to comment.