Skip to content

Commit b35d2bf

Browse files
BilalG1N2D4
andauthored
fix unknown theme error (#773)
<!-- Make sure you've read the CONTRIBUTING.md guidelines: https://github.com/stack-auth/stack-auth/blob/dev/CONTRIBUTING.md --> <!-- ELLIPSIS_HIDDEN --> ---- > [!IMPORTANT] > Fixes unknown theme error by adding fallback to default theme and updates theme display names. > > - **Behavior**: > - In `route.tsx`, added fallback to `DEFAULT_EMAIL_THEME_ID` if current theme is unknown. > - Updates active theme in `tenancy.completeConfig.emails` if unknown. > - **Display Name Updates**: > - Changed `displayName` from `default-light` to `Default Light` and `default-dark` to `Default Dark` in `emails.ts` and `schema-fields.ts`. > - **Tests**: > - Updated test cases in `email-themes.test.ts` to reflect new display names. > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=stack-auth%2Fstack-auth&utm_source=github&utm_medium=referral)<sup> for 8d2b9f7. You can [customize](https://app.ellipsis.dev/stack-auth/settings/summaries) this summary. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN --> --------- Co-authored-by: Konsti Wohlwend <n2d4xc@gmail.com>
1 parent cf7a540 commit b35d2bf

4 files changed

Lines changed: 31 additions & 11 deletions

File tree

apps/backend/src/app/api/latest/internal/email-themes/route.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { overrideEnvironmentConfigOverride } from "@/lib/config";
22
import { DEFAULT_EMAIL_THEMES } from "@/lib/email-themes";
33
import { globalPrismaClient } from "@/prisma-client";
44
import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler";
5+
import { DEFAULT_EMAIL_THEME_ID } from "@stackframe/stack-shared/dist/helpers/emails";
56
import { adaptSchema, yupArray, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields";
67
import { generateUuid } from "@stackframe/stack-shared/dist/utils/uuids";
78

@@ -68,7 +69,26 @@ export const GET = createSmartRouteHandler({
6869
}).defined(),
6970
}),
7071
async handler({ auth: { tenancy } }) {
71-
const themes = Object.entries(tenancy.completeConfig.emails.themeList).map(([id, theme]) => ({
72+
const themeList = tenancy.completeConfig.emails.themeList;
73+
const currentActiveTheme = tenancy.completeConfig.emails.theme;
74+
if (!(currentActiveTheme in themeList)) {
75+
let newActiveTheme: string;
76+
if (DEFAULT_EMAIL_THEME_ID in themeList) {
77+
newActiveTheme = DEFAULT_EMAIL_THEME_ID;
78+
} else {
79+
newActiveTheme = Object.keys(themeList)[0];
80+
}
81+
await overrideEnvironmentConfigOverride({
82+
tx: globalPrismaClient,
83+
projectId: tenancy.project.id,
84+
branchId: tenancy.branchId,
85+
environmentConfigOverrideOverride: {
86+
"emails.theme": newActiveTheme,
87+
},
88+
});
89+
}
90+
91+
const themes = Object.entries(themeList).map(([id, theme]) => ({
7292
id,
7393
display_name: theme.displayName,
7494
}));

apps/e2e/tests/backend/endpoints/api/v1/email-themes.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { describe } from "vitest";
33
import { it } from "../../../../helpers";
44
import { niceBackendFetch, Project } from "../../../backend-helpers";
55

6-
const validThemeId = "1df07ae6-abf3-4a40-83a5-a1a2cbe336ac"; // default-light theme
6+
const validThemeId = "1df07ae6-abf3-4a40-83a5-a1a2cbe336ac"; // Default Light theme
77
const invalidThemeId = randomUUID();
88

99
const validTsxSource = `import { Html, Tailwind, Body } from '@react-email/components';
@@ -89,7 +89,7 @@ describe("get email theme", () => {
8989
NiceResponse {
9090
"status": 200,
9191
"body": {
92-
"display_name": "default-light",
92+
"display_name": "Default Light",
9393
"tsx_source": deindent\`
9494
import { Html, Tailwind, Body } from '@react-email/components';
9595
function EmailTheme({ children }: { children: React.ReactNode }) {
@@ -188,7 +188,7 @@ describe("update email theme", () => {
188188
NiceResponse {
189189
"status": 200,
190190
"body": {
191-
"display_name": "default-light",
191+
"display_name": "Default Light",
192192
"rendered_html": deindent\`
193193
<div>Mock api key detected, themeComponent: import { Html, Tailwind, Body } from '@react-email/components';
194194
function EmailTheme({ children }: { children: React.ReactNode }) {
@@ -240,7 +240,7 @@ describe("update email theme", () => {
240240
NiceResponse {
241241
"status": 200,
242242
"body": {
243-
"display_name": "default-light",
243+
"display_name": "Default Light",
244244
"tsx_source": deindent\`
245245
import { Html, Tailwind, Body } from '@react-email/components';
246246
function EmailTheme({ children }: { children: React.ReactNode }) {
@@ -284,11 +284,11 @@ describe("create email theme", () => {
284284
"body": {
285285
"themes": [
286286
{
287-
"display_name": "default-light",
287+
"display_name": "Default Light",
288288
"id": "<stripped UUID>",
289289
},
290290
{
291-
"display_name": "default-dark",
291+
"display_name": "Default Dark",
292292
"id": "<stripped UUID>",
293293
},
294294
],
@@ -304,7 +304,7 @@ describe("create email theme", () => {
304304
method: "POST",
305305
accessType: "admin",
306306
body: {
307-
display_name: "default-light",
307+
display_name: "Default Light",
308308
},
309309
}
310310
);

packages/stack-shared/src/helpers/emails.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ export const DEFAULT_EMAIL_THEME_ID = "1df07ae6-abf3-4a40-83a5-a1a2cbe336ac";
3333

3434
export const DEFAULT_EMAIL_THEMES = {
3535
[DEFAULT_EMAIL_THEME_ID]: {
36-
displayName: 'default-light',
36+
displayName: 'Default Light',
3737
tsxSource: LightEmailTheme,
3838
},
3939
"a0172b5d-cff0-463b-83bb-85124697373a": {
40-
displayName: 'default-dark',
40+
displayName: 'Default Dark',
4141
tsxSource: DarkEmailTheme,
4242
},
4343
};

packages/stack-shared/src/schema-fields.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ export const emailThemeSchema = yupString().meta({ openapiField: { description:
368368
export const emailThemeListSchema = yupRecord(
369369
yupString().uuid(),
370370
yupObject({
371-
displayName: yupString().meta({ openapiField: { description: 'Email theme name', exampleValue: 'default-light' } }).defined(),
371+
displayName: yupString().meta({ openapiField: { description: 'Email theme name', exampleValue: 'Default Light' } }).defined(),
372372
tsxSource: yupString().meta({ openapiField: { description: 'Email theme source code tsx component' } }).defined(),
373373
})
374374
).meta({ openapiField: { description: 'Record of email theme IDs to their display name and source code' } });

0 commit comments

Comments
 (0)