SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION SAILPOINT.ES_V2_GET_ALL_MAPICS_USERS ( 
	ENVIRONMENT VARCHAR(2) ) 
	RETURNS CLOB(2147483647)   
	LANGUAGE SQL 
	SPECIFIC SAILPOINT.V2_ESRTVMUSRS 
	NOT DETERMINISTIC 
	MODIFIES SQL DATA 
	CALLED ON NULL INPUT 
	NOT FENCED 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DBGVIEW = *SOURCE , 
	DECRESULT = (31, 31, 00) , 
	DLYPRP = *NO , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *OWNER , 
	SRTSEQ = *HEX   
	BEGIN 
SET CISTOOLS . LIBL = CISTOOLS . SET_LIBRARY_LIST ( ENVIRONMENT ) ; 
RETURN 
( 
SELECT JSON_OBJECT ( 
'data' : 
JSON_ARRAYAGG ( 
JSON_OBJECT ( 
'userId' VALUE TRIM ( 
CAST ( F0BUCD AS VARCHAR ( 10 ) CCSID 37 ) ) , 
'userName' VALUE TRIM ( 
CAST ( F0H1TX AS VARCHAR ( 60 ) CCSID 37 ) ) , 
'userType' VALUE TRIM ( CAST ( 
CASE EXTYPE 
WHEN ' ' THEN 'General User' 
WHEN 'A' THEN 'Administrator' 
WHEN 'S' THEN 'System/Application User' 
WHEN 'G' THEN 'Generic User' 
ELSE '(Unknown)' 
END AS VARCHAR ( 50 ) CCSID 37 ) ) , 
'userDept' VALUE JSON_OBJECT ( 
TRIM ( CAST ( EXDEPT AS VARCHAR ( 10 ) CCSID 37 ) ) VALUE 
TRIM ( CAST ( EUDESC AS VARCHAR ( 60 ) CCSID 37 ) ) 
ABSENT ON NULL 
) , 'userEmpNo' VALUE TRIM ( 
CAST ( EXT08A AS VARCHAR ( 10 ) CCSID 37 ) ) , 
'status' VALUE TRIM ( CAST ( 
CASE EXSTAT 
WHEN 'A' THEN 'Active' 
WHEN ' ' THEN 'Unknown' 
WHEN 'D' THEN 'Disabled' 
END AS VARCHAR ( 15 ) CCSID 37 ) ) 
) 
) , 
'errors' : JSON_ARRAY ( ) 
) 
FROM MZF0REP 
JOIN ESUEXP 
LEFT JOIN ESDPTP 
ON EXDEPT = EUDEPT 
ON F0BUCD = EXUSER 
JOIN MZGUCPS0 
ON GUBUCD = F0BUCD 
WHERE EXTYPE <> 'R' 
AND F0UGRP = 0 ) ; 
END  ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION SAILPOINT.V2_ESRTVMUSRS 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION SAILPOINT.V2_ESRTVMUSRS 
TO PUBLIC ; 

