@@ -9,11 +9,14 @@ import type { FlySystem } from '../contracts/fly-system';
99import type { Operations } from '@crystallize/schema/mass-operation' ;
1010import type { Logger } from '../contracts/logger' ;
1111import { getMassOperationBulkTask } from '../../command/mass-operation/run' ;
12+ import type { InstallBoilerplateStore } from '../../ui/journeys/install-boilerplate/create-store' ;
1213
1314type Deps = {
1415 createCrystallizeClient : typeof createClient ;
1516 runMassOperation : ReturnType < typeof createRunMassOperationHandler > ;
1617 credentialsRetriever : CredentialRetriever ;
18+ crystallizeEnvironment : 'staging' | 'production' ;
19+ installBoilerplateCommandStore : InstallBoilerplateStore ;
1720 flySystem : FlySystem ;
1821 logger : Logger ;
1922} ;
@@ -29,13 +32,27 @@ export type CreateCleanTenantHandlerDefinition = CommandHandlerDefinition<
2932 Awaited < ReturnType < typeof handler > >
3033> ;
3134
35+ const sleep = ( second : number ) => new Promise ( ( resolve ) => setTimeout ( resolve , second * 1000 ) ) ;
36+
3237const handler = async (
3338 envelope : Envelope < Command > ,
34- { createCrystallizeClient, credentialsRetriever, runMassOperation, flySystem, logger } : Deps ,
39+ {
40+ createCrystallizeClient,
41+ credentialsRetriever,
42+ runMassOperation,
43+ flySystem,
44+ logger,
45+ crystallizeEnvironment,
46+ installBoilerplateCommandStore,
47+ } : Deps ,
3548) : Promise < {
3649 id : string ;
3750 identifier : string ;
3851} > => {
52+ const { storage, atoms } = installBoilerplateCommandStore ;
53+ const addTraceLog = ( log : string ) => storage . set ( atoms . addTraceLogAtom , log ) ;
54+ const addTraceError = ( log : string ) => storage . set ( atoms . addTraceErrorAtom , log ) ;
55+
3956 const { tenant, credentials } = envelope . message ;
4057 const finalCredentials = credentials || ( await credentialsRetriever . getCredentials ( ) ) ;
4158 const client = createCrystallizeClient ( {
@@ -67,6 +84,7 @@ const handler = async (
6784 } ,
6885 ) ;
6986 const { id, identifier } = createResult . tenant . create ;
87+ addTraceLog ( `Tenant created with id: ${ id } .` ) ;
7088 const shapeIdentifiers = [ 'default-product' , 'default-folder' , 'default-document' ] ;
7189 const mutation = {
7290 shape : shapeIdentifiers . reduce ( ( memo : Record < string , any > , shapeIdentifier : string ) => {
@@ -85,6 +103,7 @@ const handler = async (
85103 } ;
86104 const query = jsonToGraphQLQuery ( { mutation } ) ;
87105 await client . pimApi ( query ) ;
106+ addTraceLog ( `Shape cleaned.` ) ;
88107
89108 // if we have a folder, we check that folder for .crystallize folder and convention
90109 const { folder } = envelope . message ;
@@ -115,18 +134,23 @@ const handler = async (
115134 } ,
116135 } as Envelope < RunMassOperationCommand > ) ;
117136 logger . debug ( 'Mass operation task created' , startedTask ) ;
137+ addTraceLog ( `Mass operation task created: ${ startedTask ?. id } ` ) ;
138+ await sleep ( 10 ) ; // we have an easy 10 sec sleep here to let the task start
118139 while ( startedTask ?. status !== 'complete' ) {
119- await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
120140 const get = await cClient . nextPimApi ( getMassOperationBulkTask , { id : startedTask ?. id } ) ;
121141 if ( get . bulkTask . error ) {
122142 throw new Error ( get . data . bulkTask . error ) ;
123143 }
124144 startedTask = get . bulkTask ;
145+ await sleep ( 3 ) ; // then we check every 3 seconds
125146 }
126147 } catch ( e ) {
148+ addTraceError ( `Failed to run mass operation.` ) ;
127149 logger . error ( 'Failed to run mass operation' , e ) ;
128150 }
151+ addTraceLog ( `Mass operation completed.` ) ;
129152
153+ // now the extra mutations
130154 try {
131155 const results = await cClient . pimApi ( `#graphql
132156 query {
@@ -156,15 +180,32 @@ const handler = async (
156180 } [ ] ;
157181
158182 for ( const { mutation, sets } of Object . values ( extraMutations ) ) {
159- await Promise . all ( [
183+ await Promise . all (
160184 sets . map ( async ( set ) => {
161185 await cClient . pimApi ( mutation , set ) ;
162186 } ) ,
163- ] ) ;
187+ ) ;
164188 }
165189 } catch ( e ) {
190+ addTraceError ( `Failed to run extra mutations.` ) ;
166191 logger . error ( 'Failed to run extra mutations' , e ) ;
167192 }
193+
194+ // now the index
195+ await cClient . nextPimApi ( `mutation { igniteTenant }` ) ;
196+ // check the 404
197+
198+ const discoHost = crystallizeEnvironment === 'staging' ? 'api-dev.crystallize.digital' : 'api.crystallize.com' ;
199+ let discoApiPingResponseCode = 404 ;
200+
201+ await sleep ( 15 ) ; // easy 15 sec sleep to let the index finish
202+ do {
203+ const discoApiPingResponse = await fetch ( `https://${ discoHost } /${ identifier } /discovery` ) ;
204+ discoApiPingResponseCode = discoApiPingResponse . status ;
205+ sleep ( 5 ) ; // then every 5 seconds
206+ } while ( discoApiPingResponseCode === 404 ) ;
207+
208+ addTraceLog ( `Tenant ignited in Discovery API.` ) ;
168209 return {
169210 id,
170211 identifier,
0 commit comments