Skip to content

Commit 995080f

Browse files
committed
refactor: replace create and update functions with upsert for build infra profiles
1 parent 82caa5f commit 995080f

File tree

5 files changed

+90
-51
lines changed

5 files changed

+90
-51
lines changed

src/Pages/GlobalConfigurations/BuildInfra/UseBuildInfraForm.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
BuildInfraToleranceOperatorType,
1919
BuildInfraToleranceValueType,
2020
CREATE_MODE_REQUIRED_INPUT_FIELDS,
21-
createBuildInfraProfile,
2221
DEFAULT_PROFILE_NAME,
2322
DEFAULT_TOLERANCE_EFFECT,
2423
DEFAULT_TOLERANCE_OPERATOR,
@@ -29,7 +28,7 @@ import {
2928
ProfileInputErrorType,
3029
TARGET_PLATFORM_ERROR_FIELDS_MAP,
3130
ToleranceHeaderType,
32-
updateBuildInfraProfile,
31+
upsertBuildInfraProfile,
3332
UseBuildInfraFormProps,
3433
UseBuildInfraFormResponseType,
3534
ValidateNodeSelectorParamsType,
@@ -285,6 +284,7 @@ const useBuildInfraForm = ({
285284
name,
286285
editProfile,
287286
handleSuccessRedirection,
287+
canConfigureUseK8sDriver,
288288
}: UseBuildInfraFormProps): UseBuildInfraFormResponseType => {
289289
const fromCreateView = !name
290290

@@ -293,6 +293,7 @@ const useBuildInfraForm = ({
293293
getBuildInfraProfileByName({
294294
name: name ?? DEFAULT_PROFILE_NAME,
295295
fromCreateView,
296+
canConfigureUseK8sDriver,
296297
}),
297298
[name],
298299
)
@@ -766,11 +767,11 @@ const useBuildInfraForm = ({
766767

767768
setLoadingActionRequest(true)
768769
try {
769-
if (editProfile) {
770-
await updateBuildInfraProfile({ name, profileInput })
771-
} else {
772-
await createBuildInfraProfile({ profileInput })
773-
}
770+
await upsertBuildInfraProfile({
771+
name,
772+
profileInput,
773+
canConfigureUseK8sDriver,
774+
})
774775
setLoadingActionRequest(false)
775776
ToastManager.showToast({
776777
variant: ToastVariantType.success,

src/Pages/GlobalConfigurations/BuildInfra/constants.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import {
2626
BuildInfraLocators,
2727
BuildInfraMetaConfigTypes,
2828
ProfileInputErrorType,
29-
BuildInfraProfileBase,
30-
BuildInfraProfileVariants,
3129
HandleProfileInputChangeType,
3230
BuildInfraProfileAdditionalErrorKeysType,
3331
BuildInfraAPIVersionType,
@@ -176,13 +174,6 @@ export const PROFILE_INPUT_ERROR_FIELDS = Object.fromEntries(
176174
// fields required to be filled before submitting the form in create view, since we pre-populate the form with default values so no need in configs
177175
export const CREATE_MODE_REQUIRED_INPUT_FIELDS = [BuildInfraMetaConfigTypes.NAME]
178176

179-
export const CREATE_PROFILE_BASE_VALUE: BuildInfraProfileBase = {
180-
name: '',
181-
description: '',
182-
type: BuildInfraProfileVariants.NORMAL,
183-
appCount: 0,
184-
}
185-
186177
export const BUILD_INFRA_TEST_IDS = {
187178
SUBMIT_BUTTON: 'build-infra-submit-button',
188179
CANCEL_BUTTON: 'build-infra-cancel-button',

src/Pages/GlobalConfigurations/BuildInfra/services.tsx

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ import {
2424
BuildInfraProfileDTO,
2525
BuildInfraProfileInfoDTO,
2626
BuildInfraProfileResponseType,
27-
CreateBuildInfraProfileType,
2827
GetBuildInfraProfileType,
29-
UpdateBuildInfraProfileType,
28+
UpsertBuildInfraProfileServiceParamsType,
3029
} from './types'
3130

3231
export const getBuildInfraProfileByName = async ({
3332
name,
3433
fromCreateView,
34+
canConfigureUseK8sDriver,
3535
}: GetBuildInfraProfileType): Promise<BuildInfraProfileResponseType> => {
3636
try {
3737
const profilePayload: Pick<BuildInfraProfileDTO['profile'], 'name'> = { name }
@@ -45,25 +45,33 @@ export const getBuildInfraProfileByName = async ({
4545
defaultConfigurations,
4646
profile,
4747
fromCreateView,
48+
canConfigureUseK8sDriver,
4849
})
4950
} catch (error) {
5051
showError(error)
5152
throw error
5253
}
5354
}
5455

55-
export const updateBuildInfraProfile = async ({ name, profileInput }: UpdateBuildInfraProfileType) => {
56-
const updateProfilePayload: Pick<BuildInfraProfileDTO['profile'], 'name'> = { name }
57-
const response = await put<ReturnType<typeof put>, BuildInfraProfileInfoDTO>(
58-
getUrlWithSearchParams(getBuildInfraProfileEndpoint(), updateProfilePayload),
59-
getBuildInfraProfilePayload(profileInput),
60-
)
56+
export const upsertBuildInfraProfile = async ({
57+
name,
58+
profileInput,
59+
canConfigureUseK8sDriver,
60+
}: UpsertBuildInfraProfileServiceParamsType) => {
61+
const isEditView = !!name
62+
const baseEndpoint = getBuildInfraProfileEndpoint()
63+
const payload = getBuildInfraProfilePayload(profileInput, canConfigureUseK8sDriver)
64+
65+
if (isEditView) {
66+
const updateProfileQueryPayload: Pick<BuildInfraProfileDTO['profile'], 'name'> = { name }
67+
const response = await put<ReturnType<typeof put>, BuildInfraProfileInfoDTO>(
68+
getUrlWithSearchParams(baseEndpoint, updateProfileQueryPayload),
69+
payload,
70+
)
6171

72+
return response
73+
}
74+
75+
const response = await post<ReturnType<typeof post>, BuildInfraProfileInfoDTO>(baseEndpoint, payload)
6276
return response
6377
}
64-
65-
export const createBuildInfraProfile = async ({ profileInput }: CreateBuildInfraProfileType) =>
66-
post<ReturnType<typeof post>, BuildInfraProfileInfoDTO>(
67-
getBuildInfraProfileEndpoint(),
68-
getBuildInfraProfilePayload(profileInput),
69-
)

src/Pages/GlobalConfigurations/BuildInfra/types.tsx

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ interface BuildInfraProfileBaseDTO {
232232
type: BuildInfraProfileVariants
233233
appCount?: number
234234
active?: boolean
235+
useK8sDriver?: boolean
235236
}
236237

237238
export interface BuildInfraProfileBase extends BuildInfraProfileBaseDTO {}
@@ -247,20 +248,6 @@ export interface BuildInfraProfileData extends BuildInfraProfileBase {
247248
configurations: Record<string, BuildInfraConfigurationMapType>
248249
}
249250

250-
export interface GetBuildInfraProfileType {
251-
name: string
252-
fromCreateView?: boolean
253-
}
254-
255-
export interface BuildInfraProfileResponseType {
256-
configurationUnits: BuildInfraUnitsMapType | null
257-
profile: BuildInfraProfileData | null
258-
/**
259-
* To be used in case user is creating configuration for new platform
260-
*/
261-
fallbackPlatformConfigurationMap: BuildInfraProfileData['configurations']
262-
}
263-
264251
export interface UseBuildInfraFormProps {
265252
/**
266253
* Name of the profile, if not provided assumption would be for create view
@@ -274,6 +261,24 @@ export interface UseBuildInfraFormProps {
274261
* If true, call this on form submission success
275262
*/
276263
handleSuccessRedirection?: () => void
264+
/**
265+
* @default - false
266+
*/
267+
canConfigureUseK8sDriver: boolean
268+
}
269+
270+
export interface GetBuildInfraProfileType extends Pick<UseBuildInfraFormProps, 'canConfigureUseK8sDriver'> {
271+
name: string
272+
fromCreateView?: boolean
273+
}
274+
275+
export interface BuildInfraProfileResponseType {
276+
configurationUnits: BuildInfraUnitsMapType | null
277+
profile: BuildInfraProfileData | null
278+
/**
279+
* To be used in case user is creating configuration for new platform
280+
*/
281+
fallbackPlatformConfigurationMap: BuildInfraProfileData['configurations']
277282
}
278283

279284
export enum BuildInfraProfileAdditionalErrorKeysType {
@@ -476,8 +481,13 @@ export interface FooterProps {
476481
loading?: boolean
477482
}
478483

479-
export interface UpdateBuildInfraProfileType extends Pick<UseBuildInfraFormResponseType, 'profileInput'> {
480-
name: string
484+
export interface UpsertBuildInfraProfileServiceParamsType
485+
extends Pick<UseBuildInfraFormResponseType, 'profileInput'>,
486+
Pick<UseBuildInfraFormProps, 'canConfigureUseK8sDriver'> {
487+
/**
488+
* If not given would consider as create view
489+
*/
490+
name?: string
481491
}
482492

483493
export interface CreateBuildInfraProfileType extends Pick<UseBuildInfraFormResponseType, 'profileInput'> {}
@@ -519,7 +529,8 @@ export interface BuildInfraProfileDTO extends BaseBuildInfraProfileDTO {
519529

520530
export interface BuildInfraProfileTransformerParamsType
521531
extends BuildInfraProfileDTO,
522-
Pick<GetBuildInfraProfileType, 'fromCreateView'> {}
532+
Pick<GetBuildInfraProfileType, 'fromCreateView'>,
533+
Pick<GetBuildInfraProfileType, 'canConfigureUseK8sDriver'> {}
523534

524535
export interface GetPlatformConfigurationsWithDefaultValuesParamsType {
525536
profileConfigurationsMap: BuildInfraConfigurationMapTypeWithoutDefaultFallback
@@ -544,3 +555,6 @@ export interface ValidateNodeSelectorParamsType
544555
selector: BuildInfraNodeSelectorValueType
545556
existingKeys: string[]
546557
}
558+
559+
export interface GetBaseProfileObjectParamsType
560+
extends Pick<BuildInfraProfileTransformerParamsType, 'canConfigureUseK8sDriver' | 'fromCreateView' | 'profile'> {}

src/Pages/GlobalConfigurations/BuildInfra/utils.tsx

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ import {
3131
BuildInfraProfileInfoDTO,
3232
BuildInfraProfileResponseType,
3333
BuildInfraProfileTransformerParamsType,
34+
BuildInfraProfileVariants,
3435
BuildInfraToleranceOperatorType,
3536
BuildInfraToleranceValueType,
3637
CreateBuildInfraProfileType,
38+
GetBaseProfileObjectParamsType,
3739
GetPlatformConfigurationsWithDefaultValuesParamsType,
3840
} from './types'
3941
import {
40-
CREATE_PROFILE_BASE_VALUE,
4142
BUILD_INFRA_DEFAULT_PLATFORM_NAME,
4243
BUILD_INFRA_LATEST_API_VERSION,
4344
INFRA_CONFIG_NOT_SUPPORTED_BY_BUILD_X,
@@ -106,9 +107,22 @@ export const parsePlatformConfigIntoValue = (
106107
}
107108
}
108109

109-
const getBaseProfileObject = (fromCreateView: boolean, profile: BuildInfraProfileInfoDTO): BuildInfraProfileBase => {
110+
const getBaseProfileObject = ({
111+
fromCreateView,
112+
profile,
113+
canConfigureUseK8sDriver,
114+
}: GetBaseProfileObjectParamsType): BuildInfraProfileBase => {
115+
const parsedUseK8sDriverValue = profile?.useK8sDriver || true
116+
const useK8sDriver = canConfigureUseK8sDriver ? parsedUseK8sDriverValue : null
117+
110118
if (fromCreateView) {
111-
return structuredClone(CREATE_PROFILE_BASE_VALUE)
119+
return {
120+
name: '',
121+
description: '',
122+
type: BuildInfraProfileVariants.NORMAL,
123+
appCount: 0,
124+
useK8sDriver,
125+
}
112126
}
113127

114128
return {
@@ -117,6 +131,7 @@ const getBaseProfileObject = (fromCreateView: boolean, profile: BuildInfraProfil
117131
description: profile.description,
118132
type: profile.type,
119133
appCount: profile.appCount,
134+
useK8sDriver,
120135
}
121136
}
122137

@@ -190,6 +205,7 @@ export const getTransformedBuildInfraProfileResponse = ({
190205
defaultConfigurations,
191206
profile,
192207
fromCreateView,
208+
canConfigureUseK8sDriver,
193209
}: BuildInfraProfileTransformerParamsType): BuildInfraProfileResponseType => {
194210
// Ideal assumption is defaultConfigurations would contain all the required keys
195211
const globalProfilePlatformConfigMap = getConfigurationMapWithoutDefaultFallback(defaultConfigurations)
@@ -254,14 +270,20 @@ export const getTransformedBuildInfraProfileResponse = ({
254270
configurationUnits,
255271
fallbackPlatformConfigurationMap,
256272
profile: {
257-
...(profile && getBaseProfileObject(fromCreateView, profile)),
273+
...(profile &&
274+
getBaseProfileObject({
275+
fromCreateView,
276+
profile,
277+
canConfigureUseK8sDriver,
278+
})),
258279
configurations,
259280
},
260281
}
261282
}
262283

263284
export const getBuildInfraProfilePayload = (
264285
profileInput: CreateBuildInfraProfileType['profileInput'],
286+
canConfigureUseK8sDriver: boolean,
265287
): BuildInfraProfileInfoDTO => {
266288
const platformConfigurationMap = profileInput.configurations || {}
267289
const configurations: BuildInfraProfileInfoDTO['configurations'] = Object.entries(platformConfigurationMap).reduce<
@@ -312,6 +334,9 @@ export const getBuildInfraProfilePayload = (
312334
description: profileInput.description,
313335
type: profileInput.type,
314336
configurations,
337+
...(canConfigureUseK8sDriver && {
338+
useK8sDriver: profileInput.useK8sDriver,
339+
}),
315340
}
316341
return payload
317342
}

0 commit comments

Comments
 (0)