@@ -8,6 +8,7 @@ import { exit } from "process";
88import sourceSchema from './schema/jsonapi.json' ;
99import mapping from './schema/mapping.json' ;
1010import destinationSchema from './schema/p2881.json' ;
11+ import md5 from 'md5' ;
1112
1213class App {
1314
@@ -41,9 +42,6 @@ class App {
4142 let url : string | undefined = process . env . SOURCE_ENDPOINT ;
4243 const sourceSchema = await this . getSourceSchema ( ) ;
4344 const transformationMapping = this . getTransformationMapping ( ) ;
44- // Set up the destination object.
45- let destinationData : MappedItem = { "learning_experience_sets" : [ ] } ;
46-
4745 // Loop through JSON:API pages. This will download the a single page,
4846 // then transforms the data by adding all of the included data,
4947 // then maps each source object within the page to a learning experience.
@@ -59,21 +57,26 @@ class App {
5957 this . outputToConsole ( sourceValidated . errors , 'error' ) ;
6058 exit ( 1 ) ;
6159 }
60+
6261 const jsonApiData = new JsonAPIDeserializer ( sourceJSONData as JsonApi ) ;
63- destinationData = await this . transformSourceData ( jsonApiData . data , transformationMapping , destinationData ) ;
62+ const destinationData : MappedItem = await this . transformSourceData ( jsonApiData . data , transformationMapping , { } ) ;
63+ // Get the destination schema.
64+ const destinationSchema = await this . getDestinationSchema ( ) ;
65+ const values = Object . values ( destinationData ) ;
66+ for ( let i = 0 ; i < values . length ; i ++ ) {
67+ let value = values [ i ] ;
68+ value = this . addHashes ( value ) ;
69+ // Validate the destination data against the schema.
70+ const destinationValidated = this . validateData ( value , destinationSchema ) ;
71+ if ( ! destinationValidated . valid ) {
72+ this . outputToConsole ( destinationValidated . errors , 'error' ) ;
73+ exit ( 2 ) ;
74+ }
75+ await this . sendDestinationData ( destinationData ) ;
76+ }
6477 url = jsonApiData . nextPage ;
6578 url = url ?. replace ( this . sourceWebService . host , '' ) ;
6679 }
67-
68- // Get the destination schema.
69- const destinationSchema = await this . getDestinationSchema ( ) ;
70- // Validate the destination data against the schema.
71- const destinationValidated = this . validateData ( destinationData , destinationSchema ) ;
72- if ( ! destinationValidated . valid ) {
73- this . outputToConsole ( destinationValidated . errors , 'error' ) ;
74- exit ( 2 ) ;
75- }
76- await this . sendDestinationData ( destinationData ) ;
7780 }
7881
7982 /**
@@ -135,17 +138,16 @@ class App {
135138 private transformSourceData ( sourceData : DataItem [ ] , mapFile : Mapper , destinationItem : MappedItem ) : MappedItem {
136139 // Transform
137140 this . outputToConsole ( 'transforming source to destination' ) ;
138- const indexOffset = ( destinationItem . learning_experience_sets as MappedItem [ ] ) . length ;
139141 // Map destination structure
140142 sourceData . forEach ( ( sourceItem , index ) => {
141- ( destinationItem . learning_experience_sets as MappedItem [ ] ) [ index + indexOffset ] = { } ;
143+ destinationItem [ index ] = { } ;
142144 Object . keys ( mapFile ) . forEach ( ( mapKey ) => {
143145 const sourceKey = mapFile [ mapKey ] ;
144146 const value = JsonMapper . getValueFromSource ( sourceKey , ( sourceItem as unknown ) as MappedItem ) ;
145147 if ( ! value ) {
146148 return ;
147149 }
148- JsonMapper . mapValueToDestinationObject ( mapKey , value , ( destinationItem . learning_experience_sets as MappedItem [ ] ) [ index + indexOffset ] ) ;
150+ JsonMapper . mapValueToDestinationObject ( mapKey , value , destinationItem [ index ] as MappedItem ) ;
149151 } ) ;
150152 } ) ;
151153
@@ -164,6 +166,17 @@ class App {
164166 return destinationSchema ;
165167 }
166168
169+ /**
170+ * Adds metadata_key and metadata_key_hash.
171+ * @param value The destination data.
172+ * @returns The destination data with hash.
173+ */
174+ private addHashes ( value : MappedItem ) : MappedItem {
175+ value [ "metadata_key_hash" ] = md5 ( value . metadata as string ) ;
176+ value [ "metadata_hash" ] = md5 ( value . metadata_key as string ) ;
177+ return value ;
178+ }
179+
167180 /**
168181 * Outputs and/or sends the destination data.
169182 * @param data The formatted data.
0 commit comments