/* * Audit logging utilities for SaaS VOC Cloud Functions */ /** * Safely serialize a value, removing sensitive fields */ function safeValue(value, depth) { if (depth > 5 || value === null || value === undefined) return value; if (value instanceof Date) return value; if (Array.isArray(value)) return value.slice(0, 100).map((item) => safeValue(item, depth + 1)); if (typeof value !== 'object') return value; const output = {}; Object.keys(value).slice(0, 100).forEach((key) => { if (/token|secret|password|credential|authorization|master/i.test(key)) return; output[key] = safeValue(value[key], depth + 1); }); return output; } /** * Generate a unique public ID */ function publicId() { return 'cf-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 10); } /** * Get user ID from active request */ function getUserId() { return Parse?.request?.user?.id || null; } /** * Append an audit log entry */ async function appendAudit(workspaceId, action, entityType, entityId, metadata) { const audit = new Parse.Object('VocAuditLog'); audit.set('publicId', publicId()); audit.set('workspaceId', workspaceId); audit.set('actorUserId', getUserId()); audit.set('action', action); audit.set('entityType', entityType); audit.set('entityId', entityId || null); audit.set('metadata', safeValue(metadata || {}, 0)); await audit.save(null, { useMasterKey: true }); } /* // module.exports = { // appendAudit, // }; */