@@ -26,7 +26,8 @@ export class MappingController {
26
26
@Post ( 'transform/:format' )
27
27
@ApiConsumes ( 'multipart/form-data' )
28
28
@ApiBody ( {
29
- description : 'The spreadsheet to be transformed.' ,
29
+ description :
30
+ 'Transform a spreadsheet with a mapping into the internal invoice format.' ,
30
31
required : true ,
31
32
schema : {
32
33
type : 'object' ,
@@ -35,7 +36,7 @@ export class MappingController {
35
36
type : 'string' ,
36
37
format : 'binary' ,
37
38
nullable : true ,
38
- description : 'The spreadsheet to be transformed.'
39
+ description : 'The spreadsheet to be transformed.' ,
39
40
} ,
40
41
data : {
41
42
type : 'string' ,
@@ -122,4 +123,58 @@ export class MappingController {
122
123
}
123
124
}
124
125
}
126
+
127
+ @Post ( 'migrate' )
128
+ @ApiConsumes ( 'multipart/form-data' )
129
+ @ApiBody ( {
130
+ description : 'Migrate a mapping to the latest version.' ,
131
+ required : true ,
132
+ schema : {
133
+ type : 'object' ,
134
+ properties : {
135
+ mapping : {
136
+ type : 'string' ,
137
+ format : 'binary' ,
138
+ nullable : false ,
139
+ description : 'The mapping file in YAML or JSON format.' ,
140
+ } ,
141
+ } ,
142
+ } ,
143
+ } )
144
+ @ApiResponse ( {
145
+ status : 201 ,
146
+ description : 'Migration successful. The output is in JSON format.' ,
147
+ } )
148
+ @ApiResponse ( {
149
+ status : 400 ,
150
+ description : 'Bad request with error details' ,
151
+ } )
152
+ @UseInterceptors ( FileFieldsInterceptor ( [ { name : 'mapping' , maxCount : 1 } ] ) )
153
+ migrate (
154
+ @UploadedFiles ( )
155
+ files : {
156
+ mapping : Express . Multer . File [ ] ;
157
+ } ,
158
+ ) : string {
159
+ const mappingFile = files . mapping ?. [ 0 ] ;
160
+ if ( ! mappingFile ) {
161
+ throw new BadRequestException ( 'No mapping file uploaded' ) ;
162
+ }
163
+
164
+ try {
165
+ return JSON . stringify (
166
+ this . mappingService . migrate ( mappingFile . buffer . toString ( ) ) ,
167
+ ) ;
168
+ } catch ( error ) {
169
+ if ( error instanceof ValidationError ) {
170
+ throw new BadRequestException ( {
171
+ message : 'Migration failed.' ,
172
+ details : error ,
173
+ } ) ;
174
+ } else {
175
+ this . logger . error ( `unknown error: ${ error . message } \n${ error . stack } ` ) ;
176
+ throw new InternalServerErrorException ( ) ;
177
+ }
178
+ }
179
+ }
125
180
}
0 commit comments