diff --git a/packages/builder/src/stores/portal/auditLogs.js b/packages/builder/src/stores/portal/auditLogs.js new file mode 100644 index 0000000000..9381bea86a --- /dev/null +++ b/packages/builder/src/stores/portal/auditLogs.js @@ -0,0 +1,37 @@ +import { writable, get } from "svelte/store" +import { API } from "api" +import { licensing } from "stores/portal" + +export function createAuditLogsStore() { + const { subscribe, set } = writable({ + logs: [], + }) + + async function search(opts = {}) { + if (get(licensing).auditLogsEnabled) { + const paged = await API.searchAuditLogs(opts) + set({ + ...paged, + ...opts, + }) + return paged + } + } + + async function getEventDefinitions() { + return await API.getEventDefinitions() + } + + async function downloadLogs(opts = {}) { + return await API.downloadLogs(opts) + } + + return { + subscribe, + search, + getEventDefinitions, + downloadLogs, + } +} + +export const environment = createAuditLogsStore() diff --git a/packages/frontend-core/src/api/auditLogs.js b/packages/frontend-core/src/api/auditLogs.js new file mode 100644 index 0000000000..9858a88c0b --- /dev/null +++ b/packages/frontend-core/src/api/auditLogs.js @@ -0,0 +1,58 @@ +const buildOpts = ({ + userIds, + appIds, + startDate, + endDate, + metadataSearch, + event, +}) => { + const opts = {} + + if (startDate && endDate) { + opts.startDate = startDate + opts.endDate = endDate + } + + if (metadataSearch) { + opts.metadataSearch = metadataSearch + } + + if (event) { + opts.event = event + } + + if (userIds) { + opts.userId = userIds + } + + if (appIds) { + opts.appId = appIds + } + + return opts +} + +export const buildAuditLogsEndpoints = API => ({ + /** + * Gets a list of users in the current tenant. + */ + searchAuditLogs: async opts => { + return await API.post({ + url: `/api/auditlogs/search`, + body: buildOpts(opts), + }) + }, + + getEventDefinitions: async () => { + return await API.get({ + url: `/api/auditlogs/definitions`, + }) + }, + + downloadLogs: async opts => { + return await API.post({ + url: `/api/auditlogs/definitions`, + body: buildOpts(opts), + }) + }, +}) diff --git a/packages/frontend-core/src/api/index.js b/packages/frontend-core/src/api/index.js index e2935b416b..f8eee45cb8 100644 --- a/packages/frontend-core/src/api/index.js +++ b/packages/frontend-core/src/api/index.js @@ -28,6 +28,8 @@ import { buildPluginEndpoints } from "./plugins" import { buildBackupsEndpoints } from "./backups" import { buildEnvironmentVariableEndpoints } from "./environmentVariables" import { buildEventEndpoints } from "./events" +import { buildAuditLogsEndpoints } from "./auditLogs" + const defaultAPIClientConfig = { /** * Certain definitions can't change at runtime for client apps, such as the @@ -250,5 +252,6 @@ export const createAPIClient = config => { ...buildBackupsEndpoints(API), ...buildEnvironmentVariableEndpoints(API), ...buildEventEndpoints(API), + ...buildAuditLogsEndpoints(API), } }