feat(fund): 备用金账户编号改为带用户名的可读编号
This commit is contained in:
@@ -278,6 +278,37 @@ function generateNo(prefix: string) {
|
||||
return randomId(prefix, 5).toUpperCase()
|
||||
}
|
||||
|
||||
function buildFundAccountNoBase(username: string): string {
|
||||
const slug =
|
||||
String(username || '')
|
||||
.trim()
|
||||
.replace(/[^A-Za-z0-9_]+/g, '_')
|
||||
.replace(/^_+|_+$/g, '')
|
||||
.toUpperCase()
|
||||
.slice(0, 32) || 'ACCOUNT'
|
||||
return `FA-${slug}`
|
||||
}
|
||||
|
||||
/** 账户编号直接带用户名,列表里一眼能对应到人;重名时追加 -2、-3。 */
|
||||
async function allocateFundAccountNo(client: PoolClient, username: string): Promise<string> {
|
||||
const base = buildFundAccountNoBase(username)
|
||||
let candidate = base
|
||||
for (let suffix = 2; suffix <= 99; suffix += 1) {
|
||||
const existing = await client.query(
|
||||
'SELECT 1 FROM fund_accounts WHERE account_no = $1 LIMIT 1',
|
||||
[candidate],
|
||||
)
|
||||
if (!existing.rowCount) {
|
||||
return candidate
|
||||
}
|
||||
candidate = `${base}-${suffix}`
|
||||
}
|
||||
throw createHttpError('生成备用金账户编号失败', {
|
||||
statusCode: 500,
|
||||
errorCode: 'fund_account_no_conflict',
|
||||
})
|
||||
}
|
||||
|
||||
function requiredRow<T>(row: T | undefined, message: string): T {
|
||||
if (!row) {
|
||||
throw createHttpError(message, {
|
||||
@@ -974,7 +1005,7 @@ export async function createAdminFundAccount(payload: JsonObject, session: Admin
|
||||
})
|
||||
}
|
||||
|
||||
const accountNo = generateNo('FA')
|
||||
const accountNo = await allocateFundAccountNo(client, user.username)
|
||||
const insertResult = await client.query<FundAccountRow>(
|
||||
`
|
||||
INSERT INTO fund_accounts (
|
||||
@@ -1062,7 +1093,7 @@ export async function ensureAdminFundAgentAccounts(payload: JsonObject, session:
|
||||
|
||||
const created: Array<{ id: number; accountNo: string; ownerName: string }> = []
|
||||
for (const user of usersResult.rows) {
|
||||
const accountNo = generateNo('FA')
|
||||
const accountNo = await allocateFundAccountNo(client, user.username)
|
||||
const insertResult = await client.query<FundAccountRow>(
|
||||
`
|
||||
INSERT INTO fund_accounts (
|
||||
|
||||
Reference in New Issue
Block a user