190 lines
6.4 KiB
TypeScript
190 lines
6.4 KiB
TypeScript
import { getPayload } from './qaOps'
|
|
|
|
export interface WeeklyMailSettings {
|
|
host: string
|
|
port: number
|
|
username: string
|
|
from: string
|
|
recipients: string[]
|
|
cc_recipients: string[]
|
|
review_address: string
|
|
weekday: number
|
|
hour: number
|
|
enabled: boolean
|
|
has_password: boolean
|
|
revision: number
|
|
updated_by: string
|
|
updated_at: string
|
|
}
|
|
|
|
export interface WeeklyMailConfirmation {
|
|
week: string
|
|
has_image: boolean
|
|
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)
|
|
const recipients = v.recipients === null ? [] : v.recipients
|
|
if (!Array.isArray(recipients) || recipients.some((item) => typeof item !== 'string')) return bad()
|
|
const cc = v.cc_recipients === null || v.cc_recipients === undefined ? [] : v.cc_recipients
|
|
if (!Array.isArray(cc) || cc.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: recipients as string[],
|
|
cc_recipients: cc as string[],
|
|
review_address: typeof v.review_address === 'string' ? v.review_address : '',
|
|
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,
|
|
has_image: v.has_image === true,
|
|
}
|
|
}
|
|
|
|
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')))
|
|
}
|
|
|
|
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,
|
|
cc_recipients: form.cc_recipients,
|
|
review_address: form.review_address,
|
|
weekday: form.weekday,
|
|
hour: form.hour,
|
|
enabled: form.enabled,
|
|
}),
|
|
})
|
|
return parseWeeklyMailSettings(payload)
|
|
}
|
|
|
|
export async function fetchWeeklyMailConfirmation(week: string): Promise<WeeklyMailConfirmation | null> {
|
|
const payload = (await getPayload(endpoint('confirmations', week))) as { confirmation: unknown }
|
|
return parseWeeklyMailConfirmation(payload.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 { confirmation: unknown }).confirmation, week)
|
|
}
|
|
|
|
export async function fetchWeeklyMailSends(week: string): Promise<WeeklyMailSend[]> {
|
|
const payload = (await getPayload(endpoint('sends') + '?' + new URLSearchParams({ week }).toString())) as {
|
|
sends: unknown
|
|
}
|
|
return parseWeeklyMailSends(payload.sends)
|
|
}
|
|
|
|
export const weeklyMailWeekdayLabels = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
|
|
|
|
export async function sendWeeklyMailTest(week: string): Promise<string> {
|
|
const payload = (await getPayload(endpoint('test'), undefined, {
|
|
method: 'POST',
|
|
headers,
|
|
body: JSON.stringify({ week }),
|
|
})) as { message_id: string }
|
|
if (typeof payload.message_id !== 'string' || !payload.message_id) bad()
|
|
return payload.message_id
|
|
}
|
|
|
|
export async function uploadWeeklyMailImage(week: string, png: Blob): Promise<number> {
|
|
const buffer = await png.arrayBuffer()
|
|
let binary = ''
|
|
const bytes = new Uint8Array(buffer)
|
|
for (let index = 0; index < bytes.length; index += 0x8000)
|
|
binary += String.fromCharCode(...bytes.subarray(index, index + 0x8000))
|
|
const payload = (await getPayload(endpoint('confirmations', week) + '/image', undefined, {
|
|
method: 'POST',
|
|
headers,
|
|
body: JSON.stringify({ png_base64: btoa(binary) }),
|
|
})) as { bytes: number }
|
|
if (typeof payload.bytes !== 'number' || payload.bytes <= 0) bad()
|
|
return payload.bytes
|
|
}
|