Add read-only Noya admin panel with assignments and UX polish.
Ship admin auth, user overview, assessments, chats, assignment filters/export, security hardening, and a separate desktop admin shell with clearer assessment answers. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
import type { AssignmentFormField } from './definitions.js';
|
||||
|
||||
export type FormValidationIssue = {
|
||||
fieldKey: string;
|
||||
message: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Validate response/draft data against a formSchema.
|
||||
* Incomplete drafts are allowed at save time; call this before completion / «done» day entries.
|
||||
*/
|
||||
export function validateFormData(
|
||||
schema: AssignmentFormField[] | null,
|
||||
data: Record<string, unknown>,
|
||||
options?: { requireAllRequired?: boolean },
|
||||
): FormValidationIssue[] {
|
||||
const requireAll = options?.requireAllRequired !== false;
|
||||
const issues: FormValidationIssue[] = [];
|
||||
|
||||
if (!schema || schema.length === 0) {
|
||||
const text = String(data.text ?? '').trim();
|
||||
if (requireAll && text.length < 3) {
|
||||
issues.push({ fieldKey: 'text', message: 'پاسخ کوتاه است' });
|
||||
}
|
||||
return issues;
|
||||
}
|
||||
|
||||
for (const field of schema) {
|
||||
if (field.visibleWhen) {
|
||||
const gate = data[field.visibleWhen.field];
|
||||
if (gate !== field.visibleWhen.equals) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!requireAll && !field.required) continue;
|
||||
if (!field.required && requireAll) {
|
||||
// optional fields: if present as list, still validate filled items' required subfields lightly
|
||||
if (field.type === 'list' && Array.isArray(data[field.key])) {
|
||||
validateListItems(field, data[field.key] as Record<string, unknown>[], issues, false);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
const raw = data[field.key];
|
||||
|
||||
if (field.type === 'checkbox') {
|
||||
if (field.required && raw !== true) {
|
||||
issues.push({ fieldKey: field.key, message: `«${field.label}» الزامی است` });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (field.type === 'list') {
|
||||
if (!Array.isArray(raw) || raw.length === 0) {
|
||||
issues.push({ fieldKey: field.key, message: `حداقل یک مورد برای «${field.label}» لازم است` });
|
||||
continue;
|
||||
}
|
||||
validateListItems(field, raw as Record<string, unknown>[], issues, true);
|
||||
continue;
|
||||
}
|
||||
|
||||
const text = String(raw ?? '').trim();
|
||||
if (field.required && text.length === 0) {
|
||||
issues.push({ fieldKey: field.key, message: `«${field.label}» را پر کنید` });
|
||||
}
|
||||
}
|
||||
|
||||
return issues;
|
||||
}
|
||||
|
||||
function validateListItems(
|
||||
field: AssignmentFormField,
|
||||
items: Record<string, unknown>[],
|
||||
issues: FormValidationIssue[],
|
||||
requireItem: boolean,
|
||||
) {
|
||||
const itemFields = field.itemFields ?? [];
|
||||
let anyCompleteItem = false;
|
||||
|
||||
items.forEach((item, index) => {
|
||||
let itemHasContent = false;
|
||||
for (const sub of itemFields) {
|
||||
if (sub.type === 'checkbox') {
|
||||
if (item[sub.key] === true) itemHasContent = true;
|
||||
if (sub.required && requireItem && item[sub.key] !== true) {
|
||||
issues.push({
|
||||
fieldKey: `${field.key}[${index}].${sub.key}`,
|
||||
message: `مورد ${index + 1}: «${sub.label}» الزامی است`,
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const text = String(item[sub.key] ?? '').trim();
|
||||
if (text) itemHasContent = true;
|
||||
if (sub.required && requireItem && !text) {
|
||||
issues.push({
|
||||
fieldKey: `${field.key}[${index}].${sub.key}`,
|
||||
message: `مورد ${index + 1}: «${sub.label}» را پر کنید`,
|
||||
});
|
||||
}
|
||||
}
|
||||
if (itemHasContent) anyCompleteItem = true;
|
||||
});
|
||||
|
||||
if (requireItem && field.required && !anyCompleteItem) {
|
||||
issues.push({ fieldKey: field.key, message: `حداقل یک مورد کامل برای «${field.label}» لازم است` });
|
||||
}
|
||||
}
|
||||
|
||||
export function formatValidationMessage(issues: FormValidationIssue[]): string {
|
||||
if (issues.length === 0) return '';
|
||||
return issues.map((i) => i.message).join('؛ ');
|
||||
}
|
||||
|
||||
/** Build a readable summary from structured answers (for HomeworkReport bridge). */
|
||||
export function summarizeFormData(
|
||||
schema: AssignmentFormField[] | null,
|
||||
data: Record<string, unknown>,
|
||||
): string {
|
||||
if (!schema || schema.length === 0) {
|
||||
return String(data.text ?? '').trim();
|
||||
}
|
||||
|
||||
const lines: string[] = [];
|
||||
for (const field of schema) {
|
||||
const raw = data[field.key];
|
||||
if (field.type === 'checkbox') {
|
||||
lines.push(`${field.label}: ${raw ? 'بله' : 'خیر'}`);
|
||||
continue;
|
||||
}
|
||||
if (field.type === 'list' && Array.isArray(raw)) {
|
||||
const parts = (raw as Record<string, unknown>[])
|
||||
.map((item, index) => {
|
||||
const sub = (field.itemFields ?? [])
|
||||
.map((sf) => {
|
||||
if (sf.type === 'checkbox') return `${sf.label}: ${item[sf.key] ? 'بله' : 'خیر'}`;
|
||||
const text = String(item[sf.key] ?? '').trim();
|
||||
return text ? `${sf.label}: ${text}` : null;
|
||||
})
|
||||
.filter(Boolean);
|
||||
return sub.length ? `مورد ${index + 1}: ${sub.join('؛ ')}` : null;
|
||||
})
|
||||
.filter(Boolean);
|
||||
if (parts.length) lines.push(`${field.label}:\n${parts.join('\n')}`);
|
||||
continue;
|
||||
}
|
||||
const text = String(raw ?? '').trim();
|
||||
if (text) lines.push(`${field.label}: ${text}`);
|
||||
}
|
||||
return lines.join('\n').trim();
|
||||
}
|
||||
Reference in New Issue
Block a user