Admin panel in English and Polish, reworded per docs/dev/admin-ux.md
- admin-i18n module: AdminText { en, pl }, component texts under the ipal
namespace (project translations win), en + pl admin languages when the
project sets none (project's supportedLanguages wins)
- every plugin label, description, option, tab and group as { en, pl };
explicit labels instead of auto-labels; sentence case; glossary terms
- visible renames (slugs, names, data unchanged): Cookie banner, Form
messages, Privacy group, Search engines, Maintenance, Spam protection,
E-mail sending, stats nav link Overview (hidden without access)
- Polish visitor defaults by page locale: cookie banner, form messages,
validators, lightbox, accessibility widget; {field} uses the label
- fixes: e-mail status banner used Turnstile wording; developer words and
env var names out of editor messages; stats numbers/dates in the admin
language; test e-mail no longer says ipal-kit
- test/adminUx.test.ts: length limits, en + pl present, no developer words
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
eeac33ed2f
commit
6b46bc0c14
@@ -3,6 +3,7 @@ import type { Endpoint, PayloadRequest } from 'payload'
|
||||
import { addDataAndFileToRequest } from 'payload'
|
||||
|
||||
import { userMeetsRole } from '../../access/predicates.js'
|
||||
import { serverText } from '../../integrations/texts.js'
|
||||
|
||||
/**
|
||||
* Custom endpoint: send a test email to a given address through whatever
|
||||
@@ -17,56 +18,74 @@ import { userMeetsRole } from '../../access/predicates.js'
|
||||
* Returns the adapter's own result so the panel can show exactly what happened,
|
||||
* including the transport-specific error (SMTP auth failure, Graph 401, etc.).
|
||||
*/
|
||||
/** The site name from Site settings (in the request's locale), or '' when unavailable. */
|
||||
async function siteName(req: PayloadRequest): Promise<string> {
|
||||
try {
|
||||
const doc = (await req.payload.findGlobal({ slug: 'site-settings' as never, depth: 0, req })) as { siteName?: unknown }
|
||||
return typeof doc?.siteName === 'string' ? doc.siteName.trim() : ''
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
const escapeHtml = (v: string) =>
|
||||
v.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"')
|
||||
|
||||
export const testEmailEndpoint: Endpoint = {
|
||||
handler: async (req: PayloadRequest) => {
|
||||
// Messages in the language of the admin user who clicked the button.
|
||||
const i18n = req.i18n as Parameters<typeof serverText>[0]
|
||||
// Auth: only admins may trigger a send — the same gate as the
|
||||
// SiteIntegrations global that holds the transport settings.
|
||||
if (!req.user) {
|
||||
return Response.json({ error: 'Unauthorized', ok: false }, { status: 401 })
|
||||
return Response.json({ error: serverText(i18n, 'testEmailLoggedOut'), ok: false }, { status: 401 })
|
||||
}
|
||||
if (!userMeetsRole(req, 'admin')) {
|
||||
return Response.json({ error: 'Forbidden', ok: false }, { status: 403 })
|
||||
return Response.json({ error: serverText(i18n, 'testEmailForbidden'), ok: false }, { status: 403 })
|
||||
}
|
||||
|
||||
await addDataAndFileToRequest(req)
|
||||
const to = (req.data?.to as string | undefined)?.trim()
|
||||
|
||||
if (!to || !/^[^@\s]+@[^\s@][^\s.@]*\.[^\s@]+$/.test(to)) {
|
||||
return Response.json(
|
||||
{ error: 'Provide a valid recipient address.', ok: false },
|
||||
{ status: 400 },
|
||||
)
|
||||
return Response.json({ error: serverText(i18n, 'testEmailInvalidRecipient'), ok: false }, { status: 400 })
|
||||
}
|
||||
|
||||
const site = await siteName(req)
|
||||
const subject = site ? serverText(i18n, 'testEmailSubject', { site }) : serverText(i18n, 'testEmailSubjectGeneric')
|
||||
const text = site ? serverText(i18n, 'testEmailBody', { site }) : serverText(i18n, 'testEmailBodyGeneric')
|
||||
|
||||
try {
|
||||
const info = await req.payload.sendEmail({
|
||||
html: '<p>This is a test message from <strong>ipal-kit</strong>. If you received it, outbound email is configured correctly.</p>',
|
||||
subject: 'ipal-kit — test email',
|
||||
text: 'This is a test message from ipal-kit. If you received it, outbound email is configured correctly.',
|
||||
html: `<p>${escapeHtml(text)}</p>`,
|
||||
subject,
|
||||
text,
|
||||
to,
|
||||
})
|
||||
|
||||
// Payload's sendEmail resolves with the adapter's result. Our adapters
|
||||
// return { sent: boolean, error?: string }; nodemailer returns info with
|
||||
// messageId. Normalize to a simple ok/message for the panel.
|
||||
// return { sent: boolean, error?: string, code?: string }; nodemailer
|
||||
// returns info with messageId. Normalize to a simple ok/message for the panel.
|
||||
const sent =
|
||||
info && typeof info === 'object' && 'sent' in info
|
||||
? (info as { sent?: boolean }).sent !== false
|
||||
: true
|
||||
|
||||
if (!sent) {
|
||||
const error = (info as { error?: string })?.error ?? 'Send failed (see server logs).'
|
||||
const failed = info as { code?: string; detail?: string; error?: string }
|
||||
req.payload.logger.error(`[ipal] Test email not sent: ${failed?.detail ?? failed?.error ?? 'unknown reason'}`)
|
||||
const error =
|
||||
failed?.code === 'not-configured'
|
||||
? serverText(i18n, 'testEmailNotConfigured')
|
||||
: serverText(i18n, 'testEmailFailed')
|
||||
return Response.json({ error, ok: false }, { status: 502 })
|
||||
}
|
||||
|
||||
return Response.json({ message: `Test email sent to ${to}.`, ok: true })
|
||||
return Response.json({ message: serverText(i18n, 'testEmailSent', { to }), ok: true })
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
req.payload.logger.error(`[ipal] Test email failed: ${message}`)
|
||||
return Response.json(
|
||||
{ error: 'Send failed. Check transport settings and server logs.', ok: false },
|
||||
{ status: 502 },
|
||||
)
|
||||
return Response.json({ error: serverText(i18n, 'testEmailFailed'), ok: false }, { status: 502 })
|
||||
}
|
||||
},
|
||||
method: 'post',
|
||||
|
||||
Reference in New Issue
Block a user