后端建立 TypeScript 构建链路

This commit is contained in:
yml
2026-05-21 13:36:38 +08:00
parent 25671078d6
commit 1476522ab5
18 changed files with 798 additions and 101 deletions
@@ -1,7 +1,46 @@
import { query } from '../db/client.js'
export async function createAdminAuditLog(input) {
const result = await query(
type AdminAuditLogRow = {
[column: string]: unknown
id: number
actor_user_id: number | null
actor_username: string
actor_role: string
action: string
target_type: string
target_id: string
payload_json: string | Record<string, unknown>
created_at: string
}
type AdminAuditLogCreateInput = {
actorUserId?: number | string | null
actorUsername?: string
actorRole?: string
action: string
targetType: string
targetId: string
payloadJson?: string
createdAt: string
}
type AdminAuditLogListInput = {
actorUsername?: string
action?: string
targetType?: string
dateFrom?: string
dateTo?: string
page?: number | string
pageSize?: number | string
}
type AdminAuditLogListResult = {
items: AdminAuditLogRow[]
total: number
}
export async function createAdminAuditLog(input: AdminAuditLogCreateInput): Promise<AdminAuditLogRow | null> {
const result = await query<AdminAuditLogRow>(
`
INSERT INTO admin_audit_logs (
actor_user_id,
@@ -30,14 +69,14 @@ export async function createAdminAuditLog(input) {
return result.rows[0] || null
}
export async function getAdminAuditLogById(logId) {
const result = await query('SELECT * FROM admin_audit_logs WHERE id = $1 LIMIT 1', [Number(logId)])
export async function getAdminAuditLogById(logId: number | string): Promise<AdminAuditLogRow | null> {
const result = await query<AdminAuditLogRow>('SELECT * FROM admin_audit_logs WHERE id = $1 LIMIT 1', [Number(logId)])
return result.rows[0] || null
}
export async function listAdminAuditLogs(queryInput = {}) {
const conditions = []
const params = []
export async function listAdminAuditLogs(queryInput: AdminAuditLogListInput = {}): Promise<AdminAuditLogListResult> {
const conditions: string[] = []
const params: unknown[] = []
if (queryInput.actorUsername) {
params.push(queryInput.actorUsername)
@@ -69,14 +108,14 @@ export async function listAdminAuditLogs(queryInput = {}) {
const pageSize = Number(queryInput.pageSize) || 20
const offset = (page - 1) * pageSize
const totalResult = await query(
const totalResult = await query<{ [column: string]: unknown, total: number }>(
`SELECT COUNT(*)::int AS total FROM admin_audit_logs ${whereClause}`,
params,
)
params.push(pageSize)
params.push(offset)
const itemsResult = await query(
const itemsResult = await query<AdminAuditLogRow>(
`
SELECT *
FROM admin_audit_logs
@@ -1,7 +1,38 @@
import { query } from '../db/client.js'
export async function createClaimToken(input) {
const result = await query(
type ClaimTokenRow = {
[column: string]: unknown
id: number
task_id: number
token: string
status: string
expired_at: string
used_at: string | null
max_use_count: number
used_count: number
created_at: string
updated_at: string
}
type ClaimTokenCreateInput = {
taskId: number
token: string
status: string
expiredAt: string
usedAt?: string | null
maxUseCount: number
usedCount: number
createdAt: string
updatedAt: string
}
type ClaimTokenPatch = Partial<Pick<
ClaimTokenRow,
'status' | 'expired_at' | 'used_at' | 'max_use_count' | 'used_count' | 'updated_at'
>>
export async function createClaimToken(input: ClaimTokenCreateInput): Promise<ClaimTokenRow | null> {
const result = await query<ClaimTokenRow>(
`
INSERT INTO claim_tokens (
task_id,
@@ -42,24 +73,24 @@ export async function createClaimToken(input) {
return result.rows[0] || null
}
export async function getClaimTokenById(tokenId) {
const result = await query('SELECT * FROM claim_tokens WHERE id = $1 LIMIT 1', [Number(tokenId)])
export async function getClaimTokenById(tokenId: number | string): Promise<ClaimTokenRow | null> {
const result = await query<ClaimTokenRow>('SELECT * FROM claim_tokens WHERE id = $1 LIMIT 1', [Number(tokenId)])
return result.rows[0] || null
}
export async function findClaimTokenByToken(token) {
const result = await query('SELECT * FROM claim_tokens WHERE token = $1 LIMIT 1', [String(token || '').trim()])
export async function findClaimTokenByToken(token: string): Promise<ClaimTokenRow | null> {
const result = await query<ClaimTokenRow>('SELECT * FROM claim_tokens WHERE token = $1 LIMIT 1', [String(token || '').trim()])
return result.rows[0] || null
}
export async function updateClaimToken(tokenId, patch) {
export async function updateClaimToken(tokenId: number | string, patch: ClaimTokenPatch): Promise<ClaimTokenRow | null> {
const current = await getClaimTokenById(tokenId)
if (!current) {
return null
}
const next = { ...current, ...patch }
const result = await query(
const result = await query<ClaimTokenRow>(
`
UPDATE claim_tokens
SET
@@ -1,38 +0,0 @@
import { query } from '../db/client.js'
export async function createTaskEvent(taskId, eventType, payload = {}, createdAt) {
const result = await query(
`
INSERT INTO task_events (
task_id,
event_type,
payload_json,
created_at
) VALUES ($1, $2, $3::jsonb, $4)
RETURNING *
`,
[
Number(taskId),
String(eventType || '').trim(),
JSON.stringify(payload || {}),
createdAt,
],
)
return result.rows[0] || null
}
export async function listTaskEventsByTaskId(taskId, { limit = 20 } = {}) {
const result = await query(
`
SELECT *
FROM task_events
WHERE task_id = $1
ORDER BY created_at DESC, id DESC
LIMIT $2
`,
[Number(taskId), Math.max(1, Number(limit || 20))],
)
return result.rows
}
@@ -0,0 +1,59 @@
import { query } from '../db/client.js'
type TaskEventRow = {
[column: string]: unknown
id: number
task_id: number
event_type: string
payload_json: string | Record<string, unknown>
created_at: string
}
type TaskEventListOptions = {
limit?: number | string
}
export async function createTaskEvent(
taskId: number | string,
eventType: string,
payload: unknown = {},
createdAt: string | undefined = undefined,
): Promise<TaskEventRow | null> {
const result = await query<TaskEventRow>(
`
INSERT INTO task_events (
task_id,
event_type,
payload_json,
created_at
) VALUES ($1, $2, $3::jsonb, $4)
RETURNING *
`,
[
Number(taskId),
String(eventType || '').trim(),
JSON.stringify(payload || {}),
createdAt,
],
)
return result.rows[0] || null
}
export async function listTaskEventsByTaskId(
taskId: number | string,
{ limit = 20 }: TaskEventListOptions = {},
): Promise<TaskEventRow[]> {
const result = await query<TaskEventRow>(
`
SELECT *
FROM task_events
WHERE task_id = $1
ORDER BY created_at DESC, id DESC
LIMIT $2
`,
[Number(taskId), Math.max(1, Number(limit || 20))],
)
return result.rows
}