Skip to content

Commit

Permalink
feat(core): add update email template details api (#7017)
Browse files Browse the repository at this point in the history
add update email tempalte details api
  • Loading branch information
simeng-li authored Feb 11, 2025
1 parent bf7f399 commit a94f3b1
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
39 changes: 39 additions & 0 deletions packages/core/src/routes/email-template/index.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,45 @@
}
}
}
},
"/api/email-templates/{id}/details": {
"patch": {
"summary": "Update email template details",
"description": "Update the details of an email template by its ID.",
"requestBody": {
"content": {
"application/json": {
"schema": {
"properties": {
"subject": {
"description": "The template of the email subject."
},
"content": {
"description": "The template of the email body."
},
"contentType": {
"description": "The content type of the email body. (Only required by some specific email providers.)"
},
"replyTo": {
"description": "The reply name template of the email. If not provided, the target email address will be used. (The render logic may differ based on the email provider.)"
},
"sendFrom": {
"description": "The send from name template of the email. If not provided, the default Logto email address will be used. (The render logic may differ based on the email provider.)"
}
}
}
}
}
},
"responses": {
"200": {
"description": "The updated email template."
},
"404": {
"description": "The email template was not found."
}
}
}
}
}
}
31 changes: 30 additions & 1 deletion packages/core/src/routes/email-template/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmailTemplates } from '@logto/schemas';
import { emailTemplateDetailsGuard, EmailTemplates } from '@logto/schemas';
import { generateStandardId } from '@logto/shared';
import { z } from 'zod';

Expand Down Expand Up @@ -78,6 +78,35 @@ export default function emailTemplateRoutes<T extends ManagementApiRouter>(
}
);

router.patch(
`${pathPrefix}/:id/details`,
koaGuard({
params: z.object({
id: z.string(),
}),
body: emailTemplateDetailsGuard.partial(),
response: EmailTemplates.guard,
status: [200, 404],
}),
async (ctx, next) => {
const {
params: { id },
body,
} = ctx.guard;

const { details } = await emailTemplatesQueries.findById(id);

ctx.body = await emailTemplatesQueries.updateById(id, {
details: {
...details,
...body,
},
});

return next();
}
);

router.delete(
`${pathPrefix}/:id`,
koaGuard({
Expand Down
13 changes: 12 additions & 1 deletion packages/integration-tests/src/api/email-templates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { type CreateEmailTemplate, type EmailTemplate } from '@logto/schemas';
import {
type EmailTemplateDetails,
type CreateEmailTemplate,
type EmailTemplate,
} from '@logto/schemas';

import { authedAdminApi } from './index.js';

Expand All @@ -22,4 +26,11 @@ export class EmailTemplatesApi {
): Promise<EmailTemplate[]> {
return authedAdminApi.get(path, { searchParams: where }).json<EmailTemplate[]>();
}

async updateTemplateDetailsById(
id: string,
details: Partial<EmailTemplateDetails>
): Promise<EmailTemplate> {
return authedAdminApi.patch(`${path}/${id}/details`, { json: details }).json<EmailTemplate>();
}
}
20 changes: 20 additions & 0 deletions packages/integration-tests/src/tests/api/email-templates.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TemplateType } from '@logto/connector-kit';
import { type EmailTemplateDetails } from '@logto/schemas';

import { mockEmailTemplates } from '#src/__mocks__/email-templates.js';
import { EmailTemplatesApiTest } from '#src/helpers/email-templates.js';
Expand Down Expand Up @@ -80,4 +81,23 @@ devFeatureTest.describe('email templates', () => {
status: 404,
});
});

it('should partially update email template details by ID successfully', async () => {
const [template] = await emailTemplatesApi.create(mockEmailTemplates);

const updatedDetails: Partial<EmailTemplateDetails> = {
subject: `${template!.details.subject} updated`,
replyTo: 'logto test',
};

const updated = await emailTemplatesApi.updateTemplateDetailsById(template!.id, updatedDetails);
expect(updated.details).toEqual({ ...template!.details, ...updatedDetails });
});

it('should throw 404 when trying to partially update email template details by invalid ID', async () => {
await expectRejects(emailTemplatesApi.updateTemplateDetailsById('invalid-id', {}), {
code: 'entity.not_exists_with_id',
status: 404,
});
});
});

0 comments on commit a94f3b1

Please sign in to comment.