Skip to content

Commit

Permalink
chore: move getProfileByName transformer into separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhishekA1509 committed Jan 18, 2024
1 parent d07df56 commit 5079acc
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 57 deletions.
1 change: 1 addition & 0 deletions src/Pages/GlobalConfigurations/BuildInfra/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { default as BuildInfraDescriptor } from './Descriptor'
export { default as BuildInfraFooter } from './Footer'
export { default as BuildInfraConfigForm } from './BuildInfraConfigForm'
export * from './services'
export * from './utils'
export * from './types'
export * from './constants'
118 changes: 64 additions & 54 deletions src/Pages/GlobalConfigurations/BuildInfra/services.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BuildInfraConfigTypes,
BuildInfraConfigurationMapType,
BuildInfraConfigurationType,
BuildInfraProfileAPIResponseType,
BuildInfraProfileResponseType,
BuildInfraProfileVariants,
BuildInfraUnitsMapType,
Expand Down Expand Up @@ -329,6 +330,67 @@ const getSampleResponse2 = (name: string) => ({
},
})

// Would recieve a single profile and return transformed response
export const getTransformedBuildInfraProfileResponse = ({
configurationUnits,
defaultConfigurations,
profile,
}: BuildInfraProfileAPIResponseType): BuildInfraProfileResponseType => {
const configurationUnitsMap = configurationUnits?.reduce((acc, profileUnitList) => {
acc[profileUnitList.name] = profileUnitList.units.reduce((accumulator, units) => {
accumulator[units.name] = units
return accumulator
}, {} as ConfigurationUnitMapType)
return acc
}, {} as BuildInfraUnitsMapType)

// Assuming would contain all the keys
const defaultConfigurationsMap =
defaultConfigurations?.reduce((acc, configuration) => {
acc[configuration.key] = configuration
return acc
}, {}) ?? {}

const profileConfigurations =
profile?.configurations?.reduce((acc, configuration) => {
acc[configuration.key] = configuration
return acc
}, {}) ?? {}

// traversing defaultConfigurationsMap and if key is present in profileConfigurations then use that else use defaultConfigurationsMap
const configurations = Object.keys(defaultConfigurationsMap).reduce((acc, key) => {
const defaultConfiguration: BuildInfraConfigurationType = defaultConfigurationsMap[key]
const profileConfiguration = profileConfigurations[key]
// TODO: Would remove profileName from it since it can change and won't send profileName to api
if (profileConfiguration) {
acc[key] = profileConfiguration
} else {
// Removing id from it since we do not have a configuration
// While generating payload, we will check on the basis of id and active flag.
acc[key] = {
key,
value: defaultConfiguration.value,
// saving profile name as undefined and this would be a check, if we are deriving from default or not
unit: defaultConfiguration.unit,
active: false,
}
}
acc[key].defaultValue = {
value: defaultConfiguration.value,
unit: defaultConfiguration.unit,
}
return acc
}, {} as BuildInfraConfigurationMapType)

return {
configurationUnits: configurationUnitsMap,
profile: {
...(profile && profile),
configurations,
},
}
}

export const getBuildInfraProfileByName = async (name: string): Promise<BuildInfraProfileResponseType> => {
// Adding a timeout to show the loader
// eslint-disable-next-line no-promise-executor-return
Expand All @@ -339,60 +401,8 @@ export const getBuildInfraProfileByName = async (name: string): Promise<BuildInf
const { code, result } = response

if (code === 200 && result) {
const { configurationUnits, defaultConfigurations } = result
const configurationUnitsMap = configurationUnits?.reduce((acc, profileUnitList) => {
acc[profileUnitList.name] = profileUnitList.units.reduce((accumulator, units) => {
accumulator[units.name] = units
return accumulator
}, {} as ConfigurationUnitMapType)
return acc
}, {} as BuildInfraUnitsMapType)

// Assuming would contain all the keys
const defaultConfigurationsMap =
defaultConfigurations?.reduce((acc, configuration) => {
acc[configuration.key] = configuration
return acc
}, {}) ?? {}

const profileConfigurations =
result.profile?.configurations?.reduce((acc, configuration) => {
acc[configuration.key] = configuration
return acc
}, {}) ?? {}

// traversing defaultConfigurationsMap and if key is present in profileConfigurations then use that else use defaultConfigurationsMap
const configurations = Object.keys(defaultConfigurationsMap).reduce((acc, key) => {
const defaultConfiguration: BuildInfraConfigurationType = defaultConfigurationsMap[key]
const profileConfiguration = profileConfigurations[key]
// TODO: Would remove profileName from it since it can change and won't send profileName to api
if (profileConfiguration) {
acc[key] = profileConfiguration
} else {
// Removing id from it since we do not have a configuration
// While generating payload, we will check on the basis of id and active flag.
acc[key] = {
key,
value: defaultConfiguration.value,
// saving profile name as undefined and this would be a check, if we are deriving from default or not
unit: defaultConfiguration.unit,
active: false,
}
}
acc[key].defaultValue = {
value: defaultConfiguration.value,
unit: defaultConfiguration.unit,
}
return acc
}, {} as BuildInfraConfigurationMapType)

return {
configurationUnits: configurationUnitsMap,
profile: {
...result.profile,
configurations,
},
}
const { configurationUnits, defaultConfigurations, profile } = result as BuildInfraProfileAPIResponseType
return getTransformedBuildInfraProfileResponse({ configurationUnits, defaultConfigurations, profile })
}

return {
Expand Down
39 changes: 36 additions & 3 deletions src/Pages/GlobalConfigurations/BuildInfra/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,38 @@ export interface BuildInfraConfigValuesType {
unit?: ConfigurationUnitType['name']
}

export interface BuildInfraConfigurationType extends BuildInfraConfigValuesType {
interface BuildInfraProfileConfigBase {
id?: number
key: BuildInfraConfigTypes
profileName: string
active: boolean
id?: number
}

export interface BuildInfraProfileConfigResponseDataType
extends BuildInfraConfigValuesType,
BuildInfraProfileConfigBase {}

export interface BuildInfraConfigurationType extends BuildInfraConfigValuesType, BuildInfraProfileConfigBase {
defaultValue: BuildInfraConfigValuesType
}

export type BuildInfraConfigurationMapType = {
[key in BuildInfraConfigTypes]: BuildInfraConfigurationType
}

export interface BuildInfraProfileData {
interface BuildInfraProfileBase {
id?: number
name: string
description: string
type: BuildInfraProfileVariants
appCount: number
}

export interface BuildInfraProfileResponseDataType extends BuildInfraProfileBase {
configurations: BuildInfraProfileConfigResponseDataType[]
}

export interface BuildInfraProfileData extends BuildInfraProfileBase {
configurations: BuildInfraConfigurationMapType
}

Expand Down Expand Up @@ -280,3 +295,21 @@ export interface InheritingHeaderProps {
defaultHeading: BuildInfraFormFieldType['heading']
inhertingData: BuildInfraConfigValuesType[]
}

export interface BuildInfraConfigResponseDataType {
name: BuildInfraConfigTypes
units: ConfigurationUnitType[]
}

interface BaseBuildInfraProfileResponseType {
defaultConfigurations: BuildInfraProfileConfigResponseDataType[]
configurationUnits: BuildInfraConfigResponseDataType[]
}

export interface BuildInfraListResponseType extends BaseBuildInfraProfileResponseType {
profiles: BuildInfraProfileConfigResponseDataType[]
}

export interface BuildInfraProfileAPIResponseType extends BaseBuildInfraProfileResponseType {
profile: BuildInfraProfileResponseDataType
}

0 comments on commit 5079acc

Please sign in to comment.