From 29659813efa90cb5d78b03249a69a5ae08b1a99a Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Mon, 3 Oct 2022 14:02:58 +0100 Subject: [PATCH] Add document and api types --- packages/types/src/api/web/app/backup.ts | 15 +++++++++ packages/types/src/api/web/app/index.ts | 1 + packages/types/src/api/web/index.ts | 2 ++ packages/types/src/api/web/schedule.ts | 15 +++++++++ packages/types/src/api/web/user.ts | 7 ++++ packages/types/src/documents/app/backup.ts | 21 ++++++++++++ packages/types/src/documents/app/index.ts | 1 + packages/types/src/documents/global/index.ts | 2 +- .../types/src/documents/global/schedule.ts | 32 +++++++++++++++++++ .../types/src/documents/global/schedules.ts | 9 ------ .../src/api/controllers/global/users.ts | 4 ++- packages/worker/src/sdk/users/users.ts | 7 +++- 12 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 packages/types/src/api/web/app/backup.ts create mode 100644 packages/types/src/api/web/app/index.ts create mode 100644 packages/types/src/api/web/schedule.ts create mode 100644 packages/types/src/documents/app/backup.ts create mode 100644 packages/types/src/documents/global/schedule.ts delete mode 100644 packages/types/src/documents/global/schedules.ts diff --git a/packages/types/src/api/web/app/backup.ts b/packages/types/src/api/web/app/backup.ts new file mode 100644 index 0000000000..57ffba0d70 --- /dev/null +++ b/packages/types/src/api/web/app/backup.ts @@ -0,0 +1,15 @@ +import { AppBackupTrigger } from "../../../documents" + +export interface SearchAppBackupsRequest { + trigger: AppBackupTrigger + startDate: string + endDate: string +} + +export interface CreateAppBackupRequest { + name: string +} + +export interface UpdateAppBackupRequest { + name: string +} diff --git a/packages/types/src/api/web/app/index.ts b/packages/types/src/api/web/app/index.ts new file mode 100644 index 0000000000..1d73755cb6 --- /dev/null +++ b/packages/types/src/api/web/app/index.ts @@ -0,0 +1 @@ +export * from "./backup" diff --git a/packages/types/src/api/web/index.ts b/packages/types/src/api/web/index.ts index 0129fb38d9..1dbe22aa46 100644 --- a/packages/types/src/api/web/index.ts +++ b/packages/types/src/api/web/index.ts @@ -1,3 +1,5 @@ export * from "./analytics" export * from "./user" export * from "./errors" +export * from "./schedule" +export * from "./app" diff --git a/packages/types/src/api/web/schedule.ts b/packages/types/src/api/web/schedule.ts new file mode 100644 index 0000000000..bf762b1603 --- /dev/null +++ b/packages/types/src/api/web/schedule.ts @@ -0,0 +1,15 @@ +import { + ScheduleMetadata, + ScheduleRepeatPeriod, + ScheduleType, +} from "../../documents" + +export interface CreateScheduleRequest { + type: ScheduleType + name: string + startDate: string + repeat: ScheduleRepeatPeriod + metadata: ScheduleMetadata +} + +export interface UpdateScheduleRequest extends CreateScheduleRequest {} diff --git a/packages/types/src/api/web/user.ts b/packages/types/src/api/web/user.ts index c66d3203e8..98ffcdf360 100644 --- a/packages/types/src/api/web/user.ts +++ b/packages/types/src/api/web/user.ts @@ -44,3 +44,10 @@ export interface InviteUsersResponse { successful: { email: string }[] unsuccessful: { email: string; reason: string }[] } + +export interface SearchUsersRequest { + page?: string + email?: string + appId?: string + userIds?: string[] +} diff --git a/packages/types/src/documents/app/backup.ts b/packages/types/src/documents/app/backup.ts new file mode 100644 index 0000000000..a935ed5ba1 --- /dev/null +++ b/packages/types/src/documents/app/backup.ts @@ -0,0 +1,21 @@ +import { Document } from "../document" + +export enum AppBackupTrigger { + PUBLISH = "publish", + MANUAL = "manual", + SCHEDULED = "scheduled", +} + +export interface AppBackupContents { + datasources: string[] + screens: string[] + automations: string[] +} + +export interface AppBackup extends Document { + trigger: AppBackupTrigger + name: string + date: string + userId: string + contents: AppBackupContents +} diff --git a/packages/types/src/documents/app/index.ts b/packages/types/src/documents/app/index.ts index e8b29257fc..dad594b804 100644 --- a/packages/types/src/documents/app/index.ts +++ b/packages/types/src/documents/app/index.ts @@ -10,3 +10,4 @@ export * from "./view" export * from "../document" export * from "./row" export * from "./user" +export * from "./backup" diff --git a/packages/types/src/documents/global/index.ts b/packages/types/src/documents/global/index.ts index edce416a3f..9f779d4937 100644 --- a/packages/types/src/documents/global/index.ts +++ b/packages/types/src/documents/global/index.ts @@ -3,4 +3,4 @@ export * from "./user" export * from "./userGroup" export * from "./plugin" export * from "./quotas" -export * from "./schedules" +export * from "./schedule" diff --git a/packages/types/src/documents/global/schedule.ts b/packages/types/src/documents/global/schedule.ts new file mode 100644 index 0000000000..63bb9284fe --- /dev/null +++ b/packages/types/src/documents/global/schedule.ts @@ -0,0 +1,32 @@ +import { Document } from "../document" + +export enum ScheduleType { + APP_BACKUP = "app_backup", +} + +export enum ScheduleRepeatPeriod { + DAILY = "daily", + WEEKLY = "weekly", + MONTHLY = "monthly", +} + +export interface Schedule extends Document { + type: ScheduleType + name: string + startDate: string + repeat: ScheduleRepeatPeriod + metadata: ScheduleMetadata +} + +export type ScheduleMetadata = AppBackupScheduleMetadata + +export const isAppBackupMetadata = ( + type: ScheduleType, + metadata: ScheduleMetadata +): metadata is AppBackupScheduleMetadata => { + return type === ScheduleType.APP_BACKUP +} + +export interface AppBackupScheduleMetadata { + apps: string[] +} diff --git a/packages/types/src/documents/global/schedules.ts b/packages/types/src/documents/global/schedules.ts deleted file mode 100644 index 357a841a9a..0000000000 --- a/packages/types/src/documents/global/schedules.ts +++ /dev/null @@ -1,9 +0,0 @@ -export enum ScheduleType { - APP_BACKUP = "app_backup", -} - -export enum ScheduleRepeatPeriod { - DAILY = "daily", - WEEKLY = "weekly", - MONTHLY = "monthly", -} diff --git a/packages/worker/src/api/controllers/global/users.ts b/packages/worker/src/api/controllers/global/users.ts index 8894330f67..ea1df5b45a 100644 --- a/packages/worker/src/api/controllers/global/users.ts +++ b/packages/worker/src/api/controllers/global/users.ts @@ -7,6 +7,7 @@ import { CloudAccount, InviteUserRequest, InviteUsersRequest, + SearchUsersRequest, User, } from "@budibase/types" import { @@ -144,7 +145,8 @@ export const destroy = async (ctx: any) => { } export const search = async (ctx: any) => { - const paginated = await sdk.users.paginatedUsers(ctx.request.body) + const body = ctx.request.body as SearchUsersRequest + const paginated = await sdk.users.paginatedUsers(body) // user hashed password shouldn't ever be returned for (let user of paginated.data) { if (user) { diff --git a/packages/worker/src/sdk/users/users.ts b/packages/worker/src/sdk/users/users.ts index 775514ea5e..3b98c8ef52 100644 --- a/packages/worker/src/sdk/users/users.ts +++ b/packages/worker/src/sdk/users/users.ts @@ -27,6 +27,7 @@ import { MigrationType, PlatformUserByEmail, RowResponse, + SearchUsersRequest, User, } from "@budibase/types" import { sendEmail } from "../../utilities/email" @@ -56,7 +57,8 @@ export const paginatedUsers = async ({ page, email, appId, -}: { page?: string; email?: string; appId?: string } = {}) => { + userIds, +}: SearchUsersRequest = {}) => { const db = tenancy.getGlobalDB() // get one extra document, to have the next page const opts: any = { @@ -77,6 +79,9 @@ export const paginatedUsers = async ({ } else if (email) { userList = await usersCore.searchGlobalUsersByEmail(email, opts) property = "email" + } + if (userIds) { + // TODO: search users by userIds } else { // no search, query allDocs const response = await db.allDocs(dbUtils.getGlobalUserParams(null, opts))