From f0e26ecf6ac0f666857e32bd7ecae1194b4a631f Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 20 Mar 2025 16:44:05 +0100 Subject: [PATCH] Update crud usages --- packages/backend-core/src/docIds/params.ts | 10 ++ packages/server/src/sdk/app/oauth2/crud.ts | 102 ++++++++------------- packages/types/src/documents/app/oauth2.ts | 7 +- packages/types/src/documents/document.ts | 1 - 4 files changed, 50 insertions(+), 70 deletions(-) diff --git a/packages/backend-core/src/docIds/params.ts b/packages/backend-core/src/docIds/params.ts index 61708bb71b..748f681049 100644 --- a/packages/backend-core/src/docIds/params.ts +++ b/packages/backend-core/src/docIds/params.ts @@ -200,3 +200,13 @@ export function getStartEndKeyURL(baseKey: any, tenantId?: string) { export const getPluginParams = (pluginId?: string | null, otherProps = {}) => { return getDocParams(DocumentType.PLUGIN, pluginId, otherProps) } + +/** + * Gets parameters for retrieving OAuth2 configs, this is a utility function for the getDocParams function. + */ +export const getOAuth2ConfigParams = ( + configId?: string | null, + otherProps = {} +) => { + return getDocParams(DocumentType.OAUTH2_CONFIG, configId, otherProps) +} diff --git a/packages/server/src/sdk/app/oauth2/crud.ts b/packages/server/src/sdk/app/oauth2/crud.ts index dd1a0fa1c7..353b09d693 100644 --- a/packages/server/src/sdk/app/oauth2/crud.ts +++ b/packages/server/src/sdk/app/oauth2/crud.ts @@ -1,101 +1,77 @@ -import { context, HTTPError, utils } from "@budibase/backend-core" +import { context, docIds, HTTPError, utils } from "@budibase/backend-core" import { - Database, DocumentType, OAuth2Config, - OAuth2Configs, PASSWORD_REPLACEMENT, SEPARATOR, - VirtualDocumentType, + WithRequired, } from "@budibase/types" -async function getDocument(db: Database = context.getAppDB()) { - const result = await db.tryGet(DocumentType.OAUTH2_CONFIG) - return result +async function guardName(name: string, id?: string) { + const existingConfigs = await fetch() + + if (existingConfigs.find(c => c.name === name && c._id !== id)) { + throw new HTTPError( + `OAuth2 config with name '${name}' is already taken.`, + 400 + ) + } } export async function fetch(): Promise { - const result = await getDocument() - if (!result) { - return [] - } - return Object.values(result.configs) + const db = context.getAppDB() + const docs = await db.allDocs(docIds.getOAuth2ConfigParams()) + const result = docs.rows.map(r => r.doc!) + return result } export async function create( config: Omit ): Promise { const db = context.getAppDB() - const doc: OAuth2Configs = (await getDocument(db)) ?? { - _id: DocumentType.OAUTH2_CONFIG, - configs: {}, - } - if (Object.values(doc.configs).find(c => c.name === config.name)) { - throw new HTTPError("Name already used", 400) - } + await guardName(config.name) - const id = `${VirtualDocumentType.OAUTH2_CONFIG}${SEPARATOR}${utils.newid()}` - doc.configs[id] = { - id, + const response = await db.put({ + id: `${DocumentType.OAUTH2_CONFIG}${SEPARATOR}${utils.newid()}`, + ...config, + }) + return { + _id: response.id, + _rev: response.rev, ...config, } - - await db.put(doc) - return doc.configs[id] } export async function get(id: string): Promise { - const doc = await getDocument() - return doc?.configs?.[id] + const db = context.getAppDB() + return await db.tryGet(id) } -export async function update(config: OAuth2Config): Promise { +export async function update( + config: WithRequired +): Promise { const db = context.getAppDB() - const doc: OAuth2Configs = (await getDocument(db)) ?? { - _id: DocumentType.OAUTH2_CONFIG, - configs: {}, + await guardName(config.name, config._id) + + const existing = await get(config._id) + if (!existing) { + throw new HTTPError(`OAuth2 config with id '${config._id}' not found.`, 404) } - if (!doc.configs[config.id]) { - throw new HTTPError(`OAuth2 config with id '${config.id}' not found.`, 404) - } - - if ( - Object.values(doc.configs).find( - c => c.name === config.name && c.id !== config.id - ) - ) { - throw new HTTPError( - `OAuth2 config with name '${config.name}' is already taken.`, - 400 - ) - } - - doc.configs[config.id] = { + const toUpdate = { ...config, clientSecret: config.clientSecret === PASSWORD_REPLACEMENT - ? doc.configs[config.id].clientSecret + ? existing.clientSecret : config.clientSecret, } - await db.put(doc) - return doc.configs[config.id] + const result = await db.put(toUpdate) + return { ...toUpdate, _rev: result.rev } } -export async function remove(configId: string): Promise { +export async function remove(configId: string, _rev: string): Promise { const db = context.getAppDB() - const doc: OAuth2Configs = (await getDocument(db)) ?? { - _id: DocumentType.OAUTH2_CONFIG, - configs: {}, - } - - if (!doc.configs[configId]) { - throw new HTTPError(`OAuth2 config with id '${configId}' not found.`, 404) - } - - delete doc.configs[configId] - - await db.put(doc) + await db.remove(configId, _rev) } diff --git a/packages/types/src/documents/app/oauth2.ts b/packages/types/src/documents/app/oauth2.ts index 74ed11ab70..d2ad895529 100644 --- a/packages/types/src/documents/app/oauth2.ts +++ b/packages/types/src/documents/app/oauth2.ts @@ -5,15 +5,10 @@ export enum OAuth2CredentialsMethod { BODY = "BODY", } -export interface OAuth2Config { - id: string +export interface OAuth2Config extends Document { name: string url: string clientId: string clientSecret: string method: OAuth2CredentialsMethod } - -export interface OAuth2Configs extends Document { - configs: Record -} diff --git a/packages/types/src/documents/document.ts b/packages/types/src/documents/document.ts index a4d8ad11ac..eadf2e6b71 100644 --- a/packages/types/src/documents/document.ts +++ b/packages/types/src/documents/document.ts @@ -82,7 +82,6 @@ export enum InternalTable { export enum VirtualDocumentType { VIEW = "view", ROW_ACTION = "row_action", - OAUTH2_CONFIG = "oauth2", } // Because VirtualDocumentTypes can overlap, we need to make sure that we search