diff --git a/packages/server/src/api/controllers/automation.ts b/packages/server/src/api/controllers/automation.ts index b7c29efa6f..d1bd580331 100644 --- a/packages/server/src/api/controllers/automation.ts +++ b/packages/server/src/api/controllers/automation.ts @@ -25,6 +25,7 @@ import { getActionDefinitions as actionDefs } from "../../automations/actions" import sdk from "../../sdk" import { builderSocket } from "../../websockets" import env from "../../environment" +import { DocumentDestroyResponse } from "@budibase/nano" async function getActionDefinitions() { return removeDeprecated(await actionDefs()) @@ -209,7 +210,7 @@ export async function find(ctx: UserCtx) { ctx.body = await db.get(ctx.params.id) } -export async function destroy(ctx: UserCtx) { +export async function destroy(ctx: UserCtx) { const db = context.getAppDB() const automationId = ctx.params.id const oldAutomation = await db.get(automationId) diff --git a/packages/server/src/api/controllers/layout.ts b/packages/server/src/api/controllers/layout.ts index 69e4ad91ed..1a15432b88 100644 --- a/packages/server/src/api/controllers/layout.ts +++ b/packages/server/src/api/controllers/layout.ts @@ -1,9 +1,9 @@ import { EMPTY_LAYOUT } from "../../constants/layouts" import { generateLayoutID, getScreenParams } from "../../db/utils" import { events, context } from "@budibase/backend-core" -import { BBContext, Layout } from "@budibase/types" +import { BBContext, Layout, UserCtx } from "@budibase/types" -export async function save(ctx: BBContext) { +export async function save(ctx: UserCtx) { const db = context.getAppDB() let layout = ctx.request.body diff --git a/packages/server/src/api/controllers/query/index.ts b/packages/server/src/api/controllers/query/index.ts index 768c921150..973718ba48 100644 --- a/packages/server/src/api/controllers/query/index.ts +++ b/packages/server/src/api/controllers/query/index.ts @@ -73,7 +73,7 @@ const _import = async (ctx: UserCtx) => { } export { _import as import } -export async function save(ctx: UserCtx) { +export async function save(ctx: UserCtx) { const db = context.getAppDB() const query: Query = ctx.request.body diff --git a/packages/server/src/api/controllers/screen.ts b/packages/server/src/api/controllers/screen.ts index 446fe2e5fa..ee8e0ff892 100644 --- a/packages/server/src/api/controllers/screen.ts +++ b/packages/server/src/api/controllers/screen.ts @@ -7,7 +7,13 @@ import { roles, } from "@budibase/backend-core" import { updateAppPackage } from "./application" -import { Plugin, ScreenProps, BBContext, Screen } from "@budibase/types" +import { + Plugin, + ScreenProps, + BBContext, + Screen, + UserCtx, +} from "@budibase/types" import { builderSocket } from "../../websockets" export async function fetch(ctx: BBContext) { @@ -31,7 +37,7 @@ export async function fetch(ctx: BBContext) { ) } -export async function save(ctx: BBContext) { +export async function save(ctx: UserCtx) { const db = context.getAppDB() let screen = ctx.request.body diff --git a/packages/server/src/api/routes/tests/datasource.spec.ts b/packages/server/src/api/routes/tests/datasource.spec.ts index 41229b0a2a..032da71b80 100644 --- a/packages/server/src/api/routes/tests/datasource.spec.ts +++ b/packages/server/src/api/routes/tests/datasource.spec.ts @@ -86,7 +86,7 @@ describe("/datasources", () => { }) // check variables in cache let contents = await checkCacheForDynamicVariable( - query._id, + query._id!, "variable3" ) expect(contents.rows.length).toEqual(1) @@ -102,7 +102,7 @@ describe("/datasources", () => { expect(res.body.errors).toBeUndefined() // check variables no longer in cache - contents = await checkCacheForDynamicVariable(query._id, "variable3") + contents = await checkCacheForDynamicVariable(query._id!, "variable3") expect(contents).toBe(null) }) }) diff --git a/packages/server/src/api/routes/tests/queries/query.seq.spec.ts b/packages/server/src/api/routes/tests/queries/query.seq.spec.ts index 52d35fa782..2bbc8366ea 100644 --- a/packages/server/src/api/routes/tests/queries/query.seq.spec.ts +++ b/packages/server/src/api/routes/tests/queries/query.seq.spec.ts @@ -467,7 +467,10 @@ describe("/queries", () => { queryString: "test={{ variable3 }}", }) // check its in cache - const contents = await checkCacheForDynamicVariable(base._id, "variable3") + const contents = await checkCacheForDynamicVariable( + base._id!, + "variable3" + ) expect(contents.rows.length).toEqual(1) const responseBody = await preview(datasource, { path: "www.failonce.com", @@ -490,7 +493,7 @@ describe("/queries", () => { queryString: "test={{ variable3 }}", }) // check its in cache - let contents = await checkCacheForDynamicVariable(base._id, "variable3") + let contents = await checkCacheForDynamicVariable(base._id!, "variable3") expect(contents.rows.length).toEqual(1) // delete the query @@ -500,7 +503,7 @@ describe("/queries", () => { .expect(200) // check variables no longer in cache - contents = await checkCacheForDynamicVariable(base._id, "variable3") + contents = await checkCacheForDynamicVariable(base._id!, "variable3") expect(contents).toBe(null) }) }) diff --git a/packages/server/src/api/routes/tests/utilities/TestFunctions.ts b/packages/server/src/api/routes/tests/utilities/TestFunctions.ts index 53e90396aa..0576b1e748 100644 --- a/packages/server/src/api/routes/tests/utilities/TestFunctions.ts +++ b/packages/server/src/api/routes/tests/utilities/TestFunctions.ts @@ -4,6 +4,7 @@ import { AppStatus } from "../../../../db/utils" import { roles, tenancy, context, db } from "@budibase/backend-core" import env from "../../../../environment" import Nano from "@budibase/nano" +import TestConfiguration from "src/tests/utilities/TestConfiguration" class Request { appId: any @@ -52,10 +53,10 @@ export const clearAllApps = async ( }) } -export const clearAllAutomations = async (config: any) => { +export const clearAllAutomations = async (config: TestConfiguration) => { const automations = await config.getAllAutomations() for (let auto of automations) { - await context.doInAppContext(config.appId, async () => { + await context.doInAppContext(config.getAppId(), async () => { await config.deleteAutomation(auto) }) } @@ -101,7 +102,12 @@ export const checkBuilderEndpoint = async ({ method, url, body, -}: any) => { +}: { + config: TestConfiguration + method: string + url: string + body: any +}) => { const headers = await config.login({ userId: "us_fail", builder: false, diff --git a/packages/server/src/constants/layouts.ts b/packages/server/src/constants/layouts.ts index 835a5d2e15..f4eb337c2d 100644 --- a/packages/server/src/constants/layouts.ts +++ b/packages/server/src/constants/layouts.ts @@ -1,9 +1,11 @@ +import { Layout } from "@budibase/types" + export const BASE_LAYOUT_PROP_IDS = { PRIVATE: "layout_private_master", PUBLIC: "layout_public_master", } -export const EMPTY_LAYOUT = { +export const EMPTY_LAYOUT: Layout = { componentLibraries: ["@budibase/standard-components"], title: "{{ name }}", favicon: "./_shared/favicon.png", diff --git a/packages/server/src/constants/screens.ts b/packages/server/src/constants/screens.ts index 6c88b0f957..1107289ea0 100644 --- a/packages/server/src/constants/screens.ts +++ b/packages/server/src/constants/screens.ts @@ -1,5 +1,6 @@ import { roles } from "@budibase/backend-core" import { BASE_LAYOUT_PROP_IDS } from "./layouts" +import { Screen } from "@budibase/types" export function createHomeScreen( config: { @@ -9,10 +10,8 @@ export function createHomeScreen( roleId: roles.BUILTIN_ROLE_IDS.BASIC, route: "/", } -) { +): Screen { return { - description: "", - url: "", layoutId: BASE_LAYOUT_PROP_IDS.PRIVATE, props: { _id: "d834fea2-1b3e-4320-ab34-f9009f5ecc59", diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 599675bd4e..70794934cc 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -53,9 +53,12 @@ import { Datasource, FieldType, INTERNAL_TABLE_SOURCE_ID, + Layout, + Query, RelationshipFieldMetadata, RelationshipType, Row, + Screen, SearchParams, SourceName, Table, @@ -63,6 +66,7 @@ import { User, UserCtx, View, + Webhook, WithRequired, } from "@budibase/types" @@ -182,6 +186,15 @@ export default class TestConfiguration { return this.automation } + getDatasource() { + if (!this.datasource) { + throw new Error( + "datasource has not been initialised, call config.init() first" + ) + } + return this.datasource + } + async doInContext( appId: string | undefined, task: () => Promise @@ -288,10 +301,10 @@ export default class TestConfiguration { // UTILS - _req, Res>( + _req | void, Res>( handler: (ctx: UserCtx) => Promise, body?: Req, - params?: Record + params?: Record ): Promise { // create a fake request ctx const request: any = {} @@ -399,7 +412,7 @@ export default class TestConfiguration { builder, prodApp, }: { - roleId: string + roleId?: string userId: string builder: boolean prodApp: boolean @@ -415,7 +428,7 @@ export default class TestConfiguration { await this.globalUser({ _id: userId, builder: { global: builder }, - roles: { [appId]: roleId }, + roles: { [appId]: roleId || roles.BUILTIN_ROLE_IDS.BASIC }, }) } await sessions.createASession(userId, { @@ -772,7 +785,7 @@ export default class TestConfiguration { return this._req(automationController.fetch) } - async deleteAutomation(automation?: any) { + async deleteAutomation(automation?: Automation) { automation = automation || this.automation if (!automation) { return @@ -783,7 +796,7 @@ export default class TestConfiguration { }) } - async createWebhook(config?: any) { + async createWebhook(config?: Webhook) { if (!this.automation) { throw "Must create an automation before creating webhook." } @@ -811,7 +824,7 @@ export default class TestConfiguration { return { ...this.datasource, _id: this.datasource!._id! } } - async restDatasource(cfg?: any) { + async restDatasource(cfg?: Record) { return this.createDatasource({ datasource: { ...basicDatasource().datasource, @@ -868,24 +881,23 @@ export default class TestConfiguration { // QUERY - async createQuery(config?: any) { - if (!this.datasource && !config) { - throw "No datasource created for query." - } - config = config || basicQuery(this.datasource!._id!) - return this._req(queryController.save, config) + async createQuery(config?: Query) { + return this._req( + queryController.save, + config || basicQuery(this.getDatasource()._id!) + ) } // SCREEN - async createScreen(config?: any) { + async createScreen(config?: Screen) { config = config || basicScreen() return this._req(screenController.save, config) } // LAYOUT - async createLayout(config?: any) { + async createLayout(config?: Layout) { config = config || basicLayout() return await this._req(layoutController.save, config) } diff --git a/packages/server/src/tests/utilities/structures.ts b/packages/server/src/tests/utilities/structures.ts index 2fecf15fd6..5b50bd1175 100644 --- a/packages/server/src/tests/utilities/structures.ts +++ b/packages/server/src/tests/utilities/structures.ts @@ -22,6 +22,8 @@ import { INTERNAL_TABLE_SOURCE_ID, TableSourceType, Query, + Webhook, + WebhookActionType, } from "@budibase/types" import { LoopInput, LoopStepType } from "../../definitions/automations" @@ -407,12 +409,12 @@ export function basicLayout() { return cloneDeep(EMPTY_LAYOUT) } -export function basicWebhook(automationId: string) { +export function basicWebhook(automationId: string): Webhook { return { live: true, name: "webhook", action: { - type: "automation", + type: WebhookActionType.AUTOMATION, target: automationId, }, } diff --git a/packages/types/src/documents/app/layout.ts b/packages/types/src/documents/app/layout.ts index 06542f680d..51ce511712 100644 --- a/packages/types/src/documents/app/layout.ts +++ b/packages/types/src/documents/app/layout.ts @@ -1,6 +1,11 @@ import { Document } from "../document" export interface Layout extends Document { + componentLibraries: string[] + title: string + favicon: string + stylesheets: string[] props: any layoutId?: string + name?: string } diff --git a/packages/types/src/documents/app/screen.ts b/packages/types/src/documents/app/screen.ts index 58c00ef3d6..4977c79b0b 100644 --- a/packages/types/src/documents/app/screen.ts +++ b/packages/types/src/documents/app/screen.ts @@ -22,4 +22,5 @@ export interface Screen extends Document { routing: ScreenRouting props: ScreenProps name?: string + pluginAdded?: boolean }