1- import { Request , Response } from "express" ;
1+ import { EventEmitter } from "node:events" ;
2+ import type { Request , Response } from "express" ;
23import { v4 as uuidv4 } from "uuid" ;
34import { MigrationService } from "../services/MigrationService" ;
45import { SigningService } from "../services/SigningService" ;
5- import { EventEmitter } from "events" ;
66
77export class MigrationController {
88 private migrationService : MigrationService ;
@@ -15,20 +15,27 @@ export class MigrationController {
1515 this . eventEmitter = new EventEmitter ( ) ;
1616
1717 // Forward migration service events
18- this . migrationService . on ( "migration-update" , ( migrationId : string , data : unknown ) => {
19- this . eventEmitter . emit ( migrationId , data ) ;
20- } ) ;
18+ this . migrationService . on (
19+ "migration-update" ,
20+ ( migrationId : string , data : unknown ) => {
21+ this . eventEmitter . emit ( migrationId , data ) ;
22+ } ,
23+ ) ;
2124 }
2225
2326 initiate = async ( req : Request , res : Response ) => {
2427 try {
2528 if ( ! req . user ) {
26- return res . status ( 401 ) . json ( { error : "Authentication required" } ) ;
29+ return res
30+ . status ( 401 )
31+ . json ( { error : "Authentication required" } ) ;
2732 }
2833
2934 const { provisionerUrl } = req . body ;
3035 if ( ! provisionerUrl ) {
31- return res . status ( 400 ) . json ( { error : "provisionerUrl is required" } ) ;
36+ return res
37+ . status ( 400 )
38+ . json ( { error : "provisionerUrl is required" } ) ;
3239 }
3340
3441 const eName = req . user . ename ;
@@ -47,23 +54,30 @@ export class MigrationController {
4754 console . error ( "Error initiating migration:" , error ) ;
4855 return res . status ( 500 ) . json ( {
4956 error :
50- error instanceof Error ? error . message : "Internal server error" ,
57+ error instanceof Error
58+ ? error . message
59+ : "Internal server error" ,
5160 } ) ;
5261 }
5362 } ;
5463
5564 sign = async ( req : Request , res : Response ) => {
5665 try {
5766 if ( ! req . user ) {
58- return res . status ( 401 ) . json ( { error : "Authentication required" } ) ;
67+ return res
68+ . status ( 401 )
69+ . json ( { error : "Authentication required" } ) ;
5970 }
6071
6172 const { migrationId } = req . body ;
6273 if ( ! migrationId ) {
63- return res . status ( 400 ) . json ( { error : "migrationId is required" } ) ;
74+ return res
75+ . status ( 400 )
76+ . json ( { error : "migrationId is required" } ) ;
6477 }
6578
66- const migration = await this . migrationService . getMigrationById ( migrationId ) ;
79+ const migration =
80+ await this . migrationService . getMigrationById ( migrationId ) ;
6781 if ( ! migration ) {
6882 return res . status ( 404 ) . json ( { error : "Migration not found" } ) ;
6983 }
@@ -149,6 +163,13 @@ export class MigrationController {
149163
150164 // Start migration process
151165 const migrationId = result . migrationId ;
166+ if ( ! migrationId ) {
167+ return res . status ( 400 ) . json ( {
168+ success : false ,
169+ error : "Migration ID not found in signing result" ,
170+ } ) ;
171+ }
172+
152173 await this . processMigration ( migrationId ) ;
153174
154175 return res . status ( 200 ) . json ( {
@@ -188,15 +209,20 @@ export class MigrationController {
188209 deleteOld = async ( req : Request , res : Response ) => {
189210 try {
190211 if ( ! req . user ) {
191- return res . status ( 401 ) . json ( { error : "Authentication required" } ) ;
212+ return res
213+ . status ( 401 )
214+ . json ( { error : "Authentication required" } ) ;
192215 }
193216
194217 const { migrationId } = req . body ;
195218 if ( ! migrationId ) {
196- return res . status ( 400 ) . json ( { error : "migrationId is required" } ) ;
219+ return res
220+ . status ( 400 )
221+ . json ( { error : "migrationId is required" } ) ;
197222 }
198223
199- const migration = await this . migrationService . getMigrationById ( migrationId ) ;
224+ const migration =
225+ await this . migrationService . getMigrationById ( migrationId ) ;
200226 if ( ! migration ) {
201227 return res . status ( 404 ) . json ( { error : "Migration not found" } ) ;
202228 }
@@ -206,7 +232,9 @@ export class MigrationController {
206232 }
207233
208234 if ( ! migration . oldEvaultId ) {
209- return res . status ( 400 ) . json ( { error : "Old evault ID not found" } ) ;
235+ return res
236+ . status ( 400 )
237+ . json ( { error : "Old evault ID not found" } ) ;
210238 }
211239
212240 await this . migrationService . deleteOldEvault (
@@ -222,7 +250,8 @@ export class MigrationController {
222250 } ;
223251
224252 private async processMigration ( migrationId : string ) : Promise < void > {
225- const migration = await this . migrationService . getMigrationById ( migrationId ) ;
253+ const migration =
254+ await this . migrationService . getMigrationById ( migrationId ) ;
226255 if ( ! migration ) {
227256 throw new Error ( "Migration not found" ) ;
228257 }
@@ -232,7 +261,7 @@ export class MigrationController {
232261 if ( ! migration . provisionerUrl ) {
233262 throw new Error ( "Provisioner URL not found in migration" ) ;
234263 }
235-
264+
236265 const { evaultId, uri : newEvaultUri } =
237266 await this . migrationService . provisionNewEvault (
238267 migrationId ,
@@ -277,9 +306,11 @@ export class MigrationController {
277306 evaultId ,
278307 ) ;
279308 } catch ( error ) {
280- console . error ( `[MIGRATION ERROR] Migration ${ migrationId } failed:` , error ) ;
309+ console . error (
310+ `[MIGRATION ERROR] Migration ${ migrationId } failed:` ,
311+ error ,
312+ ) ;
281313 throw error ;
282314 }
283315 }
284316}
285-
0 commit comments