Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(schemas): add new email templates table #7002

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { sql } from '@silverhand/slonik';

import type { AlterationScript } from '../lib/types/alteration.js';

import { applyTableRls, dropTableRls } from './utils/1704934999-tables.js';

const alteration: AlterationScript = {
up: async (pool) => {
await pool.query(sql`
create table email_templates (
tenant_id varchar(21) not null
references tenants (id) on update cascade on delete cascade,
id varchar(21) not null,
language_tag varchar(16) not null,
template_type varchar(64) not null,
details jsonb not null,
created_at timestamptz not null default now(),
primary key (tenant_id, id),
constraint email_templates__tenant_id__language_tag__template_type
unique (tenant_id, language_tag, template_type)
);

create index email_templates__tenant_id__language_tag
on email_templates (tenant_id, language_tag);

create index email_templates__tenant_id__template_type
on email_templates (tenant_id, template_type);
`);

await applyTableRls(pool, 'email_templates');
},
down: async (pool) => {
await dropTableRls(pool, 'email_templates');
await pool.query(sql`
drop table if exists email_templates;
`);
},
};

export default alteration;
39 changes: 39 additions & 0 deletions packages/schemas/src/foundations/jsonb-types/email-templates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { z } from 'zod';

export { TemplateType, templateTypeGuard } from '@logto/connector-kit';

export type EmailTemplateDetails = {
subject: string;
content: string;
/**
* OPTIONAL: The content type of the email template.
*
* Some email clients may render email templates differently based on the content type. (e.g. Sendgrid, Mailgun)
* Use this field to specify the content type of the email template.
*/
contentType?: 'text/html' | 'text/plain';
/**
* OPTIONAL: Custom replyTo template.
*
* Based on the email client, the replyTo field may be used to customize the reply-to field of the email.
* @remarks
* The original reply email value can be found in the template variables.
*/
replyTo?: string;
/**
* OPTIONAL: Custom from template.
*
* Based on the email client, the sendFrom field may be used to customize the from field of the email.
* @remarks
* The sender email value can be found in the template variables.
*/
sendFrom?: string;
};

export const emailTemplateDetailsGuard = z.object({
subject: z.string(),
content: z.string(),
contentType: z.union([z.literal('text/html'), z.literal('text/plain')]).optional(),
replyTo: z.string().optional(),
sendFrom: z.string().optional(),
}) satisfies z.ZodType<EmailTemplateDetails>;
1 change: 1 addition & 0 deletions packages/schemas/src/foundations/jsonb-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './verification-records.js';
export * from './account-centers.js';
export * from './saml-application-configs.js';
export * from './saml-application-sessions.js';
export * from './email-templates.js';

export {
configurableConnectorMetadataGuard,
Expand Down
18 changes: 18 additions & 0 deletions packages/schemas/tables/email_templates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
create table email_templates (
tenant_id varchar(21) not null
references tenants (id) on update cascade on delete cascade,
id varchar(21) not null,
language_tag varchar(16) not null,
template_type varchar(64) /* @use TemplateType */ not null,
details jsonb /* @use EmailTemplateDetails */ not null,
created_at timestamptz not null default now(),
primary key (tenant_id, id),
constraint email_templates__tenant_id__language_tag__template_type
unique (tenant_id, language_tag, template_type)
);

create index email_templates__tenant_id__language_tag
on email_templates (tenant_id, language_tag);

create index email_templates__tenant_id__template_type
on email_templates (tenant_id, template_type);
Loading