@@ -24,7 +24,7 @@ async function main() {
2424 parser . add_argument ( "--post" , {
2525 help : "Commit changes to DHIS2 (default: validate only)" ,
2626 default : false ,
27- action : "storeTrue " ,
27+ action : "store_true " ,
2828 } ) ;
2929
3030 parser . add_argument ( "--dataElement-submission" , {
@@ -66,6 +66,14 @@ type WarningDataElement = {
6666 reason : string ;
6767} ;
6868
69+ type TrimmedNameDataElement = {
70+ originalId : string ;
71+ cloneId : string ;
72+ originalName : string ;
73+ trimmedName : string ;
74+ reason : string ;
75+ } ;
76+
6977type SkippedSection = {
7078 id : string ;
7179 name : string ;
@@ -113,7 +121,10 @@ async function generateDataSetApproval(options: {
113121
114122 const originalDataElements = await getDataElementsDetails ( api , dataElementIds ) ;
115123
116- const { validDataElements, warningDataElements } = filterDataElementsWithCode ( originalDataElements ) ;
124+ const { trimmedDataElements, trimmedOriginalDataElements, trimmedNameDataElements } =
125+ trimDataElementNames ( originalDataElements ) ;
126+
127+ const { validDataElements, warningDataElements } = filterDataElementsWithCode ( trimmedDataElements ) ;
117128
118129 if ( warningDataElements . length > 0 ) {
119130 saveWarningDataElementsToFile ( warningDataElements , dataSetCode ) ;
@@ -128,9 +139,17 @@ async function generateDataSetApproval(options: {
128139 const approvalDE = dataElementApproval ? [ createCustomDataElement ( dataElementApproval ) ] : [ ] ;
129140 const customDataElements = [ ...submissionDE , ...approvalDE ] ;
130141
131- const allNewDataElements = [ ...newDataElements , ...customDataElements ] ;
142+ const allNewDataElements = [ ...trimmedOriginalDataElements , ... newDataElements , ...customDataElements ] ;
132143
133144 const dataElementIdMap = createDataElementIdMap ( validDataElements , newDataElements ) ;
145+ const trimmedNameWarnings = buildTrimmedNameWarnings ( trimmedNameDataElements , dataElementIdMap ) ;
146+
147+ if ( trimmedNameWarnings . length > 0 ) {
148+ saveTrimmedNameDataElementsToFile ( trimmedNameWarnings , dataSetCode ) ;
149+ console . warn (
150+ `Warning: ${ trimmedNameWarnings . length } dataElement(s) had name trimmed. See trimmed file for details.`
151+ ) ;
152+ }
134153
135154 const originalSections = await getSectionsByDataSet ( api , originalDataSet . id ) ;
136155
@@ -197,6 +216,13 @@ function saveWarningDataElementsToFile(warnings: WarningDataElement[], dataSetCo
197216 console . debug ( `Warning dataElements saved to: ${ filename } ` ) ;
198217}
199218
219+ function saveTrimmedNameDataElementsToFile ( trimmed : TrimmedNameDataElement [ ] , dataSetCode : string ) : void {
220+ const timestamp = Date . now ( ) ;
221+ const filename = `trimmed_names_${ dataSetCode } _${ timestamp } .json` ;
222+ fs . writeFileSync ( filename , JSON . stringify ( trimmed , null , 2 ) ) ;
223+ console . debug ( `Trimmed name dataElements saved to: ${ filename } ` ) ;
224+ }
225+
200226type D2DataSet = {
201227 id : string ;
202228 name : string ;
@@ -338,6 +364,66 @@ function ensureSuffix(value: string): string {
338364 return value . endsWith ( SUFFIX ) ? value : addSuffix ( value ) ;
339365}
340366
367+ function trimNameForSuffix ( name : string ) : { trimmedName : string ; wasTrimmed : boolean } {
368+ const maxLength = MAX_NAME_LENGTH - SUFFIX . length ;
369+ const trimmedName = name . length > maxLength ? name . slice ( 0 , maxLength ) : name ;
370+
371+ return { trimmedName, wasTrimmed : trimmedName !== name } ;
372+ }
373+
374+ function trimDataElementNames ( dataElements : D2DataElement [ ] ) : {
375+ trimmedDataElements : D2DataElement [ ] ;
376+ trimmedOriginalDataElements : D2DataElement [ ] ;
377+ trimmedNameDataElements : Array < Omit < TrimmedNameDataElement , "cloneId" > > ;
378+ } {
379+ const trimmedDataElements = dataElements . map ( dataElement => {
380+ const { trimmedName, wasTrimmed } = trimNameForSuffix ( dataElement . name ) ;
381+ return wasTrimmed ? { ...dataElement , name : trimmedName } : dataElement ;
382+ } ) ;
383+
384+ const trimmedOriginalDataElements = _ ( dataElements )
385+ . map ( dataElement => {
386+ const { trimmedName, wasTrimmed } = trimNameForSuffix ( dataElement . name ) ;
387+ return wasTrimmed ? { ...dataElement , name : trimmedName } : undefined ;
388+ } )
389+ . compact ( )
390+ . value ( ) ;
391+
392+ const trimmedNameDataElements = _ ( dataElements )
393+ . map ( dataElement => {
394+ const { trimmedName, wasTrimmed } = trimNameForSuffix ( dataElement . name ) ;
395+ return wasTrimmed
396+ ? {
397+ originalId : dataElement . id ,
398+ originalName : dataElement . name ,
399+ trimmedName,
400+ reason : `Name exceeds ${ MAX_NAME_LENGTH - SUFFIX . length } characters` ,
401+ }
402+ : undefined ;
403+ } )
404+ . compact ( )
405+ . value ( ) ;
406+
407+ return { trimmedDataElements, trimmedOriginalDataElements, trimmedNameDataElements } ;
408+ }
409+
410+ function buildTrimmedNameWarnings (
411+ trimmedNameDataElements : Array < Omit < TrimmedNameDataElement , "cloneId" > > ,
412+ dataElementIdMap : Record < string , string >
413+ ) : TrimmedNameDataElement [ ] {
414+ return _ . compact (
415+ trimmedNameDataElements . map ( trimmed => {
416+ const cloneId = dataElementIdMap [ trimmed . originalId ] ;
417+ return cloneId
418+ ? {
419+ ...trimmed ,
420+ cloneId,
421+ }
422+ : undefined ;
423+ } )
424+ ) ;
425+ }
426+
341427function createCustomDataElement ( name : string ) : NewDataElement {
342428 const nameWithSuffix = ensureSuffix ( name ) ;
343429 const shortName = nameWithSuffix . substring ( 0 , MAX_SHORT_NAME_LENGTH ) ;
0 commit comments