Global Functions
Audit Logging - Done
audit logging function logauditevent(userid, workspaceid, action, type, object, detail, datetimeutc) description the logauditevent function captures essential audit information every time a user performs an action within your application it gathers user identifiers, action details, timestamp, ip address, geolocation, and device information, then compiles all data into a structured json object suitable for logging input parameters parameter type description userid string the unique identifier of the user performing the action workspaceid string the unique identifier of the workspace or project where the action is performed action string the type of action performed e g login logout create edit delete download type string the category or type of object affected document user expert setting data object string a name for the type of object selected these should be short and standardized for example on everyanswer we may use this to be the name of the expert that is modified detail string additional long details or context about the action performed datetime date utc date/time of event script / logs an audit event with comprehensive user, device, and location information @param {string} userid the unique identifier of the user performing the action @param {string} workspaceid the unique identifier of the workspace or project @param {string} action a descriptive string detailing the action performed @param {string} type the category or type of action (e g , 'create', 'update', 'delete') @param {string} object the target object of the action (e g , 'document', 'user') @returns {promise\<void>} a promise that resolves when the audit log is processed / async function logauditevent(userid, workspaceid, action, type, object) { // 1 collect the current date and time in utc const timestamp = new date() toisostring(); // 2 initialize variables for ip and location let ip = 'unavailable'; let location = 'unavailable'; try { // 2 a fetch the user's ip address using a third party api const ipresponse = await fetch('https //api ipify org?format=json'); if (ipresponse ok) { const ipdata = await ipresponse json(); ip = ipdata ip; // 2 b fetch geolocation data based on the ip address const georesponse = await fetch(`https //ipapi co/${ip}/json/`); if (georesponse ok) { const geodata = await georesponse json(); location = `${geodata city}, ${geodata region}, ${geodata country name}`; } else { console warn('failed to fetch geolocation data '); } } else { console warn('failed to fetch ip address '); } } catch (error) { console error('error fetching ip or geolocation ', error); } // 3 collect device information from the browser const deviceinfo = { useragent navigator useragent, platform navigator platform, screenresolution `${window\ screen width}x${window\ screen height}`, language navigator language }; // 4 assemble the audit log object const auditlog = { userid, workspaceid, action, type, object, timestamp, ip, location, deviceinfo }; // 5 convert the audit log object to json const auditlogjson = json stringify(auditlog); // 6 todo integrate with our audit logging system // example / try { const response = await fetch('https //your backend api com/audit log', { method 'post', headers { 'content type' 'application/json' }, body auditlogjson }); if (!response ok) { console error('failed to send audit log ', response statustext); } } catch (error) { console error('error sending audit log ', error); } / // 7 for demonstration purposes, log the audit data to the console remove from prod version console log('audit log ', auditlogjson); } notes gathers details about the user’s device and browser environment, including useragent information about the browser and operating system platform the platform on which the browser is running (e g , win32, macintel) screenresolution the screen’s width and height in pixels language the preferred language setting of the browser dependencies retrieves the user’s public ip address by making a request to the ipify api uses the obtained ip address to fetch geolocation data (city, region, country) from the ipapi co api