删除库存和旧回调管理
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { query, withTransaction } from '../db/client.js'
|
||||
import { query } from '../db/client.js'
|
||||
|
||||
type AdminUserRow = {
|
||||
id: number
|
||||
@@ -8,7 +8,6 @@ type AdminUserRow = {
|
||||
status: string
|
||||
created_at: string
|
||||
updated_at: string
|
||||
inventory_group_codes: string[]
|
||||
}
|
||||
|
||||
type AdminUserCreateInput = {
|
||||
@@ -39,15 +38,8 @@ type AdminUserListResult = {
|
||||
}
|
||||
|
||||
const ADMIN_USER_SELECT = `
|
||||
SELECT
|
||||
au.*,
|
||||
COALESCE(bindings.inventory_group_codes, ARRAY[]::text[]) AS inventory_group_codes
|
||||
SELECT au.*
|
||||
FROM admin_users au
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT ARRAY_AGG(auigb.inventory_group_code ORDER BY auigb.inventory_group_code) AS inventory_group_codes
|
||||
FROM admin_user_inventory_group_bindings auigb
|
||||
WHERE auigb.admin_user_id = au.id
|
||||
) bindings ON TRUE
|
||||
`
|
||||
|
||||
export async function getAdminUserById(userId: number | string): Promise<AdminUserRow | null> {
|
||||
@@ -183,39 +175,3 @@ export async function countActiveAdminUsers(): Promise<number> {
|
||||
)
|
||||
return Number(result.rows[0]?.total || 0)
|
||||
}
|
||||
|
||||
export async function replaceAdminUserInventoryGroupBindings(
|
||||
userId: number | string,
|
||||
inventoryGroupCodes: unknown[] = [],
|
||||
timestamp: string,
|
||||
): Promise<AdminUserRow | null> {
|
||||
const normalizedUserId = Number(userId)
|
||||
const normalizedCodes = Array.from(new Set((Array.isArray(inventoryGroupCodes) ? inventoryGroupCodes : [])
|
||||
.map((value) => String(value || '').trim())
|
||||
.filter(Boolean)))
|
||||
|
||||
await withTransaction(async (client) => {
|
||||
await client.query(
|
||||
'DELETE FROM admin_user_inventory_group_bindings WHERE admin_user_id = $1',
|
||||
[normalizedUserId],
|
||||
)
|
||||
|
||||
for (const inventoryGroupCode of normalizedCodes) {
|
||||
await client.query(
|
||||
`
|
||||
INSERT INTO admin_user_inventory_group_bindings (
|
||||
admin_user_id,
|
||||
inventory_group_code,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (admin_user_id, inventory_group_code) DO UPDATE
|
||||
SET updated_at = EXCLUDED.updated_at
|
||||
`,
|
||||
[normalizedUserId, inventoryGroupCode, timestamp, timestamp],
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
return getAdminUserById(normalizedUserId)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user