@@ -15,50 +15,55 @@ export const ImportStackAction: RequestAction<ImportStackEndpoint> = async ({ in
1515
1616 await logger . information ( { message : `Importing stack: ${ stackName } ` } )
1717
18- const addedRepoIds : string [ ] = [ ]
19- const addedDepIds : string [ ] = [ ]
20- const addedServiceIds : string [ ] = [ ]
18+ const repositories = body . repositories . map ( ( repo ) => ( {
19+ ...repo ,
20+ stackName,
21+ createdAt : now ,
22+ updatedAt : now ,
23+ } ) )
2124
22- try {
23- const stackStore = sm . getStoreFor ( Stack , 'name' )
24- await stackStore . add ( { ...body . stack , createdAt : now , updatedAt : now } )
25-
26- const repoStore = sm . getStoreFor ( GitHubRepository , 'id' )
27- for ( const repo of body . repositories ) {
28- await repoStore . add ( { ...repo , stackName, createdAt : now , updatedAt : now } )
29- addedRepoIds . push ( repo . id )
30- }
25+ const dependencies = body . dependencies . map ( ( dep ) => ( {
26+ ...dep ,
27+ stackName,
28+ createdAt : now ,
29+ updatedAt : now ,
30+ } ) )
3131
32- const depStore = sm . getStoreFor ( Dependency , 'id' )
33- for ( const dep of body . dependencies ) {
34- await depStore . add ( { ...dep , stackName, createdAt : now , updatedAt : now } )
35- addedDepIds . push ( dep . id )
36- }
32+ const services = body . services . map ( ( svc ) => ( {
33+ ...svc ,
34+ stackName,
35+ installStatus : 'not-installed' as const ,
36+ buildStatus : 'not-built' as const ,
37+ runStatus : 'stopped' as const ,
38+ createdAt : now ,
39+ updatedAt : now ,
40+ } ) )
3741
38- const serviceStore = sm . getStoreFor ( Service , 'id' )
39- for ( const svc of body . services ) {
40- await serviceStore . add ( {
41- ...svc ,
42- stackName,
43- installStatus : 'not-installed' ,
44- buildStatus : 'not-built' ,
45- runStatus : 'stopped' ,
46- createdAt : now ,
47- updatedAt : now ,
48- } )
49- addedServiceIds . push ( svc . id )
50- }
42+ try {
43+ await sm . getStoreFor ( Stack , 'name' ) . add ( { ...body . stack , createdAt : now , updatedAt : now } )
44+ await sm . getStoreFor ( GitHubRepository , 'id' ) . add ( ...repositories )
45+ await sm . getStoreFor ( Dependency , 'id' ) . add ( ...dependencies )
46+ await sm . getStoreFor ( Service , 'id' ) . add ( ...services )
5147 } catch ( error ) {
5248 await logger . warning ( { message : `Import failed for stack ${ stackName } , rolling back` , data : { error } } )
5349
54- for ( const id of addedServiceIds ) {
55- await sm . getStoreFor ( Service , 'id' ) . remove ( id ) . catch ( ( ) => { } )
50+ for ( const svc of services ) {
51+ await sm
52+ . getStoreFor ( Service , 'id' )
53+ . remove ( svc . id )
54+ . catch ( ( ) => { } )
5655 }
57- for ( const id of addedDepIds ) {
58- await sm . getStoreFor ( Dependency , 'id' ) . remove ( id ) . catch ( ( ) => { } )
56+ for ( const dep of dependencies ) {
57+ await sm
58+ . getStoreFor ( Dependency , 'id' )
59+ . remove ( dep . id )
60+ . catch ( ( ) => { } )
5961 }
60- for ( const id of addedRepoIds ) {
61- await sm . getStoreFor ( GitHubRepository , 'id' ) . remove ( id ) . catch ( ( ) => { } )
62+ for ( const repo of repositories ) {
63+ await sm
64+ . getStoreFor ( GitHubRepository , 'id' )
65+ . remove ( repo . id )
66+ . catch ( ( ) => { } )
6267 }
6368 await sm
6469 . getStoreFor ( Stack , 'name' )
0 commit comments