22 lines
1.5 KiB
TypeScript
22 lines
1.5 KiB
TypeScript
import type { Demand } from '../demand.ts';
|
|||
|
|
import { getDatabase } from './database.ts';
|
||
|
|
import { AuthError, type AuthUser } from './auth.ts';
|
||
|
|
|
||
|
|
/** Resolve missing assignments without rewriting existing responsibilities or history. */
|
||
|
|
export function withDefaultDeveloper(demand: Demand): Demand {
|
||
|
|
if (demand.review || ['草稿', '已取消', '已完成'].includes(demand.status)) return demand;
|
||
|
|
const admin = getDatabase().prepare(`SELECT id, username, display_name FROM accounts
|
||
|
|
WHERE role = 'admin' AND disabled = 0 ORDER BY created_at, id LIMIT 1`)
|
||
|
|
.first<{ id: string; username: string; display_name: string }>();
|
||
|
|
if (!admin) throw new AuthError('暂无启用的系统管理员,请联系管理员恢复账号后重试。', 503);
|
||
|
|
return { ...demand, review: { developer: { userId: admin.id, username: admin.username, displayName: admin.display_name }, proposals: [] } };
|
||
|
|
}
|
||
|
|
|
||
|
|
/** A read-only administrator can only recover an unavailable developer's assignment. */
|
||
|
|
export function canTakeOverDevelopment(demand: Demand, user: AuthUser): boolean {
|
||
|
|
if (user.role !== 'admin' || !demand.review || ['草稿', '已取消'].includes(demand.status) || demand.review.developer.userId === user.userId) return false;
|
||
|
|
return Boolean(getDatabase().prepare(`SELECT 1 FROM accounts
|
||
|
|
WHERE id = ? AND role = 'admin' AND disabled = 0 AND must_change_password = 0
|
||
|
|
AND NOT EXISTS (SELECT 1 FROM accounts WHERE id = ? AND disabled = 0)`)
|
||
|
|
.bind(user.userId, demand.review.developer.userId).first());
|
||
|
|
}
|