周报页新增自动发送区:管理员配置 SMTP/收件人/发送星期与小时,授权码用主密钥加密保存。 确认绑定当前草稿版本与正文哈希,正文再改则确认失效;worker 每分钟仅在启用、到点、周已结束且未发送过时发一次。 邮件同时提供渲染后的 HTML 与可读纯文本;SMTP 仅允许 TLS,拒绝明文。 迁移 000060 新增设置、确认与发送记录三张表,唯一键防重复发送。
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
import { getPayload } from './qaOps'
|
||||
|
||||
export interface WeeklyMailSettings {
|
||||
host: string
|
||||
port: number
|
||||
username: string
|
||||
from: string
|
||||
recipients: string[]
|
||||
weekday: number
|
||||
hour: number
|
||||
enabled: boolean
|
||||
has_password: boolean
|
||||
revision: number
|
||||
updated_by: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export interface WeeklyMailConfirmation {
|
||||
week: string
|
||||
draft_revision: number
|
||||
body_sha256: string
|
||||
confirmed_by: string
|
||||
confirmed_at: string
|
||||
current: boolean
|
||||
}
|
||||
|
||||
export interface WeeklyMailSend {
|
||||
week: string
|
||||
draft_revision: number
|
||||
status: 'sending' | 'sent' | 'failed'
|
||||
attempts: number
|
||||
last_error: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
const bad = (): never => {
|
||||
throw new Error('周报邮件响应无效,未显示或未标记为已保存')
|
||||
}
|
||||
const obj = (v: unknown): Record<string, unknown> =>
|
||||
v && typeof v === 'object' && !Array.isArray(v) ? (v as Record<string, unknown>) : bad()
|
||||
const str = (v: unknown): string => (typeof v === 'string' ? v : bad())
|
||||
const num = (v: unknown): number => (typeof v === 'number' && Number.isSafeInteger(v) && v >= 0 ? v : bad())
|
||||
|
||||
export function parseWeeklyMailSettings(value: unknown): WeeklyMailSettings {
|
||||
const v = obj(value)
|
||||
if (!Array.isArray(v.recipients) || v.recipients.some((item) => typeof item !== 'string')) return bad()
|
||||
if (typeof v.enabled !== 'boolean' || typeof v.has_password !== 'boolean') return bad()
|
||||
const weekday = num(v.weekday),
|
||||
hour = num(v.hour),
|
||||
port = num(v.port)
|
||||
if (weekday < 1 || weekday > 7 || hour > 23 || port < 1 || port > 65535) return bad()
|
||||
return {
|
||||
host: str(v.host),
|
||||
port,
|
||||
username: str(v.username),
|
||||
from: str(v.from),
|
||||
recipients: v.recipients as string[],
|
||||
weekday,
|
||||
hour,
|
||||
enabled: v.enabled,
|
||||
has_password: v.has_password,
|
||||
revision: num(v.revision),
|
||||
updated_by: str(v.updated_by),
|
||||
updated_at: v.updated_at === null ? '' : str(v.updated_at),
|
||||
}
|
||||
}
|
||||
|
||||
export function parseWeeklyMailConfirmation(value: unknown, week: string): WeeklyMailConfirmation | null {
|
||||
if (value === null) return null
|
||||
const v = obj(value)
|
||||
if (v.week !== week || typeof v.current !== 'boolean') return bad()
|
||||
return {
|
||||
week,
|
||||
draft_revision: num(v.draft_revision),
|
||||
body_sha256: str(v.body_sha256),
|
||||
confirmed_by: str(v.confirmed_by),
|
||||
confirmed_at: str(v.confirmed_at),
|
||||
current: v.current,
|
||||
}
|
||||
}
|
||||
|
||||
export function parseWeeklyMailSends(value: unknown): WeeklyMailSend[] {
|
||||
if (!Array.isArray(value)) return bad()
|
||||
return value.map((raw) => {
|
||||
const v = obj(raw)
|
||||
if (!['sending', 'sent', 'failed'].includes(str(v.status))) return bad()
|
||||
return {
|
||||
week: str(v.week),
|
||||
draft_revision: num(v.draft_revision),
|
||||
status: v.status as WeeklyMailSend['status'],
|
||||
attempts: num(v.attempts),
|
||||
last_error: str(v.last_error),
|
||||
updated_at: str(v.updated_at),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const endpoint = (path: string, week?: string) =>
|
||||
`/api/v1/qa-ops/master/weekly-mail/${path}${week ? '/' + encodeURIComponent(week) : ''}`
|
||||
const headers = { 'Content-Type': 'application/json', 'X-Governance-Action': 'qa-weekly-mail' }
|
||||
|
||||
export async function fetchWeeklyMailSettings(): Promise<WeeklyMailSettings> {
|
||||
return parseWeeklyMailSettings(((await getPayload(endpoint('settings'))) as { data: unknown }).data)
|
||||
}
|
||||
|
||||
export async function saveWeeklyMailSettings(
|
||||
base: number,
|
||||
form: Omit<WeeklyMailSettings, 'has_password' | 'revision' | 'updated_by' | 'updated_at'> & {
|
||||
password: string
|
||||
clear_password: boolean
|
||||
}
|
||||
): Promise<WeeklyMailSettings> {
|
||||
const payload = await getPayload(endpoint('settings'), undefined, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
base_revision: base,
|
||||
host: form.host,
|
||||
port: form.port,
|
||||
username: form.username,
|
||||
password: form.password,
|
||||
clear_password: form.clear_password,
|
||||
from: form.from,
|
||||
recipients: form.recipients,
|
||||
weekday: form.weekday,
|
||||
hour: form.hour,
|
||||
enabled: form.enabled,
|
||||
}),
|
||||
})
|
||||
return parseWeeklyMailSettings((payload as { data: unknown }).data)
|
||||
}
|
||||
|
||||
export async function fetchWeeklyMailConfirmation(week: string): Promise<WeeklyMailConfirmation | null> {
|
||||
const payload = await getPayload(endpoint('confirmations', week))
|
||||
return parseWeeklyMailConfirmation((payload as { data: { confirmation: unknown } }).data.confirmation, week)
|
||||
}
|
||||
|
||||
export async function confirmWeeklyMail(week: string, confirm: boolean): Promise<WeeklyMailConfirmation | null> {
|
||||
const payload = await getPayload(endpoint('confirmations', week), undefined, {
|
||||
method: confirm ? 'POST' : 'DELETE',
|
||||
headers,
|
||||
})
|
||||
return parseWeeklyMailConfirmation((payload as { data: { confirmation: unknown } }).data.confirmation, week)
|
||||
}
|
||||
|
||||
export async function fetchWeeklyMailSends(week: string): Promise<WeeklyMailSend[]> {
|
||||
const payload = await getPayload(endpoint('sends') + '?' + new URLSearchParams({ week }).toString())
|
||||
return parseWeeklyMailSends((payload as { data: { sends: unknown } }).data.sends)
|
||||
}
|
||||
|
||||
export const weeklyMailWeekdayLabels = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
|
||||
Reference in New Issue
Block a user