0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-17 22:04:19 -05:00

feat(core): add update email template details api (#7017)

add update email tempalte details api
This commit is contained in:
simeng-li 2025-02-11 14:58:36 +08:00 committed by GitHub
parent bf7f399844
commit a94f3b1c83
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 101 additions and 2 deletions

View file

@ -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."
}
}
}
}
}
}

View file

@ -1,4 +1,4 @@
import { EmailTemplates } from '@logto/schemas';
import { emailTemplateDetailsGuard, EmailTemplates } from '@logto/schemas';
import { generateStandardId } from '@logto/shared';
import { z } from 'zod';
@ -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({

View file

@ -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';
@ -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>();
}
}

View file

@ -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';
@ -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,
});
});
});