Eu tenho esse código, mas preciso retornar funções puras em vez de funções mutáveis.
É possível? Talvez usando OOP? Não sei como melhorar isso.
Tentei retornar os valores de cada função, mas não obtive sucesso.
import { AssetStatusType } from '@domain/interfaces/common'
import {
BothComponentType,
ComponentsType,
GroupFiltersType,
OperationType,
OverviewModelType,
PendenciesOverviewType,
PendenciesType,
StructurePendenciesCount,
StructureStatusCount
} from '../../types'
const updateStatusCount = (
statusCount: StructureStatusCount,
status: AssetStatusType
) => {
statusCount[status] = (statusCount[status] || 0) + 1
}
const updatePendenciesCount = (
pendenciesCount: StructurePendenciesCount,
key: AssetStatusType,
subKey: OperationType | PendenciesOverviewType
) => {
pendenciesCount[key] = pendenciesCount[key] ?? {}
pendenciesCount[key][subKey] = (pendenciesCount[key]?.[subKey] || 0) + 1
}
const processOverviewPendencies = (
pendencies: PendenciesType[] | null | undefined,
pendenciesCount: StructurePendenciesCount,
countedIds: Set<string>,
id: string
) => {
if (pendencies?.length) {
for (const { state, pendencyType } of pendencies) {
const uniqueId = `${state}-${pendencyType}-${id}`
if (!countedIds.has(uniqueId)) {
updatePendenciesCount(pendenciesCount, state, pendencyType)
countedIds.add(uniqueId)
}
}
}
}
const processOverviewComponents = (
components: ComponentsType[],
isGroupByTree: boolean,
statusCount: StructureStatusCount,
pendenciesCount: StructurePendenciesCount,
countedIds: Set<string>,
id: string,
type: BothComponentType
) => {
for (const { pendencies, status, operationType } of components) {
if (isGroupByTree && type === 'location') {
updateStatusCount(statusCount, status)
if (operationType) {
updatePendenciesCount(pendenciesCount, status, operationType)
}
}
processOverviewPendencies(pendencies, pendenciesCount, countedIds, id)
}
}
const processOverviewData = (
data: OverviewModelType[],
groupBy: GroupFiltersType,
statusCount: StructureStatusCount,
pendenciesCount: StructurePendenciesCount,
countedIds: Set<string>
) => {
const isGroupByTree = groupBy === 'tree'
const isGroupByAsset = groupBy === 'asset'
for (const { id, status, components, operationType, type } of data) {
if (isGroupByAsset || type === 'asset') {
updateStatusCount(statusCount, status)
if (operationType) {
updatePendenciesCount(pendenciesCount, status, operationType)
}
}
processOverviewComponents(
components,
isGroupByTree,
statusCount,
pendenciesCount,
countedIds,
id,
type
)
}
}
export const calculateOverviewCounts = (
data: OverviewModelType[],
groupBy: GroupFiltersType
) => {
const statusCount: StructureStatusCount = {} as StructureStatusCount
const pendenciesCount: StructurePendenciesCount =
{} as StructurePendenciesCount
const countedIds = new Set<string>()
processOverviewData(data, groupBy, statusCount, pendenciesCount, countedIds)
return { ...statusCount, pendencies: pendenciesCount }
}
Existe uma maneira mais limpa e elegante de fazer isso? Preciso retornar um objeto como este:
// calculateOverviewCounts return this
{
pendencies: StructurePendenciesCount;
working: number;
inAlert: number;
warning: number;
stopped: number;
off: number;
}
StructurePendenciesCount é: