31 lines
888 B
JavaScript
31 lines
888 B
JavaScript
import { runtimeConfig } from '../../config/runtime.js'
|
|
import { createClaimToken } from '../../repositories/claim-token-repo.js'
|
|
import { addHours, nowIso } from '../../utils/time.js'
|
|
import { randomToken } from '../../utils/random.js'
|
|
|
|
export async function createTaskClaimToken(taskId) {
|
|
const createdAt = nowIso()
|
|
const expiredAt = addHours(createdAt, Number(runtimeConfig.orders.tokenTtlHours || 24))
|
|
const token = await createClaimToken({
|
|
taskId,
|
|
token: randomToken(24),
|
|
status: 'active',
|
|
expiredAt,
|
|
usedAt: null,
|
|
maxUseCount: 1,
|
|
usedCount: 0,
|
|
createdAt,
|
|
updatedAt: createdAt,
|
|
})
|
|
|
|
return {
|
|
...token,
|
|
claimUrl: buildClaimUrl(token.token),
|
|
}
|
|
}
|
|
|
|
export function buildClaimUrl(token) {
|
|
const baseUrl = String(runtimeConfig.orders.claimBaseUrl || '').trim()
|
|
return baseUrl ? `${baseUrl.replace(/\/+$/, '')}/${token}` : token
|
|
}
|