Files
order_site/apps/backend/src/repositories/claim-token-repo.js
T
2026-04-08 16:30:42 +08:00

70 lines
1.4 KiB
JavaScript

import { getDb } from '../db/client.js'
export function createClaimToken(input) {
const result = getDb().prepare(`
INSERT INTO claim_tokens (
task_id,
token,
status,
expired_at,
used_at,
max_use_count,
used_count,
created_at,
updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`).run(
input.taskId,
input.token,
input.status,
input.expiredAt,
input.usedAt,
input.maxUseCount,
input.usedCount,
input.createdAt,
input.updatedAt,
)
return getClaimTokenById(Number(result.lastInsertRowid))
}
export function getClaimTokenById(tokenId) {
return getDb().prepare('SELECT * FROM claim_tokens WHERE id = ? LIMIT 1').get(tokenId) || null
}
export function findClaimTokenByToken(token) {
return getDb().prepare('SELECT * FROM claim_tokens WHERE token = ? LIMIT 1').get(token) || null
}
export function updateClaimToken(tokenId, patch) {
const current = getClaimTokenById(tokenId)
if (!current) {
return null
}
const next = { ...current, ...patch }
getDb().prepare(`
UPDATE claim_tokens
SET
status = ?,
expired_at = ?,
used_at = ?,
max_use_count = ?,
used_count = ?,
updated_at = ?
WHERE id = ?
`).run(
next.status,
next.expired_at,
next.used_at,
next.max_use_count,
next.used_count,
next.updated_at,
tokenId,
)
return getClaimTokenById(tokenId)
}