@@ -791,3 +791,197 @@ export function updateIMSOrgModal(lambdaContext) {
791791 }
792792 } ;
793793}
794+
795+ /* Handles "Start Onboarding" button click for IMS org onboarding */
796+ export function startLLMOOrgOnboarding ( lambdaContext ) {
797+ const { log } = lambdaContext ;
798+
799+ return async ( {
800+ ack, body, client, respond,
801+ } ) => {
802+ try {
803+ await ack ( ) ;
804+
805+ const { user } = body ;
806+
807+ await respond ( {
808+ text : `:gear: ${ user . name } started the IMS org onboarding process...` ,
809+ replace_original : true ,
810+ } ) ;
811+
812+ const originalChannel = body . channel ?. id ;
813+ const originalThreadTs = body . message ?. thread_ts || body . message ?. ts ;
814+
815+ await client . views . open ( {
816+ trigger_id : body . trigger_id ,
817+ view : {
818+ type : 'modal' ,
819+ callback_id : 'onboard_llmo_org_modal' ,
820+ private_metadata : JSON . stringify ( {
821+ originalChannel,
822+ originalThreadTs,
823+ } ) ,
824+ title : {
825+ type : 'plain_text' ,
826+ text : 'Onboard IMS Org' ,
827+ } ,
828+ submit : {
829+ type : 'plain_text' ,
830+ text : 'Start Onboarding' ,
831+ } ,
832+ close : {
833+ type : 'plain_text' ,
834+ text : 'Cancel' ,
835+ } ,
836+ blocks : [
837+ {
838+ type : 'section' ,
839+ text : {
840+ type : 'mrkdwn' ,
841+ text : ':rocket: *LLMO IMS Org Onboarding*\n\nProvide the IMS Organization ID to onboard for LLMO.' ,
842+ } ,
843+ } ,
844+ {
845+ type : 'input' ,
846+ block_id : 'ims_org_input' ,
847+ element : {
848+ type : 'plain_text_input' ,
849+ action_id : 'ims_org_id' ,
850+ placeholder : {
851+ type : 'plain_text' ,
852+ text : 'ABC123@AdobeOrg' ,
853+ } ,
854+ } ,
855+ label : {
856+ type : 'plain_text' ,
857+ text : 'IMS Organization ID' ,
858+ } ,
859+ } ,
860+ ] ,
861+ } ,
862+ } ) ;
863+
864+ log . debug ( `User ${ user . id } started IMS org onboarding process.` ) ;
865+ } catch ( error ) {
866+ log . error ( 'Error starting IMS org onboarding:' , error ) ;
867+ await postErrorMessage ( respond , error ) ;
868+ }
869+ } ;
870+ }
871+
872+ /* Handles IMS org onboarding modal submission */
873+ export function onboardLLMOOrgModal ( lambdaContext ) {
874+ const { log, dataAccess, imsClient } = lambdaContext ;
875+
876+ return async ( { ack, body, client } ) => {
877+ try {
878+ const { user, view } = body ;
879+ const { values } = view . state ;
880+
881+ const imsOrgId = values . ims_org_input ?. ims_org_id ?. value ?. trim ( ) ;
882+ const metadata = JSON . parse ( view . private_metadata || '{}' ) ;
883+ const { originalChannel, originalThreadTs } = metadata ;
884+
885+ if ( ! imsOrgId ) {
886+ await ack ( {
887+ response_action : 'errors' ,
888+ errors : {
889+ ims_org_input : 'IMS Organization ID is required' ,
890+ } ,
891+ } ) ;
892+ return ;
893+ }
894+
895+ await ack ( ) ;
896+ const responseChannel = originalChannel || body . user . id ;
897+ const responseThreadTs = originalChannel ? originalThreadTs : undefined ;
898+
899+ const slackContext = {
900+ say : async ( message ) => {
901+ await client . chat . postMessage ( {
902+ channel : responseChannel ,
903+ text : message ,
904+ thread_ts : responseThreadTs ,
905+ } ) ;
906+ } ,
907+ client,
908+ channelId : responseChannel ,
909+ threadTs : responseThreadTs ,
910+ } ;
911+
912+ await slackContext . say ( `:gear: Starting LLMO IMS org onboarding for *${ imsOrgId } *...` ) ;
913+
914+ // Create or find organization
915+ const { Organization } = dataAccess ;
916+ let organization = await Organization . findByImsOrgId ( imsOrgId ) ;
917+
918+ if ( ! organization ) {
919+ log . info ( `Creating new organization for IMS Org ID: ${ imsOrgId } ` ) ;
920+ await slackContext . say ( `Creating organization for IMS Org ID: ${ imsOrgId } ` ) ;
921+
922+ // Fetch IMS org details
923+ let imsOrgDetails ;
924+ try {
925+ imsOrgDetails = await imsClient . getImsOrganizationDetails ( imsOrgId ) ;
926+ } catch ( error ) {
927+ log . error ( `Error retrieving IMS Org details: ${ error . message } ` ) ;
928+ await slackContext . say ( `:x: Could not find an IMS org with the ID *${ imsOrgId } *.` ) ;
929+ return ;
930+ }
931+
932+ if ( ! imsOrgDetails ) {
933+ await slackContext . say ( `:x: Could not find an IMS org with the ID *${ imsOrgId } *.` ) ;
934+ return ;
935+ }
936+
937+ organization = await Organization . create ( {
938+ name : imsOrgDetails . orgName ,
939+ imsOrgId,
940+ } ) ;
941+ await organization . save ( ) ;
942+ log . info ( `Created organization ${ organization . getId ( ) } for IMS Org ID: ${ imsOrgId } ` ) ;
943+ } else {
944+ log . info ( `Found existing organization ${ organization . getId ( ) } for IMS Org ID: ${ imsOrgId } ` ) ;
945+ await slackContext . say ( `Found existing organization for IMS Org ID: ${ imsOrgId } ` ) ;
946+ }
947+
948+ try {
949+ const tierClient = TierClient . createForOrg ( lambdaContext , organization , LLMO_PRODUCT_CODE ) ;
950+ const { entitlement : existingEntitlement } = await tierClient . checkValidEntitlement ( ) ;
951+ const { entitlement } = await tierClient . createEntitlement ( LLMO_TIER ) ;
952+ const wasNewlyCreated = ! existingEntitlement
953+ || existingEntitlement . getId ( ) !== entitlement . getId ( ) ;
954+
955+ if ( wasNewlyCreated ) {
956+ log . info ( `Successfully created LLMO entitlement ${ entitlement . getId ( ) } for organization ${ organization . getId ( ) } ` ) ;
957+ } else {
958+ log . info ( `Found existing LLMO entitlement ${ entitlement . getId ( ) } for organization ${ organization . getId ( ) } ` ) ;
959+ }
960+
961+ await slackContext . say ( `:white_check_mark: *LLMO IMS org onboarding completed successfully!*
962+
963+ *Organization:* ${ organization . getName ( ) }
964+ *IMS Org ID:* ${ imsOrgId }
965+ *Entitlement ID:* ${ entitlement . getId ( ) }
966+ *Tier:* ${ LLMO_TIER }
967+ *Status:* ${ wasNewlyCreated ? 'New entitlement created' : 'Existing entitlement found and verified' }
968+
969+ The organization has been onboarded for LLMO.` ) ;
970+ } catch ( error ) {
971+ log . error ( `Error creating entitlement for organization: ${ error . message } ` ) ;
972+ await slackContext . say ( `:x: Failed to create LLMO entitlement for organization: ${ error . message } ` ) ;
973+ return ;
974+ }
975+
976+ log . debug ( `IMS org onboarding completed for ${ imsOrgId } by user ${ user . id } ` ) ;
977+ } catch ( error ) {
978+ log . error ( 'Error handling IMS org onboarding modal:' , error ) ;
979+ await ack ( {
980+ response_action : 'errors' ,
981+ errors : {
982+ ims_org_input : 'There was an error processing the onboarding request.' ,
983+ } ,
984+ } ) ;
985+ }
986+ } ;
987+ }
0 commit comments