Skip to content

Commit f470b81

Browse files
committed
add migration endpoint
1 parent e1b3bbf commit f470b81

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

apps/server/src/mapping/mapping.controller.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export class MappingController {
2626
@Post('transform/:format')
2727
@ApiConsumes('multipart/form-data')
2828
@ApiBody({
29-
description: 'The spreadsheet to be transformed.',
29+
description:
30+
'Transform a spreadsheet with a mapping into the internal invoice format.',
3031
required: true,
3132
schema: {
3233
type: 'object',
@@ -35,7 +36,7 @@ export class MappingController {
3536
type: 'string',
3637
format: 'binary',
3738
nullable: true,
38-
description: 'The spreadsheet to be transformed.'
39+
description: 'The spreadsheet to be transformed.',
3940
},
4041
data: {
4142
type: 'string',
@@ -122,4 +123,58 @@ export class MappingController {
122123
}
123124
}
124125
}
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+
}
125180
}

apps/server/src/mapping/mapping.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ export class MappingService {
2020

2121
return this.mappingService.transform(dataBuffer, format, mappingData);
2222
}
23+
24+
migrate(mapping: string): Mapping {
25+
const mappingData = yaml.load(mapping) as Mapping;
26+
27+
return this.mappingService.migrate(mappingData);
28+
}
2329
}

0 commit comments

Comments
 (0)