Skip to content

Commit be3e28a

Browse files
author
naman-contentstack
committed
resolved comments
1 parent 8033da5 commit be3e28a

File tree

3 files changed

+62
-29
lines changed

3 files changed

+62
-29
lines changed

packages/contentstack-branches/src/config/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ const config = {
22
skip: 0,
33
limit: 100
44
};
5+
6+
export const FIELD_TYPES = ['modified', 'added', 'deleted'] as const;
7+
58
export default config;

packages/contentstack-branches/src/interfaces/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ export interface CSVRow {
8989
targetBranchValue: string;
9090
}
9191

92+
export interface AddCSVRowParams {
93+
srNo: number;
94+
contentTypeName: string;
95+
fieldName: string;
96+
fieldType: string;
97+
sourceValue: string;
98+
targetValue: string;
99+
}
100+
92101
export interface ContentTypeItem {
93102
title?: string;
94103
uid?: string;

packages/contentstack-branches/src/utils/csv-utility.ts

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { writeFileSync, existsSync, mkdirSync } from 'fs';
22
import { join } from 'path';
33
import { cliux, log, sanitizePath } from '@contentstack/cli-utilities';
4-
import { BranchDiffVerboseRes, CSVRow, ModifiedFieldsInput, ContentTypeItem } from '../interfaces';
4+
import { BranchDiffVerboseRes, CSVRow, ModifiedFieldsInput, ContentTypeItem, AddCSVRowParams } from '../interfaces';
5+
import { FIELD_TYPES } from '../config';
56

67
/**
78
* Get display name for a field with special handling for system fields
@@ -178,14 +179,13 @@ function addContentTypeRows(
178179
for (const item of items) {
179180
const contentTypeName = item?.title || item?.uid || 'Unknown';
180181

181-
csvRows.push({
182+
addCSVRow(csvRows, {
182183
srNo: getSrNo(),
183184
contentTypeName,
184185
fieldName: 'Content Type',
185-
fieldPath: 'N/A',
186-
operation,
187-
sourceBranchValue: 'N/A',
188-
targetBranchValue: 'N/A',
186+
fieldType: operation,
187+
sourceValue: 'N/A',
188+
targetValue: 'N/A'
189189
});
190190
}
191191
}
@@ -204,7 +204,12 @@ export function generateCSVDataFromVerbose(verboseRes: BranchDiffVerboseRes): CS
204204
const contentTypeName = moduleDetail?.moduleDetails?.title || moduleDetail?.moduleDetails?.uid || 'Unknown';
205205

206206
if (moduleDetail.modifiedFields) {
207-
addFieldChangesToCSV(csvRows, contentTypeName, moduleDetail.modifiedFields, 'modified', srNo);
207+
addFieldChangesToCSV(csvRows, {
208+
contentTypeName,
209+
modifiedFields: moduleDetail.modifiedFields,
210+
operation: 'modified',
211+
startSrNo: srNo
212+
});
208213
srNo += getFieldCount(moduleDetail.modifiedFields);
209214
}
210215
}
@@ -216,47 +221,63 @@ export function generateCSVDataFromVerbose(verboseRes: BranchDiffVerboseRes): CS
216221
return csvRows;
217222
}
218223

224+
/**
225+
* Add a CSV row with common properties
226+
* @param csvRows - Array of CSV rows to add to
227+
* @param params - Object containing CSV row parameters
228+
*/
229+
function addCSVRow(csvRows: CSVRow[], params: AddCSVRowParams): void {
230+
csvRows.push({
231+
srNo: params.srNo,
232+
contentTypeName: params.contentTypeName,
233+
fieldName: params.fieldName,
234+
fieldPath: 'N/A',
235+
operation: params.fieldType,
236+
sourceBranchValue: params.sourceValue,
237+
targetBranchValue: params.targetValue,
238+
});
239+
}
240+
219241
/**
220242
* Add field changes to CSV rows
221243
* @param csvRows - Array of CSV rows to add to
222-
* @param contentTypeName - Name of the content type
223-
* @param modifiedFields - Field changes data
224-
* @param operation - Type of operation (modified, added, deleted)
225-
* @param startSrNo - Starting serial number
244+
* @param params - Object containing field changes parameters
226245
*/
227-
function addFieldChangesToCSV(csvRows: CSVRow[], contentTypeName: string, modifiedFields: ModifiedFieldsInput, operation: string, startSrNo: number): void {
228-
const fieldTypes = ['modified', 'added', 'deleted'];
229-
let srNo = startSrNo;
246+
function addFieldChangesToCSV(csvRows: CSVRow[], params: {
247+
contentTypeName: string;
248+
modifiedFields: ModifiedFieldsInput;
249+
operation: string;
250+
startSrNo: number;
251+
}): void {
252+
const fieldTypes = FIELD_TYPES;
253+
let srNo = params.startSrNo;
230254

231255
fieldTypes.forEach(fieldType => {
232-
const fields = modifiedFields[fieldType];
256+
const fields = params.modifiedFields[fieldType];
233257
if (!fields) return;
234258

235259
fields.forEach(field => {
236260
const fieldName = getFieldDisplayName(field);
237-
const fieldPath = generateFieldPath(field, contentTypeName);
238261

239262
if (field.propertyChanges?.length > 0) {
240263
field.propertyChanges.forEach(propertyChange => {
241-
csvRows.push({
264+
addCSVRow(csvRows, {
242265
srNo: srNo++,
243-
contentTypeName,
266+
contentTypeName: params.contentTypeName,
244267
fieldName,
245-
fieldPath,
246-
operation: fieldType,
247-
sourceBranchValue: formatValue(propertyChange.newValue),
248-
targetBranchValue: formatValue(propertyChange.oldValue),
268+
fieldType,
269+
sourceValue: formatValue(propertyChange.newValue),
270+
targetValue: formatValue(propertyChange.oldValue)
249271
});
250272
});
251273
} else {
252-
csvRows.push({
274+
addCSVRow(csvRows, {
253275
srNo: srNo++,
254-
contentTypeName,
276+
contentTypeName: params.contentTypeName,
255277
fieldName,
256-
fieldPath,
257-
operation: fieldType,
258-
sourceBranchValue: fieldType === 'added' ? 'N/A' : formatValue(field),
259-
targetBranchValue: fieldType === 'deleted' ? 'N/A' : formatValue(field),
278+
fieldType,
279+
sourceValue: fieldType === 'added' ? 'N/A' : formatValue(field),
280+
targetValue: fieldType === 'deleted' ? 'N/A' : formatValue(field)
260281
});
261282
}
262283
});
@@ -270,7 +291,7 @@ function addFieldChangesToCSV(csvRows: CSVRow[], contentTypeName: string, modifi
270291
*/
271292
function getFieldCount(modifiedFields: ModifiedFieldsInput): number {
272293
let count = 0;
273-
const fieldTypes = ['modified', 'added', 'deleted'];
294+
const fieldTypes = FIELD_TYPES;
274295

275296
fieldTypes.forEach(fieldType => {
276297
const fields = modifiedFields[fieldType];

0 commit comments

Comments
 (0)