add store and api funcs

This commit is contained in:
Peter Clement 2023-02-13 16:32:14 +00:00
parent 00388c4b04
commit 6d60c27521
3 changed files with 98 additions and 0 deletions

View File

@ -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()

View File

@ -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),
})
},
})

View File

@ -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),
}
}