From cbab77ea240228d86dc86f63669f18d5108256ff Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 5 Dec 2024 14:59:15 +0000 Subject: [PATCH] Email API typing. --- packages/types/src/api/web/global/email.ts | 28 +++++++++++++++++++ packages/types/src/api/web/global/index.ts | 1 + .../src/api/controllers/global/email.ts | 11 ++++++-- .../worker/src/api/routes/global/email.ts | 2 +- .../src/api/routes/global/tests/email.spec.ts | 2 +- .../api/routes/global/tests/realEmail.spec.ts | 3 +- .../api/routes/global/tests/templates.spec.ts | 7 ++--- packages/worker/src/constants/index.ts | 10 +------ .../worker/src/constants/templates/index.ts | 9 ++---- packages/worker/src/sdk/auth/auth.ts | 3 +- packages/worker/src/sdk/users/users.ts | 2 +- packages/worker/src/utilities/email.ts | 9 ++++-- packages/worker/src/utilities/templates.ts | 7 ++--- 13 files changed, 57 insertions(+), 37 deletions(-) create mode 100644 packages/types/src/api/web/global/email.ts diff --git a/packages/types/src/api/web/global/email.ts b/packages/types/src/api/web/global/email.ts new file mode 100644 index 0000000000..a0ca0e8485 --- /dev/null +++ b/packages/types/src/api/web/global/email.ts @@ -0,0 +1,28 @@ +import { EmailAttachment, EmailInvite } from "../../../documents" + +export enum EmailTemplatePurpose { + CORE = "core", + BASE = "base", + PASSWORD_RECOVERY = "password_recovery", + INVITATION = "invitation", + WELCOME = "welcome", + CUSTOM = "custom", +} + +export interface SendEmailRequest { + workspaceId?: string + email: string + userId: string + purpose: EmailTemplatePurpose + contents?: string + from?: string + subject: string + cc?: boolean + bcc?: boolean + automation?: boolean + invite?: EmailInvite + attachments?: EmailAttachment[] +} +export interface SendEmailResponse extends Record { + message: string +} diff --git a/packages/types/src/api/web/global/index.ts b/packages/types/src/api/web/global/index.ts index c43f2928d8..0cabb97974 100644 --- a/packages/types/src/api/web/global/index.ts +++ b/packages/types/src/api/web/global/index.ts @@ -5,3 +5,4 @@ export * from "./configs" export * from "./scim" export * from "./license" export * from "./oldMigration" +export * from "./email" diff --git a/packages/worker/src/api/controllers/global/email.ts b/packages/worker/src/api/controllers/global/email.ts index 74a811376a..ad0fc3fa32 100644 --- a/packages/worker/src/api/controllers/global/email.ts +++ b/packages/worker/src/api/controllers/global/email.ts @@ -1,8 +1,15 @@ import { sendEmail as sendEmailFn } from "../../../utilities/email" import { tenancy } from "@budibase/backend-core" -import { BBContext, User } from "@budibase/types" +import { + UserCtx, + User, + SendEmailRequest, + SendEmailResponse, +} from "@budibase/types" -export async function sendEmail(ctx: BBContext) { +export async function sendEmail( + ctx: UserCtx +) { let { workspaceId, email, diff --git a/packages/worker/src/api/routes/global/email.ts b/packages/worker/src/api/routes/global/email.ts index 94e6b9bce5..6b60da47b5 100644 --- a/packages/worker/src/api/routes/global/email.ts +++ b/packages/worker/src/api/routes/global/email.ts @@ -1,7 +1,7 @@ import Router from "@koa/router" import * as controller from "../../controllers/global/email" -import { EmailTemplatePurpose } from "../../../constants" import { auth } from "@budibase/backend-core" +import { EmailTemplatePurpose } from "@budibase/types" import Joi from "joi" const router: Router = new Router() diff --git a/packages/worker/src/api/routes/global/tests/email.spec.ts b/packages/worker/src/api/routes/global/tests/email.spec.ts index 49d7c3bbeb..eb46c501d9 100644 --- a/packages/worker/src/api/routes/global/tests/email.spec.ts +++ b/packages/worker/src/api/routes/global/tests/email.spec.ts @@ -1,8 +1,8 @@ jest.mock("nodemailer") +import { EmailTemplatePurpose } from "@budibase/types" import { TestConfiguration, mocks } from "../../../../tests" const sendMailMock = mocks.email.mock() -import { EmailTemplatePurpose } from "../../../../constants" describe("/api/global/email", () => { const config = new TestConfiguration() diff --git a/packages/worker/src/api/routes/global/tests/realEmail.spec.ts b/packages/worker/src/api/routes/global/tests/realEmail.spec.ts index 99dfb7f824..bf5ed7b4ee 100644 --- a/packages/worker/src/api/routes/global/tests/realEmail.spec.ts +++ b/packages/worker/src/api/routes/global/tests/realEmail.spec.ts @@ -1,11 +1,10 @@ jest.unmock("node-fetch") import { TestConfiguration } from "../../../../tests" -import { EmailTemplatePurpose } from "../../../../constants" import { objectStore } from "@budibase/backend-core" import { helpers } from "@budibase/shared-core" import tk from "timekeeper" -import { EmailAttachment } from "@budibase/types" +import { EmailAttachment, EmailTemplatePurpose } from "@budibase/types" const fetch = require("node-fetch") diff --git a/packages/worker/src/api/routes/global/tests/templates.spec.ts b/packages/worker/src/api/routes/global/tests/templates.spec.ts index 982cae28b5..786023101e 100644 --- a/packages/worker/src/api/routes/global/tests/templates.spec.ts +++ b/packages/worker/src/api/routes/global/tests/templates.spec.ts @@ -1,9 +1,6 @@ -import { - EmailTemplatePurpose, - TemplateMetadata, - TemplateType, -} from "../../../../constants" +import { TemplateMetadata, TemplateType } from "../../../../constants" import { TestConfiguration } from "../../../../tests" +import { EmailTemplatePurpose } from "@budibase/types" // TODO diff --git a/packages/worker/src/constants/index.ts b/packages/worker/src/constants/index.ts index 17fce666db..309fdeebc1 100644 --- a/packages/worker/src/constants/index.ts +++ b/packages/worker/src/constants/index.ts @@ -1,4 +1,5 @@ import { constants } from "@budibase/backend-core" +import { EmailTemplatePurpose } from "@budibase/types" export const LOGO_URL = "https://d33wubrfki0l68.cloudfront.net/aac32159d7207b5085e74a7ef67afbb7027786c5/2b1fd/img/logo/bb-emblem.svg" @@ -19,15 +20,6 @@ export enum TemplateType { EMAIL = "email", } -export enum EmailTemplatePurpose { - CORE = "core", - BASE = "base", - PASSWORD_RECOVERY = "password_recovery", - INVITATION = "invitation", - WELCOME = "welcome", - CUSTOM = "custom", -} - export enum TemplateMetadataNames { BASE = "Base format", PASSWORD_RECOVERY = "Password recovery", diff --git a/packages/worker/src/constants/templates/index.ts b/packages/worker/src/constants/templates/index.ts index 6dd3f556a6..43c5b0870f 100644 --- a/packages/worker/src/constants/templates/index.ts +++ b/packages/worker/src/constants/templates/index.ts @@ -1,13 +1,8 @@ import { readStaticFile } from "../../utilities/fileSystem" -import { - EmailTemplatePurpose, - TemplateType, - TemplatePurpose, - GLOBAL_OWNER, -} from "../index" +import { TemplateType, TemplatePurpose, GLOBAL_OWNER } from "../index" import { join } from "path" import { db as dbCore, tenancy } from "@budibase/backend-core" -import { Template } from "@budibase/types" +import { Template, EmailTemplatePurpose } from "@budibase/types" export const EmailTemplates = { [EmailTemplatePurpose.PASSWORD_RECOVERY]: readStaticFile( diff --git a/packages/worker/src/sdk/auth/auth.ts b/packages/worker/src/sdk/auth/auth.ts index 078ac441e9..82602f58aa 100644 --- a/packages/worker/src/sdk/auth/auth.ts +++ b/packages/worker/src/sdk/auth/auth.ts @@ -8,11 +8,10 @@ import { utils as coreUtils, cache, } from "@budibase/backend-core" -import { PlatformLogoutOpts, User } from "@budibase/types" +import { PlatformLogoutOpts, User, EmailTemplatePurpose } from "@budibase/types" import jwt from "jsonwebtoken" import * as userSdk from "../users" import * as emails from "../../utilities/email" -import { EmailTemplatePurpose } from "../../constants" // LOGIN / LOGOUT diff --git a/packages/worker/src/sdk/users/users.ts b/packages/worker/src/sdk/users/users.ts index 9dc1bac0e9..343108cbeb 100644 --- a/packages/worker/src/sdk/users/users.ts +++ b/packages/worker/src/sdk/users/users.ts @@ -3,9 +3,9 @@ import { InviteUserRequest, InviteUsersRequest, InviteUsersResponse, + EmailTemplatePurpose, } from "@budibase/types" import { sendEmail } from "../../utilities/email" -import { EmailTemplatePurpose } from "../../constants" export async function invite( users: InviteUsersRequest diff --git a/packages/worker/src/utilities/email.ts b/packages/worker/src/utilities/email.ts index bf686f647c..fa9dd7a6fa 100644 --- a/packages/worker/src/utilities/email.ts +++ b/packages/worker/src/utilities/email.ts @@ -1,9 +1,14 @@ import env from "../environment" -import { EmailTemplatePurpose, TemplateType } from "../constants" +import { TemplateType } from "../constants" import { getTemplateByPurpose, EmailTemplates } from "../constants/templates" import { getSettingsTemplateContext } from "./templates" import { processString } from "@budibase/string-templates" -import { User, SendEmailOpts, SMTPInnerConfig } from "@budibase/types" +import { + User, + SendEmailOpts, + SMTPInnerConfig, + EmailTemplatePurpose, +} from "@budibase/types" import { configs, cache, objectStore } from "@budibase/backend-core" import ical from "ical-generator" import _ from "lodash" diff --git a/packages/worker/src/utilities/templates.ts b/packages/worker/src/utilities/templates.ts index c9e86ae4f9..cdfbb9b0ff 100644 --- a/packages/worker/src/utilities/templates.ts +++ b/packages/worker/src/utilities/templates.ts @@ -1,9 +1,6 @@ import { tenancy, configs } from "@budibase/backend-core" -import { - InternalTemplateBinding, - LOGO_URL, - EmailTemplatePurpose, -} from "../constants" +import { EmailTemplatePurpose } from "@budibase/types" +import { InternalTemplateBinding, LOGO_URL } from "../constants" import { checkSlashesInUrl } from "./index" const BASE_COMPANY = "Budibase"