From da19c4b6086c74bab10a9c058b783673f4e65555 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 13 Dec 2024 08:58:46 +0000 Subject: [PATCH 1/3] Convert portal audit logs store to TS --- .../builder/src/stores/portal/auditLogs.js | 43 ------------------ .../builder/src/stores/portal/auditLogs.ts | 45 +++++++++++++++++++ .../builder/src/stores/portal/licensing.js | 1 + 3 files changed, 46 insertions(+), 43 deletions(-) delete mode 100644 packages/builder/src/stores/portal/auditLogs.js create mode 100644 packages/builder/src/stores/portal/auditLogs.ts diff --git a/packages/builder/src/stores/portal/auditLogs.js b/packages/builder/src/stores/portal/auditLogs.js deleted file mode 100644 index 9abf8ec11b..0000000000 --- a/packages/builder/src/stores/portal/auditLogs.js +++ /dev/null @@ -1,43 +0,0 @@ -import { writable, get } from "svelte/store" -import { API } from "api" -import { licensing } from "stores/portal" - -export function createAuditLogsStore() { - const { subscribe, update } = writable({ - events: {}, - logs: {}, - }) - - async function search(opts = {}) { - if (get(licensing).auditLogsEnabled) { - const paged = await API.searchAuditLogs(opts) - - update(state => { - return { ...state, logs: { ...paged, opts } } - }) - - return paged - } - } - - async function getEventDefinitions() { - const events = await API.getEventDefinitions() - - update(state => { - return { ...state, ...events } - }) - } - - function getDownloadUrl(opts = {}) { - return API.getDownloadUrl(opts) - } - - return { - subscribe, - search, - getEventDefinitions, - getDownloadUrl, - } -} - -export const auditLogs = createAuditLogsStore() diff --git a/packages/builder/src/stores/portal/auditLogs.ts b/packages/builder/src/stores/portal/auditLogs.ts new file mode 100644 index 0000000000..10d79120ee --- /dev/null +++ b/packages/builder/src/stores/portal/auditLogs.ts @@ -0,0 +1,45 @@ +import { get } from "svelte/store" +import { API } from "api" +import { licensing } from "./licensing" +import BudiStore from "../BudiStore" +import { + DownloadAuditLogsRequest, + SearchAuditLogsRequest, + SearchAuditLogsResponse, +} from "@budibase/types" + +interface PortalAuditLogsStore { + events?: Record + logs?: SearchAuditLogsResponse +} + +export class AuditLogsStore extends BudiStore { + constructor() { + super({}) + } + + async search(opts: SearchAuditLogsRequest = {}) { + if (get(licensing).auditLogsEnabled) { + const res = await API.searchAuditLogs(opts) + this.update(state => ({ + ...state, + logs: res, + })) + return res + } + } + + async getEventDefinitions() { + const res = await API.getEventDefinitions() + this.update(state => ({ + ...state, + events: res.events, + })) + } + + getDownloadUrl(opts: DownloadAuditLogsRequest = {}) { + return API.getDownloadUrl(opts) + } +} + +export const auditLogs = new AuditLogsStore() diff --git a/packages/builder/src/stores/portal/licensing.js b/packages/builder/src/stores/portal/licensing.js index 9abc376cd0..72a44a8fa0 100644 --- a/packages/builder/src/stores/portal/licensing.js +++ b/packages/builder/src/stores/portal/licensing.js @@ -24,6 +24,7 @@ export const createLicensingStore = () => { scimEnabled: false, budibaseAIEnabled: false, customAIConfigsEnabled: false, + auditLogsEnabled: false, // the currently used quotas from the db quotaUsage: undefined, // derived quota metrics for percentages used From 1529eecc68f9a38a2ca1b71e6c360d593f83a6e6 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 13 Dec 2024 09:07:20 +0000 Subject: [PATCH 2/3] Revert changes --- packages/builder/src/stores/BudiStore.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/builder/src/stores/BudiStore.ts b/packages/builder/src/stores/BudiStore.ts index 19b2c394fb..c645ea6a24 100644 --- a/packages/builder/src/stores/BudiStore.ts +++ b/packages/builder/src/stores/BudiStore.ts @@ -11,7 +11,7 @@ export default class BudiStore implements Writable { set: Writable["set"] constructor(init: T, opts?: BudiStoreOpts) { - const store = writable({ ...init }) + const store = writable(init) /** * Internal Svelte store From 3f5067ecc98fec414a4de0391a8f194119092a86 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 13 Dec 2024 09:12:08 +0000 Subject: [PATCH 3/3] Ensure audit log store properties are properly optional --- .../pages/builder/portal/account/auditLogs/index.svelte | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/builder/src/pages/builder/portal/account/auditLogs/index.svelte b/packages/builder/src/pages/builder/portal/account/auditLogs/index.svelte index 4759f8d8ce..d498c557b2 100644 --- a/packages/builder/src/pages/builder/portal/account/auditLogs/index.svelte +++ b/packages/builder/src/pages/builder/portal/account/auditLogs/index.svelte @@ -160,8 +160,8 @@ events: selectedEvents, }) logsPageInfo.fetched( - $auditLogs.logs.hasNextPage, - $auditLogs.logs.bookmark + $auditLogs.logs?.hasNextPage, + $auditLogs.logs?.bookmark ) } catch (error) { notifications.error(`Error getting audit logs - ${error}`) @@ -200,6 +200,8 @@ return Object.entries(obj).map(([id, label]) => { return { id, label } }) + } else { + return [] } } @@ -316,7 +318,7 @@ viewDetails(detail)} {customRenderers} - data={$auditLogs.logs.data} + data={$auditLogs.logs?.data} allowEditColumns={false} allowEditRows={false} allowSelectRows={false}