Implement 15 platform features: digest, availability, templates, comparison, live voting SSE, file versioning, mentorship, messaging, analytics, drafts, webhooks, peer review, audit enhancements, i18n
Build and Push Docker Image / build (push) Has been cancelled
Details
Build and Push Docker Image / build (push) Has been cancelled
Details
Features implemented: - F1: Email digest notifications with cron endpoint and per-user frequency - F2: Jury availability windows and workload preferences in smart assignment - F3: Round templates with save-from-round and CRUD management - F4: Side-by-side project comparison view for jury members - F5: Real-time voting dashboard with Server-Sent Events (SSE) - F6: Live voting UX: QR codes, audience voting, tie-breaking, score animations - F7: File versioning, inline preview, bulk download with presigned URLs - F8: Mentor dashboard: milestones, private notes, activity tracking - F9: Communication hub with broadcasts, templates, and recipient targeting - F10: Advanced analytics: cross-round comparison, juror consistency, diversity metrics, PDF export - F11: Applicant draft saving with magic link resume and cron cleanup - F12: Webhook integration layer with HMAC signing, retry, and delivery logs - F13: Peer review discussions with anonymized scores and threaded comments - F14: Audit log enhancements: before/after diffs, session grouping, anomaly detection, retention - F15: i18n foundation with next-intl (EN/FR), cookie-based locale, language switcher Schema: 12 new models, field additions to User, Project, ProjectFile, LiveVotingSession, LiveVote, MentorAssignment, AuditLog, Program New routers: roundTemplate, message, webhook (registered in _app.ts) New services: email-digest, webhook-dispatcher New cron endpoints: /api/cron/digest, /api/cron/draft-cleanup, /api/cron/audit-cleanup New API routes: /api/live-voting/stream (SSE), /api/files/bulk-download All features are admin-configurable via SystemSettings or per-model settingsJson fields. Docker build verified successfully. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
f038c95777
commit
59436ed67a
|
|
@ -0,0 +1,321 @@
|
|||
{
|
||||
"common": {
|
||||
"loading": "Loading...",
|
||||
"save": "Save",
|
||||
"cancel": "Cancel",
|
||||
"delete": "Delete",
|
||||
"edit": "Edit",
|
||||
"create": "Create",
|
||||
"close": "Close",
|
||||
"confirm": "Confirm",
|
||||
"back": "Back",
|
||||
"next": "Next",
|
||||
"submit": "Submit",
|
||||
"search": "Search",
|
||||
"filter": "Filter",
|
||||
"export": "Export",
|
||||
"import": "Import",
|
||||
"refresh": "Refresh",
|
||||
"actions": "Actions",
|
||||
"status": "Status",
|
||||
"name": "Name",
|
||||
"email": "Email",
|
||||
"role": "Role",
|
||||
"date": "Date",
|
||||
"description": "Description",
|
||||
"settings": "Settings",
|
||||
"yes": "Yes",
|
||||
"no": "No",
|
||||
"all": "All",
|
||||
"none": "None",
|
||||
"noResults": "No results found",
|
||||
"error": "Error",
|
||||
"success": "Success",
|
||||
"warning": "Warning",
|
||||
"info": "Info",
|
||||
"required": "Required",
|
||||
"optional": "Optional",
|
||||
"total": "Total",
|
||||
"page": "Page",
|
||||
"of": "of",
|
||||
"showing": "Showing",
|
||||
"entries": "entries",
|
||||
"perPage": "per page",
|
||||
"language": "Language",
|
||||
"english": "English",
|
||||
"french": "French"
|
||||
},
|
||||
"auth": {
|
||||
"signIn": "Sign In",
|
||||
"signOut": "Sign Out",
|
||||
"signUp": "Sign Up",
|
||||
"email": "Email Address",
|
||||
"password": "Password",
|
||||
"forgotPassword": "Forgot Password?",
|
||||
"resetPassword": "Reset Password",
|
||||
"newPassword": "New Password",
|
||||
"confirmPassword": "Confirm Password",
|
||||
"currentPassword": "Current Password",
|
||||
"magicLink": "Sign in with Magic Link",
|
||||
"magicLinkSent": "Check your email for a sign-in link",
|
||||
"invalidCredentials": "Invalid email or password",
|
||||
"accountLocked": "Account locked. Try again later.",
|
||||
"setPassword": "Set Password",
|
||||
"passwordRequirements": "Password must be at least 8 characters",
|
||||
"passwordsDoNotMatch": "Passwords do not match",
|
||||
"welcomeBack": "Welcome back",
|
||||
"signInDescription": "Enter your email to sign in to your account",
|
||||
"orContinueWith": "Or continue with"
|
||||
},
|
||||
"nav": {
|
||||
"dashboard": "Dashboard",
|
||||
"programs": "Programs",
|
||||
"rounds": "Rounds",
|
||||
"projects": "Projects",
|
||||
"users": "Users",
|
||||
"evaluations": "Evaluations",
|
||||
"assignments": "Assignments",
|
||||
"analytics": "Analytics",
|
||||
"settings": "Settings",
|
||||
"audit": "Audit Log",
|
||||
"myProjects": "My Projects",
|
||||
"myEvaluations": "My Evaluations",
|
||||
"profile": "Profile",
|
||||
"help": "Help",
|
||||
"notifications": "Notifications",
|
||||
"mentoring": "Mentoring",
|
||||
"liveVoting": "Live Voting",
|
||||
"applications": "Applications",
|
||||
"messages": "Messages"
|
||||
},
|
||||
"dashboard": {
|
||||
"title": "Dashboard",
|
||||
"welcome": "Welcome, {name}",
|
||||
"overview": "Overview",
|
||||
"recentActivity": "Recent Activity",
|
||||
"pendingEvaluations": "Pending Evaluations",
|
||||
"completedEvaluations": "Completed Evaluations",
|
||||
"totalProjects": "Total Projects",
|
||||
"activeRounds": "Active Rounds",
|
||||
"assignedProjects": "Assigned Projects",
|
||||
"upcomingDeadlines": "Upcoming Deadlines",
|
||||
"quickActions": "Quick Actions",
|
||||
"noActivity": "No recent activity"
|
||||
},
|
||||
"programs": {
|
||||
"title": "Programs",
|
||||
"createProgram": "Create Program",
|
||||
"editProgram": "Edit Program",
|
||||
"programName": "Program Name",
|
||||
"year": "Year",
|
||||
"status": "Status",
|
||||
"rounds": "Rounds",
|
||||
"projects": "Projects",
|
||||
"noPrograms": "No programs found",
|
||||
"deleteConfirm": "Are you sure you want to delete this program?"
|
||||
},
|
||||
"rounds": {
|
||||
"title": "Rounds",
|
||||
"createRound": "Create Round",
|
||||
"editRound": "Edit Round",
|
||||
"roundName": "Round Name",
|
||||
"roundType": "Round Type",
|
||||
"startDate": "Start Date",
|
||||
"endDate": "End Date",
|
||||
"votingWindow": "Voting Window",
|
||||
"criteria": "Evaluation Criteria",
|
||||
"status": "Status",
|
||||
"active": "Active",
|
||||
"closed": "Closed",
|
||||
"upcoming": "Upcoming",
|
||||
"noRounds": "No rounds found"
|
||||
},
|
||||
"projects": {
|
||||
"title": "Projects",
|
||||
"createProject": "Create Project",
|
||||
"editProject": "Edit Project",
|
||||
"projectName": "Project Title",
|
||||
"teamName": "Team Name",
|
||||
"country": "Country",
|
||||
"category": "Category",
|
||||
"status": "Status",
|
||||
"description": "Description",
|
||||
"files": "Files",
|
||||
"evaluations": "Evaluations",
|
||||
"noProjects": "No projects found",
|
||||
"importCsv": "Import CSV",
|
||||
"bulkStatusUpdate": "Bulk Status Update",
|
||||
"viewDetails": "View Details",
|
||||
"assignMentor": "Assign Mentor",
|
||||
"oceanIssue": "Ocean Issue"
|
||||
},
|
||||
"evaluations": {
|
||||
"title": "Evaluations",
|
||||
"submitEvaluation": "Submit Evaluation",
|
||||
"draft": "Draft",
|
||||
"submitted": "Submitted",
|
||||
"score": "Score",
|
||||
"feedback": "Feedback",
|
||||
"criteria": "Criteria",
|
||||
"globalScore": "Global Score",
|
||||
"decision": "Decision",
|
||||
"recommend": "Recommend",
|
||||
"doNotRecommend": "Do Not Recommend",
|
||||
"saveAsDraft": "Save as Draft",
|
||||
"finalSubmit": "Final Submit",
|
||||
"confirmSubmit": "Are you sure? This action cannot be undone.",
|
||||
"progress": "Progress",
|
||||
"completionRate": "Completion Rate",
|
||||
"noEvaluations": "No evaluations yet",
|
||||
"evaluationSummary": "Evaluation Summary",
|
||||
"strengths": "Strengths",
|
||||
"weaknesses": "Weaknesses",
|
||||
"overallAssessment": "Overall Assessment"
|
||||
},
|
||||
"users": {
|
||||
"title": "Users",
|
||||
"createUser": "Create User",
|
||||
"editUser": "Edit User",
|
||||
"inviteUser": "Invite User",
|
||||
"bulkImport": "Bulk Import",
|
||||
"sendInvitation": "Send Invitation",
|
||||
"resendInvitation": "Resend Invitation",
|
||||
"role": "Role",
|
||||
"status": "Status",
|
||||
"active": "Active",
|
||||
"invited": "Invited",
|
||||
"suspended": "Suspended",
|
||||
"noUsers": "No users found",
|
||||
"expertiseTags": "Expertise Tags",
|
||||
"maxAssignments": "Max Assignments",
|
||||
"lastLogin": "Last Login",
|
||||
"deleteConfirm": "Are you sure you want to delete this user?"
|
||||
},
|
||||
"assignments": {
|
||||
"title": "Assignments",
|
||||
"assign": "Assign",
|
||||
"unassign": "Unassign",
|
||||
"bulkAssign": "Bulk Assign",
|
||||
"smartAssign": "Smart Assignment",
|
||||
"manual": "Manual",
|
||||
"algorithm": "Algorithm",
|
||||
"aiAuto": "AI Auto",
|
||||
"noAssignments": "No assignments",
|
||||
"assignedTo": "Assigned to",
|
||||
"assignedBy": "Assigned by"
|
||||
},
|
||||
"files": {
|
||||
"title": "Files",
|
||||
"upload": "Upload File",
|
||||
"download": "Download",
|
||||
"delete": "Delete File",
|
||||
"fileName": "File Name",
|
||||
"fileType": "File Type",
|
||||
"fileSize": "File Size",
|
||||
"uploadDate": "Upload Date",
|
||||
"noFiles": "No files uploaded",
|
||||
"dragAndDrop": "Drag and drop files here",
|
||||
"maxSize": "Maximum file size: {size}",
|
||||
"version": "Version",
|
||||
"versionHistory": "Version History",
|
||||
"replaceFile": "Replace File",
|
||||
"bulkDownload": "Bulk Download"
|
||||
},
|
||||
"settings": {
|
||||
"title": "Settings",
|
||||
"general": "General",
|
||||
"branding": "Branding",
|
||||
"email": "Email",
|
||||
"security": "Security",
|
||||
"ai": "AI Configuration",
|
||||
"storage": "Storage",
|
||||
"language": "Language",
|
||||
"defaultLanguage": "Default Language",
|
||||
"availableLanguages": "Available Languages",
|
||||
"languageDescription": "Configure the platform's language settings",
|
||||
"saved": "Settings saved successfully",
|
||||
"saveFailed": "Failed to save settings"
|
||||
},
|
||||
"liveVoting": {
|
||||
"title": "Live Voting",
|
||||
"session": "Session",
|
||||
"startVoting": "Start Voting",
|
||||
"stopVoting": "Stop Voting",
|
||||
"endSession": "End Session",
|
||||
"timeRemaining": "Time Remaining",
|
||||
"castVote": "Cast Your Vote",
|
||||
"voteSubmitted": "Vote submitted",
|
||||
"results": "Results",
|
||||
"juryScore": "Jury Score",
|
||||
"audienceScore": "Audience Score",
|
||||
"weightedTotal": "Weighted Total",
|
||||
"noVotes": "No votes yet",
|
||||
"votingClosed": "Voting has closed",
|
||||
"presentationSettings": "Presentation Settings",
|
||||
"audienceVoting": "Audience Voting"
|
||||
},
|
||||
"mentor": {
|
||||
"title": "Mentoring",
|
||||
"myMentees": "My Mentees",
|
||||
"projectDetails": "Project Details",
|
||||
"sendMessage": "Send Message",
|
||||
"notes": "Notes",
|
||||
"addNote": "Add Note",
|
||||
"milestones": "Milestones",
|
||||
"completeMilestone": "Mark Complete",
|
||||
"activity": "Activity",
|
||||
"lastViewed": "Last Viewed",
|
||||
"noMentees": "No mentees assigned"
|
||||
},
|
||||
"profile": {
|
||||
"title": "Profile",
|
||||
"editProfile": "Edit Profile",
|
||||
"name": "Full Name",
|
||||
"email": "Email",
|
||||
"phone": "Phone Number",
|
||||
"country": "Country",
|
||||
"bio": "Bio",
|
||||
"expertise": "Areas of Expertise",
|
||||
"notifications": "Notification Preferences",
|
||||
"digestFrequency": "Digest Frequency",
|
||||
"availability": "Availability",
|
||||
"workload": "Preferred Workload",
|
||||
"changePassword": "Change Password",
|
||||
"deleteAccount": "Delete Account",
|
||||
"deleteAccountConfirm": "This action cannot be undone. All your data will be permanently deleted."
|
||||
},
|
||||
"onboarding": {
|
||||
"welcome": "Welcome to MOPC",
|
||||
"setupProfile": "Let's set up your profile",
|
||||
"step1": "Personal Information",
|
||||
"step2": "Expertise & Preferences",
|
||||
"step3": "Review & Complete",
|
||||
"complete": "Complete Setup",
|
||||
"skip": "Skip for now"
|
||||
},
|
||||
"errors": {
|
||||
"generic": "Something went wrong. Please try again.",
|
||||
"notFound": "Page not found",
|
||||
"unauthorized": "You are not authorized to access this page",
|
||||
"forbidden": "Access denied",
|
||||
"serverError": "Internal server error",
|
||||
"networkError": "Network error. Please check your connection.",
|
||||
"sessionExpired": "Your session has expired. Please sign in again.",
|
||||
"validationError": "Please check the form for errors"
|
||||
},
|
||||
"notifications": {
|
||||
"title": "Notifications",
|
||||
"markAllRead": "Mark all as read",
|
||||
"noNotifications": "No notifications",
|
||||
"viewAll": "View all notifications"
|
||||
},
|
||||
"coi": {
|
||||
"title": "Conflict of Interest",
|
||||
"declaration": "COI Declaration",
|
||||
"declareConflict": "Declare Conflict of Interest",
|
||||
"noConflict": "No conflict of interest",
|
||||
"hasConflict": "Conflict declared",
|
||||
"reason": "Reason for conflict",
|
||||
"confirmDeclaration": "I confirm this declaration is accurate"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,321 @@
|
|||
{
|
||||
"common": {
|
||||
"loading": "Chargement...",
|
||||
"save": "Enregistrer",
|
||||
"cancel": "Annuler",
|
||||
"delete": "Supprimer",
|
||||
"edit": "Modifier",
|
||||
"create": "Cr\u00e9er",
|
||||
"close": "Fermer",
|
||||
"confirm": "Confirmer",
|
||||
"back": "Retour",
|
||||
"next": "Suivant",
|
||||
"submit": "Soumettre",
|
||||
"search": "Rechercher",
|
||||
"filter": "Filtrer",
|
||||
"export": "Exporter",
|
||||
"import": "Importer",
|
||||
"refresh": "Actualiser",
|
||||
"actions": "Actions",
|
||||
"status": "Statut",
|
||||
"name": "Nom",
|
||||
"email": "E-mail",
|
||||
"role": "R\u00f4le",
|
||||
"date": "Date",
|
||||
"description": "Description",
|
||||
"settings": "Param\u00e8tres",
|
||||
"yes": "Oui",
|
||||
"no": "Non",
|
||||
"all": "Tous",
|
||||
"none": "Aucun",
|
||||
"noResults": "Aucun r\u00e9sultat trouv\u00e9",
|
||||
"error": "Erreur",
|
||||
"success": "Succ\u00e8s",
|
||||
"warning": "Avertissement",
|
||||
"info": "Information",
|
||||
"required": "Obligatoire",
|
||||
"optional": "Facultatif",
|
||||
"total": "Total",
|
||||
"page": "Page",
|
||||
"of": "de",
|
||||
"showing": "Affichage de",
|
||||
"entries": "entr\u00e9es",
|
||||
"perPage": "par page",
|
||||
"language": "Langue",
|
||||
"english": "Anglais",
|
||||
"french": "Fran\u00e7ais"
|
||||
},
|
||||
"auth": {
|
||||
"signIn": "Se connecter",
|
||||
"signOut": "Se d\u00e9connecter",
|
||||
"signUp": "S'inscrire",
|
||||
"email": "Adresse e-mail",
|
||||
"password": "Mot de passe",
|
||||
"forgotPassword": "Mot de passe oubli\u00e9 ?",
|
||||
"resetPassword": "R\u00e9initialiser le mot de passe",
|
||||
"newPassword": "Nouveau mot de passe",
|
||||
"confirmPassword": "Confirmer le mot de passe",
|
||||
"currentPassword": "Mot de passe actuel",
|
||||
"magicLink": "Se connecter avec un lien magique",
|
||||
"magicLinkSent": "V\u00e9rifiez votre e-mail pour un lien de connexion",
|
||||
"invalidCredentials": "E-mail ou mot de passe invalide",
|
||||
"accountLocked": "Compte verrouill\u00e9. R\u00e9essayez plus tard.",
|
||||
"setPassword": "D\u00e9finir le mot de passe",
|
||||
"passwordRequirements": "Le mot de passe doit contenir au moins 8 caract\u00e8res",
|
||||
"passwordsDoNotMatch": "Les mots de passe ne correspondent pas",
|
||||
"welcomeBack": "Bon retour",
|
||||
"signInDescription": "Entrez votre e-mail pour vous connecter \u00e0 votre compte",
|
||||
"orContinueWith": "Ou continuer avec"
|
||||
},
|
||||
"nav": {
|
||||
"dashboard": "Tableau de bord",
|
||||
"programs": "Programmes",
|
||||
"rounds": "Tours",
|
||||
"projects": "Projets",
|
||||
"users": "Utilisateurs",
|
||||
"evaluations": "\u00c9valuations",
|
||||
"assignments": "Affectations",
|
||||
"analytics": "Analytique",
|
||||
"settings": "Param\u00e8tres",
|
||||
"audit": "Journal d'audit",
|
||||
"myProjects": "Mes projets",
|
||||
"myEvaluations": "Mes \u00e9valuations",
|
||||
"profile": "Profil",
|
||||
"help": "Aide",
|
||||
"notifications": "Notifications",
|
||||
"mentoring": "Mentorat",
|
||||
"liveVoting": "Vote en direct",
|
||||
"applications": "Candidatures",
|
||||
"messages": "Messages"
|
||||
},
|
||||
"dashboard": {
|
||||
"title": "Tableau de bord",
|
||||
"welcome": "Bienvenue, {name}",
|
||||
"overview": "Aper\u00e7u",
|
||||
"recentActivity": "Activit\u00e9 r\u00e9cente",
|
||||
"pendingEvaluations": "\u00c9valuations en attente",
|
||||
"completedEvaluations": "\u00c9valuations termin\u00e9es",
|
||||
"totalProjects": "Total des projets",
|
||||
"activeRounds": "Tours actifs",
|
||||
"assignedProjects": "Projets assign\u00e9s",
|
||||
"upcomingDeadlines": "\u00c9ch\u00e9ances \u00e0 venir",
|
||||
"quickActions": "Actions rapides",
|
||||
"noActivity": "Aucune activit\u00e9 r\u00e9cente"
|
||||
},
|
||||
"programs": {
|
||||
"title": "Programmes",
|
||||
"createProgram": "Cr\u00e9er un programme",
|
||||
"editProgram": "Modifier le programme",
|
||||
"programName": "Nom du programme",
|
||||
"year": "Ann\u00e9e",
|
||||
"status": "Statut",
|
||||
"rounds": "Tours",
|
||||
"projects": "Projets",
|
||||
"noPrograms": "Aucun programme trouv\u00e9",
|
||||
"deleteConfirm": "\u00cates-vous s\u00fbr de vouloir supprimer ce programme ?"
|
||||
},
|
||||
"rounds": {
|
||||
"title": "Tours",
|
||||
"createRound": "Cr\u00e9er un tour",
|
||||
"editRound": "Modifier le tour",
|
||||
"roundName": "Nom du tour",
|
||||
"roundType": "Type de tour",
|
||||
"startDate": "Date de d\u00e9but",
|
||||
"endDate": "Date de fin",
|
||||
"votingWindow": "Fen\u00eatre de vote",
|
||||
"criteria": "Crit\u00e8res d'\u00e9valuation",
|
||||
"status": "Statut",
|
||||
"active": "Actif",
|
||||
"closed": "Cl\u00f4tur\u00e9",
|
||||
"upcoming": "\u00c0 venir",
|
||||
"noRounds": "Aucun tour trouv\u00e9"
|
||||
},
|
||||
"projects": {
|
||||
"title": "Projets",
|
||||
"createProject": "Cr\u00e9er un projet",
|
||||
"editProject": "Modifier le projet",
|
||||
"projectName": "Titre du projet",
|
||||
"teamName": "Nom de l'\u00e9quipe",
|
||||
"country": "Pays",
|
||||
"category": "Cat\u00e9gorie",
|
||||
"status": "Statut",
|
||||
"description": "Description",
|
||||
"files": "Fichiers",
|
||||
"evaluations": "\u00c9valuations",
|
||||
"noProjects": "Aucun projet trouv\u00e9",
|
||||
"importCsv": "Importer CSV",
|
||||
"bulkStatusUpdate": "Mise \u00e0 jour en masse du statut",
|
||||
"viewDetails": "Voir les d\u00e9tails",
|
||||
"assignMentor": "Assigner un mentor",
|
||||
"oceanIssue": "Probl\u00e9matique oc\u00e9anique"
|
||||
},
|
||||
"evaluations": {
|
||||
"title": "\u00c9valuations",
|
||||
"submitEvaluation": "Soumettre l'\u00e9valuation",
|
||||
"draft": "Brouillon",
|
||||
"submitted": "Soumis",
|
||||
"score": "Note",
|
||||
"feedback": "Commentaires",
|
||||
"criteria": "Crit\u00e8res",
|
||||
"globalScore": "Note globale",
|
||||
"decision": "D\u00e9cision",
|
||||
"recommend": "Recommander",
|
||||
"doNotRecommend": "Ne pas recommander",
|
||||
"saveAsDraft": "Enregistrer comme brouillon",
|
||||
"finalSubmit": "Soumission finale",
|
||||
"confirmSubmit": "\u00cates-vous s\u00fbr ? Cette action est irr\u00e9versible.",
|
||||
"progress": "Progression",
|
||||
"completionRate": "Taux de compl\u00e9tion",
|
||||
"noEvaluations": "Aucune \u00e9valuation pour le moment",
|
||||
"evaluationSummary": "R\u00e9sum\u00e9 de l'\u00e9valuation",
|
||||
"strengths": "Points forts",
|
||||
"weaknesses": "Points faibles",
|
||||
"overallAssessment": "\u00c9valuation globale"
|
||||
},
|
||||
"users": {
|
||||
"title": "Utilisateurs",
|
||||
"createUser": "Cr\u00e9er un utilisateur",
|
||||
"editUser": "Modifier l'utilisateur",
|
||||
"inviteUser": "Inviter un utilisateur",
|
||||
"bulkImport": "Importation en masse",
|
||||
"sendInvitation": "Envoyer l'invitation",
|
||||
"resendInvitation": "Renvoyer l'invitation",
|
||||
"role": "R\u00f4le",
|
||||
"status": "Statut",
|
||||
"active": "Actif",
|
||||
"invited": "Invit\u00e9",
|
||||
"suspended": "Suspendu",
|
||||
"noUsers": "Aucun utilisateur trouv\u00e9",
|
||||
"expertiseTags": "Tags d'expertise",
|
||||
"maxAssignments": "Affectations max.",
|
||||
"lastLogin": "Derni\u00e8re connexion",
|
||||
"deleteConfirm": "\u00cates-vous s\u00fbr de vouloir supprimer cet utilisateur ?"
|
||||
},
|
||||
"assignments": {
|
||||
"title": "Affectations",
|
||||
"assign": "Affecter",
|
||||
"unassign": "D\u00e9saffecter",
|
||||
"bulkAssign": "Affectation en masse",
|
||||
"smartAssign": "Affectation intelligente",
|
||||
"manual": "Manuel",
|
||||
"algorithm": "Algorithme",
|
||||
"aiAuto": "IA automatique",
|
||||
"noAssignments": "Aucune affectation",
|
||||
"assignedTo": "Affect\u00e9 \u00e0",
|
||||
"assignedBy": "Affect\u00e9 par"
|
||||
},
|
||||
"files": {
|
||||
"title": "Fichiers",
|
||||
"upload": "T\u00e9l\u00e9charger un fichier",
|
||||
"download": "T\u00e9l\u00e9charger",
|
||||
"delete": "Supprimer le fichier",
|
||||
"fileName": "Nom du fichier",
|
||||
"fileType": "Type de fichier",
|
||||
"fileSize": "Taille du fichier",
|
||||
"uploadDate": "Date de t\u00e9l\u00e9chargement",
|
||||
"noFiles": "Aucun fichier t\u00e9l\u00e9charg\u00e9",
|
||||
"dragAndDrop": "Glissez-d\u00e9posez les fichiers ici",
|
||||
"maxSize": "Taille maximale du fichier : {size}",
|
||||
"version": "Version",
|
||||
"versionHistory": "Historique des versions",
|
||||
"replaceFile": "Remplacer le fichier",
|
||||
"bulkDownload": "T\u00e9l\u00e9chargement en masse"
|
||||
},
|
||||
"settings": {
|
||||
"title": "Param\u00e8tres",
|
||||
"general": "G\u00e9n\u00e9ral",
|
||||
"branding": "Image de marque",
|
||||
"email": "E-mail",
|
||||
"security": "S\u00e9curit\u00e9",
|
||||
"ai": "Configuration IA",
|
||||
"storage": "Stockage",
|
||||
"language": "Langue",
|
||||
"defaultLanguage": "Langue par d\u00e9faut",
|
||||
"availableLanguages": "Langues disponibles",
|
||||
"languageDescription": "Configurer les param\u00e8tres de langue de la plateforme",
|
||||
"saved": "Param\u00e8tres enregistr\u00e9s avec succ\u00e8s",
|
||||
"saveFailed": "\u00c9chec de l'enregistrement des param\u00e8tres"
|
||||
},
|
||||
"liveVoting": {
|
||||
"title": "Vote en direct",
|
||||
"session": "Session",
|
||||
"startVoting": "D\u00e9marrer le vote",
|
||||
"stopVoting": "Arr\u00eater le vote",
|
||||
"endSession": "Terminer la session",
|
||||
"timeRemaining": "Temps restant",
|
||||
"castVote": "Voter",
|
||||
"voteSubmitted": "Vote soumis",
|
||||
"results": "R\u00e9sultats",
|
||||
"juryScore": "Note du jury",
|
||||
"audienceScore": "Note du public",
|
||||
"weightedTotal": "Total pond\u00e9r\u00e9",
|
||||
"noVotes": "Aucun vote pour le moment",
|
||||
"votingClosed": "Le vote est clos",
|
||||
"presentationSettings": "Param\u00e8tres de pr\u00e9sentation",
|
||||
"audienceVoting": "Vote du public"
|
||||
},
|
||||
"mentor": {
|
||||
"title": "Mentorat",
|
||||
"myMentees": "Mes mentees",
|
||||
"projectDetails": "D\u00e9tails du projet",
|
||||
"sendMessage": "Envoyer un message",
|
||||
"notes": "Notes",
|
||||
"addNote": "Ajouter une note",
|
||||
"milestones": "Jalons",
|
||||
"completeMilestone": "Marquer comme termin\u00e9",
|
||||
"activity": "Activit\u00e9",
|
||||
"lastViewed": "Derni\u00e8re consultation",
|
||||
"noMentees": "Aucun mentee assign\u00e9"
|
||||
},
|
||||
"profile": {
|
||||
"title": "Profil",
|
||||
"editProfile": "Modifier le profil",
|
||||
"name": "Nom complet",
|
||||
"email": "E-mail",
|
||||
"phone": "Num\u00e9ro de t\u00e9l\u00e9phone",
|
||||
"country": "Pays",
|
||||
"bio": "Biographie",
|
||||
"expertise": "Domaines d'expertise",
|
||||
"notifications": "Pr\u00e9f\u00e9rences de notification",
|
||||
"digestFrequency": "Fr\u00e9quence du digest",
|
||||
"availability": "Disponibilit\u00e9",
|
||||
"workload": "Charge de travail pr\u00e9f\u00e9r\u00e9e",
|
||||
"changePassword": "Changer le mot de passe",
|
||||
"deleteAccount": "Supprimer le compte",
|
||||
"deleteAccountConfirm": "Cette action est irr\u00e9versible. Toutes vos donn\u00e9es seront d\u00e9finitivement supprim\u00e9es."
|
||||
},
|
||||
"onboarding": {
|
||||
"welcome": "Bienvenue sur MOPC",
|
||||
"setupProfile": "Configurons votre profil",
|
||||
"step1": "Informations personnelles",
|
||||
"step2": "Expertise et pr\u00e9f\u00e9rences",
|
||||
"step3": "V\u00e9rification et finalisation",
|
||||
"complete": "Terminer la configuration",
|
||||
"skip": "Passer pour le moment"
|
||||
},
|
||||
"errors": {
|
||||
"generic": "Une erreur s'est produite. Veuillez r\u00e9essayer.",
|
||||
"notFound": "Page non trouv\u00e9e",
|
||||
"unauthorized": "Vous n'\u00eates pas autoris\u00e9 \u00e0 acc\u00e9der \u00e0 cette page",
|
||||
"forbidden": "Acc\u00e8s refus\u00e9",
|
||||
"serverError": "Erreur interne du serveur",
|
||||
"networkError": "Erreur r\u00e9seau. V\u00e9rifiez votre connexion.",
|
||||
"sessionExpired": "Votre session a expir\u00e9. Veuillez vous reconnecter.",
|
||||
"validationError": "Veuillez v\u00e9rifier le formulaire pour les erreurs"
|
||||
},
|
||||
"notifications": {
|
||||
"title": "Notifications",
|
||||
"markAllRead": "Tout marquer comme lu",
|
||||
"noNotifications": "Aucune notification",
|
||||
"viewAll": "Voir toutes les notifications"
|
||||
},
|
||||
"coi": {
|
||||
"title": "Conflit d'int\u00e9r\u00eats",
|
||||
"declaration": "D\u00e9claration de COI",
|
||||
"declareConflict": "D\u00e9clarer un conflit d'int\u00e9r\u00eats",
|
||||
"noConflict": "Aucun conflit d'int\u00e9r\u00eats",
|
||||
"hasConflict": "Conflit d\u00e9clar\u00e9",
|
||||
"reason": "Raison du conflit",
|
||||
"confirmDeclaration": "Je confirme que cette d\u00e9claration est exacte"
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
import type { NextConfig } from 'next'
|
||||
import createNextIntlPlugin from 'next-intl/plugin'
|
||||
|
||||
const nextConfig: NextConfig = {
|
||||
output: 'standalone',
|
||||
|
|
@ -14,4 +15,6 @@ const nextConfig: NextConfig = {
|
|||
},
|
||||
}
|
||||
|
||||
export default nextConfig
|
||||
const withNextIntl = createNextIntlPlugin('./src/i18n/request.ts')
|
||||
|
||||
export default withNextIntl(nextConfig)
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -67,6 +67,7 @@
|
|||
"motion": "^11.15.0",
|
||||
"next": "^15.1.0",
|
||||
"next-auth": "^5.0.0-beta.25",
|
||||
"next-intl": "^4.8.2",
|
||||
"nodemailer": "^7.0.7",
|
||||
"openai": "^6.16.0",
|
||||
"papaparse": "^5.4.1",
|
||||
|
|
|
|||
|
|
@ -110,6 +110,12 @@ enum SettingCategory {
|
|||
SECURITY
|
||||
DEFAULTS
|
||||
WHATSAPP
|
||||
DIGEST
|
||||
ANALYTICS
|
||||
AUDIT_CONFIG
|
||||
INTEGRATIONS
|
||||
LOCALIZATION
|
||||
COMMUNICATION
|
||||
}
|
||||
|
||||
enum NotificationChannel {
|
||||
|
|
@ -210,6 +216,13 @@ model User {
|
|||
notificationPreference NotificationChannel @default(EMAIL)
|
||||
whatsappOptIn Boolean @default(false)
|
||||
|
||||
// Digest preferences (F1)
|
||||
digestFrequency String @default("none") // none, daily, weekly
|
||||
|
||||
// Availability & workload (F2)
|
||||
availabilityJson Json? @db.JsonB // Array of { start, end } date ranges
|
||||
preferredWorkload Int? // Preferred number of assignments
|
||||
|
||||
// Onboarding (Phase 2B)
|
||||
onboardingCompletedAt DateTime?
|
||||
|
||||
|
|
@ -269,6 +282,24 @@ model User {
|
|||
// Mentor messages
|
||||
mentorMessages MentorMessage[] @relation("MentorMessageSender")
|
||||
|
||||
// Digest logs (F1)
|
||||
digestLogs DigestLog[]
|
||||
|
||||
// Mentor notes & milestones (F8)
|
||||
mentorNotesMade MentorNote[] @relation("MentorNoteAuthor")
|
||||
milestoneCompletions MentorMilestoneCompletion[] @relation("MilestoneCompletedByUser")
|
||||
|
||||
// Messages (F9)
|
||||
sentMessages Message[] @relation("MessageSender")
|
||||
messageRecipients MessageRecipient[]
|
||||
|
||||
// Webhooks (F12)
|
||||
createdWebhooks Webhook[] @relation("WebhookCreatedBy")
|
||||
|
||||
// Discussion comments (F13)
|
||||
discussionComments DiscussionComment[]
|
||||
closedDiscussions EvaluationDiscussion[] @relation("DiscussionClosedBy")
|
||||
|
||||
// NextAuth relations
|
||||
accounts Account[]
|
||||
sessions Session[]
|
||||
|
|
@ -338,6 +369,7 @@ model Program {
|
|||
partners Partner[]
|
||||
specialAwards SpecialAward[]
|
||||
taggingJobs TaggingJob[]
|
||||
mentorMilestones MentorMilestone[]
|
||||
|
||||
@@unique([name, year])
|
||||
@@index([status])
|
||||
|
|
@ -475,6 +507,11 @@ model Project {
|
|||
logoKey String? // Storage key (e.g., "logos/project456/1234567890.png")
|
||||
logoProvider String? // Storage provider used: 's3' or 'local'
|
||||
|
||||
// Draft saving (F11)
|
||||
isDraft Boolean @default(false)
|
||||
draftDataJson Json? @db.JsonB
|
||||
draftExpiresAt DateTime?
|
||||
|
||||
// Flexible fields
|
||||
tags String[] @default([]) // "Ocean Conservation", "Tech", etc.
|
||||
metadataJson Json? @db.JsonB // Custom fields from Typeform, etc.
|
||||
|
|
@ -498,6 +535,7 @@ model Project {
|
|||
statusHistory ProjectStatusHistory[]
|
||||
mentorMessages MentorMessage[]
|
||||
evaluationSummaries EvaluationSummary[]
|
||||
discussions EvaluationDiscussion[]
|
||||
|
||||
@@index([roundId])
|
||||
@@index([status])
|
||||
|
|
@ -526,6 +564,10 @@ model ProjectFile {
|
|||
|
||||
isLate Boolean @default(false) // Uploaded after round deadline
|
||||
|
||||
// File versioning (F7)
|
||||
version Int @default(1)
|
||||
replacedById String? // Points to newer version of this file
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
// Relations
|
||||
|
|
@ -675,6 +717,10 @@ model AuditLog {
|
|||
// Details
|
||||
detailsJson Json? @db.JsonB // Before/after values, additional context
|
||||
|
||||
// Audit enhancements (F14)
|
||||
sessionId String? // Groups actions in same user session
|
||||
previousDataJson Json? @db.JsonB // Snapshot of data before change
|
||||
|
||||
// Request info
|
||||
ipAddress String?
|
||||
userAgent String?
|
||||
|
|
@ -951,6 +997,12 @@ model LiveVotingSession {
|
|||
votingEndsAt DateTime?
|
||||
projectOrderJson Json? @db.JsonB // Array of project IDs in presentation order
|
||||
|
||||
// Live voting UX enhancements (F5/F6)
|
||||
presentationSettingsJson Json? @db.JsonB // theme, auto-advance, branding
|
||||
allowAudienceVotes Boolean @default(false)
|
||||
audienceVoteWeight Float @default(0) // 0-1 weight relative to jury
|
||||
tieBreakerMethod String @default("admin_decides") // admin_decides, highest_individual, revote
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
|
|
@ -962,12 +1014,13 @@ model LiveVotingSession {
|
|||
}
|
||||
|
||||
model LiveVote {
|
||||
id String @id @default(cuid())
|
||||
sessionId String
|
||||
projectId String
|
||||
userId String
|
||||
score Int // 1-10
|
||||
votedAt DateTime @default(now())
|
||||
id String @id @default(cuid())
|
||||
sessionId String
|
||||
projectId String
|
||||
userId String
|
||||
score Int // 1-10
|
||||
isAudienceVote Boolean @default(false) // F6: audience voting
|
||||
votedAt DateTime @default(now())
|
||||
|
||||
// Relations
|
||||
session LiveVotingSession @relation(fields: [sessionId], references: [id], onDelete: Cascade)
|
||||
|
|
@ -977,6 +1030,7 @@ model LiveVote {
|
|||
@@index([sessionId])
|
||||
@@index([projectId])
|
||||
@@index([userId])
|
||||
@@index([isAudienceVote])
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
|
@ -1020,9 +1074,15 @@ model MentorAssignment {
|
|||
expertiseMatchScore Float?
|
||||
aiReasoning String? @db.Text
|
||||
|
||||
// Mentor dashboard enhancements (F8)
|
||||
lastViewedAt DateTime?
|
||||
completionStatus String @default("in_progress") // in_progress, completed, paused
|
||||
|
||||
// Relations
|
||||
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
|
||||
mentor User @relation("MentorAssignments", fields: [mentorId], references: [id])
|
||||
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
|
||||
mentor User @relation("MentorAssignments", fields: [mentorId], references: [id])
|
||||
notes MentorNote[]
|
||||
milestoneCompletions MentorMilestoneCompletion[]
|
||||
|
||||
@@index([mentorId])
|
||||
@@index([method])
|
||||
|
|
@ -1427,3 +1487,242 @@ model MentorMessage {
|
|||
|
||||
@@index([projectId, createdAt])
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// DIGEST LOGS (F1: Email Digest)
|
||||
// =============================================================================
|
||||
|
||||
model DigestLog {
|
||||
id String @id @default(cuid())
|
||||
userId String
|
||||
digestType String // "daily", "weekly"
|
||||
contentJson Json @db.JsonB
|
||||
sentAt DateTime @default(now())
|
||||
|
||||
// Relations
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@index([userId])
|
||||
@@index([sentAt])
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// ROUND TEMPLATES (F3)
|
||||
// =============================================================================
|
||||
|
||||
model RoundTemplate {
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
description String? @db.Text
|
||||
programId String? // null = global template
|
||||
roundType RoundType @default(EVALUATION)
|
||||
criteriaJson Json @db.JsonB
|
||||
settingsJson Json? @db.JsonB
|
||||
assignmentConfig Json? @db.JsonB
|
||||
createdBy String
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index([programId])
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// MENTOR NOTES & MILESTONES (F8)
|
||||
// =============================================================================
|
||||
|
||||
model MentorNote {
|
||||
id String @id @default(cuid())
|
||||
mentorAssignmentId String
|
||||
authorId String
|
||||
content String @db.Text
|
||||
isVisibleToAdmin Boolean @default(true)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
// Relations
|
||||
mentorAssignment MentorAssignment @relation(fields: [mentorAssignmentId], references: [id], onDelete: Cascade)
|
||||
author User @relation("MentorNoteAuthor", fields: [authorId], references: [id])
|
||||
|
||||
@@index([mentorAssignmentId])
|
||||
}
|
||||
|
||||
model MentorMilestone {
|
||||
id String @id @default(cuid())
|
||||
programId String
|
||||
name String
|
||||
description String? @db.Text
|
||||
isRequired Boolean @default(false)
|
||||
deadlineOffsetDays Int?
|
||||
sortOrder Int @default(0)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
// Relations
|
||||
program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
|
||||
completions MentorMilestoneCompletion[]
|
||||
|
||||
@@index([programId])
|
||||
@@index([sortOrder])
|
||||
}
|
||||
|
||||
model MentorMilestoneCompletion {
|
||||
id String @id @default(cuid())
|
||||
milestoneId String
|
||||
mentorAssignmentId String
|
||||
completedAt DateTime @default(now())
|
||||
completedById String
|
||||
|
||||
// Relations
|
||||
milestone MentorMilestone @relation(fields: [milestoneId], references: [id], onDelete: Cascade)
|
||||
mentorAssignment MentorAssignment @relation(fields: [mentorAssignmentId], references: [id], onDelete: Cascade)
|
||||
completedBy User @relation("MilestoneCompletedByUser", fields: [completedById], references: [id])
|
||||
|
||||
@@unique([milestoneId, mentorAssignmentId])
|
||||
@@index([mentorAssignmentId])
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// COMMUNICATION HUB (F9)
|
||||
// =============================================================================
|
||||
|
||||
model Message {
|
||||
id String @id @default(cuid())
|
||||
senderId String
|
||||
recipientType String // USER, ROLE, ROUND_JURY, PROGRAM_TEAM, ALL
|
||||
recipientFilter Json? @db.JsonB
|
||||
roundId String?
|
||||
templateId String?
|
||||
subject String
|
||||
body String @db.Text
|
||||
deliveryChannels String[]
|
||||
scheduledAt DateTime?
|
||||
sentAt DateTime?
|
||||
metadata Json? @db.JsonB
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
// Relations
|
||||
sender User @relation("MessageSender", fields: [senderId], references: [id])
|
||||
template MessageTemplate? @relation(fields: [templateId], references: [id])
|
||||
recipients MessageRecipient[]
|
||||
|
||||
@@index([senderId])
|
||||
@@index([sentAt])
|
||||
@@index([scheduledAt])
|
||||
}
|
||||
|
||||
model MessageTemplate {
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
category String
|
||||
subject String
|
||||
body String @db.Text
|
||||
variables Json? @db.JsonB
|
||||
isActive Boolean @default(true)
|
||||
createdBy String
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
// Relations
|
||||
messages Message[]
|
||||
|
||||
@@index([category])
|
||||
@@index([isActive])
|
||||
}
|
||||
|
||||
model MessageRecipient {
|
||||
id String @id @default(cuid())
|
||||
messageId String
|
||||
userId String
|
||||
channel String // EMAIL, IN_APP, WHATSAPP
|
||||
isRead Boolean @default(false)
|
||||
readAt DateTime?
|
||||
deliveredAt DateTime?
|
||||
|
||||
// Relations
|
||||
message Message @relation(fields: [messageId], references: [id], onDelete: Cascade)
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@index([messageId])
|
||||
@@index([userId, isRead])
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// WEBHOOKS (F12)
|
||||
// =============================================================================
|
||||
|
||||
model Webhook {
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
url String
|
||||
secret String // HMAC signing key
|
||||
events String[]
|
||||
headers Json? @db.JsonB
|
||||
isActive Boolean @default(true)
|
||||
maxRetries Int @default(3)
|
||||
createdById String
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
// Relations
|
||||
createdBy User @relation("WebhookCreatedBy", fields: [createdById], references: [id])
|
||||
deliveries WebhookDelivery[]
|
||||
|
||||
@@index([isActive])
|
||||
}
|
||||
|
||||
model WebhookDelivery {
|
||||
id String @id @default(cuid())
|
||||
webhookId String
|
||||
event String
|
||||
payload Json @db.JsonB
|
||||
responseStatus Int?
|
||||
responseBody String? @db.Text
|
||||
attempts Int @default(0)
|
||||
lastAttemptAt DateTime?
|
||||
status String @default("PENDING") // PENDING, DELIVERED, FAILED
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
// Relations
|
||||
webhook Webhook @relation(fields: [webhookId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@index([webhookId])
|
||||
@@index([status])
|
||||
@@index([createdAt])
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// PEER REVIEW / EVALUATION DISCUSSIONS (F13)
|
||||
// =============================================================================
|
||||
|
||||
model EvaluationDiscussion {
|
||||
id String @id @default(cuid())
|
||||
projectId String
|
||||
roundId String
|
||||
status String @default("open") // open, closed
|
||||
createdAt DateTime @default(now())
|
||||
closedAt DateTime?
|
||||
closedById String?
|
||||
|
||||
// Relations
|
||||
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
|
||||
closedBy User? @relation("DiscussionClosedBy", fields: [closedById], references: [id])
|
||||
comments DiscussionComment[]
|
||||
|
||||
@@unique([projectId, roundId])
|
||||
@@index([roundId])
|
||||
@@index([status])
|
||||
}
|
||||
|
||||
model DiscussionComment {
|
||||
id String @id @default(cuid())
|
||||
discussionId String
|
||||
userId String
|
||||
content String @db.Text
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
// Relations
|
||||
discussion EvaluationDiscussion @relation(fields: [discussionId], references: [id], onDelete: Cascade)
|
||||
user User @relation(fields: [userId], references: [id])
|
||||
|
||||
@@index([discussionId, createdAt])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,7 +48,11 @@ import {
|
|||
ChevronRight,
|
||||
RefreshCw,
|
||||
RotateCcw,
|
||||
AlertTriangle,
|
||||
Layers,
|
||||
ArrowLeftRight,
|
||||
} from 'lucide-react'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { formatDate } from '@/lib/utils'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
|
|
@ -127,6 +131,7 @@ export default function AuditLogPage() {
|
|||
const [page, setPage] = useState(1)
|
||||
const [expandedRows, setExpandedRows] = useState<Set<string>>(new Set())
|
||||
const [showFilters, setShowFilters] = useState(true)
|
||||
const [groupBySession, setGroupBySession] = useState(false)
|
||||
|
||||
// Build query input
|
||||
const queryInput = useMemo(
|
||||
|
|
@ -153,6 +158,11 @@ export default function AuditLogPage() {
|
|||
perPage: 100,
|
||||
})
|
||||
|
||||
// Fetch anomalies
|
||||
const { data: anomalyData } = trpc.audit.getAnomalies.useQuery({}, {
|
||||
retry: false,
|
||||
})
|
||||
|
||||
// Export mutation
|
||||
const exportLogs = trpc.export.auditLogs.useQuery(
|
||||
{
|
||||
|
|
@ -384,6 +394,54 @@ export default function AuditLogPage() {
|
|||
</Card>
|
||||
</Collapsible>
|
||||
|
||||
{/* Anomaly Alerts */}
|
||||
{anomalyData && anomalyData.anomalies.length > 0 && (
|
||||
<Card className="border-amber-500/50 bg-amber-500/5">
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-lg flex items-center gap-2 text-amber-700">
|
||||
<AlertTriangle className="h-5 w-5" />
|
||||
Anomaly Alerts ({anomalyData.anomalies.length})
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-2">
|
||||
{anomalyData.anomalies.slice(0, 5).map((anomaly, i) => (
|
||||
<div key={i} className="flex items-start gap-3 rounded-lg border border-amber-200 bg-white p-3 text-sm">
|
||||
<AlertTriangle className="h-4 w-4 text-amber-500 shrink-0 mt-0.5" />
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="font-medium">{anomaly.isRapid ? 'Rapid Activity' : 'Bulk Operations'}</p>
|
||||
<p className="text-xs text-muted-foreground">{String(anomaly.actionCount)} actions in {String(anomaly.timeWindowMinutes)} min ({anomaly.actionsPerMinute.toFixed(1)}/min)</p>
|
||||
{anomaly.userId && (
|
||||
<p className="text-xs text-muted-foreground mt-1">
|
||||
User: {String(anomaly.user?.name || anomaly.userId)}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<span className="text-xs text-muted-foreground shrink-0">
|
||||
{String(anomaly.actionCount)} actions
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Session Grouping Toggle */}
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<Switch
|
||||
id="session-grouping"
|
||||
checked={groupBySession}
|
||||
onCheckedChange={setGroupBySession}
|
||||
/>
|
||||
<label htmlFor="session-grouping" className="text-sm cursor-pointer flex items-center gap-1">
|
||||
<Layers className="h-4 w-4" />
|
||||
Group by Session
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Results */}
|
||||
{isLoading ? (
|
||||
<AuditLogSkeleton />
|
||||
|
|
@ -485,6 +543,28 @@ export default function AuditLogPage() {
|
|||
</pre>
|
||||
</div>
|
||||
)}
|
||||
{!!(log as Record<string, unknown>).previousDataJson && (
|
||||
<div>
|
||||
<p className="text-xs font-medium text-muted-foreground mb-1 flex items-center gap-1">
|
||||
<ArrowLeftRight className="h-3 w-3" />
|
||||
Changes (Before / After)
|
||||
</p>
|
||||
<DiffViewer
|
||||
before={(log as Record<string, unknown>).previousDataJson}
|
||||
after={log.detailsJson}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
{groupBySession && !!(log as Record<string, unknown>).sessionId && (
|
||||
<div>
|
||||
<p className="text-xs font-medium text-muted-foreground">
|
||||
Session ID
|
||||
</p>
|
||||
<p className="font-mono text-xs">
|
||||
{String((log as Record<string, unknown>).sessionId)}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
|
|
@ -625,6 +705,42 @@ export default function AuditLogPage() {
|
|||
)
|
||||
}
|
||||
|
||||
function DiffViewer({ before, after }: { before: unknown; after: unknown }) {
|
||||
const beforeObj = typeof before === 'object' && before !== null ? before as Record<string, unknown> : {}
|
||||
const afterObj = typeof after === 'object' && after !== null ? after as Record<string, unknown> : {}
|
||||
const allKeys = Array.from(new Set([...Object.keys(beforeObj), ...Object.keys(afterObj)]))
|
||||
const changedKeys = allKeys.filter(
|
||||
(key) => JSON.stringify(beforeObj[key]) !== JSON.stringify(afterObj[key])
|
||||
)
|
||||
|
||||
if (changedKeys.length === 0) {
|
||||
return (
|
||||
<p className="text-xs text-muted-foreground italic">No differences detected</p>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="rounded-lg border overflow-hidden text-xs font-mono">
|
||||
<div className="grid grid-cols-3 bg-muted p-2 font-medium">
|
||||
<span>Field</span>
|
||||
<span>Before</span>
|
||||
<span>After</span>
|
||||
</div>
|
||||
{changedKeys.map((key) => (
|
||||
<div key={key} className="grid grid-cols-3 p-2 border-t">
|
||||
<span className="font-medium text-muted-foreground">{key}</span>
|
||||
<span className="text-red-600 break-all">
|
||||
{beforeObj[key] !== undefined ? JSON.stringify(beforeObj[key]) : '--'}
|
||||
</span>
|
||||
<span className="text-green-600 break-all">
|
||||
{afterObj[key] !== undefined ? JSON.stringify(afterObj[key]) : '--'}
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function AuditLogSkeleton() {
|
||||
return (
|
||||
<Card>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,551 @@
|
|||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
import {
|
||||
Tabs,
|
||||
TabsContent,
|
||||
TabsList,
|
||||
TabsTrigger,
|
||||
} from '@/components/ui/tabs'
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from '@/components/ui/table'
|
||||
import {
|
||||
Send,
|
||||
Mail,
|
||||
Bell,
|
||||
Clock,
|
||||
Loader2,
|
||||
LayoutTemplate,
|
||||
AlertCircle,
|
||||
Inbox,
|
||||
CheckCircle2,
|
||||
} from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
import { formatDate } from '@/lib/utils'
|
||||
|
||||
type RecipientType = 'ALL' | 'ROLE' | 'ROUND_JURY' | 'PROGRAM_TEAM' | 'USER'
|
||||
|
||||
const RECIPIENT_TYPE_OPTIONS: { value: RecipientType; label: string }[] = [
|
||||
{ value: 'ALL', label: 'All Users' },
|
||||
{ value: 'ROLE', label: 'By Role' },
|
||||
{ value: 'ROUND_JURY', label: 'Round Jury' },
|
||||
{ value: 'PROGRAM_TEAM', label: 'Program Team' },
|
||||
{ value: 'USER', label: 'Specific User' },
|
||||
]
|
||||
|
||||
const ROLES = ['JURY_MEMBER', 'MENTOR', 'OBSERVER', 'APPLICANT', 'PROGRAM_ADMIN']
|
||||
|
||||
export default function MessagesPage() {
|
||||
const [recipientType, setRecipientType] = useState<RecipientType>('ALL')
|
||||
const [selectedRole, setSelectedRole] = useState('')
|
||||
const [roundId, setRoundId] = useState('')
|
||||
const [selectedProgramId, setSelectedProgramId] = useState('')
|
||||
const [selectedUserId, setSelectedUserId] = useState('')
|
||||
const [subject, setSubject] = useState('')
|
||||
const [body, setBody] = useState('')
|
||||
const [selectedTemplateId, setSelectedTemplateId] = useState('')
|
||||
const [deliveryChannels, setDeliveryChannels] = useState<string[]>(['EMAIL', 'IN_APP'])
|
||||
const [isScheduled, setIsScheduled] = useState(false)
|
||||
const [scheduledAt, setScheduledAt] = useState('')
|
||||
|
||||
const utils = trpc.useUtils()
|
||||
|
||||
// Fetch supporting data
|
||||
const { data: rounds } = trpc.round.listAll.useQuery()
|
||||
const { data: programs } = trpc.program.list.useQuery()
|
||||
const { data: templates } = trpc.message.listTemplates.useQuery()
|
||||
const { data: users } = trpc.user.list.useQuery(
|
||||
{ page: 1, perPage: 100 },
|
||||
{ enabled: recipientType === 'USER' }
|
||||
)
|
||||
|
||||
// Fetch sent messages for history
|
||||
const { data: sentMessages, isLoading: loadingSent } = trpc.message.inbox.useQuery(
|
||||
{ page: 1, pageSize: 50 }
|
||||
)
|
||||
|
||||
const sendMutation = trpc.message.send.useMutation({
|
||||
onSuccess: (data) => {
|
||||
const count = (data as Record<string, unknown>)?.recipientCount || ''
|
||||
toast.success(`Message sent successfully${count ? ` to ${count} recipients` : ''}`)
|
||||
resetForm()
|
||||
utils.message.inbox.invalidate()
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const resetForm = () => {
|
||||
setSubject('')
|
||||
setBody('')
|
||||
setSelectedTemplateId('')
|
||||
setSelectedRole('')
|
||||
setRoundId('')
|
||||
setSelectedProgramId('')
|
||||
setSelectedUserId('')
|
||||
setIsScheduled(false)
|
||||
setScheduledAt('')
|
||||
}
|
||||
|
||||
const handleTemplateSelect = (templateId: string) => {
|
||||
setSelectedTemplateId(templateId)
|
||||
if (templateId && templateId !== '__none__' && templates) {
|
||||
const template = (templates as Array<Record<string, unknown>>).find(
|
||||
(t) => String(t.id) === templateId
|
||||
)
|
||||
if (template) {
|
||||
setSubject(String(template.subject || ''))
|
||||
setBody(String(template.body || ''))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const toggleChannel = (channel: string) => {
|
||||
setDeliveryChannels((prev) =>
|
||||
prev.includes(channel)
|
||||
? prev.filter((c) => c !== channel)
|
||||
: [...prev, channel]
|
||||
)
|
||||
}
|
||||
|
||||
const buildRecipientFilter = (): unknown => {
|
||||
switch (recipientType) {
|
||||
case 'ROLE':
|
||||
return selectedRole ? { role: selectedRole } : undefined
|
||||
case 'USER':
|
||||
return selectedUserId ? { userId: selectedUserId } : undefined
|
||||
case 'PROGRAM_TEAM':
|
||||
return selectedProgramId ? { programId: selectedProgramId } : undefined
|
||||
default:
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
const handleSend = () => {
|
||||
if (!subject.trim()) {
|
||||
toast.error('Subject is required')
|
||||
return
|
||||
}
|
||||
if (!body.trim()) {
|
||||
toast.error('Message body is required')
|
||||
return
|
||||
}
|
||||
if (deliveryChannels.length === 0) {
|
||||
toast.error('Select at least one delivery channel')
|
||||
return
|
||||
}
|
||||
if (recipientType === 'ROLE' && !selectedRole) {
|
||||
toast.error('Please select a role')
|
||||
return
|
||||
}
|
||||
if (recipientType === 'ROUND_JURY' && !roundId) {
|
||||
toast.error('Please select a round')
|
||||
return
|
||||
}
|
||||
if (recipientType === 'PROGRAM_TEAM' && !selectedProgramId) {
|
||||
toast.error('Please select a program')
|
||||
return
|
||||
}
|
||||
if (recipientType === 'USER' && !selectedUserId) {
|
||||
toast.error('Please select a user')
|
||||
return
|
||||
}
|
||||
|
||||
sendMutation.mutate({
|
||||
recipientType,
|
||||
recipientFilter: buildRecipientFilter(),
|
||||
roundId: roundId || undefined,
|
||||
subject: subject.trim(),
|
||||
body: body.trim(),
|
||||
deliveryChannels,
|
||||
scheduledAt: isScheduled && scheduledAt ? new Date(scheduledAt).toISOString() : undefined,
|
||||
templateId: selectedTemplateId && selectedTemplateId !== '__none__' ? selectedTemplateId : undefined,
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold tracking-tight">Communication Hub</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Send messages and notifications to platform users
|
||||
</p>
|
||||
</div>
|
||||
<Button variant="outline" asChild>
|
||||
<Link href="/admin/messages/templates">
|
||||
<LayoutTemplate className="mr-2 h-4 w-4" />
|
||||
Templates
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<Tabs defaultValue="compose">
|
||||
<TabsList>
|
||||
<TabsTrigger value="compose">
|
||||
<Send className="mr-2 h-4 w-4" />
|
||||
Compose
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="history">
|
||||
<Inbox className="mr-2 h-4 w-4" />
|
||||
Sent History
|
||||
</TabsTrigger>
|
||||
</TabsList>
|
||||
|
||||
<TabsContent value="compose" className="space-y-4 mt-4">
|
||||
{/* Compose Form */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Compose Message</CardTitle>
|
||||
<CardDescription>
|
||||
Send a message via email, in-app notifications, or both
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
{/* Recipient type */}
|
||||
<div className="space-y-2">
|
||||
<Label>Recipient Type</Label>
|
||||
<Select
|
||||
value={recipientType}
|
||||
onValueChange={(v) => {
|
||||
setRecipientType(v as RecipientType)
|
||||
setSelectedRole('')
|
||||
setRoundId('')
|
||||
setSelectedProgramId('')
|
||||
setSelectedUserId('')
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{RECIPIENT_TYPE_OPTIONS.map((opt) => (
|
||||
<SelectItem key={opt.value} value={opt.value}>
|
||||
{opt.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{/* Conditional sub-filters */}
|
||||
{recipientType === 'ROLE' && (
|
||||
<div className="space-y-2">
|
||||
<Label>Select Role</Label>
|
||||
<Select value={selectedRole} onValueChange={setSelectedRole}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Choose a role..." />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{ROLES.map((role) => (
|
||||
<SelectItem key={role} value={role}>
|
||||
{role.replace(/_/g, ' ')}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{recipientType === 'ROUND_JURY' && (
|
||||
<div className="space-y-2">
|
||||
<Label>Select Round</Label>
|
||||
<Select value={roundId} onValueChange={setRoundId}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Choose a round..." />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{(rounds as Array<{ id: string; name: string; program?: { name: string } }> | undefined)?.map((round) => (
|
||||
<SelectItem key={round.id} value={round.id}>
|
||||
{round.program ? `${round.program.name} - ${round.name}` : round.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{recipientType === 'PROGRAM_TEAM' && (
|
||||
<div className="space-y-2">
|
||||
<Label>Select Program</Label>
|
||||
<Select value={selectedProgramId} onValueChange={setSelectedProgramId}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Choose a program..." />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{(programs as Array<{ id: string; name: string }> | undefined)?.map((prog) => (
|
||||
<SelectItem key={prog.id} value={prog.id}>
|
||||
{prog.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{recipientType === 'USER' && (
|
||||
<div className="space-y-2">
|
||||
<Label>Select User</Label>
|
||||
<Select value={selectedUserId} onValueChange={setSelectedUserId}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Choose a user..." />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{(users as { users: Array<{ id: string; name: string | null; email: string }> } | undefined)?.users?.map((u) => (
|
||||
<SelectItem key={u.id} value={u.id}>
|
||||
{u.name || u.email}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{recipientType === 'ALL' && (
|
||||
<div className="flex items-center gap-2 rounded-lg bg-amber-500/10 p-3 text-amber-700">
|
||||
<AlertCircle className="h-4 w-4 shrink-0" />
|
||||
<p className="text-sm">
|
||||
This message will be sent to all platform users.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Template selector */}
|
||||
{templates && (templates as unknown[]).length > 0 && (
|
||||
<div className="space-y-2">
|
||||
<Label>Template (optional)</Label>
|
||||
<Select value={selectedTemplateId} onValueChange={handleTemplateSelect}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Load from template..." />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="__none__">No template</SelectItem>
|
||||
{(templates as Array<Record<string, unknown>>).map((t) => (
|
||||
<SelectItem key={String(t.id)} value={String(t.id)}>
|
||||
{String(t.name)}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Subject */}
|
||||
<div className="space-y-2">
|
||||
<Label>Subject</Label>
|
||||
<Input
|
||||
placeholder="Message subject..."
|
||||
value={subject}
|
||||
onChange={(e) => setSubject(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Body */}
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<Label>Message Body</Label>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
Variables: {'{{projectName}}'}, {'{{userName}}'}, {'{{deadline}}'}, {'{{roundName}}'}, {'{{programName}}'}
|
||||
</span>
|
||||
</div>
|
||||
<Textarea
|
||||
placeholder="Write your message..."
|
||||
value={body}
|
||||
onChange={(e) => setBody(e.target.value)}
|
||||
rows={6}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Delivery channels */}
|
||||
<div className="space-y-2">
|
||||
<Label>Delivery Channels</Label>
|
||||
<div className="flex items-center gap-6">
|
||||
<div className="flex items-center gap-2">
|
||||
<Checkbox
|
||||
id="channel-email"
|
||||
checked={deliveryChannels.includes('EMAIL')}
|
||||
onCheckedChange={() => toggleChannel('EMAIL')}
|
||||
/>
|
||||
<label htmlFor="channel-email" className="text-sm cursor-pointer flex items-center gap-1">
|
||||
<Mail className="h-3 w-3" />
|
||||
Email
|
||||
</label>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Checkbox
|
||||
id="channel-inapp"
|
||||
checked={deliveryChannels.includes('IN_APP')}
|
||||
onCheckedChange={() => toggleChannel('IN_APP')}
|
||||
/>
|
||||
<label htmlFor="channel-inapp" className="text-sm cursor-pointer flex items-center gap-1">
|
||||
<Bell className="h-3 w-3" />
|
||||
In-App
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Schedule */}
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<Switch
|
||||
id="schedule-toggle"
|
||||
checked={isScheduled}
|
||||
onCheckedChange={setIsScheduled}
|
||||
/>
|
||||
<label htmlFor="schedule-toggle" className="text-sm cursor-pointer flex items-center gap-1">
|
||||
<Clock className="h-3 w-3" />
|
||||
Schedule for later
|
||||
</label>
|
||||
</div>
|
||||
{isScheduled && (
|
||||
<Input
|
||||
type="datetime-local"
|
||||
value={scheduledAt}
|
||||
onChange={(e) => setScheduledAt(e.target.value)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Send button */}
|
||||
<div className="flex justify-end">
|
||||
<Button onClick={handleSend} disabled={sendMutation.isPending}>
|
||||
{sendMutation.isPending ? (
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<Send className="mr-2 h-4 w-4" />
|
||||
)}
|
||||
{isScheduled ? 'Schedule' : 'Send Message'}
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="history" className="mt-4">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Sent Messages</CardTitle>
|
||||
<CardDescription>
|
||||
Recent messages sent through the platform
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{loadingSent ? (
|
||||
<div className="space-y-3">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<div key={i} className="flex items-center gap-4">
|
||||
<Skeleton className="h-4 w-48" />
|
||||
<Skeleton className="h-6 w-24" />
|
||||
<Skeleton className="h-4 w-32 ml-auto" />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : sentMessages && sentMessages.items.length > 0 ? (
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Subject</TableHead>
|
||||
<TableHead className="hidden md:table-cell">From</TableHead>
|
||||
<TableHead className="hidden md:table-cell">Channel</TableHead>
|
||||
<TableHead className="hidden lg:table-cell">Status</TableHead>
|
||||
<TableHead className="text-right">Date</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{sentMessages.items.map((item: Record<string, unknown>) => {
|
||||
const msg = item.message as Record<string, unknown> | undefined
|
||||
const sender = msg?.sender as Record<string, unknown> | undefined
|
||||
const channel = String(item.channel || 'EMAIL')
|
||||
const isRead = !!item.isRead
|
||||
|
||||
return (
|
||||
<TableRow key={String(item.id)}>
|
||||
<TableCell>
|
||||
<div className="flex items-center gap-2">
|
||||
{!isRead && (
|
||||
<div className="h-2 w-2 rounded-full bg-primary shrink-0" />
|
||||
)}
|
||||
<span className={isRead ? 'text-muted-foreground' : 'font-medium'}>
|
||||
{String(msg?.subject || 'No subject')}
|
||||
</span>
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="hidden md:table-cell text-sm text-muted-foreground">
|
||||
{String(sender?.name || sender?.email || 'System')}
|
||||
</TableCell>
|
||||
<TableCell className="hidden md:table-cell">
|
||||
<Badge variant="outline" className="text-xs">
|
||||
{channel === 'EMAIL' ? (
|
||||
<><Mail className="mr-1 h-3 w-3" />Email</>
|
||||
) : (
|
||||
<><Bell className="mr-1 h-3 w-3" />In-App</>
|
||||
)}
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell className="hidden lg:table-cell">
|
||||
{isRead ? (
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
<CheckCircle2 className="mr-1 h-3 w-3" />
|
||||
Read
|
||||
</Badge>
|
||||
) : (
|
||||
<Badge variant="default" className="text-xs">New</Badge>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-right text-sm text-muted-foreground">
|
||||
{msg?.createdAt
|
||||
? formatDate(msg.createdAt as string | Date)
|
||||
: ''}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)
|
||||
})}
|
||||
</TableBody>
|
||||
</Table>
|
||||
) : (
|
||||
<div className="flex flex-col items-center justify-center py-12 text-center">
|
||||
<Inbox className="h-12 w-12 text-muted-foreground/50" />
|
||||
<p className="mt-2 font-medium">No messages yet</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Sent messages will appear here.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,472 @@
|
|||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from '@/components/ui/dialog'
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from '@/components/ui/table'
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@/components/ui/alert-dialog'
|
||||
import {
|
||||
ArrowLeft,
|
||||
Plus,
|
||||
Pencil,
|
||||
Trash2,
|
||||
Loader2,
|
||||
LayoutTemplate,
|
||||
Eye,
|
||||
Variable,
|
||||
} from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
const AVAILABLE_VARIABLES = [
|
||||
{ name: '{{projectName}}', desc: 'Project title' },
|
||||
{ name: '{{userName}}', desc: "Recipient's name" },
|
||||
{ name: '{{deadline}}', desc: 'Deadline date' },
|
||||
{ name: '{{roundName}}', desc: 'Round name' },
|
||||
{ name: '{{programName}}', desc: 'Program name' },
|
||||
]
|
||||
|
||||
interface TemplateFormData {
|
||||
name: string
|
||||
category: string
|
||||
subject: string
|
||||
body: string
|
||||
variables: string[]
|
||||
isActive: boolean
|
||||
}
|
||||
|
||||
const defaultForm: TemplateFormData = {
|
||||
name: '',
|
||||
category: '',
|
||||
subject: '',
|
||||
body: '',
|
||||
variables: [],
|
||||
isActive: true,
|
||||
}
|
||||
|
||||
export default function MessageTemplatesPage() {
|
||||
const [dialogOpen, setDialogOpen] = useState(false)
|
||||
const [editingId, setEditingId] = useState<string | null>(null)
|
||||
const [deleteId, setDeleteId] = useState<string | null>(null)
|
||||
const [formData, setFormData] = useState<TemplateFormData>(defaultForm)
|
||||
const [showPreview, setShowPreview] = useState(false)
|
||||
|
||||
const utils = trpc.useUtils()
|
||||
|
||||
const { data: templates, isLoading } = trpc.message.listTemplates.useQuery()
|
||||
|
||||
const createMutation = trpc.message.createTemplate.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.message.listTemplates.invalidate()
|
||||
toast.success('Template created')
|
||||
closeDialog()
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const updateMutation = trpc.message.updateTemplate.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.message.listTemplates.invalidate()
|
||||
toast.success('Template updated')
|
||||
closeDialog()
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const deleteMutation = trpc.message.deleteTemplate.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.message.listTemplates.invalidate()
|
||||
toast.success('Template deleted')
|
||||
setDeleteId(null)
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const closeDialog = () => {
|
||||
setDialogOpen(false)
|
||||
setEditingId(null)
|
||||
setFormData(defaultForm)
|
||||
setShowPreview(false)
|
||||
}
|
||||
|
||||
const openEdit = (template: Record<string, unknown>) => {
|
||||
setEditingId(String(template.id))
|
||||
setFormData({
|
||||
name: String(template.name || ''),
|
||||
category: String(template.category || ''),
|
||||
subject: String(template.subject || ''),
|
||||
body: String(template.body || ''),
|
||||
variables: Array.isArray(template.variables) ? template.variables.map(String) : [],
|
||||
isActive: template.isActive !== false,
|
||||
})
|
||||
setDialogOpen(true)
|
||||
}
|
||||
|
||||
const insertVariable = (variable: string) => {
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
body: prev.body + variable,
|
||||
}))
|
||||
}
|
||||
|
||||
const handleSubmit = () => {
|
||||
if (!formData.name.trim() || !formData.subject.trim()) {
|
||||
toast.error('Name and subject are required')
|
||||
return
|
||||
}
|
||||
|
||||
const payload = {
|
||||
name: formData.name.trim(),
|
||||
category: formData.category.trim() || 'General',
|
||||
subject: formData.subject.trim(),
|
||||
body: formData.body.trim(),
|
||||
variables: formData.variables.length > 0 ? formData.variables : undefined,
|
||||
}
|
||||
|
||||
if (editingId) {
|
||||
updateMutation.mutate({ id: editingId, ...payload, isActive: formData.isActive })
|
||||
} else {
|
||||
createMutation.mutate(payload)
|
||||
}
|
||||
}
|
||||
|
||||
const getPreviewText = (text: string): string => {
|
||||
return text
|
||||
.replace(/\{\{userName\}\}/g, 'John Doe')
|
||||
.replace(/\{\{projectName\}\}/g, 'Ocean Cleanup Initiative')
|
||||
.replace(/\{\{roundName\}\}/g, 'Round 1 - Semi-Finals')
|
||||
.replace(/\{\{programName\}\}/g, 'MOPC 2026')
|
||||
.replace(/\{\{deadline\}\}/g, 'March 15, 2026')
|
||||
}
|
||||
|
||||
const isPending = createMutation.isPending || updateMutation.isPending
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center gap-4">
|
||||
<Button variant="ghost" asChild className="-ml-4">
|
||||
<Link href="/admin/messages">
|
||||
<ArrowLeft className="mr-2 h-4 w-4" />
|
||||
Back to Messages
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold tracking-tight">Message Templates</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Create and manage reusable message templates
|
||||
</p>
|
||||
</div>
|
||||
<Dialog open={dialogOpen} onOpenChange={(open) => !open && closeDialog()}>
|
||||
<DialogTrigger asChild>
|
||||
<Button onClick={() => { setFormData(defaultForm); setEditingId(null); setDialogOpen(true) }}>
|
||||
<Plus className="mr-2 h-4 w-4" />
|
||||
Create Template
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="max-w-2xl max-h-[90vh] overflow-y-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{editingId ? 'Edit Template' : 'Create Template'}</DialogTitle>
|
||||
<DialogDescription>
|
||||
Define a reusable message template with variable placeholders.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4">
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<div className="space-y-2">
|
||||
<Label>Template Name</Label>
|
||||
<Input
|
||||
placeholder="e.g., Evaluation Reminder"
|
||||
value={formData.name}
|
||||
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Category</Label>
|
||||
<Input
|
||||
placeholder="e.g., Notification, Reminder"
|
||||
value={formData.category}
|
||||
onChange={(e) => setFormData({ ...formData, category: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Subject</Label>
|
||||
<Input
|
||||
placeholder="e.g., Reminder: {{roundName}} evaluation deadline"
|
||||
value={formData.subject}
|
||||
onChange={(e) => setFormData({ ...formData, subject: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<Label>Message Body</Label>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => setShowPreview(!showPreview)}
|
||||
>
|
||||
<Eye className="mr-1 h-3 w-3" />
|
||||
{showPreview ? 'Edit' : 'Preview'}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{showPreview ? (
|
||||
<Card>
|
||||
<CardContent className="p-4">
|
||||
<p className="text-sm font-medium mb-2">
|
||||
Subject: {getPreviewText(formData.subject)}
|
||||
</p>
|
||||
<div className="text-sm whitespace-pre-wrap border-t pt-2">
|
||||
{getPreviewText(formData.body) || 'No content yet'}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
) : (
|
||||
<Textarea
|
||||
placeholder="Write your template message..."
|
||||
value={formData.body}
|
||||
onChange={(e) => setFormData({ ...formData, body: e.target.value })}
|
||||
rows={8}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Variable buttons */}
|
||||
{!showPreview && (
|
||||
<div className="space-y-2">
|
||||
<Label className="flex items-center gap-1">
|
||||
<Variable className="h-3 w-3" />
|
||||
Insert Variable
|
||||
</Label>
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{AVAILABLE_VARIABLES.map((v) => (
|
||||
<Button
|
||||
key={v.name}
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="text-xs"
|
||||
onClick={() => insertVariable(v.name)}
|
||||
title={v.desc}
|
||||
>
|
||||
{v.name}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{editingId && (
|
||||
<div className="flex items-center gap-2">
|
||||
<Switch
|
||||
id="template-active"
|
||||
checked={formData.isActive}
|
||||
onCheckedChange={(checked) =>
|
||||
setFormData({ ...formData, isActive: checked })
|
||||
}
|
||||
/>
|
||||
<label htmlFor="template-active" className="text-sm cursor-pointer">
|
||||
Active
|
||||
</label>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={closeDialog}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleSubmit} disabled={isPending}>
|
||||
{isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
{editingId ? 'Update' : 'Create'}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
|
||||
{/* Variable reference panel */}
|
||||
<Card>
|
||||
<CardHeader className="pb-3">
|
||||
<CardTitle className="text-base flex items-center gap-2">
|
||||
<Variable className="h-4 w-4" />
|
||||
Available Template Variables
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex flex-wrap gap-3">
|
||||
{AVAILABLE_VARIABLES.map((v) => (
|
||||
<div key={v.name} className="flex items-center gap-2">
|
||||
<code className="text-xs bg-muted rounded px-2 py-1 font-mono">
|
||||
{v.name}
|
||||
</code>
|
||||
<span className="text-xs text-muted-foreground">{v.desc}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Templates list */}
|
||||
{isLoading ? (
|
||||
<TemplatesSkeleton />
|
||||
) : templates && (templates as unknown[]).length > 0 ? (
|
||||
<Card>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Name</TableHead>
|
||||
<TableHead className="hidden md:table-cell">Category</TableHead>
|
||||
<TableHead className="hidden md:table-cell">Subject</TableHead>
|
||||
<TableHead className="hidden lg:table-cell">Status</TableHead>
|
||||
<TableHead className="text-right">Actions</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{(templates as Array<Record<string, unknown>>).map((template) => (
|
||||
<TableRow key={String(template.id)}>
|
||||
<TableCell className="font-medium">
|
||||
{String(template.name)}
|
||||
</TableCell>
|
||||
<TableCell className="hidden md:table-cell">
|
||||
{template.category ? (
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
{String(template.category)}
|
||||
</Badge>
|
||||
) : (
|
||||
<span className="text-xs text-muted-foreground">--</span>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="hidden md:table-cell text-sm text-muted-foreground truncate max-w-[200px]">
|
||||
{String(template.subject || '')}
|
||||
</TableCell>
|
||||
<TableCell className="hidden lg:table-cell">
|
||||
{template.isActive !== false ? (
|
||||
<Badge variant="default" className="text-xs">Active</Badge>
|
||||
) : (
|
||||
<Badge variant="secondary" className="text-xs">Inactive</Badge>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<div className="flex items-center justify-end gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => openEdit(template)}
|
||||
>
|
||||
<Pencil className="h-4 w-4" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => setDeleteId(String(template.id))}
|
||||
>
|
||||
<Trash2 className="h-4 w-4 text-destructive" />
|
||||
</Button>
|
||||
</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</Card>
|
||||
) : (
|
||||
<Card>
|
||||
<CardContent className="flex flex-col items-center justify-center py-12 text-center">
|
||||
<LayoutTemplate className="h-12 w-12 text-muted-foreground/50" />
|
||||
<p className="mt-2 font-medium">No templates yet</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Create a template to speed up message composition.
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Delete confirmation */}
|
||||
<AlertDialog open={!!deleteId} onOpenChange={() => setDeleteId(null)}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Delete Template</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
Are you sure you want to delete this template? This action cannot be undone.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
onClick={() => deleteId && deleteMutation.mutate({ id: deleteId })}
|
||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||
>
|
||||
{deleteMutation.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Delete
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function TemplatesSkeleton() {
|
||||
return (
|
||||
<Card>
|
||||
<CardContent className="p-6">
|
||||
<div className="space-y-4">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<div key={i} className="flex items-center gap-4">
|
||||
<Skeleton className="h-4 w-40" />
|
||||
<Skeleton className="h-6 w-24" />
|
||||
<Skeleton className="h-4 w-48" />
|
||||
<Skeleton className="h-8 w-16 ml-auto" />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,433 @@
|
|||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { useParams } from 'next/navigation'
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from '@/components/ui/dialog'
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@/components/ui/alert-dialog'
|
||||
import {
|
||||
ArrowLeft,
|
||||
Plus,
|
||||
Pencil,
|
||||
Trash2,
|
||||
Loader2,
|
||||
ChevronUp,
|
||||
ChevronDown,
|
||||
Target,
|
||||
Calendar,
|
||||
} from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
interface MilestoneFormData {
|
||||
name: string
|
||||
description: string
|
||||
isRequired: boolean
|
||||
deadlineOffsetDays: number
|
||||
sortOrder: number
|
||||
}
|
||||
|
||||
const defaultMilestoneForm: MilestoneFormData = {
|
||||
name: '',
|
||||
description: '',
|
||||
isRequired: false,
|
||||
deadlineOffsetDays: 30,
|
||||
sortOrder: 0,
|
||||
}
|
||||
|
||||
export default function MentorshipMilestonesPage() {
|
||||
const params = useParams()
|
||||
const programId = params.id as string
|
||||
|
||||
const [dialogOpen, setDialogOpen] = useState(false)
|
||||
const [editingId, setEditingId] = useState<string | null>(null)
|
||||
const [deleteId, setDeleteId] = useState<string | null>(null)
|
||||
const [formData, setFormData] = useState<MilestoneFormData>(defaultMilestoneForm)
|
||||
|
||||
const utils = trpc.useUtils()
|
||||
|
||||
const { data: milestones, isLoading } = trpc.mentor.getMilestones.useQuery({ programId })
|
||||
|
||||
const createMutation = trpc.mentor.createMilestone.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.mentor.getMilestones.invalidate({ programId })
|
||||
toast.success('Milestone created')
|
||||
closeDialog()
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const updateMutation = trpc.mentor.updateMilestone.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.mentor.getMilestones.invalidate({ programId })
|
||||
toast.success('Milestone updated')
|
||||
closeDialog()
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const deleteMutation = trpc.mentor.deleteMilestone.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.mentor.getMilestones.invalidate({ programId })
|
||||
toast.success('Milestone deleted')
|
||||
setDeleteId(null)
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const reorderMutation = trpc.mentor.reorderMilestones.useMutation({
|
||||
onSuccess: () => utils.mentor.getMilestones.invalidate({ programId }),
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const closeDialog = () => {
|
||||
setDialogOpen(false)
|
||||
setEditingId(null)
|
||||
setFormData(defaultMilestoneForm)
|
||||
}
|
||||
|
||||
const openEdit = (milestone: Record<string, unknown>) => {
|
||||
setEditingId(String(milestone.id))
|
||||
setFormData({
|
||||
name: String(milestone.name || ''),
|
||||
description: String(milestone.description || ''),
|
||||
isRequired: Boolean(milestone.isRequired),
|
||||
deadlineOffsetDays: Number(milestone.deadlineOffsetDays || 30),
|
||||
sortOrder: Number(milestone.sortOrder || 0),
|
||||
})
|
||||
setDialogOpen(true)
|
||||
}
|
||||
|
||||
const openCreate = () => {
|
||||
const nextOrder = milestones ? (milestones as unknown[]).length : 0
|
||||
setFormData({ ...defaultMilestoneForm, sortOrder: nextOrder })
|
||||
setEditingId(null)
|
||||
setDialogOpen(true)
|
||||
}
|
||||
|
||||
const handleSubmit = () => {
|
||||
if (!formData.name.trim()) {
|
||||
toast.error('Milestone name is required')
|
||||
return
|
||||
}
|
||||
|
||||
if (editingId) {
|
||||
updateMutation.mutate({
|
||||
milestoneId: editingId,
|
||||
name: formData.name.trim(),
|
||||
description: formData.description.trim() || undefined,
|
||||
isRequired: formData.isRequired,
|
||||
deadlineOffsetDays: formData.deadlineOffsetDays,
|
||||
sortOrder: formData.sortOrder,
|
||||
})
|
||||
} else {
|
||||
createMutation.mutate({
|
||||
programId,
|
||||
name: formData.name.trim(),
|
||||
description: formData.description.trim() || undefined,
|
||||
isRequired: formData.isRequired,
|
||||
deadlineOffsetDays: formData.deadlineOffsetDays,
|
||||
sortOrder: formData.sortOrder,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const moveMilestone = (id: string, direction: 'up' | 'down') => {
|
||||
if (!milestones) return
|
||||
const list = milestones as Array<Record<string, unknown>>
|
||||
const index = list.findIndex((m) => String(m.id) === id)
|
||||
if (index === -1) return
|
||||
if (direction === 'up' && index === 0) return
|
||||
if (direction === 'down' && index === list.length - 1) return
|
||||
|
||||
const ids = list.map((m) => String(m.id))
|
||||
const [moved] = ids.splice(index, 1)
|
||||
ids.splice(direction === 'up' ? index - 1 : index + 1, 0, moved)
|
||||
|
||||
reorderMutation.mutate({ milestoneIds: ids })
|
||||
}
|
||||
|
||||
const isPending = createMutation.isPending || updateMutation.isPending
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center gap-4">
|
||||
<Button variant="ghost" asChild className="-ml-4">
|
||||
<Link href="/admin/programs">
|
||||
<ArrowLeft className="mr-2 h-4 w-4" />
|
||||
Back to Programs
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold tracking-tight">
|
||||
Mentorship Milestones
|
||||
</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Configure milestones for the mentorship program
|
||||
</p>
|
||||
</div>
|
||||
<Dialog open={dialogOpen} onOpenChange={(open) => !open && closeDialog()}>
|
||||
<DialogTrigger asChild>
|
||||
<Button onClick={openCreate}>
|
||||
<Plus className="mr-2 h-4 w-4" />
|
||||
Add Milestone
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>{editingId ? 'Edit Milestone' : 'Add Milestone'}</DialogTitle>
|
||||
<DialogDescription>
|
||||
Configure a milestone for the mentorship program.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label>Name</Label>
|
||||
<Input
|
||||
placeholder="e.g., Business Plan Review"
|
||||
value={formData.name}
|
||||
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Description</Label>
|
||||
<Textarea
|
||||
placeholder="Describe what this milestone involves..."
|
||||
value={formData.description}
|
||||
onChange={(e) =>
|
||||
setFormData({ ...formData, description: e.target.value })
|
||||
}
|
||||
rows={3}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<Checkbox
|
||||
id="milestone-required"
|
||||
checked={formData.isRequired}
|
||||
onCheckedChange={(checked) =>
|
||||
setFormData({ ...formData, isRequired: !!checked })
|
||||
}
|
||||
/>
|
||||
<label htmlFor="milestone-required" className="text-sm cursor-pointer">
|
||||
Required milestone
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Deadline Offset (days from program start)</Label>
|
||||
<Input
|
||||
type="number"
|
||||
min={1}
|
||||
max={365}
|
||||
value={formData.deadlineOffsetDays}
|
||||
onChange={(e) =>
|
||||
setFormData({
|
||||
...formData,
|
||||
deadlineOffsetDays: parseInt(e.target.value) || 30,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={closeDialog}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleSubmit} disabled={isPending}>
|
||||
{isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
{editingId ? 'Update' : 'Create'}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
|
||||
{/* Milestones list */}
|
||||
{isLoading ? (
|
||||
<MilestonesSkeleton />
|
||||
) : milestones && (milestones as unknown[]).length > 0 ? (
|
||||
<div className="space-y-2">
|
||||
{(milestones as Array<Record<string, unknown>>).map((milestone, index) => {
|
||||
const completions = milestone.completions as Array<unknown> | undefined
|
||||
const completionCount = completions ? completions.length : 0
|
||||
|
||||
return (
|
||||
<Card key={String(milestone.id)}>
|
||||
<CardContent className="p-4">
|
||||
<div className="flex items-center gap-3">
|
||||
{/* Order number and reorder buttons */}
|
||||
<div className="flex flex-col items-center gap-0.5">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-5 w-5"
|
||||
disabled={index === 0 || reorderMutation.isPending}
|
||||
onClick={() => moveMilestone(String(milestone.id), 'up')}
|
||||
>
|
||||
<ChevronUp className="h-3 w-3" />
|
||||
</Button>
|
||||
<span className="text-xs font-medium text-muted-foreground w-5 text-center">
|
||||
{index + 1}
|
||||
</span>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-5 w-5"
|
||||
disabled={
|
||||
index === (milestones as unknown[]).length - 1 ||
|
||||
reorderMutation.isPending
|
||||
}
|
||||
onClick={() => moveMilestone(String(milestone.id), 'down')}
|
||||
>
|
||||
<ChevronDown className="h-3 w-3" />
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<span className="font-medium">{String(milestone.name)}</span>
|
||||
{!!milestone.isRequired && (
|
||||
<Badge variant="default" className="text-xs">Required</Badge>
|
||||
)}
|
||||
<Badge variant="outline" className="text-xs">
|
||||
<Calendar className="mr-1 h-3 w-3" />
|
||||
Day {String(milestone.deadlineOffsetDays || 30)}
|
||||
</Badge>
|
||||
{completionCount > 0 && (
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
{completionCount} completions
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
{!!milestone.description && (
|
||||
<p className="text-sm text-muted-foreground mt-1 line-clamp-2">
|
||||
{String(milestone.description)}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex items-center gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => openEdit(milestone)}
|
||||
>
|
||||
<Pencil className="h-4 w-4" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => setDeleteId(String(milestone.id))}
|
||||
>
|
||||
<Trash2 className="h-4 w-4 text-destructive" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
) : (
|
||||
<Card>
|
||||
<CardContent className="flex flex-col items-center justify-center py-12 text-center">
|
||||
<Target className="h-12 w-12 text-muted-foreground/50" />
|
||||
<p className="mt-2 font-medium">No milestones defined</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Add milestones to track mentor-mentee progress.
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Delete confirmation */}
|
||||
<AlertDialog open={!!deleteId} onOpenChange={() => setDeleteId(null)}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Delete Milestone</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
Are you sure you want to delete this milestone? Progress data associated
|
||||
with it may be lost.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
onClick={() =>
|
||||
deleteId && deleteMutation.mutate({ milestoneId: deleteId })
|
||||
}
|
||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||
>
|
||||
{deleteMutation.isPending && (
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
)}
|
||||
Delete
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function MilestonesSkeleton() {
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<Card key={i}>
|
||||
<CardContent className="p-4">
|
||||
<div className="flex items-center gap-3">
|
||||
<Skeleton className="h-16 w-6" />
|
||||
<div className="flex-1 space-y-2">
|
||||
<Skeleton className="h-5 w-48" />
|
||||
<Skeleton className="h-4 w-full" />
|
||||
</div>
|
||||
<Skeleton className="h-8 w-16" />
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -494,6 +494,7 @@ function ProjectDetailContent({ projectId }: { projectId: string }) {
|
|||
<CardContent className="space-y-4">
|
||||
{files && files.length > 0 ? (
|
||||
<FileViewer
|
||||
projectId={projectId}
|
||||
files={files.map((f) => ({
|
||||
id: f.id,
|
||||
fileName: f.fileName,
|
||||
|
|
@ -502,6 +503,7 @@ function ProjectDetailContent({ projectId }: { projectId: string }) {
|
|||
size: f.size,
|
||||
bucket: f.bucket,
|
||||
objectKey: f.objectKey,
|
||||
version: f.version,
|
||||
}))}
|
||||
/>
|
||||
) : (
|
||||
|
|
|
|||
|
|
@ -39,6 +39,10 @@ import {
|
|||
CheckCircle2,
|
||||
PieChart,
|
||||
TrendingUp,
|
||||
GitCompare,
|
||||
UserCheck,
|
||||
Globe,
|
||||
Printer,
|
||||
} from 'lucide-react'
|
||||
import { formatDateOnly } from '@/lib/utils'
|
||||
import {
|
||||
|
|
@ -49,6 +53,9 @@ import {
|
|||
ProjectRankingsChart,
|
||||
CriteriaScoresChart,
|
||||
GeographicDistribution,
|
||||
CrossRoundComparisonChart,
|
||||
JurorConsistencyChart,
|
||||
DiversityMetricsChart,
|
||||
} from '@/components/charts'
|
||||
|
||||
function ReportsOverview() {
|
||||
|
|
@ -414,6 +421,215 @@ function RoundAnalytics() {
|
|||
)
|
||||
}
|
||||
|
||||
function CrossRoundTab() {
|
||||
const { data: programs, isLoading: programsLoading } = trpc.program.list.useQuery({ includeRounds: true })
|
||||
|
||||
const rounds = programs?.flatMap(p =>
|
||||
p.rounds.map(r => ({ id: r.id, name: r.name, programName: `${p.year} Edition` }))
|
||||
) || []
|
||||
|
||||
const [selectedRoundIds, setSelectedRoundIds] = useState<string[]>([])
|
||||
|
||||
const { data: comparison, isLoading: comparisonLoading } =
|
||||
trpc.analytics.getCrossRoundComparison.useQuery(
|
||||
{ roundIds: selectedRoundIds },
|
||||
{ enabled: selectedRoundIds.length >= 2 }
|
||||
)
|
||||
|
||||
const toggleRound = (roundId: string) => {
|
||||
setSelectedRoundIds((prev) =>
|
||||
prev.includes(roundId)
|
||||
? prev.filter((id) => id !== roundId)
|
||||
: [...prev, roundId]
|
||||
)
|
||||
}
|
||||
|
||||
if (programsLoading) {
|
||||
return <Skeleton className="h-[400px]" />
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Round selector */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Select Rounds to Compare</CardTitle>
|
||||
<CardDescription>
|
||||
Choose at least 2 rounds to compare metrics side by side
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{rounds.map((round) => {
|
||||
const isSelected = selectedRoundIds.includes(round.id)
|
||||
return (
|
||||
<Badge
|
||||
key={round.id}
|
||||
variant={isSelected ? 'default' : 'outline'}
|
||||
className="cursor-pointer text-sm py-1.5 px-3"
|
||||
onClick={() => toggleRound(round.id)}
|
||||
>
|
||||
{round.programName} - {round.name}
|
||||
</Badge>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
{selectedRoundIds.length < 2 && (
|
||||
<p className="text-sm text-muted-foreground mt-3">
|
||||
Select at least 2 rounds to enable comparison
|
||||
</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Comparison charts */}
|
||||
{comparisonLoading && selectedRoundIds.length >= 2 && (
|
||||
<div className="space-y-6">
|
||||
<Skeleton className="h-[350px]" />
|
||||
<div className="grid gap-6 lg:grid-cols-2">
|
||||
<Skeleton className="h-[300px]" />
|
||||
<Skeleton className="h-[300px]" />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{comparison && (
|
||||
<CrossRoundComparisonChart data={comparison as Array<{
|
||||
roundId: string
|
||||
roundName: string
|
||||
projectCount: number
|
||||
evaluationCount: number
|
||||
completionRate: number
|
||||
averageScore: number | null
|
||||
scoreDistribution: { score: number; count: number }[]
|
||||
}>} />
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function JurorConsistencyTab() {
|
||||
const [selectedRoundId, setSelectedRoundId] = useState<string | null>(null)
|
||||
|
||||
const { data: programs, isLoading: programsLoading } = trpc.program.list.useQuery({ includeRounds: true })
|
||||
|
||||
const rounds = programs?.flatMap(p =>
|
||||
p.rounds.map(r => ({ id: r.id, name: r.name, programName: `${p.year} Edition` }))
|
||||
) || []
|
||||
|
||||
if (rounds.length && !selectedRoundId) {
|
||||
setSelectedRoundId(rounds[0].id)
|
||||
}
|
||||
|
||||
const { data: consistency, isLoading: consistencyLoading } =
|
||||
trpc.analytics.getJurorConsistency.useQuery(
|
||||
{ roundId: selectedRoundId! },
|
||||
{ enabled: !!selectedRoundId }
|
||||
)
|
||||
|
||||
if (programsLoading) {
|
||||
return <Skeleton className="h-[400px]" />
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center gap-4">
|
||||
<label className="text-sm font-medium">Select Round:</label>
|
||||
<Select value={selectedRoundId || ''} onValueChange={setSelectedRoundId}>
|
||||
<SelectTrigger className="w-[300px]">
|
||||
<SelectValue placeholder="Select a round" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{rounds.map((round) => (
|
||||
<SelectItem key={round.id} value={round.id}>
|
||||
{round.programName} - {round.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{consistencyLoading && <Skeleton className="h-[400px]" />}
|
||||
|
||||
{consistency && (
|
||||
<JurorConsistencyChart
|
||||
data={consistency as {
|
||||
overallAverage: number
|
||||
jurors: Array<{
|
||||
userId: string
|
||||
name: string
|
||||
email: string
|
||||
evaluationCount: number
|
||||
averageScore: number
|
||||
stddev: number
|
||||
deviationFromOverall: number
|
||||
isOutlier: boolean
|
||||
}>
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function DiversityTab() {
|
||||
const [selectedRoundId, setSelectedRoundId] = useState<string | null>(null)
|
||||
|
||||
const { data: programs, isLoading: programsLoading } = trpc.program.list.useQuery({ includeRounds: true })
|
||||
|
||||
const rounds = programs?.flatMap(p =>
|
||||
p.rounds.map(r => ({ id: r.id, name: r.name, programName: `${p.year} Edition` }))
|
||||
) || []
|
||||
|
||||
if (rounds.length && !selectedRoundId) {
|
||||
setSelectedRoundId(rounds[0].id)
|
||||
}
|
||||
|
||||
const { data: diversity, isLoading: diversityLoading } =
|
||||
trpc.analytics.getDiversityMetrics.useQuery(
|
||||
{ roundId: selectedRoundId! },
|
||||
{ enabled: !!selectedRoundId }
|
||||
)
|
||||
|
||||
if (programsLoading) {
|
||||
return <Skeleton className="h-[400px]" />
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center gap-4">
|
||||
<label className="text-sm font-medium">Select Round:</label>
|
||||
<Select value={selectedRoundId || ''} onValueChange={setSelectedRoundId}>
|
||||
<SelectTrigger className="w-[300px]">
|
||||
<SelectValue placeholder="Select a round" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{rounds.map((round) => (
|
||||
<SelectItem key={round.id} value={round.id}>
|
||||
{round.programName} - {round.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{diversityLoading && <Skeleton className="h-[400px]" />}
|
||||
|
||||
{diversity && (
|
||||
<DiversityMetricsChart
|
||||
data={diversity as {
|
||||
total: number
|
||||
byCountry: { country: string; count: number; percentage: number }[]
|
||||
byCategory: { category: string; count: number; percentage: number }[]
|
||||
byOceanIssue: { issue: string; count: number; percentage: number }[]
|
||||
byTag: { tag: string; count: number; percentage: number }[]
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function ReportsPage() {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
|
|
@ -427,16 +643,40 @@ export default function ReportsPage() {
|
|||
|
||||
{/* Tabs */}
|
||||
<Tabs defaultValue="overview" className="space-y-6">
|
||||
<TabsList>
|
||||
<TabsTrigger value="overview" className="gap-2">
|
||||
<FileSpreadsheet className="h-4 w-4" />
|
||||
Overview
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="analytics" className="gap-2">
|
||||
<TrendingUp className="h-4 w-4" />
|
||||
Analytics
|
||||
</TabsTrigger>
|
||||
</TabsList>
|
||||
<div className="flex items-center justify-between flex-wrap gap-4">
|
||||
<TabsList>
|
||||
<TabsTrigger value="overview" className="gap-2">
|
||||
<FileSpreadsheet className="h-4 w-4" />
|
||||
Overview
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="analytics" className="gap-2">
|
||||
<TrendingUp className="h-4 w-4" />
|
||||
Analytics
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="cross-round" className="gap-2">
|
||||
<GitCompare className="h-4 w-4" />
|
||||
Cross-Round
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="consistency" className="gap-2">
|
||||
<UserCheck className="h-4 w-4" />
|
||||
Juror Consistency
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="diversity" className="gap-2">
|
||||
<Globe className="h-4 w-4" />
|
||||
Diversity
|
||||
</TabsTrigger>
|
||||
</TabsList>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
window.print()
|
||||
}}
|
||||
>
|
||||
<Printer className="mr-2 h-4 w-4" />
|
||||
Export PDF
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<TabsContent value="overview">
|
||||
<ReportsOverview />
|
||||
|
|
@ -445,6 +685,18 @@ export default function ReportsPage() {
|
|||
<TabsContent value="analytics">
|
||||
<RoundAnalytics />
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="cross-round">
|
||||
<CrossRoundTab />
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="consistency">
|
||||
<JurorConsistencyTab />
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="diversity">
|
||||
<DiversityTab />
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
</div>
|
||||
)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,443 @@
|
|||
'use client'
|
||||
|
||||
import { use, useState } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Separator } from '@/components/ui/separator'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog'
|
||||
import {
|
||||
ArrowLeft,
|
||||
Loader2,
|
||||
Save,
|
||||
Trash2,
|
||||
LayoutTemplate,
|
||||
Plus,
|
||||
X,
|
||||
GripVertical,
|
||||
} from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
const ROUND_TYPE_LABELS: Record<string, string> = {
|
||||
FILTERING: 'Filtering',
|
||||
EVALUATION: 'Evaluation',
|
||||
LIVE_EVENT: 'Live Event',
|
||||
}
|
||||
|
||||
const CRITERION_TYPES = [
|
||||
{ value: 'numeric', label: 'Numeric (1-10)' },
|
||||
{ value: 'text', label: 'Text' },
|
||||
{ value: 'boolean', label: 'Yes/No' },
|
||||
{ value: 'section_header', label: 'Section Header' },
|
||||
]
|
||||
|
||||
type Criterion = {
|
||||
id: string
|
||||
label: string
|
||||
type: string
|
||||
description?: string
|
||||
weight?: number
|
||||
min?: number
|
||||
max?: number
|
||||
}
|
||||
|
||||
export default function RoundTemplateDetailPage({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ id: string }>
|
||||
}) {
|
||||
const { id } = use(params)
|
||||
const router = useRouter()
|
||||
const utils = trpc.useUtils()
|
||||
|
||||
const { data: template, isLoading } = trpc.roundTemplate.getById.useQuery({ id })
|
||||
const [name, setName] = useState('')
|
||||
const [description, setDescription] = useState('')
|
||||
const [roundType, setRoundType] = useState('EVALUATION')
|
||||
const [criteria, setCriteria] = useState<Criterion[]>([])
|
||||
const [initialized, setInitialized] = useState(false)
|
||||
const [deleteOpen, setDeleteOpen] = useState(false)
|
||||
|
||||
// Initialize form state from loaded data
|
||||
if (template && !initialized) {
|
||||
setName(template.name)
|
||||
setDescription(template.description || '')
|
||||
setRoundType(template.roundType)
|
||||
setCriteria((template.criteriaJson as Criterion[]) || [])
|
||||
setInitialized(true)
|
||||
}
|
||||
|
||||
const updateTemplate = trpc.roundTemplate.update.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.roundTemplate.getById.invalidate({ id })
|
||||
utils.roundTemplate.list.invalidate()
|
||||
toast.success('Template saved')
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err.message)
|
||||
},
|
||||
})
|
||||
|
||||
const deleteTemplate = trpc.roundTemplate.delete.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.roundTemplate.list.invalidate()
|
||||
router.push('/admin/round-templates')
|
||||
toast.success('Template deleted')
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err.message)
|
||||
},
|
||||
})
|
||||
|
||||
const handleSave = () => {
|
||||
updateTemplate.mutate({
|
||||
id,
|
||||
name: name.trim(),
|
||||
description: description.trim() || undefined,
|
||||
roundType: roundType as 'FILTERING' | 'EVALUATION' | 'LIVE_EVENT',
|
||||
criteriaJson: criteria,
|
||||
})
|
||||
}
|
||||
|
||||
const addCriterion = () => {
|
||||
setCriteria([
|
||||
...criteria,
|
||||
{
|
||||
id: `criterion_${Date.now()}`,
|
||||
label: '',
|
||||
type: 'numeric',
|
||||
weight: 1,
|
||||
min: 1,
|
||||
max: 10,
|
||||
},
|
||||
])
|
||||
}
|
||||
|
||||
const updateCriterion = (index: number, updates: Partial<Criterion>) => {
|
||||
setCriteria(criteria.map((c, i) => (i === index ? { ...c, ...updates } : c)))
|
||||
}
|
||||
|
||||
const removeCriterion = (index: number) => {
|
||||
setCriteria(criteria.filter((_, i) => i !== index))
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<Skeleton className="h-9 w-36" />
|
||||
<div className="space-y-1">
|
||||
<Skeleton className="h-8 w-64" />
|
||||
<Skeleton className="h-4 w-96" />
|
||||
</div>
|
||||
<Skeleton className="h-64 w-full" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (!template) {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<Button variant="ghost" asChild className="-ml-4">
|
||||
<Link href="/admin/round-templates">
|
||||
<ArrowLeft className="mr-2 h-4 w-4" />
|
||||
Back to Templates
|
||||
</Link>
|
||||
</Button>
|
||||
<Card>
|
||||
<CardContent className="py-12 text-center">
|
||||
<p className="font-medium">Template not found</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center gap-4">
|
||||
<Button variant="ghost" asChild className="-ml-4">
|
||||
<Link href="/admin/round-templates">
|
||||
<ArrowLeft className="mr-2 h-4 w-4" />
|
||||
Back to Templates
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="flex items-start justify-between">
|
||||
<div className="flex items-center gap-3">
|
||||
<LayoutTemplate className="h-7 w-7 text-primary" />
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold tracking-tight">
|
||||
{template.name}
|
||||
</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Edit template configuration and criteria
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => setDeleteOpen(true)}
|
||||
className="text-destructive"
|
||||
>
|
||||
<Trash2 className="mr-2 h-4 w-4" />
|
||||
Delete
|
||||
</Button>
|
||||
<Button onClick={handleSave} disabled={updateTemplate.isPending}>
|
||||
{updateTemplate.isPending ? (
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<Save className="mr-2 h-4 w-4" />
|
||||
)}
|
||||
Save Changes
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Basic Info */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Basic Information</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="name">Template Name</Label>
|
||||
<Input
|
||||
id="name"
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
placeholder="e.g., Standard Evaluation Round"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="description">Description</Label>
|
||||
<Textarea
|
||||
id="description"
|
||||
value={description}
|
||||
onChange={(e) => setDescription(e.target.value)}
|
||||
placeholder="Describe what this template is for..."
|
||||
rows={3}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label>Round Type</Label>
|
||||
<Select value={roundType} onValueChange={setRoundType}>
|
||||
<SelectTrigger className="w-[240px]">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{Object.entries(ROUND_TYPE_LABELS).map(([value, label]) => (
|
||||
<SelectItem key={value} value={value}>
|
||||
{label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<div className="pt-2 text-sm text-muted-foreground">
|
||||
Created {new Date(template.createdAt).toLocaleDateString()} | Last updated{' '}
|
||||
{new Date(template.updatedAt).toLocaleDateString()}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Criteria */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<CardTitle className="text-lg">Evaluation Criteria</CardTitle>
|
||||
<CardDescription>
|
||||
Define the criteria jurors will use to evaluate projects
|
||||
</CardDescription>
|
||||
</div>
|
||||
<Button variant="outline" size="sm" onClick={addCriterion}>
|
||||
<Plus className="mr-2 h-4 w-4" />
|
||||
Add Criterion
|
||||
</Button>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{criteria.length === 0 ? (
|
||||
<div className="text-center py-8 text-muted-foreground">
|
||||
<p>No criteria defined yet.</p>
|
||||
<Button variant="outline" className="mt-3" onClick={addCriterion}>
|
||||
<Plus className="mr-2 h-4 w-4" />
|
||||
Add First Criterion
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-4">
|
||||
{criteria.map((criterion, index) => (
|
||||
<div key={criterion.id}>
|
||||
{index > 0 && <Separator className="mb-4" />}
|
||||
<div className="flex items-start gap-3">
|
||||
<div className="mt-2 text-muted-foreground">
|
||||
<GripVertical className="h-5 w-5" />
|
||||
</div>
|
||||
<div className="flex-1 grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
|
||||
<div className="sm:col-span-2 lg:col-span-2">
|
||||
<Label className="text-xs text-muted-foreground">Label</Label>
|
||||
<Input
|
||||
value={criterion.label}
|
||||
onChange={(e) =>
|
||||
updateCriterion(index, { label: e.target.value })
|
||||
}
|
||||
placeholder="e.g., Innovation"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Label className="text-xs text-muted-foreground">Type</Label>
|
||||
<Select
|
||||
value={criterion.type}
|
||||
onValueChange={(val) =>
|
||||
updateCriterion(index, { type: val })
|
||||
}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{CRITERION_TYPES.map((t) => (
|
||||
<SelectItem key={t.value} value={t.value}>
|
||||
{t.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
{criterion.type === 'numeric' && (
|
||||
<div>
|
||||
<Label className="text-xs text-muted-foreground">Weight</Label>
|
||||
<Input
|
||||
type="number"
|
||||
min={0}
|
||||
max={10}
|
||||
step={0.1}
|
||||
value={criterion.weight ?? 1}
|
||||
onChange={(e) =>
|
||||
updateCriterion(index, {
|
||||
weight: parseFloat(e.target.value) || 1,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<div className="sm:col-span-2 lg:col-span-4">
|
||||
<Label className="text-xs text-muted-foreground">
|
||||
Description (optional)
|
||||
</Label>
|
||||
<Input
|
||||
value={criterion.description || ''}
|
||||
onChange={(e) =>
|
||||
updateCriterion(index, { description: e.target.value })
|
||||
}
|
||||
placeholder="Help text for jurors..."
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="mt-5 h-8 w-8 text-muted-foreground hover:text-destructive"
|
||||
onClick={() => removeCriterion(index)}
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Template Metadata */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Template Info</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="grid gap-4 sm:grid-cols-3 text-sm">
|
||||
<div>
|
||||
<p className="text-muted-foreground">Type</p>
|
||||
<Badge variant="secondary" className="mt-1">
|
||||
{ROUND_TYPE_LABELS[template.roundType] || template.roundType}
|
||||
</Badge>
|
||||
</div>
|
||||
<div>
|
||||
<p className="text-muted-foreground">Criteria Count</p>
|
||||
<p className="font-medium mt-1">{criteria.length}</p>
|
||||
</div>
|
||||
<div>
|
||||
<p className="text-muted-foreground">Has Custom Settings</p>
|
||||
<p className="font-medium mt-1">
|
||||
{template.settingsJson && Object.keys(template.settingsJson as object).length > 0
|
||||
? 'Yes'
|
||||
: 'No'}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Delete Dialog */}
|
||||
<Dialog open={deleteOpen} onOpenChange={setDeleteOpen}>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>Delete Template</DialogTitle>
|
||||
<DialogDescription>
|
||||
Are you sure you want to delete "{template.name}"? This action
|
||||
cannot be undone.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => setDeleteOpen(false)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
variant="destructive"
|
||||
onClick={() => deleteTemplate.mutate({ id })}
|
||||
disabled={deleteTemplate.isPending}
|
||||
>
|
||||
{deleteTemplate.isPending && (
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
)}
|
||||
Delete Template
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,302 @@
|
|||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from '@/components/ui/dialog'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
import {
|
||||
LayoutTemplate,
|
||||
Plus,
|
||||
Calendar,
|
||||
Settings2,
|
||||
Trash2,
|
||||
Loader2,
|
||||
} from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
const ROUND_TYPE_LABELS: Record<string, string> = {
|
||||
FILTERING: 'Filtering',
|
||||
EVALUATION: 'Evaluation',
|
||||
LIVE_EVENT: 'Live Event',
|
||||
}
|
||||
|
||||
const ROUND_TYPE_COLORS: Record<string, 'default' | 'secondary' | 'outline'> = {
|
||||
FILTERING: 'secondary',
|
||||
EVALUATION: 'default',
|
||||
LIVE_EVENT: 'outline',
|
||||
}
|
||||
|
||||
export default function RoundTemplatesPage() {
|
||||
const utils = trpc.useUtils()
|
||||
const { data: templates, isLoading } = trpc.roundTemplate.list.useQuery()
|
||||
const [createOpen, setCreateOpen] = useState(false)
|
||||
const [newName, setNewName] = useState('')
|
||||
const [newDescription, setNewDescription] = useState('')
|
||||
const [newRoundType, setNewRoundType] = useState('EVALUATION')
|
||||
const [deleteId, setDeleteId] = useState<string | null>(null)
|
||||
|
||||
const createTemplate = trpc.roundTemplate.create.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.roundTemplate.list.invalidate()
|
||||
setCreateOpen(false)
|
||||
setNewName('')
|
||||
setNewDescription('')
|
||||
setNewRoundType('EVALUATION')
|
||||
toast.success('Template created')
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err.message)
|
||||
},
|
||||
})
|
||||
|
||||
const deleteTemplate = trpc.roundTemplate.delete.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.roundTemplate.list.invalidate()
|
||||
setDeleteId(null)
|
||||
toast.success('Template deleted')
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err.message)
|
||||
},
|
||||
})
|
||||
|
||||
const handleCreate = () => {
|
||||
if (!newName.trim()) return
|
||||
createTemplate.mutate({
|
||||
name: newName.trim(),
|
||||
description: newDescription.trim() || undefined,
|
||||
roundType: newRoundType as 'FILTERING' | 'EVALUATION' | 'LIVE_EVENT',
|
||||
criteriaJson: [],
|
||||
})
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<Skeleton className="h-9 w-48" />
|
||||
<Skeleton className="h-9 w-40" />
|
||||
</div>
|
||||
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
|
||||
{[...Array(3)].map((_, i) => (
|
||||
<Skeleton key={i} className="h-40" />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold tracking-tight">
|
||||
Round Templates
|
||||
</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Save and reuse round configurations across editions
|
||||
</p>
|
||||
</div>
|
||||
<Dialog open={createOpen} onOpenChange={setCreateOpen}>
|
||||
<DialogTrigger asChild>
|
||||
<Button>
|
||||
<Plus className="mr-2 h-4 w-4" />
|
||||
New Template
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>Create Template</DialogTitle>
|
||||
<DialogDescription>
|
||||
Create a blank template. You can also save an existing round as a template from the round detail page.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="space-y-4 py-2">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="template-name">Name</Label>
|
||||
<Input
|
||||
id="template-name"
|
||||
placeholder="e.g., Standard Evaluation Round"
|
||||
value={newName}
|
||||
onChange={(e) => setNewName(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="template-description">Description</Label>
|
||||
<Textarea
|
||||
id="template-description"
|
||||
placeholder="Describe what this template is for..."
|
||||
value={newDescription}
|
||||
onChange={(e) => setNewDescription(e.target.value)}
|
||||
rows={3}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="template-type">Round Type</Label>
|
||||
<Select value={newRoundType} onValueChange={setNewRoundType}>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{Object.entries(ROUND_TYPE_LABELS).map(([value, label]) => (
|
||||
<SelectItem key={value} value={value}>
|
||||
{label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => setCreateOpen(false)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
onClick={handleCreate}
|
||||
disabled={!newName.trim() || createTemplate.isPending}
|
||||
>
|
||||
{createTemplate.isPending && (
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
)}
|
||||
Create
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
|
||||
{/* Templates Grid */}
|
||||
{templates && templates.length > 0 ? (
|
||||
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
|
||||
{templates.map((template) => {
|
||||
const criteria = (template.criteriaJson as Array<unknown>) || []
|
||||
const hasSettings = template.settingsJson && Object.keys(template.settingsJson as object).length > 0
|
||||
|
||||
return (
|
||||
<Card key={template.id} className="group relative transition-colors hover:bg-muted/50">
|
||||
<Link href={`/admin/round-templates/${template.id}`}>
|
||||
<CardHeader className="pb-3">
|
||||
<div className="flex items-start justify-between">
|
||||
<CardTitle className="text-lg flex items-center gap-2">
|
||||
<LayoutTemplate className="h-5 w-5 text-primary" />
|
||||
{template.name}
|
||||
</CardTitle>
|
||||
<Badge variant={ROUND_TYPE_COLORS[template.roundType] || 'secondary'}>
|
||||
{ROUND_TYPE_LABELS[template.roundType] || template.roundType}
|
||||
</Badge>
|
||||
</div>
|
||||
{template.description && (
|
||||
<CardDescription className="line-clamp-2">
|
||||
{template.description}
|
||||
</CardDescription>
|
||||
)}
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex items-center gap-4 text-sm text-muted-foreground">
|
||||
<div className="flex items-center gap-1">
|
||||
<Settings2 className="h-4 w-4" />
|
||||
{criteria.length} criteria
|
||||
</div>
|
||||
{hasSettings && (
|
||||
<Badge variant="outline" className="text-xs">
|
||||
Custom settings
|
||||
</Badge>
|
||||
)}
|
||||
<div className="flex items-center gap-1 ml-auto">
|
||||
<Calendar className="h-3.5 w-3.5" />
|
||||
{new Date(template.createdAt).toLocaleDateString()}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Link>
|
||||
{/* Delete button */}
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="absolute top-3 right-3 opacity-0 group-hover:opacity-100 transition-opacity h-8 w-8"
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
setDeleteId(template.id)
|
||||
}}
|
||||
>
|
||||
<Trash2 className="h-4 w-4 text-muted-foreground hover:text-destructive" />
|
||||
</Button>
|
||||
</Card>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
) : (
|
||||
<Card>
|
||||
<CardContent className="flex flex-col items-center justify-center py-12 text-center">
|
||||
<LayoutTemplate className="h-12 w-12 text-muted-foreground/50" />
|
||||
<p className="mt-2 font-medium">No templates yet</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Create a template or save an existing round configuration as a template
|
||||
</p>
|
||||
<Button className="mt-4" onClick={() => setCreateOpen(true)}>
|
||||
<Plus className="mr-2 h-4 w-4" />
|
||||
Create Template
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Delete Confirmation Dialog */}
|
||||
<Dialog open={!!deleteId} onOpenChange={() => setDeleteId(null)}>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>Delete Template</DialogTitle>
|
||||
<DialogDescription>
|
||||
Are you sure you want to delete this template? This action cannot be undone.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => setDeleteId(null)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
variant="destructive"
|
||||
onClick={() => deleteId && deleteTemplate.mutate({ id: deleteId })}
|
||||
disabled={deleteTemplate.isPending}
|
||||
>
|
||||
{deleteTemplate.isPending && (
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
)}
|
||||
Delete
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -32,7 +32,10 @@ import {
|
|||
type Criterion,
|
||||
} from '@/components/forms/evaluation-form-builder'
|
||||
import { RoundTypeSettings } from '@/components/forms/round-type-settings'
|
||||
import { ArrowLeft, Loader2, AlertCircle, AlertTriangle, Bell } from 'lucide-react'
|
||||
import { ArrowLeft, Loader2, AlertCircle, AlertTriangle, Bell, GitCompare, MessageSquare, FileText, Calendar } from 'lucide-react'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { Slider } from '@/components/ui/slider'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { DateTimePicker } from '@/components/ui/datetime-picker'
|
||||
import {
|
||||
Select,
|
||||
|
|
@ -458,6 +461,321 @@ function EditRoundContent({ roundId }: { roundId: string }) {
|
|||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Jury Features */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg flex items-center gap-2">
|
||||
<GitCompare className="h-5 w-5" />
|
||||
Jury Features
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Configure project comparison and peer review for jury members
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-6">
|
||||
{/* Comparison settings */}
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="space-y-0.5">
|
||||
<Label className="text-sm font-medium">Enable Project Comparison</Label>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Allow jury members to compare projects side by side
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={Boolean(roundSettings.enable_comparison)}
|
||||
onCheckedChange={(checked) =>
|
||||
setRoundSettings((prev) => ({
|
||||
...prev,
|
||||
enable_comparison: checked,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
{!!roundSettings.enable_comparison && (
|
||||
<div className="space-y-2 pl-4 border-l-2 border-muted">
|
||||
<Label className="text-sm">Max Projects to Compare</Label>
|
||||
<Input
|
||||
type="number"
|
||||
min={2}
|
||||
max={5}
|
||||
value={Number(roundSettings.comparison_max_projects || 3)}
|
||||
onChange={(e) =>
|
||||
setRoundSettings((prev) => ({
|
||||
...prev,
|
||||
comparison_max_projects: parseInt(e.target.value) || 3,
|
||||
}))
|
||||
}
|
||||
className="max-w-[120px]"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Peer review settings */}
|
||||
<div className="border-t pt-4 space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="space-y-0.5">
|
||||
<Label className="text-sm font-medium flex items-center gap-1">
|
||||
<MessageSquare className="h-4 w-4" />
|
||||
Enable Peer Review / Discussion
|
||||
</Label>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Allow jury members to discuss and see aggregated scores
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={Boolean(roundSettings.peer_review_enabled)}
|
||||
onCheckedChange={(checked) =>
|
||||
setRoundSettings((prev) => ({
|
||||
...prev,
|
||||
peer_review_enabled: checked,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
{!!roundSettings.peer_review_enabled && (
|
||||
<div className="space-y-4 pl-4 border-l-2 border-muted">
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm">Divergence Threshold</Label>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Score divergence level that triggers a warning (0.0 - 1.0)
|
||||
</p>
|
||||
<Input
|
||||
type="number"
|
||||
min={0}
|
||||
max={1}
|
||||
step={0.05}
|
||||
value={Number(roundSettings.divergence_threshold || 0.3)}
|
||||
onChange={(e) =>
|
||||
setRoundSettings((prev) => ({
|
||||
...prev,
|
||||
divergence_threshold: parseFloat(e.target.value) || 0.3,
|
||||
}))
|
||||
}
|
||||
className="max-w-[120px]"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm">Anonymization Level</Label>
|
||||
<Select
|
||||
value={String(roundSettings.anonymization_level || 'partial')}
|
||||
onValueChange={(v) =>
|
||||
setRoundSettings((prev) => ({
|
||||
...prev,
|
||||
anonymization_level: v,
|
||||
}))
|
||||
}
|
||||
>
|
||||
<SelectTrigger className="max-w-[200px]">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="none">No anonymization</SelectItem>
|
||||
<SelectItem value="partial">Partial (Juror 1, 2...)</SelectItem>
|
||||
<SelectItem value="full">Full anonymization</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm">Discussion Window (hours)</Label>
|
||||
<Input
|
||||
type="number"
|
||||
min={1}
|
||||
max={720}
|
||||
value={Number(roundSettings.discussion_window_hours || 48)}
|
||||
onChange={(e) =>
|
||||
setRoundSettings((prev) => ({
|
||||
...prev,
|
||||
discussion_window_hours: parseInt(e.target.value) || 48,
|
||||
}))
|
||||
}
|
||||
className="max-w-[120px]"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm">Max Comment Length</Label>
|
||||
<Input
|
||||
type="number"
|
||||
min={100}
|
||||
max={5000}
|
||||
value={Number(roundSettings.max_comment_length || 2000)}
|
||||
onChange={(e) =>
|
||||
setRoundSettings((prev) => ({
|
||||
...prev,
|
||||
max_comment_length: parseInt(e.target.value) || 2000,
|
||||
}))
|
||||
}
|
||||
className="max-w-[120px]"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* File Settings */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg flex items-center gap-2">
|
||||
<FileText className="h-5 w-5" />
|
||||
File Settings
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Configure allowed file types and versioning for this round
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm">Allowed File Types</Label>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Comma-separated MIME types or extensions
|
||||
</p>
|
||||
<Input
|
||||
placeholder="application/pdf, video/mp4, image/jpeg"
|
||||
value={String(roundSettings.allowed_file_types || '')}
|
||||
onChange={(e) =>
|
||||
setRoundSettings((prev) => ({
|
||||
...prev,
|
||||
allowed_file_types: e.target.value,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm">Max File Size (MB)</Label>
|
||||
<Input
|
||||
type="number"
|
||||
min={1}
|
||||
max={2048}
|
||||
value={Number(roundSettings.max_file_size_mb || 500)}
|
||||
onChange={(e) =>
|
||||
setRoundSettings((prev) => ({
|
||||
...prev,
|
||||
max_file_size_mb: parseInt(e.target.value) || 500,
|
||||
}))
|
||||
}
|
||||
className="max-w-[150px]"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="space-y-0.5">
|
||||
<Label className="text-sm font-medium">Enable File Versioning</Label>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Keep previous versions when files are replaced
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={Boolean(roundSettings.file_versioning)}
|
||||
onCheckedChange={(checked) =>
|
||||
setRoundSettings((prev) => ({
|
||||
...prev,
|
||||
file_versioning: checked,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
{!!roundSettings.file_versioning && (
|
||||
<div className="space-y-2 pl-4 border-l-2 border-muted">
|
||||
<Label className="text-sm">Max Versions per File</Label>
|
||||
<Input
|
||||
type="number"
|
||||
min={2}
|
||||
max={20}
|
||||
value={Number(roundSettings.max_file_versions || 5)}
|
||||
onChange={(e) =>
|
||||
setRoundSettings((prev) => ({
|
||||
...prev,
|
||||
max_file_versions: parseInt(e.target.value) || 5,
|
||||
}))
|
||||
}
|
||||
className="max-w-[120px]"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Availability Settings */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg flex items-center gap-2">
|
||||
<Calendar className="h-5 w-5" />
|
||||
Jury Availability Settings
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Configure how jury member availability affects assignments
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="space-y-0.5">
|
||||
<Label className="text-sm font-medium">Require Availability</Label>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Jury members must set availability before receiving assignments
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={Boolean(roundSettings.require_availability)}
|
||||
onCheckedChange={(checked) =>
|
||||
setRoundSettings((prev) => ({
|
||||
...prev,
|
||||
require_availability: checked,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm">Availability Mode</Label>
|
||||
<Select
|
||||
value={String(roundSettings.availability_mode || 'soft_penalty')}
|
||||
onValueChange={(v) =>
|
||||
setRoundSettings((prev) => ({
|
||||
...prev,
|
||||
availability_mode: v,
|
||||
}))
|
||||
}
|
||||
>
|
||||
<SelectTrigger className="max-w-[250px]">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="hard_block">
|
||||
Hard Block (unavailable jurors excluded)
|
||||
</SelectItem>
|
||||
<SelectItem value="soft_penalty">
|
||||
Soft Penalty (reduce assignment priority)
|
||||
</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm">
|
||||
Availability Weight ({Number(roundSettings.availability_weight || 50)}%)
|
||||
</Label>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
How much weight to give availability when using soft penalty mode
|
||||
</p>
|
||||
<Slider
|
||||
value={[Number(roundSettings.availability_weight || 50)]}
|
||||
min={0}
|
||||
max={100}
|
||||
step={5}
|
||||
onValueChange={([value]) =>
|
||||
setRoundSettings((prev) => ({
|
||||
...prev,
|
||||
availability_weight: value,
|
||||
}))
|
||||
}
|
||||
className="max-w-xs"
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Evaluation Criteria */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
'use client'
|
||||
|
||||
import { Suspense, use, useState, useEffect } from 'react'
|
||||
import { Suspense, use, useState, useEffect, useCallback } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Button } from '@/components/ui/button'
|
||||
|
|
@ -15,6 +15,15 @@ import {
|
|||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
||||
import { Progress } from '@/components/ui/progress'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
import { toast } from 'sonner'
|
||||
import {
|
||||
ArrowLeft,
|
||||
|
|
@ -28,6 +37,10 @@ import {
|
|||
AlertCircle,
|
||||
ExternalLink,
|
||||
RefreshCw,
|
||||
QrCode,
|
||||
Settings2,
|
||||
Scale,
|
||||
UserCheck,
|
||||
} from 'lucide-react'
|
||||
import {
|
||||
DndContext,
|
||||
|
|
@ -46,6 +59,8 @@ import {
|
|||
verticalListSortingStrategy,
|
||||
} from '@dnd-kit/sortable'
|
||||
import { CSS } from '@dnd-kit/utilities'
|
||||
import { useLiveVotingSSE, type VoteUpdate } from '@/hooks/use-live-voting-sse'
|
||||
import { QRCodeDisplay } from '@/components/shared/qr-code-display'
|
||||
|
||||
interface PageProps {
|
||||
params: Promise<{ id: string }>
|
||||
|
|
@ -119,11 +134,38 @@ function LiveVotingContent({ roundId }: { roundId: string }) {
|
|||
const [projectOrder, setProjectOrder] = useState<string[]>([])
|
||||
const [countdown, setCountdown] = useState<number | null>(null)
|
||||
const [votingDuration, setVotingDuration] = useState(30)
|
||||
const [liveVoteCount, setLiveVoteCount] = useState<number | null>(null)
|
||||
const [liveAvgScore, setLiveAvgScore] = useState<number | null>(null)
|
||||
|
||||
// Fetch session data
|
||||
// Fetch session data - reduced polling since SSE handles real-time
|
||||
const { data: sessionData, isLoading, refetch } = trpc.liveVoting.getSession.useQuery(
|
||||
{ roundId },
|
||||
{ refetchInterval: 2000 } // Poll every 2 seconds
|
||||
{ refetchInterval: 5000 }
|
||||
)
|
||||
|
||||
// SSE for real-time vote updates
|
||||
const onVoteUpdate = useCallback((data: VoteUpdate) => {
|
||||
setLiveVoteCount(data.totalVotes)
|
||||
setLiveAvgScore(data.averageScore)
|
||||
}, [])
|
||||
|
||||
const onSessionStatus = useCallback(() => {
|
||||
refetch()
|
||||
}, [refetch])
|
||||
|
||||
const onProjectChange = useCallback(() => {
|
||||
setLiveVoteCount(null)
|
||||
setLiveAvgScore(null)
|
||||
refetch()
|
||||
}, [refetch])
|
||||
|
||||
const { isConnected } = useLiveVotingSSE(
|
||||
sessionData?.id || null,
|
||||
{
|
||||
onVoteUpdate,
|
||||
onSessionStatus,
|
||||
onProjectChange,
|
||||
}
|
||||
)
|
||||
|
||||
// Mutations
|
||||
|
|
@ -166,6 +208,26 @@ function LiveVotingContent({ roundId }: { roundId: string }) {
|
|||
},
|
||||
})
|
||||
|
||||
const updateSessionConfig = trpc.liveVoting.updateSessionConfig.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Session config updated')
|
||||
refetch()
|
||||
},
|
||||
onError: (error) => {
|
||||
toast.error(error.message)
|
||||
},
|
||||
})
|
||||
|
||||
const updatePresentationSettings = trpc.liveVoting.updatePresentationSettings.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Presentation settings updated')
|
||||
refetch()
|
||||
},
|
||||
onError: (error) => {
|
||||
toast.error(error.message)
|
||||
},
|
||||
})
|
||||
|
||||
const sensors = useSensors(
|
||||
useSensor(PointerSensor),
|
||||
useSensor(KeyboardSensor, {
|
||||
|
|
@ -446,61 +508,185 @@ function LiveVotingContent({ roundId }: { roundId: string }) {
|
|||
<CardTitle className="flex items-center gap-2">
|
||||
<Users className="h-5 w-5" />
|
||||
Current Votes
|
||||
{isConnected && (
|
||||
<span className="h-2 w-2 rounded-full bg-green-500 animate-pulse" />
|
||||
)}
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{sessionData.currentVotes.length === 0 ? (
|
||||
<p className="text-muted-foreground text-center py-4">
|
||||
No votes yet
|
||||
</p>
|
||||
) : (
|
||||
<div className="space-y-2">
|
||||
<div className="flex justify-between text-sm">
|
||||
<span className="text-muted-foreground">Total votes</span>
|
||||
<span className="font-medium">
|
||||
{sessionData.currentVotes.length}
|
||||
</span>
|
||||
{(() => {
|
||||
const voteCount = liveVoteCount ?? sessionData.currentVotes.length
|
||||
const avgScore = liveAvgScore ?? (
|
||||
sessionData.currentVotes.length > 0
|
||||
? sessionData.currentVotes.reduce((sum, v) => sum + v.score, 0) / sessionData.currentVotes.length
|
||||
: null
|
||||
)
|
||||
if (voteCount === 0) {
|
||||
return (
|
||||
<p className="text-muted-foreground text-center py-4">
|
||||
No votes yet
|
||||
</p>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<div className="flex justify-between text-sm">
|
||||
<span className="text-muted-foreground">Total votes</span>
|
||||
<span className="font-medium">{voteCount}</span>
|
||||
</div>
|
||||
<div className="flex justify-between text-sm">
|
||||
<span className="text-muted-foreground">Average score</span>
|
||||
<span className="font-medium">
|
||||
{avgScore !== null ? avgScore.toFixed(1) : '--'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex justify-between text-sm">
|
||||
<span className="text-muted-foreground">Average score</span>
|
||||
<span className="font-medium">
|
||||
{(
|
||||
sessionData.currentVotes.reduce(
|
||||
(sum, v) => sum + v.score,
|
||||
0
|
||||
) / sessionData.currentVotes.length
|
||||
).toFixed(1)}
|
||||
)
|
||||
})()}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Session Configuration */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<Settings2 className="h-5 w-5" />
|
||||
Session Config
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<Label htmlFor="audience-votes" className="text-sm">
|
||||
Audience Voting
|
||||
</Label>
|
||||
<Switch
|
||||
id="audience-votes"
|
||||
checked={!!sessionData.allowAudienceVotes}
|
||||
onCheckedChange={(checked) => {
|
||||
updateSessionConfig.mutate({
|
||||
sessionId: sessionData.id,
|
||||
allowAudienceVotes: checked,
|
||||
})
|
||||
}}
|
||||
disabled={isCompleted}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{sessionData.allowAudienceVotes && (
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm">Audience Weight</Label>
|
||||
<div className="flex items-center gap-2">
|
||||
<input
|
||||
type="range"
|
||||
min="0"
|
||||
max="50"
|
||||
value={(sessionData.audienceVoteWeight || 0) * 100}
|
||||
onChange={(e) => {
|
||||
updateSessionConfig.mutate({
|
||||
sessionId: sessionData.id,
|
||||
audienceVoteWeight: parseInt(e.target.value) / 100,
|
||||
})
|
||||
}}
|
||||
className="flex-1"
|
||||
disabled={isCompleted}
|
||||
/>
|
||||
<span className="text-sm font-medium w-12 text-right">
|
||||
{Math.round((sessionData.audienceVoteWeight || 0) * 100)}%
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm">Tie-Breaker Method</Label>
|
||||
<Select
|
||||
value={sessionData.tieBreakerMethod || 'admin_decides'}
|
||||
onValueChange={(v) => {
|
||||
updateSessionConfig.mutate({
|
||||
sessionId: sessionData.id,
|
||||
tieBreakerMethod: v as 'admin_decides' | 'highest_individual' | 'revote',
|
||||
})
|
||||
}}
|
||||
disabled={isCompleted}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="admin_decides">Admin Decides</SelectItem>
|
||||
<SelectItem value="highest_individual">Highest Individual Score</SelectItem>
|
||||
<SelectItem value="revote">Revote</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm">Score Display Format</Label>
|
||||
<Select
|
||||
value={
|
||||
(sessionData.presentationSettingsJson as Record<string, unknown>)?.scoreDisplayFormat as string || 'bar'
|
||||
}
|
||||
onValueChange={(v) => {
|
||||
const existing = (sessionData.presentationSettingsJson as Record<string, unknown>) || {}
|
||||
updatePresentationSettings.mutate({
|
||||
sessionId: sessionData.id,
|
||||
presentationSettingsJson: {
|
||||
...existing,
|
||||
scoreDisplayFormat: v as 'bar' | 'number' | 'radial',
|
||||
},
|
||||
})
|
||||
}}
|
||||
disabled={isCompleted}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="bar">Bar Chart</SelectItem>
|
||||
<SelectItem value="number">Number Only</SelectItem>
|
||||
<SelectItem value="radial">Radial</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Links */}
|
||||
{/* QR Codes & Links */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Voting Links</CardTitle>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<QrCode className="h-5 w-5" />
|
||||
Voting Links
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Share these links with participants
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-2">
|
||||
<Button variant="outline" className="w-full justify-start" asChild>
|
||||
<Link href={`/jury/live/${sessionData.id}`} target="_blank">
|
||||
<ExternalLink className="mr-2 h-4 w-4" />
|
||||
Jury Voting Page
|
||||
</Link>
|
||||
</Button>
|
||||
<Button variant="outline" className="w-full justify-start" asChild>
|
||||
<Link
|
||||
href={`/live-scores/${sessionData.id}`}
|
||||
target="_blank"
|
||||
>
|
||||
<ExternalLink className="mr-2 h-4 w-4" />
|
||||
Public Score Display
|
||||
</Link>
|
||||
</Button>
|
||||
<CardContent className="space-y-4">
|
||||
<QRCodeDisplay
|
||||
url={`${typeof window !== 'undefined' ? window.location.origin : ''}/jury/live/${sessionData.id}`}
|
||||
title="Jury Voting"
|
||||
size={160}
|
||||
/>
|
||||
<QRCodeDisplay
|
||||
url={`${typeof window !== 'undefined' ? window.location.origin : ''}/live-scores/${sessionData.id}`}
|
||||
title="Public Scoreboard"
|
||||
size={160}
|
||||
/>
|
||||
<div className="flex flex-col gap-2 pt-2 border-t">
|
||||
<Button variant="outline" className="w-full justify-start" asChild>
|
||||
<Link href={`/jury/live/${sessionData.id}`} target="_blank">
|
||||
<ExternalLink className="mr-2 h-4 w-4" />
|
||||
Open Jury Page
|
||||
</Link>
|
||||
</Button>
|
||||
<Button variant="outline" className="w-full justify-start" asChild>
|
||||
<Link href={`/live-scores/${sessionData.id}`} target="_blank">
|
||||
<ExternalLink className="mr-2 h-4 w-4" />
|
||||
Open Scoreboard
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@ import {
|
|||
ListChecks,
|
||||
ClipboardCheck,
|
||||
Sparkles,
|
||||
LayoutTemplate,
|
||||
} from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
import { AssignProjectsDialog } from '@/components/admin/assign-projects-dialog'
|
||||
|
|
@ -126,6 +127,21 @@ function RoundDetailContent({ roundId }: { roundId: string }) {
|
|||
const startJob = trpc.filtering.startJob.useMutation()
|
||||
const finalizeResults = trpc.filtering.finalizeResults.useMutation()
|
||||
|
||||
// Save as template
|
||||
const saveAsTemplate = trpc.roundTemplate.createFromRound.useMutation({
|
||||
onSuccess: (data) => {
|
||||
toast.success('Saved as template', {
|
||||
action: {
|
||||
label: 'View',
|
||||
onClick: () => router.push(`/admin/round-templates/${data.id}`),
|
||||
},
|
||||
})
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err.message)
|
||||
},
|
||||
})
|
||||
|
||||
// AI summary bulk generation
|
||||
const bulkSummaries = trpc.evaluation.generateBulkSummaries.useMutation({
|
||||
onSuccess: (data) => {
|
||||
|
|
@ -794,6 +810,24 @@ function RoundDetailContent({ roundId }: { roundId: string }) {
|
|||
)}
|
||||
{bulkSummaries.isPending ? 'Generating...' : 'Generate AI Summaries'}
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() =>
|
||||
saveAsTemplate.mutate({
|
||||
roundId: round.id,
|
||||
name: `${round.name} Template`,
|
||||
})
|
||||
}
|
||||
disabled={saveAsTemplate.isPending}
|
||||
>
|
||||
{saveAsTemplate.isPending ? (
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<LayoutTemplate className="mr-2 h-4 w-4" />
|
||||
)}
|
||||
Save as Template
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
|
|
|
|||
|
|
@ -34,7 +34,8 @@ import {
|
|||
FormMessage,
|
||||
} from '@/components/ui/form'
|
||||
import { RoundTypeSettings } from '@/components/forms/round-type-settings'
|
||||
import { ArrowLeft, Loader2, AlertCircle, Bell } from 'lucide-react'
|
||||
import { ArrowLeft, Loader2, AlertCircle, Bell, LayoutTemplate } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
import { DateTimePicker } from '@/components/ui/datetime-picker'
|
||||
|
||||
// Available notification types for teams entering a round
|
||||
|
|
@ -71,9 +72,38 @@ function CreateRoundContent() {
|
|||
const [roundType, setRoundType] = useState<'FILTERING' | 'EVALUATION' | 'LIVE_EVENT'>('EVALUATION')
|
||||
const [roundSettings, setRoundSettings] = useState<Record<string, unknown>>({})
|
||||
const [entryNotificationType, setEntryNotificationType] = useState<string>('')
|
||||
const [selectedTemplateId, setSelectedTemplateId] = useState<string>('')
|
||||
|
||||
const utils = trpc.useUtils()
|
||||
const { data: programs, isLoading: loadingPrograms } = trpc.program.list.useQuery()
|
||||
const { data: templates } = trpc.roundTemplate.list.useQuery()
|
||||
|
||||
const loadTemplate = (templateId: string) => {
|
||||
if (!templateId || !templates) return
|
||||
const template = templates.find((t) => t.id === templateId)
|
||||
if (!template) return
|
||||
|
||||
// Apply template settings
|
||||
const typeMap: Record<string, 'FILTERING' | 'EVALUATION' | 'LIVE_EVENT'> = {
|
||||
EVALUATION: 'EVALUATION',
|
||||
SELECTION: 'EVALUATION',
|
||||
FINAL: 'EVALUATION',
|
||||
LIVE_VOTING: 'LIVE_EVENT',
|
||||
FILTERING: 'FILTERING',
|
||||
}
|
||||
setRoundType(typeMap[template.roundType] || 'EVALUATION')
|
||||
|
||||
if (template.settingsJson && typeof template.settingsJson === 'object') {
|
||||
setRoundSettings(template.settingsJson as Record<string, unknown>)
|
||||
}
|
||||
|
||||
if (template.name) {
|
||||
form.setValue('name', template.name)
|
||||
}
|
||||
|
||||
setSelectedTemplateId(templateId)
|
||||
toast.success(`Loaded template: ${template.name}`)
|
||||
}
|
||||
|
||||
const createRound = trpc.round.create.useMutation({
|
||||
onSuccess: (data) => {
|
||||
|
|
@ -155,6 +185,56 @@ function CreateRoundContent() {
|
|||
</p>
|
||||
</div>
|
||||
|
||||
{/* Template Selector */}
|
||||
{templates && templates.length > 0 && (
|
||||
<Card>
|
||||
<CardHeader className="pb-3">
|
||||
<CardTitle className="text-lg flex items-center gap-2">
|
||||
<LayoutTemplate className="h-5 w-5" />
|
||||
Start from Template
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Load settings from a saved template to get started quickly
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex items-center gap-3">
|
||||
<Select
|
||||
value={selectedTemplateId}
|
||||
onValueChange={loadTemplate}
|
||||
>
|
||||
<SelectTrigger className="w-full max-w-sm">
|
||||
<SelectValue placeholder="Select a template..." />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{templates.map((t) => (
|
||||
<SelectItem key={t.id} value={t.id}>
|
||||
{t.name}
|
||||
{t.description ? ` - ${t.description}` : ''}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{selectedTemplateId && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setSelectedTemplateId('')
|
||||
setRoundType('EVALUATION')
|
||||
setRoundSettings({})
|
||||
form.reset()
|
||||
toast.info('Template cleared')
|
||||
}}
|
||||
>
|
||||
Clear
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Form */}
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
|
||||
|
|
|
|||
|
|
@ -0,0 +1,431 @@
|
|||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from '@/components/ui/dialog'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from '@/components/ui/table'
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@/components/ui/alert-dialog'
|
||||
import {
|
||||
ArrowLeft,
|
||||
Plus,
|
||||
Pencil,
|
||||
Trash2,
|
||||
Copy,
|
||||
Loader2,
|
||||
LayoutTemplate,
|
||||
} from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
import { formatDate } from '@/lib/utils'
|
||||
|
||||
const ROUND_TYPES = [
|
||||
{ value: 'EVALUATION', label: 'Evaluation' },
|
||||
{ value: 'FILTERING', label: 'Filtering' },
|
||||
{ value: 'LIVE_EVENT', label: 'Live Event' },
|
||||
]
|
||||
|
||||
interface TemplateFormData {
|
||||
name: string
|
||||
description: string
|
||||
roundType: string
|
||||
programId: string
|
||||
criteriaJson: string
|
||||
settingsJson: string
|
||||
}
|
||||
|
||||
const defaultForm: TemplateFormData = {
|
||||
name: '',
|
||||
description: '',
|
||||
roundType: 'EVALUATION',
|
||||
programId: '',
|
||||
criteriaJson: '[]',
|
||||
settingsJson: '{}',
|
||||
}
|
||||
|
||||
export default function RoundTemplatesPage() {
|
||||
const [dialogOpen, setDialogOpen] = useState(false)
|
||||
const [editingId, setEditingId] = useState<string | null>(null)
|
||||
const [deleteId, setDeleteId] = useState<string | null>(null)
|
||||
const [formData, setFormData] = useState<TemplateFormData>(defaultForm)
|
||||
|
||||
const utils = trpc.useUtils()
|
||||
|
||||
const { data: templates, isLoading } = trpc.roundTemplate.list.useQuery()
|
||||
const { data: programs } = trpc.program.list.useQuery()
|
||||
|
||||
const createMutation = trpc.roundTemplate.create.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.roundTemplate.list.invalidate()
|
||||
toast.success('Template created')
|
||||
closeDialog()
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const updateMutation = trpc.roundTemplate.update.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.roundTemplate.list.invalidate()
|
||||
toast.success('Template updated')
|
||||
closeDialog()
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const deleteMutation = trpc.roundTemplate.delete.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.roundTemplate.list.invalidate()
|
||||
toast.success('Template deleted')
|
||||
setDeleteId(null)
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const closeDialog = () => {
|
||||
setDialogOpen(false)
|
||||
setEditingId(null)
|
||||
setFormData(defaultForm)
|
||||
}
|
||||
|
||||
const openEdit = (template: Record<string, unknown>) => {
|
||||
setEditingId(String(template.id))
|
||||
setFormData({
|
||||
name: String(template.name || ''),
|
||||
description: String(template.description || ''),
|
||||
roundType: String(template.roundType || 'EVALUATION'),
|
||||
programId: String(template.programId || ''),
|
||||
criteriaJson: JSON.stringify(template.criteriaJson || [], null, 2),
|
||||
settingsJson: JSON.stringify(template.settingsJson || {}, null, 2),
|
||||
})
|
||||
setDialogOpen(true)
|
||||
}
|
||||
|
||||
const handleSubmit = () => {
|
||||
let criteriaJson: unknown
|
||||
let settingsJson: unknown
|
||||
|
||||
try {
|
||||
criteriaJson = JSON.parse(formData.criteriaJson)
|
||||
} catch {
|
||||
toast.error('Invalid criteria JSON')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
settingsJson = JSON.parse(formData.settingsJson)
|
||||
} catch {
|
||||
toast.error('Invalid settings JSON')
|
||||
return
|
||||
}
|
||||
|
||||
const payload = {
|
||||
name: formData.name,
|
||||
description: formData.description || undefined,
|
||||
roundType: formData.roundType as 'FILTERING' | 'EVALUATION' | 'LIVE_EVENT',
|
||||
programId: formData.programId || undefined,
|
||||
criteriaJson,
|
||||
settingsJson,
|
||||
}
|
||||
|
||||
if (editingId) {
|
||||
updateMutation.mutate({ id: editingId, ...payload })
|
||||
} else {
|
||||
createMutation.mutate(payload)
|
||||
}
|
||||
}
|
||||
|
||||
const isPending = createMutation.isPending || updateMutation.isPending
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center gap-4">
|
||||
<Button variant="ghost" asChild className="-ml-4">
|
||||
<Link href="/admin/settings">
|
||||
<ArrowLeft className="mr-2 h-4 w-4" />
|
||||
Back to Settings
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold tracking-tight">Round Templates</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Create reusable templates for round configuration
|
||||
</p>
|
||||
</div>
|
||||
<Dialog open={dialogOpen} onOpenChange={(open) => !open && closeDialog()}>
|
||||
<DialogTrigger asChild>
|
||||
<Button onClick={() => { setFormData(defaultForm); setEditingId(null); setDialogOpen(true) }}>
|
||||
<Plus className="mr-2 h-4 w-4" />
|
||||
Create Template
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="max-w-lg max-h-[90vh] overflow-y-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{editingId ? 'Edit Template' : 'Create Template'}</DialogTitle>
|
||||
<DialogDescription>
|
||||
{editingId
|
||||
? 'Update the template configuration.'
|
||||
: 'Define a reusable round template.'}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label>Template Name</Label>
|
||||
<Input
|
||||
placeholder="e.g., Standard Evaluation Round"
|
||||
value={formData.name}
|
||||
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Description</Label>
|
||||
<Textarea
|
||||
placeholder="Template description..."
|
||||
value={formData.description}
|
||||
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
|
||||
rows={2}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Round Type</Label>
|
||||
<Select
|
||||
value={formData.roundType}
|
||||
onValueChange={(v) => setFormData({ ...formData, roundType: v })}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{ROUND_TYPES.map((rt) => (
|
||||
<SelectItem key={rt.value} value={rt.value}>
|
||||
{rt.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Program (optional)</Label>
|
||||
<Select
|
||||
value={formData.programId}
|
||||
onValueChange={(v) => setFormData({ ...formData, programId: v === '__none__' ? '' : v })}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Global (all programs)" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="__none__">Global (all programs)</SelectItem>
|
||||
{(programs as Array<{ id: string; name: string }> | undefined)?.map((p) => (
|
||||
<SelectItem key={p.id} value={p.id}>
|
||||
{p.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Criteria (JSON)</Label>
|
||||
<Textarea
|
||||
value={formData.criteriaJson}
|
||||
onChange={(e) => setFormData({ ...formData, criteriaJson: e.target.value })}
|
||||
rows={5}
|
||||
className="font-mono text-sm"
|
||||
placeholder='[{"name":"Innovation","maxScore":10,"weight":1}]'
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Settings (JSON)</Label>
|
||||
<Textarea
|
||||
value={formData.settingsJson}
|
||||
onChange={(e) => setFormData({ ...formData, settingsJson: e.target.value })}
|
||||
rows={3}
|
||||
className="font-mono text-sm"
|
||||
placeholder='{"requiredReviews":3}'
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={closeDialog}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleSubmit} disabled={!formData.name || isPending}>
|
||||
{isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
{editingId ? 'Update' : 'Create'}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
|
||||
{/* Templates list */}
|
||||
{isLoading ? (
|
||||
<TemplatesSkeleton />
|
||||
) : templates && (templates as unknown[]).length > 0 ? (
|
||||
<Card>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Name</TableHead>
|
||||
<TableHead className="hidden md:table-cell">Round Type</TableHead>
|
||||
<TableHead className="hidden md:table-cell">Program</TableHead>
|
||||
<TableHead className="hidden lg:table-cell">Created</TableHead>
|
||||
<TableHead className="text-right">Actions</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{(templates as Array<Record<string, unknown>>).map((template) => (
|
||||
<TableRow key={String(template.id)}>
|
||||
<TableCell>
|
||||
<div>
|
||||
<p className="font-medium">{String(template.name)}</p>
|
||||
{!!template.description && (
|
||||
<p className="text-sm text-muted-foreground line-clamp-1">
|
||||
{String(template.description)}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="hidden md:table-cell">
|
||||
<Badge variant="secondary">
|
||||
{String(template.roundType || 'EVALUATION')}
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell className="hidden md:table-cell">
|
||||
{template.program
|
||||
? String((template.program as Record<string, unknown>).name)
|
||||
: 'Global'}
|
||||
</TableCell>
|
||||
<TableCell className="hidden lg:table-cell text-sm text-muted-foreground">
|
||||
{template.createdAt ? formatDate(template.createdAt as string | Date) : ''}
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<div className="flex items-center justify-end gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => openEdit(template)}
|
||||
>
|
||||
<Pencil className="h-4 w-4" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => setDeleteId(String(template.id))}
|
||||
>
|
||||
<Trash2 className="h-4 w-4 text-destructive" />
|
||||
</Button>
|
||||
</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</Card>
|
||||
) : (
|
||||
<Card>
|
||||
<CardContent className="flex flex-col items-center justify-center py-12 text-center">
|
||||
<LayoutTemplate className="h-12 w-12 text-muted-foreground/50" />
|
||||
<p className="mt-2 font-medium">No templates yet</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Create a template to reuse round configurations across programs.
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Delete confirmation */}
|
||||
<AlertDialog open={!!deleteId} onOpenChange={() => setDeleteId(null)}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Delete Template</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
Are you sure you want to delete this template? This action cannot be undone.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
onClick={() => deleteId && deleteMutation.mutate({ id: deleteId })}
|
||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||
>
|
||||
{deleteMutation.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Delete
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function TemplatesSkeleton() {
|
||||
return (
|
||||
<Card>
|
||||
<CardContent className="p-6">
|
||||
<div className="space-y-4">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<div key={i} className="flex items-center gap-4">
|
||||
<Skeleton className="h-4 w-40" />
|
||||
<Skeleton className="h-6 w-24" />
|
||||
<Skeleton className="h-4 w-20" />
|
||||
<Skeleton className="h-8 w-16 ml-auto" />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,706 @@
|
|||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from '@/components/ui/dialog'
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from '@/components/ui/table'
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@/components/ui/alert-dialog'
|
||||
import {
|
||||
Collapsible,
|
||||
CollapsibleContent,
|
||||
CollapsibleTrigger,
|
||||
} from '@/components/ui/collapsible'
|
||||
import {
|
||||
ArrowLeft,
|
||||
Plus,
|
||||
Pencil,
|
||||
Trash2,
|
||||
Loader2,
|
||||
Webhook,
|
||||
Send,
|
||||
ChevronDown,
|
||||
ChevronUp,
|
||||
Copy,
|
||||
Eye,
|
||||
EyeOff,
|
||||
RefreshCw,
|
||||
CheckCircle2,
|
||||
XCircle,
|
||||
} from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
import { formatDate } from '@/lib/utils'
|
||||
|
||||
interface WebhookFormData {
|
||||
name: string
|
||||
url: string
|
||||
events: string[]
|
||||
headers: Array<{ key: string; value: string }>
|
||||
maxRetries: number
|
||||
}
|
||||
|
||||
const defaultForm: WebhookFormData = {
|
||||
name: '',
|
||||
url: '',
|
||||
events: [],
|
||||
headers: [],
|
||||
maxRetries: 3,
|
||||
}
|
||||
|
||||
export default function WebhooksPage() {
|
||||
const [dialogOpen, setDialogOpen] = useState(false)
|
||||
const [editingId, setEditingId] = useState<string | null>(null)
|
||||
const [deleteId, setDeleteId] = useState<string | null>(null)
|
||||
const [formData, setFormData] = useState<WebhookFormData>(defaultForm)
|
||||
const [expandedWebhook, setExpandedWebhook] = useState<string | null>(null)
|
||||
const [revealedSecrets, setRevealedSecrets] = useState<Set<string>>(new Set())
|
||||
const [deliveryPage, setDeliveryPage] = useState(1)
|
||||
|
||||
const utils = trpc.useUtils()
|
||||
|
||||
const { data: webhooks, isLoading } = trpc.webhook.list.useQuery()
|
||||
const { data: availableEvents } = trpc.webhook.getAvailableEvents.useQuery()
|
||||
|
||||
const { data: deliveryLog, isLoading: loadingDeliveries } =
|
||||
trpc.webhook.getDeliveryLog.useQuery(
|
||||
{ webhookId: expandedWebhook!, page: deliveryPage, pageSize: 10 },
|
||||
{ enabled: !!expandedWebhook }
|
||||
)
|
||||
|
||||
const createMutation = trpc.webhook.create.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.webhook.list.invalidate()
|
||||
toast.success('Webhook created')
|
||||
closeDialog()
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const updateMutation = trpc.webhook.update.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.webhook.list.invalidate()
|
||||
toast.success('Webhook updated')
|
||||
closeDialog()
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const deleteMutation = trpc.webhook.delete.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.webhook.list.invalidate()
|
||||
toast.success('Webhook deleted')
|
||||
setDeleteId(null)
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const testMutation = trpc.webhook.test.useMutation({
|
||||
onSuccess: (data) => {
|
||||
const status = (data as Record<string, unknown>)?.status
|
||||
if (status === 'DELIVERED') {
|
||||
toast.success('Test webhook delivered successfully')
|
||||
} else {
|
||||
toast.error(`Test delivery status: ${String(status || 'unknown')}`)
|
||||
}
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const regenerateSecretMutation = trpc.webhook.regenerateSecret.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.webhook.list.invalidate()
|
||||
toast.success('Secret regenerated')
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const closeDialog = () => {
|
||||
setDialogOpen(false)
|
||||
setEditingId(null)
|
||||
setFormData(defaultForm)
|
||||
}
|
||||
|
||||
const openEdit = (webhook: Record<string, unknown>) => {
|
||||
setEditingId(String(webhook.id))
|
||||
const headers = webhook.headers as Array<{ key: string; value: string }> | undefined
|
||||
setFormData({
|
||||
name: String(webhook.name || ''),
|
||||
url: String(webhook.url || ''),
|
||||
events: Array.isArray(webhook.events) ? webhook.events.map(String) : [],
|
||||
headers: Array.isArray(headers) ? headers : [],
|
||||
maxRetries: Number(webhook.maxRetries || 3),
|
||||
})
|
||||
setDialogOpen(true)
|
||||
}
|
||||
|
||||
const toggleEvent = (event: string) => {
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
events: prev.events.includes(event)
|
||||
? prev.events.filter((e) => e !== event)
|
||||
: [...prev.events, event],
|
||||
}))
|
||||
}
|
||||
|
||||
const addHeader = () => {
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
headers: [...prev.headers, { key: '', value: '' }],
|
||||
}))
|
||||
}
|
||||
|
||||
const removeHeader = (index: number) => {
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
headers: prev.headers.filter((_, i) => i !== index),
|
||||
}))
|
||||
}
|
||||
|
||||
const updateHeader = (index: number, field: 'key' | 'value', value: string) => {
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
headers: prev.headers.map((h, i) =>
|
||||
i === index ? { ...h, [field]: value } : h
|
||||
),
|
||||
}))
|
||||
}
|
||||
|
||||
const handleSubmit = () => {
|
||||
if (!formData.name || !formData.url || formData.events.length === 0) {
|
||||
toast.error('Please fill in name, URL, and select at least one event')
|
||||
return
|
||||
}
|
||||
|
||||
const payload = {
|
||||
name: formData.name,
|
||||
url: formData.url,
|
||||
events: formData.events,
|
||||
headers: formData.headers.filter((h) => h.key) as Record<string, string>[] | undefined,
|
||||
maxRetries: formData.maxRetries,
|
||||
}
|
||||
|
||||
if (editingId) {
|
||||
updateMutation.mutate({ id: editingId, ...payload })
|
||||
} else {
|
||||
createMutation.mutate(payload)
|
||||
}
|
||||
}
|
||||
|
||||
const copySecret = (secret: string) => {
|
||||
navigator.clipboard.writeText(secret)
|
||||
toast.success('Secret copied to clipboard')
|
||||
}
|
||||
|
||||
const toggleSecretVisibility = (id: string) => {
|
||||
setRevealedSecrets((prev) => {
|
||||
const next = new Set(prev)
|
||||
if (next.has(id)) next.delete(id)
|
||||
else next.add(id)
|
||||
return next
|
||||
})
|
||||
}
|
||||
|
||||
const toggleDeliveryLog = (webhookId: string) => {
|
||||
if (expandedWebhook === webhookId) {
|
||||
setExpandedWebhook(null)
|
||||
} else {
|
||||
setExpandedWebhook(webhookId)
|
||||
setDeliveryPage(1)
|
||||
}
|
||||
}
|
||||
|
||||
const events = availableEvents || []
|
||||
const isPending = createMutation.isPending || updateMutation.isPending
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center gap-4">
|
||||
<Button variant="ghost" asChild className="-ml-4">
|
||||
<Link href="/admin/settings">
|
||||
<ArrowLeft className="mr-2 h-4 w-4" />
|
||||
Back to Settings
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold tracking-tight">Webhooks</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Configure webhook endpoints for platform events
|
||||
</p>
|
||||
</div>
|
||||
<Dialog open={dialogOpen} onOpenChange={(open) => !open && closeDialog()}>
|
||||
<DialogTrigger asChild>
|
||||
<Button onClick={() => { setFormData(defaultForm); setEditingId(null); setDialogOpen(true) }}>
|
||||
<Plus className="mr-2 h-4 w-4" />
|
||||
Add Webhook
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="max-w-lg max-h-[90vh] overflow-y-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{editingId ? 'Edit Webhook' : 'Add Webhook'}</DialogTitle>
|
||||
<DialogDescription>
|
||||
Configure a webhook endpoint to receive platform events.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label>Name</Label>
|
||||
<Input
|
||||
placeholder="e.g., Slack Notifications"
|
||||
value={formData.name}
|
||||
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>URL</Label>
|
||||
<Input
|
||||
placeholder="https://example.com/webhook"
|
||||
type="url"
|
||||
value={formData.url}
|
||||
onChange={(e) => setFormData({ ...formData, url: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Events</Label>
|
||||
<div className="grid gap-2 sm:grid-cols-2">
|
||||
{(events as string[]).map((event) => (
|
||||
<div key={event} className="flex items-center gap-2">
|
||||
<Checkbox
|
||||
id={`event-${event}`}
|
||||
checked={formData.events.includes(event)}
|
||||
onCheckedChange={() => toggleEvent(event)}
|
||||
/>
|
||||
<label
|
||||
htmlFor={`event-${event}`}
|
||||
className="text-sm cursor-pointer"
|
||||
>
|
||||
{event}
|
||||
</label>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<Label>Custom Headers</Label>
|
||||
<Button variant="outline" size="sm" onClick={addHeader}>
|
||||
<Plus className="mr-1 h-3 w-3" />
|
||||
Add
|
||||
</Button>
|
||||
</div>
|
||||
{formData.headers.map((header, i) => (
|
||||
<div key={i} className="flex gap-2">
|
||||
<Input
|
||||
placeholder="Header name"
|
||||
value={header.key}
|
||||
onChange={(e) => updateHeader(i, 'key', e.target.value)}
|
||||
className="flex-1"
|
||||
/>
|
||||
<Input
|
||||
placeholder="Value"
|
||||
value={header.value}
|
||||
onChange={(e) => updateHeader(i, 'value', e.target.value)}
|
||||
className="flex-1"
|
||||
/>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => removeHeader(i)}
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Max Retries</Label>
|
||||
<Input
|
||||
type="number"
|
||||
min={0}
|
||||
max={10}
|
||||
value={formData.maxRetries}
|
||||
onChange={(e) =>
|
||||
setFormData({ ...formData, maxRetries: parseInt(e.target.value) || 0 })
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{editingId && (
|
||||
<div className="flex items-center gap-2">
|
||||
<Switch
|
||||
id="webhook-active"
|
||||
checked={
|
||||
(webhooks as Array<Record<string, unknown>> | undefined)?.find(
|
||||
(w) => String(w.id) === editingId
|
||||
)?.isActive !== false
|
||||
}
|
||||
onCheckedChange={(checked) => {
|
||||
updateMutation.mutate({ id: editingId, isActive: checked })
|
||||
}}
|
||||
/>
|
||||
<label htmlFor="webhook-active" className="text-sm cursor-pointer">
|
||||
Active
|
||||
</label>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={closeDialog}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleSubmit} disabled={isPending}>
|
||||
{isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
{editingId ? 'Update' : 'Create'}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
|
||||
{/* Webhooks list */}
|
||||
{isLoading ? (
|
||||
<WebhooksSkeleton />
|
||||
) : webhooks && (webhooks as unknown[]).length > 0 ? (
|
||||
<div className="space-y-4">
|
||||
{(webhooks as Array<Record<string, unknown>>).map((webhook) => {
|
||||
const webhookId = String(webhook.id)
|
||||
const webhookEvents = Array.isArray(webhook.events) ? webhook.events : []
|
||||
const isActive = Boolean(webhook.isActive)
|
||||
const secret = String(webhook.secret || '')
|
||||
const isRevealed = revealedSecrets.has(webhookId)
|
||||
const isExpanded = expandedWebhook === webhookId
|
||||
const recentDelivered = Number(webhook.recentDelivered || 0)
|
||||
const recentFailed = Number(webhook.recentFailed || 0)
|
||||
const deliveryCount = webhook._count
|
||||
? Number((webhook._count as Record<string, unknown>).deliveries || 0)
|
||||
: 0
|
||||
|
||||
return (
|
||||
<Card key={webhookId}>
|
||||
<CardHeader>
|
||||
<div className="flex items-start justify-between">
|
||||
<div className="space-y-1">
|
||||
<CardTitle className="text-lg flex items-center gap-2">
|
||||
{String(webhook.name)}
|
||||
{isActive ? (
|
||||
<Badge variant="default" className="text-xs">Active</Badge>
|
||||
) : (
|
||||
<Badge variant="secondary" className="text-xs">Inactive</Badge>
|
||||
)}
|
||||
</CardTitle>
|
||||
<CardDescription className="font-mono text-xs break-all max-w-md truncate">
|
||||
{String(webhook.url)}
|
||||
</CardDescription>
|
||||
</div>
|
||||
<div className="flex items-center gap-4 text-sm text-muted-foreground">
|
||||
<span>{webhookEvents.length} events</span>
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="flex items-center gap-1">
|
||||
<CheckCircle2 className="h-3 w-3 text-green-500" />
|
||||
{recentDelivered}
|
||||
</span>
|
||||
<span className="flex items-center gap-1">
|
||||
<XCircle className="h-3 w-3 text-destructive" />
|
||||
{recentFailed}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
{/* Events */}
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{webhookEvents.map((event: unknown) => (
|
||||
<Badge key={String(event)} variant="outline" className="text-xs">
|
||||
{String(event)}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Secret */}
|
||||
{secret && (
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-xs text-muted-foreground">Secret:</span>
|
||||
<code className="text-xs bg-muted rounded px-2 py-1 font-mono">
|
||||
{isRevealed ? secret : '****************************'}
|
||||
</code>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-6 w-6"
|
||||
onClick={() => toggleSecretVisibility(webhookId)}
|
||||
>
|
||||
{isRevealed ? <EyeOff className="h-3 w-3" /> : <Eye className="h-3 w-3" />}
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-6 w-6"
|
||||
onClick={() => copySecret(secret)}
|
||||
>
|
||||
<Copy className="h-3 w-3" />
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => testMutation.mutate({ id: webhookId })}
|
||||
disabled={testMutation.isPending}
|
||||
>
|
||||
{testMutation.isPending ? (
|
||||
<Loader2 className="mr-2 h-3 w-3 animate-spin" />
|
||||
) : (
|
||||
<Send className="mr-2 h-3 w-3" />
|
||||
)}
|
||||
Test
|
||||
</Button>
|
||||
<Button variant="outline" size="sm" onClick={() => openEdit(webhook)}>
|
||||
<Pencil className="mr-2 h-3 w-3" />
|
||||
Edit
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => regenerateSecretMutation.mutate({ id: webhookId })}
|
||||
disabled={regenerateSecretMutation.isPending}
|
||||
>
|
||||
<RefreshCw className="mr-2 h-3 w-3" />
|
||||
Regenerate Secret
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => setDeleteId(webhookId)}
|
||||
className="text-destructive hover:text-destructive"
|
||||
>
|
||||
<Trash2 className="mr-2 h-3 w-3" />
|
||||
Delete
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Delivery log */}
|
||||
{deliveryCount > 0 && (
|
||||
<Collapsible
|
||||
open={isExpanded}
|
||||
onOpenChange={() => toggleDeliveryLog(webhookId)}
|
||||
>
|
||||
<CollapsibleTrigger asChild>
|
||||
<Button variant="ghost" size="sm" className="w-full justify-between">
|
||||
<span>Delivery Log ({deliveryCount})</span>
|
||||
{isExpanded ? (
|
||||
<ChevronUp className="h-4 w-4" />
|
||||
) : (
|
||||
<ChevronDown className="h-4 w-4" />
|
||||
)}
|
||||
</Button>
|
||||
</CollapsibleTrigger>
|
||||
<CollapsibleContent>
|
||||
<div className="mt-2 rounded-lg border overflow-hidden">
|
||||
{loadingDeliveries ? (
|
||||
<div className="p-4 space-y-2">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<Skeleton key={i} className="h-8 w-full" />
|
||||
))}
|
||||
</div>
|
||||
) : deliveryLog && deliveryLog.items.length > 0 ? (
|
||||
<>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Timestamp</TableHead>
|
||||
<TableHead>Event</TableHead>
|
||||
<TableHead>Status</TableHead>
|
||||
<TableHead>Response</TableHead>
|
||||
<TableHead>Attempts</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{(deliveryLog.items as Array<Record<string, unknown>>).map(
|
||||
(delivery, i) => (
|
||||
<TableRow key={i}>
|
||||
<TableCell className="font-mono text-xs">
|
||||
{delivery.createdAt
|
||||
? formatDate(delivery.createdAt as string | Date)
|
||||
: ''}
|
||||
</TableCell>
|
||||
<TableCell className="text-xs">
|
||||
{String(delivery.event || '')}
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
{delivery.status === 'DELIVERED' ? (
|
||||
<Badge variant="default" className="text-xs gap-1">
|
||||
<CheckCircle2 className="h-3 w-3" />
|
||||
OK
|
||||
</Badge>
|
||||
) : delivery.status === 'PENDING' ? (
|
||||
<Badge variant="secondary" className="text-xs gap-1">
|
||||
Pending
|
||||
</Badge>
|
||||
) : (
|
||||
<Badge variant="destructive" className="text-xs gap-1">
|
||||
<XCircle className="h-3 w-3" />
|
||||
Failed
|
||||
</Badge>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-xs font-mono">
|
||||
{String(delivery.responseStatus || '-')}
|
||||
</TableCell>
|
||||
<TableCell className="text-xs">
|
||||
{String(delivery.attempts || 0)}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
{deliveryLog.totalPages > 1 && (
|
||||
<div className="flex items-center justify-between p-2 border-t">
|
||||
<span className="text-xs text-muted-foreground">
|
||||
Page {deliveryLog.page} of {deliveryLog.totalPages}
|
||||
</span>
|
||||
<div className="flex gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
disabled={deliveryPage <= 1}
|
||||
onClick={() => setDeliveryPage((p) => p - 1)}
|
||||
>
|
||||
Previous
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
disabled={deliveryPage >= deliveryLog.totalPages}
|
||||
onClick={() => setDeliveryPage((p) => p + 1)}
|
||||
>
|
||||
Next
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
<div className="p-4 text-center text-sm text-muted-foreground">
|
||||
No deliveries recorded yet.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</CollapsibleContent>
|
||||
</Collapsible>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
) : (
|
||||
<Card>
|
||||
<CardContent className="flex flex-col items-center justify-center py-12 text-center">
|
||||
<Webhook className="h-12 w-12 text-muted-foreground/50" />
|
||||
<p className="mt-2 font-medium">No webhooks configured</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Add a webhook to receive real-time notifications about platform events.
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Delete confirmation */}
|
||||
<AlertDialog open={!!deleteId} onOpenChange={() => setDeleteId(null)}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Delete Webhook</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
Are you sure you want to delete this webhook? All delivery history will be lost.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
onClick={() => deleteId && deleteMutation.mutate({ id: deleteId })}
|
||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||
>
|
||||
{deleteMutation.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Delete
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function WebhooksSkeleton() {
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{[1, 2].map((i) => (
|
||||
<Card key={i}>
|
||||
<CardHeader>
|
||||
<Skeleton className="h-5 w-40" />
|
||||
<Skeleton className="h-4 w-64" />
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-3">
|
||||
<div className="flex gap-1">
|
||||
<Skeleton className="h-5 w-24" />
|
||||
<Skeleton className="h-5 w-20" />
|
||||
<Skeleton className="h-5 w-28" />
|
||||
</div>
|
||||
<Skeleton className="h-8 w-32" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,397 @@
|
|||
'use client'
|
||||
|
||||
import { useMemo, useState } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
import {
|
||||
ArrowLeft,
|
||||
GitCompare,
|
||||
MapPin,
|
||||
Users,
|
||||
FileText,
|
||||
CheckCircle2,
|
||||
Clock,
|
||||
} from 'lucide-react'
|
||||
|
||||
export default function CompareProjectsPage() {
|
||||
const [selectedRoundId, setSelectedRoundId] = useState<string>('')
|
||||
const [selectedIds, setSelectedIds] = useState<string[]>([])
|
||||
const [comparing, setComparing] = useState(false)
|
||||
|
||||
// Fetch all assigned projects
|
||||
const { data: assignments, isLoading: loadingAssignments } =
|
||||
trpc.assignment.myAssignments.useQuery({})
|
||||
|
||||
// Derive unique rounds from assignments
|
||||
const rounds = useMemo(() => {
|
||||
if (!assignments) return []
|
||||
const roundMap = new Map<string, { id: string; name: string }>()
|
||||
for (const a of assignments as Array<{ round: { id: string; name: string } }>) {
|
||||
if (a.round && !roundMap.has(a.round.id)) {
|
||||
roundMap.set(a.round.id, { id: a.round.id, name: String(a.round.name) })
|
||||
}
|
||||
}
|
||||
return Array.from(roundMap.values())
|
||||
}, [assignments])
|
||||
|
||||
// Auto-select the first round if none selected
|
||||
const activeRoundId = selectedRoundId || (rounds.length > 0 ? rounds[0].id : '')
|
||||
|
||||
// Filter assignments to current round
|
||||
const roundProjects = useMemo(() => {
|
||||
if (!assignments || !activeRoundId) return []
|
||||
return (assignments as Array<{
|
||||
project: Record<string, unknown>
|
||||
round: { id: string; name: string }
|
||||
evaluation?: Record<string, unknown>
|
||||
}>)
|
||||
.filter((a) => a.round.id === activeRoundId)
|
||||
.map((a) => ({
|
||||
...a.project,
|
||||
roundName: a.round.name,
|
||||
evaluation: a.evaluation,
|
||||
}))
|
||||
}, [assignments, activeRoundId])
|
||||
|
||||
// Fetch comparison data when comparing
|
||||
const { data: comparisonData, isLoading: loadingComparison } =
|
||||
trpc.evaluation.getMultipleForComparison.useQuery(
|
||||
{ projectIds: selectedIds, roundId: activeRoundId },
|
||||
{ enabled: comparing && selectedIds.length >= 2 && !!activeRoundId }
|
||||
)
|
||||
|
||||
const toggleProject = (projectId: string) => {
|
||||
setSelectedIds((prev) => {
|
||||
if (prev.includes(projectId)) {
|
||||
return prev.filter((id) => id !== projectId)
|
||||
}
|
||||
if (prev.length >= 3) return prev
|
||||
return [...prev, projectId]
|
||||
})
|
||||
setComparing(false)
|
||||
}
|
||||
|
||||
const handleCompare = () => {
|
||||
if (selectedIds.length >= 2) {
|
||||
setComparing(true)
|
||||
}
|
||||
}
|
||||
|
||||
const handleReset = () => {
|
||||
setComparing(false)
|
||||
setSelectedIds([])
|
||||
}
|
||||
|
||||
const handleRoundChange = (roundId: string) => {
|
||||
setSelectedRoundId(roundId)
|
||||
setSelectedIds([])
|
||||
setComparing(false)
|
||||
}
|
||||
|
||||
if (loadingAssignments) {
|
||||
return <CompareSkeleton />
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center gap-4">
|
||||
<Button variant="ghost" asChild className="-ml-4">
|
||||
<Link href="/jury/assignments">
|
||||
<ArrowLeft className="mr-2 h-4 w-4" />
|
||||
Back to Assignments
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between flex-wrap gap-4">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold tracking-tight">Compare Projects</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Select 2-3 projects from the same round to compare side by side
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
{comparing && (
|
||||
<Button variant="outline" onClick={handleReset}>
|
||||
Reset
|
||||
</Button>
|
||||
)}
|
||||
{!comparing && (
|
||||
<Button
|
||||
onClick={handleCompare}
|
||||
disabled={selectedIds.length < 2}
|
||||
>
|
||||
<GitCompare className="mr-2 h-4 w-4" />
|
||||
Compare ({selectedIds.length})
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Round selector */}
|
||||
{rounds.length > 1 && !comparing && (
|
||||
<div className="flex items-center gap-3">
|
||||
<span className="text-sm font-medium text-muted-foreground">Round:</span>
|
||||
<Select value={activeRoundId} onValueChange={handleRoundChange}>
|
||||
<SelectTrigger className="w-[280px]">
|
||||
<SelectValue placeholder="Select a round" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{rounds.map((r) => (
|
||||
<SelectItem key={r.id} value={r.id}>
|
||||
{r.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Project selector */}
|
||||
{!comparing && (
|
||||
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
|
||||
{roundProjects.map((project: Record<string, unknown>) => {
|
||||
const projectId = project.id as string
|
||||
const isSelected = selectedIds.includes(projectId)
|
||||
const isDisabled = !isSelected && selectedIds.length >= 3
|
||||
|
||||
return (
|
||||
<Card
|
||||
key={projectId}
|
||||
className={`cursor-pointer transition-colors ${
|
||||
isSelected
|
||||
? 'border-primary ring-2 ring-primary/20'
|
||||
: isDisabled
|
||||
? 'opacity-50'
|
||||
: 'hover:border-primary/50'
|
||||
}`}
|
||||
onClick={() => !isDisabled && toggleProject(projectId)}
|
||||
>
|
||||
<CardContent className="p-4">
|
||||
<div className="flex items-start gap-3">
|
||||
<Checkbox
|
||||
checked={isSelected}
|
||||
disabled={isDisabled}
|
||||
onCheckedChange={() => toggleProject(projectId)}
|
||||
className="mt-1"
|
||||
/>
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="font-medium truncate">
|
||||
{String(project.title || 'Untitled')}
|
||||
</p>
|
||||
<p className="text-sm text-muted-foreground truncate">
|
||||
{String(project.teamName || '')}
|
||||
</p>
|
||||
{!!project.roundName && (
|
||||
<Badge variant="secondary" className="mt-1 text-xs">
|
||||
{String(project.roundName)}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
{project.evaluation ? (
|
||||
<CheckCircle2 className="h-4 w-4 text-green-500 shrink-0" />
|
||||
) : (
|
||||
<Clock className="h-4 w-4 text-muted-foreground shrink-0" />
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{roundProjects.length === 0 && !loadingAssignments && (
|
||||
<Card>
|
||||
<CardContent className="flex flex-col items-center justify-center py-12 text-center">
|
||||
<GitCompare className="h-12 w-12 text-muted-foreground/50" />
|
||||
<p className="mt-2 font-medium">No projects assigned</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
You need at least 2 assigned projects to use the comparison feature.
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Comparison view */}
|
||||
{comparing && loadingComparison && <CompareSkeleton />}
|
||||
|
||||
{comparing && comparisonData && (
|
||||
<div
|
||||
className={`grid gap-4 grid-cols-1 ${
|
||||
selectedIds.length === 2 ? 'md:grid-cols-2' : 'md:grid-cols-2 lg:grid-cols-3'
|
||||
}`}
|
||||
>
|
||||
{(comparisonData as Array<{
|
||||
project: Record<string, unknown>
|
||||
evaluation: Record<string, unknown> | null
|
||||
assignmentId: string
|
||||
}>).map((item) => (
|
||||
<ComparisonCard
|
||||
key={String(item.project.id)}
|
||||
project={item.project}
|
||||
evaluation={item.evaluation}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function ComparisonCard({
|
||||
project,
|
||||
evaluation,
|
||||
}: {
|
||||
project: Record<string, unknown>
|
||||
evaluation: Record<string, unknown> | null
|
||||
}) {
|
||||
const tags = Array.isArray(project.tags) ? project.tags : []
|
||||
const files = Array.isArray(project.files) ? project.files : []
|
||||
const scores = evaluation?.scores as Record<string, unknown> | undefined
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">{String(project.title || 'Untitled')}</CardTitle>
|
||||
<CardDescription className="flex items-center gap-1">
|
||||
<Users className="h-3 w-3" />
|
||||
{String(project.teamName || 'N/A')}
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
{/* Country */}
|
||||
{!!project.country && (
|
||||
<div className="flex items-center gap-2 text-sm">
|
||||
<MapPin className="h-4 w-4 text-muted-foreground" />
|
||||
{String(project.country)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Description */}
|
||||
{!!project.description && (
|
||||
<div>
|
||||
<p className="text-xs font-medium text-muted-foreground mb-1">Description</p>
|
||||
<p className="text-sm line-clamp-4">{String(project.description)}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Tags */}
|
||||
{tags.length > 0 && (
|
||||
<div>
|
||||
<p className="text-xs font-medium text-muted-foreground mb-1">Tags</p>
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{tags.map((tag: unknown, i: number) => (
|
||||
<Badge key={i} variant="secondary" className="text-xs">
|
||||
{String(tag)}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Files */}
|
||||
{files.length > 0 && (
|
||||
<div>
|
||||
<p className="text-xs font-medium text-muted-foreground mb-1">
|
||||
Files ({files.length})
|
||||
</p>
|
||||
<div className="space-y-1">
|
||||
{files.map((file: unknown, i: number) => {
|
||||
const f = file as Record<string, unknown>
|
||||
return (
|
||||
<div key={i} className="flex items-center gap-2 text-sm">
|
||||
<FileText className="h-3 w-3 text-muted-foreground" />
|
||||
<span className="truncate">{String(f.fileName || f.fileType || 'File')}</span>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Evaluation scores */}
|
||||
{evaluation && (
|
||||
<div className="border-t pt-3">
|
||||
<p className="text-xs font-medium text-muted-foreground mb-2">Your Evaluation</p>
|
||||
{scores && typeof scores === 'object' ? (
|
||||
<div className="space-y-1">
|
||||
{Object.entries(scores).map(([criterion, score]) => (
|
||||
<div key={criterion} className="flex items-center justify-between text-sm">
|
||||
<span className="text-muted-foreground truncate">{criterion}</span>
|
||||
<span className="font-medium tabular-nums">{String(score)}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<Badge variant="outline">
|
||||
<CheckCircle2 className="mr-1 h-3 w-3" />
|
||||
Submitted
|
||||
</Badge>
|
||||
)}
|
||||
{evaluation.globalScore != null && (
|
||||
<div className="mt-2 flex items-center justify-between font-medium text-sm border-t pt-2">
|
||||
<span>Overall Score</span>
|
||||
<span className="text-primary">{String(evaluation.globalScore)}</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!evaluation && (
|
||||
<div className="border-t pt-3">
|
||||
<Badge variant="outline">
|
||||
<Clock className="mr-1 h-3 w-3" />
|
||||
Not yet evaluated
|
||||
</Badge>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
function CompareSkeleton() {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center gap-4">
|
||||
<Skeleton className="h-9 w-36" />
|
||||
</div>
|
||||
<Skeleton className="h-8 w-64" />
|
||||
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<Card key={i}>
|
||||
<CardHeader>
|
||||
<Skeleton className="h-5 w-32" />
|
||||
<Skeleton className="h-4 w-24" />
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-3">
|
||||
<Skeleton className="h-4 w-full" />
|
||||
<Skeleton className="h-4 w-3/4" />
|
||||
<Skeleton className="h-16 w-full" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
'use client'
|
||||
|
||||
import { use, useState, useEffect } from 'react'
|
||||
import { use, useState, useEffect, useCallback } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
|
|
@ -15,7 +15,8 @@ import { Skeleton } from '@/components/ui/skeleton'
|
|||
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
||||
import { Progress } from '@/components/ui/progress'
|
||||
import { toast } from 'sonner'
|
||||
import { Clock, CheckCircle, AlertCircle, Zap } from 'lucide-react'
|
||||
import { Clock, CheckCircle, AlertCircle, Zap, Wifi, WifiOff } from 'lucide-react'
|
||||
import { useLiveVotingSSE } from '@/hooks/use-live-voting-sse'
|
||||
|
||||
interface PageProps {
|
||||
params: Promise<{ sessionId: string }>
|
||||
|
|
@ -27,12 +28,28 @@ function JuryVotingContent({ sessionId }: { sessionId: string }) {
|
|||
const [selectedScore, setSelectedScore] = useState<number | null>(null)
|
||||
const [countdown, setCountdown] = useState<number | null>(null)
|
||||
|
||||
// Fetch session data with polling
|
||||
// Fetch session data - reduced polling since SSE handles real-time
|
||||
const { data, isLoading, refetch } = trpc.liveVoting.getSessionForVoting.useQuery(
|
||||
{ sessionId },
|
||||
{ refetchInterval: 2000 } // Poll every 2 seconds
|
||||
{ refetchInterval: 10000 }
|
||||
)
|
||||
|
||||
// SSE for real-time updates
|
||||
const onSessionStatus = useCallback(() => {
|
||||
refetch()
|
||||
}, [refetch])
|
||||
|
||||
const onProjectChange = useCallback(() => {
|
||||
setSelectedScore(null)
|
||||
setCountdown(null)
|
||||
refetch()
|
||||
}, [refetch])
|
||||
|
||||
const { isConnected } = useLiveVotingSSE(sessionId, {
|
||||
onSessionStatus,
|
||||
onProjectChange,
|
||||
})
|
||||
|
||||
// Vote mutation
|
||||
const vote = trpc.liveVoting.vote.useMutation({
|
||||
onSuccess: () => {
|
||||
|
|
@ -207,9 +224,16 @@ function JuryVotingContent({ sessionId }: { sessionId: string }) {
|
|||
</Card>
|
||||
|
||||
{/* Mobile-friendly footer */}
|
||||
<p className="text-white/60 text-sm mt-4">
|
||||
MOPC Live Voting
|
||||
</p>
|
||||
<div className="flex items-center justify-center gap-2 mt-4">
|
||||
{isConnected ? (
|
||||
<Wifi className="h-3 w-3 text-green-400" />
|
||||
) : (
|
||||
<WifiOff className="h-3 w-3 text-red-400" />
|
||||
)}
|
||||
<p className="text-white/60 text-sm">
|
||||
MOPC Live Voting {isConnected ? '- Connected' : '- Reconnecting...'}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,366 @@
|
|||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { useParams, useSearchParams } from 'next/navigation'
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import {
|
||||
ArrowLeft,
|
||||
BarChart3,
|
||||
MessageSquare,
|
||||
Send,
|
||||
Loader2,
|
||||
Lock,
|
||||
User,
|
||||
} from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
import { formatDate, cn, getInitials } from '@/lib/utils'
|
||||
|
||||
export default function DiscussionPage() {
|
||||
const params = useParams()
|
||||
const searchParams = useSearchParams()
|
||||
const projectId = params.id as string
|
||||
const roundId = searchParams.get('roundId') || ''
|
||||
|
||||
const [commentText, setCommentText] = useState('')
|
||||
|
||||
const utils = trpc.useUtils()
|
||||
|
||||
// Fetch peer summary
|
||||
const { data: peerSummary, isLoading: loadingSummary } =
|
||||
trpc.evaluation.getPeerSummary.useQuery(
|
||||
{ projectId, roundId },
|
||||
{ enabled: !!roundId }
|
||||
)
|
||||
|
||||
// Fetch discussion thread
|
||||
const { data: discussion, isLoading: loadingDiscussion } =
|
||||
trpc.evaluation.getDiscussion.useQuery(
|
||||
{ projectId, roundId },
|
||||
{ enabled: !!roundId }
|
||||
)
|
||||
|
||||
// Add comment mutation
|
||||
const addCommentMutation = trpc.evaluation.addComment.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.evaluation.getDiscussion.invalidate({ projectId, roundId })
|
||||
toast.success('Comment added')
|
||||
setCommentText('')
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const handleSubmitComment = () => {
|
||||
if (!commentText.trim()) {
|
||||
toast.error('Please enter a comment')
|
||||
return
|
||||
}
|
||||
addCommentMutation.mutate({
|
||||
projectId,
|
||||
roundId,
|
||||
content: commentText.trim(),
|
||||
})
|
||||
}
|
||||
|
||||
const isLoading = loadingSummary || loadingDiscussion
|
||||
|
||||
if (!roundId) {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<Button variant="ghost" asChild className="-ml-4">
|
||||
<Link href="/jury/assignments">
|
||||
<ArrowLeft className="mr-2 h-4 w-4" />
|
||||
Back to Assignments
|
||||
</Link>
|
||||
</Button>
|
||||
<Card>
|
||||
<CardContent className="flex flex-col items-center justify-center py-12 text-center">
|
||||
<MessageSquare className="h-12 w-12 text-muted-foreground/50" />
|
||||
<p className="mt-2 font-medium">No round specified</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Please access the discussion from your assignments page.
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
return <DiscussionSkeleton />
|
||||
}
|
||||
|
||||
// Parse peer summary data
|
||||
const summary = peerSummary as Record<string, unknown> | undefined
|
||||
const averageScore = summary ? Number(summary.averageScore || 0) : 0
|
||||
const scoreRange = summary?.scoreRange as { min: number; max: number } | undefined
|
||||
const evaluationCount = summary ? Number(summary.evaluationCount || 0) : 0
|
||||
const individualScores = (summary?.scores || summary?.individualScores) as
|
||||
| Array<number>
|
||||
| undefined
|
||||
|
||||
// Parse discussion data
|
||||
const discussionData = discussion as Record<string, unknown> | undefined
|
||||
const comments = (discussionData?.comments || []) as Array<{
|
||||
id: string
|
||||
user: { id: string; name: string | null; email: string }
|
||||
content: string
|
||||
createdAt: string
|
||||
}>
|
||||
const discussionStatus = String(discussionData?.status || 'OPEN')
|
||||
const isClosed = discussionStatus === 'CLOSED'
|
||||
const closedAt = discussionData?.closedAt as string | undefined
|
||||
const closedBy = discussionData?.closedBy as Record<string, unknown> | undefined
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center gap-4">
|
||||
<Button variant="ghost" asChild className="-ml-4">
|
||||
<Link href="/jury/assignments">
|
||||
<ArrowLeft className="mr-2 h-4 w-4" />
|
||||
Back to Assignments
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold tracking-tight">
|
||||
Project Discussion
|
||||
</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Peer review discussion and anonymized score summary
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Peer Summary Card */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg flex items-center gap-2">
|
||||
<BarChart3 className="h-5 w-5" />
|
||||
Peer Summary
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Anonymized scoring overview across all evaluations
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
{/* Stats row */}
|
||||
<div className="grid gap-4 sm:grid-cols-3">
|
||||
<div className="rounded-lg border p-3 text-center">
|
||||
<p className="text-2xl font-bold">{averageScore.toFixed(1)}</p>
|
||||
<p className="text-xs text-muted-foreground">Average Score</p>
|
||||
</div>
|
||||
<div className="rounded-lg border p-3 text-center">
|
||||
<p className="text-2xl font-bold">
|
||||
{scoreRange
|
||||
? `${scoreRange.min.toFixed(1)} - ${scoreRange.max.toFixed(1)}`
|
||||
: '--'}
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground">Score Range</p>
|
||||
</div>
|
||||
<div className="rounded-lg border p-3 text-center">
|
||||
<p className="text-2xl font-bold">{evaluationCount}</p>
|
||||
<p className="text-xs text-muted-foreground">Evaluations</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Anonymized score bars */}
|
||||
{individualScores && individualScores.length > 0 && (
|
||||
<div className="space-y-2">
|
||||
<p className="text-sm font-medium">Anonymized Individual Scores</p>
|
||||
<div className="flex items-end gap-2 h-24">
|
||||
{individualScores.map((score, i) => {
|
||||
const maxPossible = scoreRange?.max || 10
|
||||
const height =
|
||||
maxPossible > 0
|
||||
? Math.max((score / maxPossible) * 100, 4)
|
||||
: 4
|
||||
|
||||
return (
|
||||
<div
|
||||
key={i}
|
||||
className="flex-1 flex flex-col items-center gap-1"
|
||||
>
|
||||
<span className="text-[10px] font-medium tabular-nums">
|
||||
{score.toFixed(1)}
|
||||
</span>
|
||||
<div
|
||||
className={cn(
|
||||
'w-full rounded-t transition-all',
|
||||
score >= averageScore
|
||||
? 'bg-primary/60'
|
||||
: 'bg-muted-foreground/30'
|
||||
)}
|
||||
style={{ height: `${height}%` }}
|
||||
/>
|
||||
<span className="text-[10px] text-muted-foreground">
|
||||
#{i + 1}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Discussion Section */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center justify-between">
|
||||
<CardTitle className="text-lg flex items-center gap-2">
|
||||
<MessageSquare className="h-5 w-5" />
|
||||
Discussion
|
||||
</CardTitle>
|
||||
{isClosed && (
|
||||
<Badge variant="secondary" className="flex items-center gap-1">
|
||||
<Lock className="h-3 w-3" />
|
||||
Closed
|
||||
{closedAt && (
|
||||
<span className="ml-1">- {formatDate(closedAt)}</span>
|
||||
)}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
<CardDescription>
|
||||
{isClosed
|
||||
? 'This discussion has been closed.'
|
||||
: 'Share your thoughts with fellow jurors about this project.'}
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
{/* Comments */}
|
||||
{comments.length > 0 ? (
|
||||
<div className="space-y-4">
|
||||
{comments.map((comment) => (
|
||||
<div key={comment.id} className="flex gap-3">
|
||||
{/* Avatar */}
|
||||
<div className="flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-muted text-xs font-medium">
|
||||
{comment.user?.name
|
||||
? getInitials(comment.user.name)
|
||||
: <User className="h-4 w-4" />}
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-sm font-medium">
|
||||
{comment.user?.name || 'Anonymous Juror'}
|
||||
</span>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{formatDate(comment.createdAt)}
|
||||
</span>
|
||||
</div>
|
||||
<p className="text-sm mt-1 whitespace-pre-wrap">
|
||||
{comment.content}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex flex-col items-center justify-center py-8 text-center">
|
||||
<MessageSquare className="h-10 w-10 text-muted-foreground/30" />
|
||||
<p className="mt-2 text-sm text-muted-foreground">
|
||||
No comments yet. Be the first to start the discussion.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Comment input */}
|
||||
{!isClosed ? (
|
||||
<div className="space-y-2 border-t pt-4">
|
||||
<Textarea
|
||||
placeholder="Write your comment..."
|
||||
value={commentText}
|
||||
onChange={(e) => setCommentText(e.target.value)}
|
||||
rows={3}
|
||||
/>
|
||||
<div className="flex justify-end">
|
||||
<Button
|
||||
onClick={handleSubmitComment}
|
||||
disabled={
|
||||
addCommentMutation.isPending || !commentText.trim()
|
||||
}
|
||||
>
|
||||
{addCommentMutation.isPending ? (
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<Send className="mr-2 h-4 w-4" />
|
||||
)}
|
||||
Post Comment
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="border-t pt-4">
|
||||
<p className="text-sm text-muted-foreground text-center">
|
||||
This discussion is closed and no longer accepts new comments.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function DiscussionSkeleton() {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<Skeleton className="h-9 w-36" />
|
||||
<div>
|
||||
<Skeleton className="h-8 w-64" />
|
||||
<Skeleton className="h-4 w-40 mt-2" />
|
||||
</div>
|
||||
|
||||
{/* Peer summary skeleton */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<Skeleton className="h-5 w-40" />
|
||||
<Skeleton className="h-4 w-56" />
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="grid gap-4 sm:grid-cols-3">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<Skeleton key={i} className="h-20 w-full" />
|
||||
))}
|
||||
</div>
|
||||
<Skeleton className="h-24 w-full" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Discussion skeleton */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<Skeleton className="h-5 w-32" />
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<div key={i} className="flex gap-3">
|
||||
<Skeleton className="h-8 w-8 rounded-full" />
|
||||
<div className="flex-1 space-y-2">
|
||||
<Skeleton className="h-4 w-32" />
|
||||
<Skeleton className="h-4 w-full" />
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
<Skeleton className="h-20 w-full" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@ import {
|
|||
import { Button } from '@/components/ui/button'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Progress } from '@/components/ui/progress'
|
||||
import { Avatar, AvatarFallback } from '@/components/ui/avatar'
|
||||
import {
|
||||
Users,
|
||||
|
|
@ -23,8 +24,11 @@ import {
|
|||
GraduationCap,
|
||||
Waves,
|
||||
Crown,
|
||||
CheckCircle2,
|
||||
Circle,
|
||||
Clock,
|
||||
} from 'lucide-react'
|
||||
import { getInitials, formatDateOnly } from '@/lib/utils'
|
||||
import { formatDateOnly } from '@/lib/utils'
|
||||
|
||||
// Status badge colors
|
||||
const statusColors: Record<string, 'default' | 'secondary' | 'destructive' | 'outline'> = {
|
||||
|
|
@ -36,6 +40,13 @@ const statusColors: Record<string, 'default' | 'secondary' | 'destructive' | 'ou
|
|||
REJECTED: 'destructive',
|
||||
}
|
||||
|
||||
// Completion status display
|
||||
const completionBadge: Record<string, { label: string; variant: 'default' | 'secondary' | 'outline' }> = {
|
||||
in_progress: { label: 'In Progress', variant: 'secondary' },
|
||||
completed: { label: 'Completed', variant: 'default' },
|
||||
paused: { label: 'Paused', variant: 'outline' },
|
||||
}
|
||||
|
||||
function DashboardSkeleton() {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
|
|
@ -44,7 +55,8 @@ function DashboardSkeleton() {
|
|||
<Skeleton className="h-4 w-64 mt-2" />
|
||||
</div>
|
||||
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<div className="grid gap-4 md:grid-cols-3">
|
||||
<Skeleton className="h-24" />
|
||||
<Skeleton className="h-24" />
|
||||
<Skeleton className="h-24" />
|
||||
</div>
|
||||
|
|
@ -66,6 +78,8 @@ export default function MentorDashboard() {
|
|||
}
|
||||
|
||||
const projects = assignments || []
|
||||
const completedCount = projects.filter((a) => a.completionStatus === 'completed').length
|
||||
const inProgressCount = projects.filter((a) => a.completionStatus === 'in_progress').length
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
|
|
@ -80,7 +94,7 @@ export default function MentorDashboard() {
|
|||
</div>
|
||||
|
||||
{/* Stats */}
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<div className="grid gap-4 md:grid-cols-3">
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">
|
||||
|
|
@ -96,6 +110,29 @@ export default function MentorDashboard() {
|
|||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">
|
||||
Completed
|
||||
</CardTitle>
|
||||
<CheckCircle2 className="h-4 w-4 text-green-500" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">{completedCount}</div>
|
||||
<div className="flex items-center gap-2 mt-1">
|
||||
{projects.length > 0 && (
|
||||
<Progress
|
||||
value={(completedCount / projects.length) * 100}
|
||||
className="h-1.5 flex-1"
|
||||
/>
|
||||
)}
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{projects.length > 0 ? Math.round((completedCount / projects.length) * 100) : 0}%
|
||||
</span>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">
|
||||
|
|
@ -141,6 +178,7 @@ export default function MentorDashboard() {
|
|||
const teamLead = project.teamMembers?.find(
|
||||
(m) => m.role === 'LEAD'
|
||||
)
|
||||
const badge = completionBadge[assignment.completionStatus] || completionBadge.in_progress
|
||||
|
||||
return (
|
||||
<Card key={assignment.id}>
|
||||
|
|
@ -153,12 +191,12 @@ export default function MentorDashboard() {
|
|||
</span>
|
||||
{project.round && (
|
||||
<>
|
||||
<span>•</span>
|
||||
<span>-</span>
|
||||
<span>{project.round.name}</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<CardTitle className="flex items-center gap-2 flex-wrap">
|
||||
{project.title}
|
||||
{project.status && (
|
||||
<Badge
|
||||
|
|
@ -167,6 +205,18 @@ export default function MentorDashboard() {
|
|||
{project.status.replace('_', ' ')}
|
||||
</Badge>
|
||||
)}
|
||||
<Badge variant={badge.variant}>
|
||||
{assignment.completionStatus === 'completed' && (
|
||||
<CheckCircle2 className="mr-1 h-3 w-3" />
|
||||
)}
|
||||
{assignment.completionStatus === 'in_progress' && (
|
||||
<Circle className="mr-1 h-3 w-3" />
|
||||
)}
|
||||
{assignment.completionStatus === 'paused' && (
|
||||
<Clock className="mr-1 h-3 w-3" />
|
||||
)}
|
||||
{badge.label}
|
||||
</Badge>
|
||||
</CardTitle>
|
||||
{project.teamName && (
|
||||
<CardDescription>{project.teamName}</CardDescription>
|
||||
|
|
@ -242,10 +292,13 @@ export default function MentorDashboard() {
|
|||
</div>
|
||||
)}
|
||||
|
||||
{/* Assignment date */}
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Assigned {formatDateOnly(assignment.assignedAt)}
|
||||
</p>
|
||||
{/* Assignment date + last viewed */}
|
||||
<div className="flex items-center justify-between text-xs text-muted-foreground">
|
||||
<span>Assigned {formatDateOnly(assignment.assignedAt)}</span>
|
||||
{assignment.lastViewedAt && (
|
||||
<span>Last viewed {formatDateOnly(assignment.lastViewedAt)}</span>
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
'use client'
|
||||
|
||||
import { Suspense, use } from 'react'
|
||||
import { Suspense, use, useState, useEffect } from 'react'
|
||||
import Link from 'next/link'
|
||||
import type { Route } from 'next'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
|
|
@ -15,6 +15,19 @@ import { Button } from '@/components/ui/button'
|
|||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Separator } from '@/components/ui/separator'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@/components/ui/alert-dialog'
|
||||
import { FileViewer } from '@/components/shared/file-viewer'
|
||||
import { MentorChat } from '@/components/shared/mentor-chat'
|
||||
import { ProjectLogoWithUrl } from '@/components/shared/project-logo-with-url'
|
||||
|
|
@ -32,8 +45,19 @@ import {
|
|||
FileText,
|
||||
ExternalLink,
|
||||
MessageSquare,
|
||||
StickyNote,
|
||||
Plus,
|
||||
Pencil,
|
||||
Trash2,
|
||||
Loader2,
|
||||
Target,
|
||||
CheckCircle2,
|
||||
Circle,
|
||||
Eye,
|
||||
EyeOff,
|
||||
} from 'lucide-react'
|
||||
import { formatDateOnly, getInitials } from '@/lib/utils'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
interface PageProps {
|
||||
params: Promise<{ id: string }>
|
||||
|
|
@ -65,6 +89,15 @@ function ProjectDetailContent({ projectId }: { projectId: string }) {
|
|||
},
|
||||
})
|
||||
|
||||
// Track view when project loads
|
||||
const trackView = trpc.mentor.trackView.useMutation()
|
||||
useEffect(() => {
|
||||
if (project?.mentorAssignment?.id) {
|
||||
trackView.mutate({ mentorAssignmentId: project.mentorAssignment.id })
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [project?.mentorAssignment?.id])
|
||||
|
||||
if (isLoading) {
|
||||
return <ProjectDetailSkeleton />
|
||||
}
|
||||
|
|
@ -99,6 +132,8 @@ function ProjectDetailContent({ projectId }: { projectId: string }) {
|
|||
|
||||
const teamLead = project.teamMembers?.find((m) => m.role === 'LEAD')
|
||||
const otherMembers = project.teamMembers?.filter((m) => m.role !== 'LEAD') || []
|
||||
const mentorAssignmentId = project.mentorAssignment?.id
|
||||
const programId = project.round?.program?.id
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
|
|
@ -126,7 +161,7 @@ function ProjectDetailContent({ projectId }: { projectId: string }) {
|
|||
</span>
|
||||
{project.round && (
|
||||
<>
|
||||
<span>•</span>
|
||||
<span>-</span>
|
||||
<span>{project.round.name}</span>
|
||||
</>
|
||||
)}
|
||||
|
|
@ -157,6 +192,19 @@ function ProjectDetailContent({ projectId }: { projectId: string }) {
|
|||
|
||||
<Separator />
|
||||
|
||||
{/* Milestones Section */}
|
||||
{programId && mentorAssignmentId && (
|
||||
<MilestonesSection
|
||||
programId={programId}
|
||||
mentorAssignmentId={mentorAssignmentId}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Private Notes Section */}
|
||||
{mentorAssignmentId && (
|
||||
<NotesSection mentorAssignmentId={mentorAssignmentId} />
|
||||
)}
|
||||
|
||||
{/* Project Info */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
|
|
@ -359,6 +407,7 @@ function ProjectDetailContent({ projectId }: { projectId: string }) {
|
|||
<CardContent>
|
||||
{project.files && project.files.length > 0 ? (
|
||||
<FileViewer
|
||||
projectId={projectId}
|
||||
files={project.files.map((f) => ({
|
||||
id: f.id,
|
||||
fileName: f.fileName,
|
||||
|
|
@ -367,6 +416,7 @@ function ProjectDetailContent({ projectId }: { projectId: string }) {
|
|||
size: f.size,
|
||||
bucket: f.bucket,
|
||||
objectKey: f.objectKey,
|
||||
version: f.version,
|
||||
}))}
|
||||
/>
|
||||
) : (
|
||||
|
|
@ -404,6 +454,386 @@ function ProjectDetailContent({ projectId }: { projectId: string }) {
|
|||
)
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Milestones Section
|
||||
// =============================================================================
|
||||
|
||||
function MilestonesSection({
|
||||
programId,
|
||||
mentorAssignmentId,
|
||||
}: {
|
||||
programId: string
|
||||
mentorAssignmentId: string
|
||||
}) {
|
||||
const { data: milestones, isLoading } = trpc.mentor.getMilestones.useQuery({ programId })
|
||||
const utils = trpc.useUtils()
|
||||
|
||||
const completeMutation = trpc.mentor.completeMilestone.useMutation({
|
||||
onSuccess: (data) => {
|
||||
utils.mentor.getMilestones.invalidate({ programId })
|
||||
if (data.allRequiredDone) {
|
||||
toast.success('All required milestones completed!')
|
||||
} else {
|
||||
toast.success('Milestone completed')
|
||||
}
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const uncompleteMutation = trpc.mentor.uncompleteMilestone.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.mentor.getMilestones.invalidate({ programId })
|
||||
toast.success('Milestone unchecked')
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<Skeleton className="h-5 w-32" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-3">
|
||||
<Skeleton className="h-8 w-full" />
|
||||
<Skeleton className="h-8 w-full" />
|
||||
<Skeleton className="h-8 w-full" />
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
if (!milestones || milestones.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
const completedCount = milestones.filter(
|
||||
(m) => m.myCompletions.length > 0
|
||||
).length
|
||||
const totalRequired = milestones.filter((m) => m.isRequired).length
|
||||
const requiredCompleted = milestones.filter(
|
||||
(m) => m.isRequired && m.myCompletions.length > 0
|
||||
).length
|
||||
|
||||
const handleToggle = (milestoneId: string, isCompleted: boolean) => {
|
||||
if (isCompleted) {
|
||||
uncompleteMutation.mutate({ milestoneId, mentorAssignmentId })
|
||||
} else {
|
||||
completeMutation.mutate({ milestoneId, mentorAssignmentId })
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center justify-between">
|
||||
<CardTitle className="text-lg flex items-center gap-2">
|
||||
<Target className="h-5 w-5" />
|
||||
Milestones
|
||||
</CardTitle>
|
||||
<Badge variant="secondary">
|
||||
{completedCount}/{milestones.length} done
|
||||
</Badge>
|
||||
</div>
|
||||
{totalRequired > 0 && (
|
||||
<CardDescription>
|
||||
{requiredCompleted}/{totalRequired} required milestones completed
|
||||
</CardDescription>
|
||||
)}
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-3">
|
||||
{milestones.map((milestone) => {
|
||||
const isCompleted = milestone.myCompletions.length > 0
|
||||
const isPending = completeMutation.isPending || uncompleteMutation.isPending
|
||||
|
||||
return (
|
||||
<div
|
||||
key={milestone.id}
|
||||
className={`flex items-start gap-3 p-3 rounded-lg border transition-colors ${
|
||||
isCompleted ? 'bg-green-50/50 border-green-200 dark:bg-green-950/20 dark:border-green-900' : ''
|
||||
}`}
|
||||
>
|
||||
<Checkbox
|
||||
checked={isCompleted}
|
||||
disabled={isPending}
|
||||
onCheckedChange={() => handleToggle(milestone.id, isCompleted)}
|
||||
className="mt-0.5"
|
||||
/>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<p className={`text-sm font-medium ${isCompleted ? 'line-through text-muted-foreground' : ''}`}>
|
||||
{milestone.name}
|
||||
</p>
|
||||
{milestone.isRequired && (
|
||||
<Badge variant="outline" className="text-xs">Required</Badge>
|
||||
)}
|
||||
</div>
|
||||
{milestone.description && (
|
||||
<p className="text-xs text-muted-foreground mt-1">
|
||||
{milestone.description}
|
||||
</p>
|
||||
)}
|
||||
{isCompleted && milestone.myCompletions[0] && (
|
||||
<p className="text-xs text-green-600 mt-1">
|
||||
Completed {formatDateOnly(milestone.myCompletions[0].completedAt)}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
{isCompleted ? (
|
||||
<CheckCircle2 className="h-4 w-4 text-green-500 shrink-0 mt-0.5" />
|
||||
) : (
|
||||
<Circle className="h-4 w-4 text-muted-foreground shrink-0 mt-0.5" />
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Notes Section
|
||||
// =============================================================================
|
||||
|
||||
function NotesSection({ mentorAssignmentId }: { mentorAssignmentId: string }) {
|
||||
const [isAdding, setIsAdding] = useState(false)
|
||||
const [editingId, setEditingId] = useState<string | null>(null)
|
||||
const [deleteId, setDeleteId] = useState<string | null>(null)
|
||||
const [noteContent, setNoteContent] = useState('')
|
||||
const [isVisibleToAdmin, setIsVisibleToAdmin] = useState(true)
|
||||
|
||||
const { data: notes, isLoading } = trpc.mentor.getNotes.useQuery({ mentorAssignmentId })
|
||||
const utils = trpc.useUtils()
|
||||
|
||||
const createMutation = trpc.mentor.createNote.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.mentor.getNotes.invalidate({ mentorAssignmentId })
|
||||
toast.success('Note saved')
|
||||
resetForm()
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const updateMutation = trpc.mentor.updateNote.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.mentor.getNotes.invalidate({ mentorAssignmentId })
|
||||
toast.success('Note updated')
|
||||
resetForm()
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const deleteMutation = trpc.mentor.deleteNote.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.mentor.getNotes.invalidate({ mentorAssignmentId })
|
||||
toast.success('Note deleted')
|
||||
setDeleteId(null)
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const resetForm = () => {
|
||||
setIsAdding(false)
|
||||
setEditingId(null)
|
||||
setNoteContent('')
|
||||
setIsVisibleToAdmin(true)
|
||||
}
|
||||
|
||||
const handleEdit = (note: { id: string; content: string; isVisibleToAdmin: boolean }) => {
|
||||
setEditingId(note.id)
|
||||
setNoteContent(note.content)
|
||||
setIsVisibleToAdmin(note.isVisibleToAdmin)
|
||||
setIsAdding(false)
|
||||
}
|
||||
|
||||
const handleSubmit = () => {
|
||||
if (!noteContent.trim()) return
|
||||
|
||||
if (editingId) {
|
||||
updateMutation.mutate({
|
||||
noteId: editingId,
|
||||
content: noteContent.trim(),
|
||||
isVisibleToAdmin,
|
||||
})
|
||||
} else {
|
||||
createMutation.mutate({
|
||||
mentorAssignmentId,
|
||||
content: noteContent.trim(),
|
||||
isVisibleToAdmin,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const isPending = createMutation.isPending || updateMutation.isPending
|
||||
|
||||
return (
|
||||
<>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center justify-between">
|
||||
<CardTitle className="text-lg flex items-center gap-2">
|
||||
<StickyNote className="h-5 w-5" />
|
||||
Private Notes
|
||||
</CardTitle>
|
||||
{!isAdding && !editingId && (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setIsAdding(true)
|
||||
setEditingId(null)
|
||||
setNoteContent('')
|
||||
setIsVisibleToAdmin(true)
|
||||
}}
|
||||
>
|
||||
<Plus className="mr-2 h-4 w-4" />
|
||||
Add Note
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
<CardDescription>
|
||||
Personal notes about this mentorship (private to you unless shared with admin)
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
{/* Add/Edit form */}
|
||||
{(isAdding || editingId) && (
|
||||
<div className="space-y-3 p-4 border rounded-lg bg-muted/30">
|
||||
<Textarea
|
||||
value={noteContent}
|
||||
onChange={(e) => setNoteContent(e.target.value)}
|
||||
placeholder="Write your note..."
|
||||
rows={4}
|
||||
className="resize-none"
|
||||
/>
|
||||
<div className="flex items-center gap-2">
|
||||
<Checkbox
|
||||
id="visible-to-admin"
|
||||
checked={isVisibleToAdmin}
|
||||
onCheckedChange={(checked) =>
|
||||
setIsVisibleToAdmin(checked === true)
|
||||
}
|
||||
/>
|
||||
<Label htmlFor="visible-to-admin" className="text-sm flex items-center gap-1">
|
||||
{isVisibleToAdmin ? (
|
||||
<Eye className="h-3.5 w-3.5 text-muted-foreground" />
|
||||
) : (
|
||||
<EyeOff className="h-3.5 w-3.5 text-muted-foreground" />
|
||||
)}
|
||||
Visible to admins
|
||||
</Label>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 justify-end">
|
||||
<Button variant="outline" size="sm" onClick={resetForm}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={handleSubmit}
|
||||
disabled={!noteContent.trim() || isPending}
|
||||
>
|
||||
{isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
{editingId ? 'Update' : 'Save'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Notes list */}
|
||||
{isLoading ? (
|
||||
<div className="space-y-3">
|
||||
<Skeleton className="h-20 w-full" />
|
||||
<Skeleton className="h-20 w-full" />
|
||||
</div>
|
||||
) : notes && notes.length > 0 ? (
|
||||
<div className="space-y-3">
|
||||
{notes.map((note) => (
|
||||
<div
|
||||
key={note.id}
|
||||
className="p-4 rounded-lg border space-y-2"
|
||||
>
|
||||
<div className="flex items-start justify-between gap-2">
|
||||
<div className="flex items-center gap-2 text-xs text-muted-foreground">
|
||||
<span>{formatDateOnly(note.createdAt)}</span>
|
||||
{note.isVisibleToAdmin ? (
|
||||
<Badge variant="outline" className="text-xs gap-1">
|
||||
<Eye className="h-3 w-3" />
|
||||
Admin visible
|
||||
</Badge>
|
||||
) : (
|
||||
<Badge variant="outline" className="text-xs gap-1">
|
||||
<EyeOff className="h-3 w-3" />
|
||||
Private
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-7 w-7"
|
||||
onClick={() => handleEdit(note)}
|
||||
>
|
||||
<Pencil className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-7 w-7"
|
||||
onClick={() => setDeleteId(note.id)}
|
||||
>
|
||||
<Trash2 className="h-3.5 w-3.5 text-destructive" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<p className="text-sm whitespace-pre-wrap">{note.content}</p>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
!isAdding && (
|
||||
<p className="text-sm text-muted-foreground text-center py-4">
|
||||
No notes yet. Click "Add Note" to start taking notes.
|
||||
</p>
|
||||
)
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Delete confirmation */}
|
||||
<AlertDialog open={!!deleteId} onOpenChange={() => setDeleteId(null)}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Delete Note</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
Are you sure you want to delete this note? This action cannot be undone.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
onClick={() => deleteId && deleteMutation.mutate({ noteId: deleteId })}
|
||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||
>
|
||||
{deleteMutation.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Delete
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Skeletons
|
||||
// =============================================================================
|
||||
|
||||
function ProjectDetailSkeleton() {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
|
|
|
|||
|
|
@ -35,7 +35,12 @@ import {
|
|||
ClipboardList,
|
||||
CheckCircle2,
|
||||
TrendingUp,
|
||||
GitCompare,
|
||||
UserCheck,
|
||||
Globe,
|
||||
Printer,
|
||||
} from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { formatDateOnly } from '@/lib/utils'
|
||||
import {
|
||||
ScoreDistributionChart,
|
||||
|
|
@ -44,6 +49,9 @@ import {
|
|||
JurorWorkloadChart,
|
||||
ProjectRankingsChart,
|
||||
CriteriaScoresChart,
|
||||
CrossRoundComparisonChart,
|
||||
JurorConsistencyChart,
|
||||
DiversityMetricsChart,
|
||||
} from '@/components/charts'
|
||||
|
||||
function OverviewTab({ selectedRoundId }: { selectedRoundId: string | null }) {
|
||||
|
|
@ -410,6 +418,121 @@ function AnalyticsTab({ selectedRoundId }: { selectedRoundId: string }) {
|
|||
)
|
||||
}
|
||||
|
||||
function CrossRoundTab() {
|
||||
const { data: programs, isLoading: programsLoading } = trpc.program.list.useQuery({ includeRounds: true })
|
||||
|
||||
const rounds = programs?.flatMap(p =>
|
||||
p.rounds.map(r => ({ id: r.id, name: r.name, programName: `${p.year} Edition` }))
|
||||
) || []
|
||||
|
||||
const [selectedRoundIds, setSelectedRoundIds] = useState<string[]>([])
|
||||
|
||||
const { data: comparison, isLoading: comparisonLoading } =
|
||||
trpc.analytics.getCrossRoundComparison.useQuery(
|
||||
{ roundIds: selectedRoundIds },
|
||||
{ enabled: selectedRoundIds.length >= 2 }
|
||||
)
|
||||
|
||||
const toggleRound = (roundId: string) => {
|
||||
setSelectedRoundIds((prev) =>
|
||||
prev.includes(roundId)
|
||||
? prev.filter((id) => id !== roundId)
|
||||
: [...prev, roundId]
|
||||
)
|
||||
}
|
||||
|
||||
if (programsLoading) return <Skeleton className="h-[400px]" />
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Select Rounds to Compare</CardTitle>
|
||||
<CardDescription>Choose at least 2 rounds</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{rounds.map((round) => (
|
||||
<Badge
|
||||
key={round.id}
|
||||
variant={selectedRoundIds.includes(round.id) ? 'default' : 'outline'}
|
||||
className="cursor-pointer text-sm py-1.5 px-3"
|
||||
onClick={() => toggleRound(round.id)}
|
||||
>
|
||||
{round.programName} - {round.name}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
{selectedRoundIds.length < 2 && (
|
||||
<p className="text-sm text-muted-foreground mt-3">
|
||||
Select at least 2 rounds to enable comparison
|
||||
</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{comparisonLoading && selectedRoundIds.length >= 2 && <Skeleton className="h-[350px]" />}
|
||||
|
||||
{comparison && (
|
||||
<CrossRoundComparisonChart data={comparison as Array<{
|
||||
roundId: string; roundName: string; projectCount: number; evaluationCount: number
|
||||
completionRate: number; averageScore: number | null
|
||||
scoreDistribution: { score: number; count: number }[]
|
||||
}>} />
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function JurorConsistencyTab({ selectedRoundId }: { selectedRoundId: string }) {
|
||||
const { data: consistency, isLoading } =
|
||||
trpc.analytics.getJurorConsistency.useQuery(
|
||||
{ roundId: selectedRoundId },
|
||||
{ enabled: !!selectedRoundId }
|
||||
)
|
||||
|
||||
if (isLoading) return <Skeleton className="h-[400px]" />
|
||||
|
||||
if (!consistency) return null
|
||||
|
||||
return (
|
||||
<JurorConsistencyChart
|
||||
data={consistency as {
|
||||
overallAverage: number
|
||||
jurors: Array<{
|
||||
userId: string; name: string; email: string
|
||||
evaluationCount: number; averageScore: number
|
||||
stddev: number; deviationFromOverall: number; isOutlier: boolean
|
||||
}>
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DiversityTab({ selectedRoundId }: { selectedRoundId: string }) {
|
||||
const { data: diversity, isLoading } =
|
||||
trpc.analytics.getDiversityMetrics.useQuery(
|
||||
{ roundId: selectedRoundId },
|
||||
{ enabled: !!selectedRoundId }
|
||||
)
|
||||
|
||||
if (isLoading) return <Skeleton className="h-[400px]" />
|
||||
|
||||
if (!diversity) return null
|
||||
|
||||
return (
|
||||
<DiversityMetricsChart
|
||||
data={diversity as {
|
||||
total: number
|
||||
byCountry: { country: string; count: number; percentage: number }[]
|
||||
byCategory: { category: string; count: number; percentage: number }[]
|
||||
byOceanIssue: { issue: string; count: number; percentage: number }[]
|
||||
byTag: { tag: string; count: number; percentage: number }[]
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export default function ObserverReportsPage() {
|
||||
const [selectedRoundId, setSelectedRoundId] = useState<string | null>(null)
|
||||
|
||||
|
|
@ -462,16 +585,38 @@ export default function ObserverReportsPage() {
|
|||
|
||||
{/* Tabs */}
|
||||
<Tabs defaultValue="overview" className="space-y-6">
|
||||
<TabsList>
|
||||
<TabsTrigger value="overview" className="gap-2">
|
||||
<FileSpreadsheet className="h-4 w-4" />
|
||||
Overview
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="analytics" className="gap-2" disabled={!selectedRoundId}>
|
||||
<TrendingUp className="h-4 w-4" />
|
||||
Analytics
|
||||
</TabsTrigger>
|
||||
</TabsList>
|
||||
<div className="flex items-center justify-between flex-wrap gap-4">
|
||||
<TabsList>
|
||||
<TabsTrigger value="overview" className="gap-2">
|
||||
<FileSpreadsheet className="h-4 w-4" />
|
||||
Overview
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="analytics" className="gap-2" disabled={!selectedRoundId}>
|
||||
<TrendingUp className="h-4 w-4" />
|
||||
Analytics
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="cross-round" className="gap-2">
|
||||
<GitCompare className="h-4 w-4" />
|
||||
Cross-Round
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="consistency" className="gap-2" disabled={!selectedRoundId}>
|
||||
<UserCheck className="h-4 w-4" />
|
||||
Juror Consistency
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="diversity" className="gap-2" disabled={!selectedRoundId}>
|
||||
<Globe className="h-4 w-4" />
|
||||
Diversity
|
||||
</TabsTrigger>
|
||||
</TabsList>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => window.print()}
|
||||
>
|
||||
<Printer className="mr-2 h-4 w-4" />
|
||||
Export PDF
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<TabsContent value="overview">
|
||||
<OverviewTab selectedRoundId={selectedRoundId} />
|
||||
|
|
@ -492,6 +637,42 @@ export default function ObserverReportsPage() {
|
|||
</Card>
|
||||
)}
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="cross-round">
|
||||
<CrossRoundTab />
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="consistency">
|
||||
{selectedRoundId ? (
|
||||
<JurorConsistencyTab selectedRoundId={selectedRoundId} />
|
||||
) : (
|
||||
<Card>
|
||||
<CardContent className="flex flex-col items-center justify-center py-12 text-center">
|
||||
<UserCheck className="h-12 w-12 text-muted-foreground/50" />
|
||||
<p className="mt-2 font-medium">Select a round</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Choose a round above to view juror consistency metrics
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="diversity">
|
||||
{selectedRoundId ? (
|
||||
<DiversityTab selectedRoundId={selectedRoundId} />
|
||||
) : (
|
||||
<Card>
|
||||
<CardContent className="flex flex-col items-center justify-center py-12 text-center">
|
||||
<Globe className="h-12 w-12 text-muted-foreground/50" />
|
||||
<p className="mt-2 font-medium">Select a round</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Choose a round above to view diversity metrics
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
</div>
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
'use client'
|
||||
|
||||
import { use } from 'react'
|
||||
import { use, useCallback, useState } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import {
|
||||
|
|
@ -12,19 +12,65 @@ import {
|
|||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Progress } from '@/components/ui/progress'
|
||||
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
||||
import { Trophy, Star, Clock, AlertCircle, Zap } from 'lucide-react'
|
||||
import { Trophy, Star, Clock, AlertCircle, Zap, Wifi, WifiOff } from 'lucide-react'
|
||||
import { useLiveVotingSSE, type VoteUpdate } from '@/hooks/use-live-voting-sse'
|
||||
|
||||
interface PageProps {
|
||||
params: Promise<{ sessionId: string }>
|
||||
}
|
||||
|
||||
interface PublicSession {
|
||||
id: string
|
||||
status: string
|
||||
currentProjectId: string | null
|
||||
votingEndsAt: string | null
|
||||
presentationSettings: Record<string, unknown> | null
|
||||
allowAudienceVotes: boolean
|
||||
}
|
||||
|
||||
interface PublicProject {
|
||||
id: string | undefined
|
||||
title: string | undefined
|
||||
teamName: string | null | undefined
|
||||
averageScore: number
|
||||
voteCount: number
|
||||
}
|
||||
|
||||
function PublicScoresContent({ sessionId }: { sessionId: string }) {
|
||||
// Fetch session data with polling
|
||||
const { data, isLoading } = trpc.liveVoting.getPublicSession.useQuery(
|
||||
// Track SSE-based score updates keyed by projectId
|
||||
const [liveScores, setLiveScores] = useState<Record<string, { avg: number; count: number }>>({})
|
||||
|
||||
// Use public (no-auth) endpoint with reduced polling since SSE handles real-time
|
||||
const { data, isLoading, refetch } = trpc.liveVoting.getPublicResults.useQuery(
|
||||
{ sessionId },
|
||||
{ refetchInterval: 2000 } // Poll every 2 seconds
|
||||
{ refetchInterval: 10000 }
|
||||
)
|
||||
|
||||
// SSE for real-time updates
|
||||
const onVoteUpdate = useCallback((update: VoteUpdate) => {
|
||||
setLiveScores((prev) => ({
|
||||
...prev,
|
||||
[update.projectId]: {
|
||||
avg: update.averageScore ?? 0,
|
||||
count: update.totalVotes,
|
||||
},
|
||||
}))
|
||||
}, [])
|
||||
|
||||
const onSessionStatus = useCallback(() => {
|
||||
refetch()
|
||||
}, [refetch])
|
||||
|
||||
const onProjectChange = useCallback(() => {
|
||||
refetch()
|
||||
}, [refetch])
|
||||
|
||||
const { isConnected } = useLiveVotingSSE(sessionId, {
|
||||
onVoteUpdate,
|
||||
onSessionStatus,
|
||||
onProjectChange,
|
||||
})
|
||||
|
||||
if (isLoading) {
|
||||
return <PublicScoresSkeleton />
|
||||
}
|
||||
|
|
@ -43,16 +89,32 @@ function PublicScoresContent({ sessionId }: { sessionId: string }) {
|
|||
)
|
||||
}
|
||||
|
||||
const isCompleted = data.session.status === 'COMPLETED'
|
||||
const isVoting = data.session.status === 'IN_PROGRESS'
|
||||
const session = data.session as PublicSession
|
||||
const projects = data.projects as PublicProject[]
|
||||
|
||||
const isCompleted = session.status === 'COMPLETED'
|
||||
const isVoting = session.status === 'IN_PROGRESS'
|
||||
|
||||
// Merge live SSE scores with fetched data
|
||||
const projectsWithLive = projects.map((project) => {
|
||||
const live = project.id ? liveScores[project.id] : null
|
||||
return {
|
||||
...project,
|
||||
averageScore: live ? live.avg : (project.averageScore || 0),
|
||||
voteCount: live ? live.count : (project.voteCount || 0),
|
||||
}
|
||||
})
|
||||
|
||||
// Sort projects by score for leaderboard
|
||||
const sortedProjects = [...data.projects].sort(
|
||||
const sortedProjects = [...projectsWithLive].sort(
|
||||
(a, b) => (b.averageScore || 0) - (a.averageScore || 0)
|
||||
)
|
||||
|
||||
// Find max score for progress bars
|
||||
const maxScore = Math.max(...data.projects.map((p) => p.averageScore || 0), 1)
|
||||
const maxScore = Math.max(...sortedProjects.map((p) => p.averageScore || 0), 1)
|
||||
|
||||
// Get presentation settings
|
||||
const presentationSettings = session.presentationSettings
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gradient-to-br from-[#053d57] to-[#557f8c] p-4 md:p-8">
|
||||
|
|
@ -62,20 +124,22 @@ function PublicScoresContent({ sessionId }: { sessionId: string }) {
|
|||
<div className="flex items-center justify-center gap-2 mb-2">
|
||||
<Zap className="h-8 w-8" />
|
||||
<h1 className="text-3xl font-bold">Live Scores</h1>
|
||||
{isConnected ? (
|
||||
<Wifi className="h-4 w-4 text-green-400" />
|
||||
) : (
|
||||
<WifiOff className="h-4 w-4 text-red-400" />
|
||||
)}
|
||||
</div>
|
||||
<p className="text-white/80">
|
||||
{data.round.program.name} - {data.round.name}
|
||||
</p>
|
||||
<Badge
|
||||
variant={isVoting ? 'default' : isCompleted ? 'secondary' : 'outline'}
|
||||
className="mt-2"
|
||||
>
|
||||
{isVoting ? 'LIVE' : isCompleted ? 'COMPLETED' : data.session.status}
|
||||
{isVoting ? 'LIVE' : isCompleted ? 'COMPLETED' : session.status}
|
||||
</Badge>
|
||||
</div>
|
||||
|
||||
{/* Current project highlight */}
|
||||
{isVoting && data.session.currentProjectId && (
|
||||
{isVoting && session.currentProjectId && (
|
||||
<Card className="border-2 border-green-500 bg-green-500/10 animate-pulse">
|
||||
<CardHeader className="pb-2">
|
||||
<div className="flex items-center gap-2 text-green-400">
|
||||
|
|
@ -85,7 +149,7 @@ function PublicScoresContent({ sessionId }: { sessionId: string }) {
|
|||
</CardHeader>
|
||||
<CardContent>
|
||||
<p className="text-xl font-semibold text-white">
|
||||
{data.projects.find((p) => p?.id === data.session.currentProjectId)?.title}
|
||||
{projects.find((p) => p?.id === session.currentProjectId)?.title}
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
|
@ -108,12 +172,13 @@ function PublicScoresContent({ sessionId }: { sessionId: string }) {
|
|||
<div className="space-y-4">
|
||||
{sortedProjects.map((project, index) => {
|
||||
if (!project) return null
|
||||
const isCurrent = project.id === data.session.currentProjectId
|
||||
const isCurrent = project.id === session.currentProjectId
|
||||
const scoreFormat = presentationSettings?.scoreDisplayFormat as string || 'bar'
|
||||
|
||||
return (
|
||||
<div
|
||||
key={project.id}
|
||||
className={`rounded-lg p-4 ${
|
||||
className={`rounded-lg p-4 transition-all duration-300 ${
|
||||
isCurrent
|
||||
? 'bg-green-500/10 border border-green-500'
|
||||
: 'bg-muted/50'
|
||||
|
|
@ -147,29 +212,58 @@ function PublicScoresContent({ sessionId }: { sessionId: string }) {
|
|||
|
||||
{/* Score */}
|
||||
<div className="shrink-0 text-right">
|
||||
<div className="flex items-center gap-1">
|
||||
<Star className="h-4 w-4 text-yellow-500" />
|
||||
<span className="text-xl font-bold">
|
||||
{project.averageScore?.toFixed(1) || '--'}
|
||||
</span>
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{project.voteCount} votes
|
||||
</p>
|
||||
{scoreFormat === 'radial' ? (
|
||||
<div className="relative w-14 h-14">
|
||||
<svg viewBox="0 0 36 36" className="w-14 h-14 -rotate-90">
|
||||
<path
|
||||
d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
className="text-muted/30"
|
||||
/>
|
||||
<path
|
||||
d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2.5"
|
||||
strokeDasharray={`${((project.averageScore || 0) / 10) * 100}, 100`}
|
||||
className="text-primary"
|
||||
/>
|
||||
</svg>
|
||||
<span className="absolute inset-0 flex items-center justify-center text-sm font-bold">
|
||||
{project.averageScore?.toFixed(1) || '--'}
|
||||
</span>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<div className="flex items-center gap-1">
|
||||
<Star className="h-4 w-4 text-yellow-500" />
|
||||
<span className="text-xl font-bold">
|
||||
{project.averageScore?.toFixed(1) || '--'}
|
||||
</span>
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{project.voteCount} votes
|
||||
</p>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Score bar */}
|
||||
<div className="mt-3">
|
||||
<Progress
|
||||
value={
|
||||
project.averageScore
|
||||
? (project.averageScore / maxScore) * 100
|
||||
: 0
|
||||
}
|
||||
className="h-2"
|
||||
/>
|
||||
</div>
|
||||
{/* Score bar - shown for 'bar' format */}
|
||||
{scoreFormat !== 'number' && scoreFormat !== 'radial' && (
|
||||
<div className="mt-3">
|
||||
<Progress
|
||||
value={
|
||||
project.averageScore
|
||||
? (project.averageScore / maxScore) * 100
|
||||
: 0
|
||||
}
|
||||
className="h-2"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
|
|
@ -178,10 +272,28 @@ function PublicScoresContent({ sessionId }: { sessionId: string }) {
|
|||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Audience voting info */}
|
||||
{session.allowAudienceVotes && isVoting && (
|
||||
<Card className="border-primary/30 bg-primary/5">
|
||||
<CardContent className="py-4 text-center">
|
||||
<p className="text-sm font-medium">
|
||||
Audience voting is enabled for this session
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Footer */}
|
||||
<p className="text-center text-white/60 text-sm">
|
||||
Scores update in real-time
|
||||
</p>
|
||||
<div className="flex items-center justify-center gap-2">
|
||||
{isConnected ? (
|
||||
<Wifi className="h-3 w-3 text-green-400" />
|
||||
) : (
|
||||
<WifiOff className="h-3 w-3 text-red-400" />
|
||||
)}
|
||||
<p className="text-center text-white/60 text-sm">
|
||||
Scores update in real-time
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
|
|
|||
|
|
@ -42,6 +42,8 @@ import {
|
|||
Camera,
|
||||
Lock,
|
||||
Bell,
|
||||
Mail,
|
||||
Calendar,
|
||||
Trash2,
|
||||
User,
|
||||
Tags,
|
||||
|
|
@ -61,6 +63,10 @@ export default function ProfileSettingsPage() {
|
|||
const [phoneNumber, setPhoneNumber] = useState('')
|
||||
const [notificationPreference, setNotificationPreference] = useState('EMAIL')
|
||||
const [expertiseTags, setExpertiseTags] = useState<string[]>([])
|
||||
const [digestFrequency, setDigestFrequency] = useState('none')
|
||||
const [preferredWorkload, setPreferredWorkload] = useState<number | null>(null)
|
||||
const [availabilityStart, setAvailabilityStart] = useState('')
|
||||
const [availabilityEnd, setAvailabilityEnd] = useState('')
|
||||
const [profileLoaded, setProfileLoaded] = useState(false)
|
||||
|
||||
// Password form state
|
||||
|
|
@ -81,6 +87,13 @@ export default function ProfileSettingsPage() {
|
|||
setPhoneNumber(user.phoneNumber || '')
|
||||
setNotificationPreference(user.notificationPreference || 'EMAIL')
|
||||
setExpertiseTags(user.expertiseTags || [])
|
||||
setDigestFrequency(user.digestFrequency || 'none')
|
||||
setPreferredWorkload(user.preferredWorkload ?? null)
|
||||
const avail = user.availabilityJson as { startDate?: string; endDate?: string } | null
|
||||
if (avail) {
|
||||
setAvailabilityStart(avail.startDate || '')
|
||||
setAvailabilityEnd(avail.endDate || '')
|
||||
}
|
||||
setProfileLoaded(true)
|
||||
}
|
||||
}, [user, profileLoaded])
|
||||
|
|
@ -93,6 +106,12 @@ export default function ProfileSettingsPage() {
|
|||
phoneNumber: phoneNumber || null,
|
||||
notificationPreference: notificationPreference as 'EMAIL' | 'WHATSAPP' | 'BOTH' | 'NONE',
|
||||
expertiseTags,
|
||||
digestFrequency: digestFrequency as 'none' | 'daily' | 'weekly',
|
||||
preferredWorkload: preferredWorkload ?? undefined,
|
||||
availabilityJson: (availabilityStart || availabilityEnd) ? {
|
||||
startDate: availabilityStart || undefined,
|
||||
endDate: availabilityEnd || undefined,
|
||||
} : undefined,
|
||||
})
|
||||
toast.success('Profile updated successfully')
|
||||
refetch()
|
||||
|
|
@ -275,6 +294,89 @@ export default function ProfileSettingsPage() {
|
|||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Email Digest */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<Mail className="h-5 w-5" />
|
||||
Email Digest
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Receive periodic email summaries of pending actions and updates
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="digest-frequency">Digest Frequency</Label>
|
||||
<Select value={digestFrequency} onValueChange={setDigestFrequency}>
|
||||
<SelectTrigger id="digest-frequency">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="none">No digest</SelectItem>
|
||||
<SelectItem value="daily">Daily summary</SelectItem>
|
||||
<SelectItem value="weekly">Weekly summary</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Get a summary of pending evaluations, upcoming deadlines, and recent activity
|
||||
</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Availability & Workload */}
|
||||
{(user.role === 'JURY_MEMBER' || user.role === 'SUPER_ADMIN' || user.role === 'PROGRAM_ADMIN') && (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<Calendar className="h-5 w-5" />
|
||||
Availability & Workload
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Set your availability window and preferred evaluation workload
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="avail-start">Available From</Label>
|
||||
<Input
|
||||
id="avail-start"
|
||||
type="date"
|
||||
value={availabilityStart}
|
||||
onChange={(e) => setAvailabilityStart(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="avail-end">Available Until</Label>
|
||||
<Input
|
||||
id="avail-end"
|
||||
type="date"
|
||||
value={availabilityEnd}
|
||||
onChange={(e) => setAvailabilityEnd(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="workload">Preferred Workload (projects per round)</Label>
|
||||
<Input
|
||||
id="workload"
|
||||
type="number"
|
||||
min={1}
|
||||
max={50}
|
||||
value={preferredWorkload ?? ''}
|
||||
onChange={(e) => setPreferredWorkload(e.target.value ? parseInt(e.target.value) : null)}
|
||||
placeholder="e.g. 10"
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
How many projects you prefer to evaluate per round
|
||||
</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Expertise Tags */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
import { NextResponse } from 'next/server'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function GET(request: NextRequest): Promise<NextResponse> {
|
||||
const cronSecret = request.headers.get('x-cron-secret')
|
||||
|
||||
if (!cronSecret || cronSecret !== process.env.CRON_SECRET) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
try {
|
||||
// Read retention period from system settings (default: 365 days)
|
||||
const retentionSetting = await prisma.systemSettings.findUnique({
|
||||
where: { key: 'audit_retention_days' },
|
||||
})
|
||||
|
||||
const retentionDays = retentionSetting
|
||||
? parseInt(retentionSetting.value, 10) || 365
|
||||
: 365
|
||||
|
||||
const cutoffDate = new Date()
|
||||
cutoffDate.setDate(cutoffDate.getDate() - retentionDays)
|
||||
|
||||
// Delete audit log entries older than the retention period
|
||||
const result = await prisma.auditLog.deleteMany({
|
||||
where: {
|
||||
timestamp: {
|
||||
lt: cutoffDate,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
cleanedUp: result.count,
|
||||
retentionDays,
|
||||
cutoffDate: cutoffDate.toISOString(),
|
||||
timestamp: new Date().toISOString(),
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Cron audit cleanup failed:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
import { NextResponse } from 'next/server'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { processDigests } from '@/server/services/email-digest'
|
||||
|
||||
export async function GET(request: NextRequest): Promise<NextResponse> {
|
||||
const cronSecret = request.headers.get('x-cron-secret')
|
||||
|
||||
if (!cronSecret || cronSecret !== process.env.CRON_SECRET) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
try {
|
||||
// Determine digest type: check query param, or default based on day of week
|
||||
const { searchParams } = new URL(request.url)
|
||||
let digestType = searchParams.get('type') as 'daily' | 'weekly' | null
|
||||
|
||||
if (!digestType) {
|
||||
const dayOfWeek = new Date().getDay()
|
||||
// Monday = 1 → run weekly; all other days → run daily
|
||||
digestType = dayOfWeek === 1 ? 'weekly' : 'daily'
|
||||
}
|
||||
|
||||
const result = await processDigests(digestType)
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
digestType,
|
||||
sent: result.sent,
|
||||
errors: result.errors,
|
||||
timestamp: new Date().toISOString(),
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Cron digest processing failed:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
import { NextResponse } from 'next/server'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function GET(request: NextRequest): Promise<NextResponse> {
|
||||
const cronSecret = request.headers.get('x-cron-secret')
|
||||
|
||||
if (!cronSecret || cronSecret !== process.env.CRON_SECRET) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
try {
|
||||
const now = new Date()
|
||||
|
||||
// Delete projects where isDraft=true AND draftExpiresAt has passed
|
||||
const result = await prisma.project.deleteMany({
|
||||
where: {
|
||||
isDraft: true,
|
||||
draftExpiresAt: {
|
||||
lt: now,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
cleanedUp: result.count,
|
||||
timestamp: now.toISOString(),
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Cron draft cleanup failed:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,124 @@
|
|||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { getPresignedUrl, BUCKET_NAME } from '@/lib/minio'
|
||||
|
||||
export async function POST(request: NextRequest): Promise<NextResponse> {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await request.json()
|
||||
const { projectId, fileIds } = body as {
|
||||
projectId?: string
|
||||
fileIds?: string[]
|
||||
}
|
||||
|
||||
if (!projectId || !fileIds || !Array.isArray(fileIds) || fileIds.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'projectId and fileIds array are required' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const userId = session.user.id
|
||||
const userRole = session.user.role
|
||||
|
||||
// Authorization: must be admin or assigned jury/mentor for this project
|
||||
const isAdmin = userRole === 'SUPER_ADMIN' || userRole === 'PROGRAM_ADMIN'
|
||||
|
||||
if (!isAdmin) {
|
||||
// Check if user is assigned as jury
|
||||
const juryAssignment = await prisma.assignment.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
projectId,
|
||||
},
|
||||
})
|
||||
|
||||
// Check if user is assigned as mentor
|
||||
const mentorAssignment = await prisma.mentorAssignment.findFirst({
|
||||
where: {
|
||||
mentorId: userId,
|
||||
projectId,
|
||||
},
|
||||
})
|
||||
|
||||
if (!juryAssignment && !mentorAssignment) {
|
||||
return NextResponse.json(
|
||||
{ error: 'You do not have access to this project\'s files' },
|
||||
{ status: 403 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch file metadata from DB
|
||||
const files = await prisma.projectFile.findMany({
|
||||
where: {
|
||||
id: { in: fileIds },
|
||||
projectId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
fileName: true,
|
||||
objectKey: true,
|
||||
mimeType: true,
|
||||
size: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (files.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No matching files found' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
// Generate signed download URLs for each file
|
||||
const downloadUrls = await Promise.all(
|
||||
files.map(async (file) => {
|
||||
try {
|
||||
const downloadUrl = await getPresignedUrl(
|
||||
BUCKET_NAME,
|
||||
file.objectKey,
|
||||
'GET',
|
||||
3600 // 1 hour expiry for bulk downloads
|
||||
)
|
||||
|
||||
return {
|
||||
id: file.id,
|
||||
fileName: file.fileName,
|
||||
mimeType: file.mimeType,
|
||||
size: file.size,
|
||||
downloadUrl,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[BulkDownload] Failed to get URL for file ${file.id}:`, error)
|
||||
return {
|
||||
id: file.id,
|
||||
fileName: file.fileName,
|
||||
mimeType: file.mimeType,
|
||||
size: file.size,
|
||||
downloadUrl: null,
|
||||
error: 'Failed to generate download URL',
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return NextResponse.json({
|
||||
projectId,
|
||||
files: downloadUrls,
|
||||
expiresIn: 3600,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('[BulkDownload] Error:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,180 @@
|
|||
import { NextRequest } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
export async function GET(request: NextRequest): Promise<Response> {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const sessionId = searchParams.get('sessionId')
|
||||
|
||||
if (!sessionId) {
|
||||
return new Response(JSON.stringify({ error: 'sessionId is required' }), {
|
||||
status: 400,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
}
|
||||
|
||||
// Verify the session exists
|
||||
const session = await prisma.liveVotingSession.findUnique({
|
||||
where: { id: sessionId },
|
||||
select: { id: true, status: true },
|
||||
})
|
||||
|
||||
if (!session) {
|
||||
return new Response(JSON.stringify({ error: 'Session not found' }), {
|
||||
status: 404,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
}
|
||||
|
||||
const encoder = new TextEncoder()
|
||||
|
||||
const stream = new ReadableStream({
|
||||
async start(controller) {
|
||||
// Track state for change detection
|
||||
let lastVoteCount = -1
|
||||
let lastProjectId: string | null = null
|
||||
let lastStatus: string | null = null
|
||||
|
||||
const sendEvent = (event: string, data: unknown) => {
|
||||
const payload = `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`
|
||||
controller.enqueue(encoder.encode(payload))
|
||||
}
|
||||
|
||||
// Send initial connection event
|
||||
sendEvent('connected', { sessionId, timestamp: new Date().toISOString() })
|
||||
|
||||
const poll = async () => {
|
||||
try {
|
||||
const currentSession = await prisma.liveVotingSession.findUnique({
|
||||
where: { id: sessionId },
|
||||
select: {
|
||||
status: true,
|
||||
currentProjectId: true,
|
||||
currentProjectIndex: true,
|
||||
votingEndsAt: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (!currentSession) {
|
||||
sendEvent('session_status', { status: 'DELETED' })
|
||||
controller.close()
|
||||
return false
|
||||
}
|
||||
|
||||
// Check for status changes
|
||||
if (lastStatus !== null && currentSession.status !== lastStatus) {
|
||||
sendEvent('session_status', {
|
||||
status: currentSession.status,
|
||||
timestamp: new Date().toISOString(),
|
||||
})
|
||||
}
|
||||
lastStatus = currentSession.status
|
||||
|
||||
// Check for project changes
|
||||
if (
|
||||
lastProjectId !== null &&
|
||||
currentSession.currentProjectId !== lastProjectId
|
||||
) {
|
||||
sendEvent('project_change', {
|
||||
projectId: currentSession.currentProjectId,
|
||||
projectIndex: currentSession.currentProjectIndex,
|
||||
timestamp: new Date().toISOString(),
|
||||
})
|
||||
}
|
||||
lastProjectId = currentSession.currentProjectId
|
||||
|
||||
// Check for vote updates on the current project
|
||||
if (currentSession.currentProjectId) {
|
||||
const voteCount = await prisma.liveVote.count({
|
||||
where: {
|
||||
sessionId,
|
||||
projectId: currentSession.currentProjectId,
|
||||
},
|
||||
})
|
||||
|
||||
if (lastVoteCount !== -1 && voteCount !== lastVoteCount) {
|
||||
// Get the latest vote info
|
||||
const latestVotes = await prisma.liveVote.findMany({
|
||||
where: {
|
||||
sessionId,
|
||||
projectId: currentSession.currentProjectId,
|
||||
},
|
||||
select: {
|
||||
score: true,
|
||||
isAudienceVote: true,
|
||||
votedAt: true,
|
||||
},
|
||||
orderBy: { votedAt: 'desc' },
|
||||
take: 1,
|
||||
})
|
||||
|
||||
const avgScore = await prisma.liveVote.aggregate({
|
||||
where: {
|
||||
sessionId,
|
||||
projectId: currentSession.currentProjectId,
|
||||
},
|
||||
_avg: { score: true },
|
||||
_count: true,
|
||||
})
|
||||
|
||||
sendEvent('vote_update', {
|
||||
projectId: currentSession.currentProjectId,
|
||||
totalVotes: voteCount,
|
||||
averageScore: avgScore._avg.score,
|
||||
latestVote: latestVotes[0] || null,
|
||||
timestamp: new Date().toISOString(),
|
||||
})
|
||||
}
|
||||
lastVoteCount = voteCount
|
||||
}
|
||||
|
||||
// Stop polling if session is completed
|
||||
if (currentSession.status === 'COMPLETED') {
|
||||
sendEvent('session_status', {
|
||||
status: 'COMPLETED',
|
||||
timestamp: new Date().toISOString(),
|
||||
})
|
||||
controller.close()
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('[SSE] Poll error:', error)
|
||||
return true // Keep trying
|
||||
}
|
||||
}
|
||||
|
||||
// Initial poll to set baseline state
|
||||
const shouldContinue = await poll()
|
||||
if (!shouldContinue) return
|
||||
|
||||
// Poll every 2 seconds
|
||||
const interval = setInterval(async () => {
|
||||
const cont = await poll()
|
||||
if (!cont) {
|
||||
clearInterval(interval)
|
||||
}
|
||||
}, 2000)
|
||||
|
||||
// Clean up on abort
|
||||
request.signal.addEventListener('abort', () => {
|
||||
clearInterval(interval)
|
||||
try {
|
||||
controller.close()
|
||||
} catch {
|
||||
// Stream may already be closed
|
||||
}
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
return new Response(stream, {
|
||||
headers: {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Connection': 'keep-alive',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
import type { Metadata } from 'next'
|
||||
import { NextIntlClientProvider } from 'next-intl'
|
||||
import { getLocale, getMessages } from 'next-intl/server'
|
||||
import './globals.css'
|
||||
import { Providers } from './providers'
|
||||
import { Toaster } from 'sonner'
|
||||
|
|
@ -14,15 +16,20 @@ export const metadata: Metadata = {
|
|||
},
|
||||
}
|
||||
|
||||
export default function RootLayout({
|
||||
export default async function RootLayout({
|
||||
children,
|
||||
}: Readonly<{
|
||||
children: React.ReactNode
|
||||
}>) {
|
||||
const locale = await getLocale()
|
||||
const messages = await getMessages()
|
||||
|
||||
return (
|
||||
<html lang="en" className="light">
|
||||
<html lang={locale} className="light">
|
||||
<body className="min-h-screen bg-background font-sans antialiased">
|
||||
<Providers>{children}</Providers>
|
||||
<NextIntlClientProvider messages={messages}>
|
||||
<Providers>{children}</Providers>
|
||||
</NextIntlClientProvider>
|
||||
<Toaster
|
||||
position="top-right"
|
||||
toastOptions={{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,156 @@
|
|||
'use client'
|
||||
|
||||
import { useState, useCallback } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { FileDown, Loader2 } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
interface PdfReportProps {
|
||||
roundId: string
|
||||
sections: string[]
|
||||
}
|
||||
|
||||
function buildReportHtml(reportData: Record<string, unknown>): string {
|
||||
const parts: string[] = []
|
||||
|
||||
parts.push(`<!DOCTYPE html><html><head>
|
||||
<title>Round Report - ${String(reportData.roundName || 'Report')}</title>
|
||||
<style>
|
||||
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;600;700&display=swap');
|
||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
body { font-family: 'Montserrat', sans-serif; color: #1a1a1a; padding: 40px; max-width: 1000px; margin: 0 auto; }
|
||||
h1 { color: #053d57; font-size: 24px; font-weight: 700; margin-bottom: 8px; }
|
||||
h2 { color: #053d57; font-size: 18px; font-weight: 600; margin: 24px 0 12px; border-bottom: 2px solid #053d57; padding-bottom: 4px; }
|
||||
p { font-size: 12px; line-height: 1.6; margin-bottom: 8px; }
|
||||
.subtitle { color: #557f8c; font-size: 14px; margin-bottom: 24px; }
|
||||
.generated { color: #888; font-size: 10px; margin-bottom: 32px; }
|
||||
table { width: 100%; border-collapse: collapse; margin: 12px 0; font-size: 11px; }
|
||||
th { background: #053d57; color: white; text-align: left; padding: 8px 12px; font-weight: 600; }
|
||||
td { padding: 6px 12px; border-bottom: 1px solid #e0e0e0; }
|
||||
tr:nth-child(even) td { background: #f8f8f8; }
|
||||
.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin: 16px 0; }
|
||||
.stat-card { background: #f0f4f8; border-radius: 8px; padding: 16px; text-align: center; }
|
||||
.stat-value { font-size: 28px; font-weight: 700; color: #053d57; }
|
||||
.stat-label { font-size: 11px; color: #557f8c; margin-top: 4px; }
|
||||
@media print { body { padding: 20px; } .no-print { display: none; } }
|
||||
</style>
|
||||
</head><body>`)
|
||||
|
||||
parts.push(`<div class="no-print" style="margin-bottom: 20px;">
|
||||
<button onclick="window.print()" style="background: #053d57; color: white; border: none; padding: 10px 20px; border-radius: 6px; cursor: pointer; font-family: Montserrat; font-weight: 600;">
|
||||
Print / Save as PDF
|
||||
</button>
|
||||
</div>`)
|
||||
|
||||
parts.push(`<h1>${escapeHtml(String(reportData.roundName || 'Round Report'))}</h1>`)
|
||||
parts.push(`<p class="subtitle">${escapeHtml(String(reportData.programName || ''))}</p>`)
|
||||
parts.push(`<p class="generated">Generated on ${new Date().toLocaleString()}</p>`)
|
||||
|
||||
const summary = reportData.summary as Record<string, unknown> | undefined
|
||||
if (summary) {
|
||||
parts.push(`<h2>Summary</h2><div class="stat-grid">`)
|
||||
parts.push(statCard(summary.totalProjects, 'Projects'))
|
||||
parts.push(statCard(summary.totalEvaluations, 'Evaluations'))
|
||||
parts.push(statCard(summary.averageScore != null ? Number(summary.averageScore).toFixed(1) : '--', 'Avg Score'))
|
||||
parts.push(statCard(summary.completionRate != null ? Number(summary.completionRate).toFixed(0) + '%' : '--', 'Completion'))
|
||||
parts.push(`</div>`)
|
||||
}
|
||||
|
||||
const rankings = reportData.rankings as Array<Record<string, unknown>> | undefined
|
||||
if (rankings && rankings.length > 0) {
|
||||
parts.push(`<h2>Project Rankings</h2><table><thead><tr>
|
||||
<th>#</th><th>Project</th><th>Team</th><th>Avg Score</th><th>Evaluations</th>
|
||||
</tr></thead><tbody>`)
|
||||
for (const p of rankings) {
|
||||
parts.push(`<tr>
|
||||
<td>${escapeHtml(String(p.rank ?? ''))}</td>
|
||||
<td>${escapeHtml(String(p.title ?? ''))}</td>
|
||||
<td>${escapeHtml(String(p.team ?? ''))}</td>
|
||||
<td>${Number(p.avgScore ?? 0).toFixed(2)}</td>
|
||||
<td>${String(p.evalCount ?? 0)}</td>
|
||||
</tr>`)
|
||||
}
|
||||
parts.push(`</tbody></table>`)
|
||||
}
|
||||
|
||||
const jurorStats = reportData.jurorStats as Array<Record<string, unknown>> | undefined
|
||||
if (jurorStats && jurorStats.length > 0) {
|
||||
parts.push(`<h2>Juror Statistics</h2><table><thead><tr>
|
||||
<th>Juror</th><th>Assigned</th><th>Completed</th><th>Completion %</th><th>Avg Score Given</th>
|
||||
</tr></thead><tbody>`)
|
||||
for (const j of jurorStats) {
|
||||
parts.push(`<tr>
|
||||
<td>${escapeHtml(String(j.name ?? ''))}</td>
|
||||
<td>${String(j.assigned ?? 0)}</td>
|
||||
<td>${String(j.completed ?? 0)}</td>
|
||||
<td>${Number(j.completionRate ?? 0).toFixed(0)}%</td>
|
||||
<td>${Number(j.avgScore ?? 0).toFixed(2)}</td>
|
||||
</tr>`)
|
||||
}
|
||||
parts.push(`</tbody></table>`)
|
||||
}
|
||||
|
||||
parts.push(`</body></html>`)
|
||||
return parts.join('')
|
||||
}
|
||||
|
||||
function escapeHtml(str: string): string {
|
||||
return str
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
}
|
||||
|
||||
function statCard(value: unknown, label: string): string {
|
||||
return `<div class="stat-card"><div class="stat-value">${escapeHtml(String(value ?? 0))}</div><div class="stat-label">${escapeHtml(label)}</div></div>`
|
||||
}
|
||||
|
||||
export function PdfReportGenerator({ roundId, sections }: PdfReportProps) {
|
||||
const [generating, setGenerating] = useState(false)
|
||||
|
||||
const { refetch } = trpc.export.getReportData.useQuery(
|
||||
{ roundId, sections },
|
||||
{ enabled: false }
|
||||
)
|
||||
|
||||
const handleGenerate = useCallback(async () => {
|
||||
setGenerating(true)
|
||||
try {
|
||||
const result = await refetch()
|
||||
if (!result.data) {
|
||||
toast.error('Failed to fetch report data')
|
||||
return
|
||||
}
|
||||
|
||||
const html = buildReportHtml(result.data as Record<string, unknown>)
|
||||
const blob = new Blob([html], { type: 'text/html;charset=utf-8' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const newWindow = window.open(url, '_blank')
|
||||
if (!newWindow) {
|
||||
toast.error('Pop-up blocked. Please allow pop-ups and try again.')
|
||||
URL.revokeObjectURL(url)
|
||||
return
|
||||
}
|
||||
// Clean up after a delay
|
||||
setTimeout(() => URL.revokeObjectURL(url), 5000)
|
||||
toast.success('Report generated. Use the Print button or Ctrl+P to save as PDF.')
|
||||
} catch {
|
||||
toast.error('Failed to generate report')
|
||||
} finally {
|
||||
setGenerating(false)
|
||||
}
|
||||
}, [refetch])
|
||||
|
||||
return (
|
||||
<Button variant="outline" onClick={handleGenerate} disabled={generating}>
|
||||
{generating ? (
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<FileDown className="mr-2 h-4 w-4" />
|
||||
)}
|
||||
{generating ? 'Generating...' : 'Export PDF Report'}
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,153 @@
|
|||
'use client'
|
||||
|
||||
import {
|
||||
BarChart,
|
||||
Bar,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
ResponsiveContainer,
|
||||
Legend,
|
||||
} from 'recharts'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
|
||||
interface RoundComparison {
|
||||
roundId: string
|
||||
roundName: string
|
||||
projectCount: number
|
||||
evaluationCount: number
|
||||
completionRate: number
|
||||
averageScore: number | null
|
||||
scoreDistribution: { score: number; count: number }[]
|
||||
}
|
||||
|
||||
interface CrossRoundComparisonProps {
|
||||
data: RoundComparison[]
|
||||
}
|
||||
|
||||
const ROUND_COLORS = ['#053d57', '#de0f1e', '#557f8c', '#f38a52', '#6ad82f']
|
||||
|
||||
export function CrossRoundComparisonChart({ data }: CrossRoundComparisonProps) {
|
||||
// Prepare comparison data
|
||||
const comparisonData = data.map((round, i) => ({
|
||||
name: round.roundName.length > 20 ? round.roundName.slice(0, 20) + '...' : round.roundName,
|
||||
projects: round.projectCount,
|
||||
evaluations: round.evaluationCount,
|
||||
completionRate: round.completionRate,
|
||||
avgScore: round.averageScore ? parseFloat(round.averageScore.toFixed(2)) : 0,
|
||||
color: ROUND_COLORS[i % ROUND_COLORS.length],
|
||||
}))
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Metrics Comparison */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Round Metrics Comparison</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="h-[350px]">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<BarChart
|
||||
data={comparisonData}
|
||||
margin={{ top: 20, right: 30, bottom: 60, left: 20 }}
|
||||
>
|
||||
<CartesianGrid strokeDasharray="3 3" className="stroke-muted" />
|
||||
<XAxis
|
||||
dataKey="name"
|
||||
angle={-25}
|
||||
textAnchor="end"
|
||||
height={60}
|
||||
tick={{ fontSize: 12 }}
|
||||
/>
|
||||
<YAxis />
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
backgroundColor: 'hsl(var(--card))',
|
||||
border: '1px solid hsl(var(--border))',
|
||||
borderRadius: '6px',
|
||||
}}
|
||||
/>
|
||||
<Legend />
|
||||
<Bar dataKey="projects" name="Projects" fill="#053d57" radius={[4, 4, 0, 0]} />
|
||||
<Bar dataKey="evaluations" name="Evaluations" fill="#557f8c" radius={[4, 4, 0, 0]} />
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Completion & Score Comparison */}
|
||||
<div className="grid gap-6 lg:grid-cols-2">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Completion Rate by Round</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="h-[300px]">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<BarChart
|
||||
data={comparisonData}
|
||||
margin={{ top: 20, right: 20, bottom: 60, left: 20 }}
|
||||
>
|
||||
<CartesianGrid strokeDasharray="3 3" className="stroke-muted" />
|
||||
<XAxis
|
||||
dataKey="name"
|
||||
angle={-25}
|
||||
textAnchor="end"
|
||||
height={60}
|
||||
tick={{ fontSize: 12 }}
|
||||
/>
|
||||
<YAxis domain={[0, 100]} unit="%" />
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
backgroundColor: 'hsl(var(--card))',
|
||||
border: '1px solid hsl(var(--border))',
|
||||
borderRadius: '6px',
|
||||
}}
|
||||
/>
|
||||
<Bar dataKey="completionRate" name="Completion %" fill="#6ad82f" radius={[4, 4, 0, 0]} />
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Average Score by Round</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="h-[300px]">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<BarChart
|
||||
data={comparisonData}
|
||||
margin={{ top: 20, right: 20, bottom: 60, left: 20 }}
|
||||
>
|
||||
<CartesianGrid strokeDasharray="3 3" className="stroke-muted" />
|
||||
<XAxis
|
||||
dataKey="name"
|
||||
angle={-25}
|
||||
textAnchor="end"
|
||||
height={60}
|
||||
tick={{ fontSize: 12 }}
|
||||
/>
|
||||
<YAxis domain={[0, 10]} />
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
backgroundColor: 'hsl(var(--card))',
|
||||
border: '1px solid hsl(var(--border))',
|
||||
borderRadius: '6px',
|
||||
}}
|
||||
/>
|
||||
<Bar dataKey="avgScore" name="Avg Score" fill="#de0f1e" radius={[4, 4, 0, 0]} />
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,230 @@
|
|||
'use client'
|
||||
|
||||
import {
|
||||
PieChart,
|
||||
Pie,
|
||||
Cell,
|
||||
Tooltip,
|
||||
ResponsiveContainer,
|
||||
Legend,
|
||||
BarChart,
|
||||
Bar,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
} from 'recharts'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
|
||||
interface DiversityData {
|
||||
total: number
|
||||
byCountry: { country: string; count: number; percentage: number }[]
|
||||
byCategory: { category: string; count: number; percentage: number }[]
|
||||
byOceanIssue: { issue: string; count: number; percentage: number }[]
|
||||
byTag: { tag: string; count: number; percentage: number }[]
|
||||
}
|
||||
|
||||
interface DiversityMetricsProps {
|
||||
data: DiversityData
|
||||
}
|
||||
|
||||
const PIE_COLORS = [
|
||||
'#053d57', '#de0f1e', '#557f8c', '#f38a52', '#6ad82f',
|
||||
'#3be31e', '#c9c052', '#e6382f', '#ed6141', '#0bd90f',
|
||||
'#8884d8', '#82ca9d', '#ffc658', '#ff7c7c', '#8dd1e1',
|
||||
]
|
||||
|
||||
export function DiversityMetricsChart({ data }: DiversityMetricsProps) {
|
||||
if (data.total === 0) {
|
||||
return (
|
||||
<Card>
|
||||
<CardContent className="flex items-center justify-center py-12">
|
||||
<p className="text-muted-foreground">No project data available</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
// Top countries for pie chart (max 10, others grouped)
|
||||
const topCountries = data.byCountry.slice(0, 10)
|
||||
const otherCountries = data.byCountry.slice(10)
|
||||
const countryPieData = otherCountries.length > 0
|
||||
? [...topCountries, {
|
||||
country: 'Others',
|
||||
count: otherCountries.reduce((sum, c) => sum + c.count, 0),
|
||||
percentage: otherCountries.reduce((sum, c) => sum + c.percentage, 0),
|
||||
}]
|
||||
: topCountries
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Summary */}
|
||||
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
|
||||
<Card>
|
||||
<CardContent className="pt-6">
|
||||
<div className="text-2xl font-bold">{data.total}</div>
|
||||
<p className="text-sm text-muted-foreground">Total Projects</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card>
|
||||
<CardContent className="pt-6">
|
||||
<div className="text-2xl font-bold">{data.byCountry.length}</div>
|
||||
<p className="text-sm text-muted-foreground">Countries Represented</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card>
|
||||
<CardContent className="pt-6">
|
||||
<div className="text-2xl font-bold">{data.byCategory.length}</div>
|
||||
<p className="text-sm text-muted-foreground">Categories</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card>
|
||||
<CardContent className="pt-6">
|
||||
<div className="text-2xl font-bold">{data.byTag.length}</div>
|
||||
<p className="text-sm text-muted-foreground">Unique Tags</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-6 lg:grid-cols-2">
|
||||
{/* Country Distribution */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Geographic Distribution</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="h-[350px]">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<PieChart>
|
||||
<Pie
|
||||
data={countryPieData}
|
||||
cx="50%"
|
||||
cy="50%"
|
||||
innerRadius={60}
|
||||
outerRadius={120}
|
||||
paddingAngle={2}
|
||||
dataKey="count"
|
||||
nameKey="country"
|
||||
label
|
||||
>
|
||||
{countryPieData.map((_, index) => (
|
||||
<Cell key={`cell-${index}`} fill={PIE_COLORS[index % PIE_COLORS.length]} />
|
||||
))}
|
||||
</Pie>
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
backgroundColor: 'hsl(var(--card))',
|
||||
border: '1px solid hsl(var(--border))',
|
||||
borderRadius: '6px',
|
||||
}}
|
||||
/>
|
||||
</PieChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Category Distribution */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Competition Categories</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{data.byCategory.length > 0 ? (
|
||||
<div className="h-[350px]">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<BarChart
|
||||
data={data.byCategory.slice(0, 10)}
|
||||
layout="vertical"
|
||||
margin={{ top: 5, right: 30, bottom: 5, left: 100 }}
|
||||
>
|
||||
<CartesianGrid strokeDasharray="3 3" className="stroke-muted" />
|
||||
<XAxis type="number" />
|
||||
<YAxis
|
||||
type="category"
|
||||
dataKey="category"
|
||||
width={90}
|
||||
tick={{ fontSize: 12 }}
|
||||
/>
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
backgroundColor: 'hsl(var(--card))',
|
||||
border: '1px solid hsl(var(--border))',
|
||||
borderRadius: '6px',
|
||||
}}
|
||||
/>
|
||||
<Bar dataKey="count" fill="#053d57" radius={[0, 4, 4, 0]} />
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-muted-foreground text-center py-8">No category data</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* Ocean Issues */}
|
||||
{data.byOceanIssue.length > 0 && (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Ocean Issues Addressed</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="h-[350px]">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<BarChart
|
||||
data={data.byOceanIssue.slice(0, 15)}
|
||||
margin={{ top: 20, right: 30, bottom: 60, left: 20 }}
|
||||
>
|
||||
<CartesianGrid strokeDasharray="3 3" className="stroke-muted" />
|
||||
<XAxis
|
||||
dataKey="issue"
|
||||
angle={-35}
|
||||
textAnchor="end"
|
||||
height={80}
|
||||
tick={{ fontSize: 11 }}
|
||||
/>
|
||||
<YAxis />
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
backgroundColor: 'hsl(var(--card))',
|
||||
border: '1px solid hsl(var(--border))',
|
||||
borderRadius: '6px',
|
||||
}}
|
||||
/>
|
||||
<Bar dataKey="count" fill="#557f8c" radius={[4, 4, 0, 0]} />
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Tags Cloud */}
|
||||
{data.byTag.length > 0 && (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Project Tags</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{data.byTag.slice(0, 30).map((tag) => (
|
||||
<Badge
|
||||
key={tag.tag}
|
||||
variant="secondary"
|
||||
className="text-sm"
|
||||
style={{
|
||||
fontSize: `${Math.max(0.7, Math.min(1.4, 0.7 + tag.percentage / 20))}rem`,
|
||||
}}
|
||||
>
|
||||
{tag.tag} ({tag.count})
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -6,3 +6,7 @@ export { ProjectRankingsChart } from './project-rankings'
|
|||
export { CriteriaScoresChart } from './criteria-scores'
|
||||
export { GeographicDistribution } from './geographic-distribution'
|
||||
export { GeographicSummaryCard } from './geographic-summary-card'
|
||||
// Advanced analytics charts (F10)
|
||||
export { CrossRoundComparisonChart } from './cross-round-comparison'
|
||||
export { JurorConsistencyChart } from './juror-consistency'
|
||||
export { DiversityMetricsChart } from './diversity-metrics'
|
||||
|
|
|
|||
|
|
@ -0,0 +1,171 @@
|
|||
'use client'
|
||||
|
||||
import {
|
||||
ScatterChart,
|
||||
Scatter,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
ResponsiveContainer,
|
||||
ReferenceLine,
|
||||
} from 'recharts'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from '@/components/ui/table'
|
||||
import { AlertTriangle } from 'lucide-react'
|
||||
|
||||
interface JurorMetric {
|
||||
userId: string
|
||||
name: string
|
||||
email: string
|
||||
evaluationCount: number
|
||||
averageScore: number
|
||||
stddev: number
|
||||
deviationFromOverall: number
|
||||
isOutlier: boolean
|
||||
}
|
||||
|
||||
interface JurorConsistencyProps {
|
||||
data: {
|
||||
overallAverage: number
|
||||
jurors: JurorMetric[]
|
||||
}
|
||||
}
|
||||
|
||||
export function JurorConsistencyChart({ data }: JurorConsistencyProps) {
|
||||
const scatterData = data.jurors.map((j) => ({
|
||||
name: j.name,
|
||||
avgScore: parseFloat(j.averageScore.toFixed(2)),
|
||||
stddev: parseFloat(j.stddev.toFixed(2)),
|
||||
evaluations: j.evaluationCount,
|
||||
isOutlier: j.isOutlier,
|
||||
}))
|
||||
|
||||
const outlierCount = data.jurors.filter((j) => j.isOutlier).length
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Scatter: Average Score vs Standard Deviation */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center justify-between">
|
||||
<span>Juror Scoring Patterns</span>
|
||||
<span className="text-sm font-normal text-muted-foreground">
|
||||
Overall Avg: {data.overallAverage.toFixed(2)}
|
||||
{outlierCount > 0 && (
|
||||
<Badge variant="destructive" className="ml-2">
|
||||
{outlierCount} outlier{outlierCount > 1 ? 's' : ''}
|
||||
</Badge>
|
||||
)}
|
||||
</span>
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="h-[400px]">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<ScatterChart margin={{ top: 20, right: 20, bottom: 20, left: 20 }}>
|
||||
<CartesianGrid strokeDasharray="3 3" className="stroke-muted" />
|
||||
<XAxis
|
||||
type="number"
|
||||
dataKey="avgScore"
|
||||
name="Average Score"
|
||||
domain={[0, 10]}
|
||||
label={{ value: 'Average Score', position: 'insideBottom', offset: -10 }}
|
||||
/>
|
||||
<YAxis
|
||||
type="number"
|
||||
dataKey="stddev"
|
||||
name="Std Deviation"
|
||||
label={{ value: 'Std Deviation', angle: -90, position: 'insideLeft' }}
|
||||
/>
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
backgroundColor: 'hsl(var(--card))',
|
||||
border: '1px solid hsl(var(--border))',
|
||||
borderRadius: '6px',
|
||||
}}
|
||||
/>
|
||||
<ReferenceLine
|
||||
x={data.overallAverage}
|
||||
stroke="#de0f1e"
|
||||
strokeDasharray="3 3"
|
||||
label={{ value: 'Avg', fill: '#de0f1e', position: 'top' }}
|
||||
/>
|
||||
<Scatter data={scatterData} fill="#053d57">
|
||||
{scatterData.map((entry, index) => (
|
||||
<circle
|
||||
key={index}
|
||||
r={Math.max(4, entry.evaluations)}
|
||||
fill={entry.isOutlier ? '#de0f1e' : '#053d57'}
|
||||
fillOpacity={0.7}
|
||||
/>
|
||||
))}
|
||||
</Scatter>
|
||||
</ScatterChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground mt-2 text-center">
|
||||
Dot size represents number of evaluations. Red dots indicate outlier jurors (2+ points from mean).
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Juror details table */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Juror Consistency Details</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Juror</TableHead>
|
||||
<TableHead className="text-right">Evaluations</TableHead>
|
||||
<TableHead className="text-right">Avg Score</TableHead>
|
||||
<TableHead className="text-right">Std Dev</TableHead>
|
||||
<TableHead className="text-right">Deviation from Mean</TableHead>
|
||||
<TableHead className="text-center">Status</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{data.jurors.map((juror) => (
|
||||
<TableRow key={juror.userId} className={juror.isOutlier ? 'bg-destructive/5' : ''}>
|
||||
<TableCell>
|
||||
<div>
|
||||
<p className="font-medium">{juror.name}</p>
|
||||
<p className="text-xs text-muted-foreground">{juror.email}</p>
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-right tabular-nums">{juror.evaluationCount}</TableCell>
|
||||
<TableCell className="text-right tabular-nums">{juror.averageScore.toFixed(2)}</TableCell>
|
||||
<TableCell className="text-right tabular-nums">{juror.stddev.toFixed(2)}</TableCell>
|
||||
<TableCell className="text-right tabular-nums">
|
||||
{juror.deviationFromOverall.toFixed(2)}
|
||||
</TableCell>
|
||||
<TableCell className="text-center">
|
||||
{juror.isOutlier ? (
|
||||
<Badge variant="destructive" className="gap-1">
|
||||
<AlertTriangle className="h-3 w-3" />
|
||||
Outlier
|
||||
</Badge>
|
||||
) : (
|
||||
<Badge variant="secondary">Normal</Badge>
|
||||
)}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -32,6 +32,8 @@ import {
|
|||
History,
|
||||
Trophy,
|
||||
User,
|
||||
LayoutTemplate,
|
||||
MessageSquare,
|
||||
} from 'lucide-react'
|
||||
import { getInitials } from '@/lib/utils'
|
||||
import { Logo } from '@/components/shared/logo'
|
||||
|
|
@ -60,6 +62,11 @@ const navigation = [
|
|||
href: '/admin/rounds' as const,
|
||||
icon: CircleDot,
|
||||
},
|
||||
{
|
||||
name: 'Templates',
|
||||
href: '/admin/round-templates' as const,
|
||||
icon: LayoutTemplate,
|
||||
},
|
||||
{
|
||||
name: 'Awards',
|
||||
href: '/admin/awards' as const,
|
||||
|
|
@ -85,6 +92,11 @@ const navigation = [
|
|||
href: '/admin/learning' as const,
|
||||
icon: BookOpen,
|
||||
},
|
||||
{
|
||||
name: 'Messages',
|
||||
href: '/admin/messages' as const,
|
||||
icon: MessageSquare,
|
||||
},
|
||||
{
|
||||
name: 'Partners',
|
||||
href: '/admin/partners' as const,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
'use client'
|
||||
|
||||
import { BookOpen, ClipboardList, Home } from 'lucide-react'
|
||||
import { BookOpen, ClipboardList, GitCompare, Home } from 'lucide-react'
|
||||
import { RoleNav, type NavItem, type RoleNavUser } from '@/components/layouts/role-nav'
|
||||
|
||||
const navigation: NavItem[] = [
|
||||
|
|
@ -14,6 +14,11 @@ const navigation: NavItem[] = [
|
|||
href: '/jury/assignments',
|
||||
icon: ClipboardList,
|
||||
},
|
||||
{
|
||||
name: 'Compare',
|
||||
href: '/jury/compare',
|
||||
icon: GitCompare,
|
||||
},
|
||||
{
|
||||
name: 'Learning Hub',
|
||||
href: '/jury/learning',
|
||||
|
|
|
|||
|
|
@ -20,6 +20,12 @@ import {
|
|||
Bell,
|
||||
Tags,
|
||||
ExternalLink,
|
||||
Newspaper,
|
||||
BarChart3,
|
||||
ShieldAlert,
|
||||
Globe,
|
||||
Webhook,
|
||||
LayoutTemplate,
|
||||
} from 'lucide-react'
|
||||
import Link from 'next/link'
|
||||
import { Button } from '@/components/ui/button'
|
||||
|
|
@ -112,9 +118,42 @@ export function SettingsContent({ initialSettings }: SettingsContentProps) {
|
|||
'autosave_interval_seconds',
|
||||
])
|
||||
|
||||
const digestSettings = getSettingsByKeys([
|
||||
'digest_enabled',
|
||||
'digest_default_frequency',
|
||||
'digest_send_hour',
|
||||
'digest_include_evaluations',
|
||||
'digest_include_assignments',
|
||||
'digest_include_deadlines',
|
||||
'digest_include_announcements',
|
||||
])
|
||||
|
||||
const analyticsSettings = getSettingsByKeys([
|
||||
'analytics_observer_scores_tab',
|
||||
'analytics_observer_progress_tab',
|
||||
'analytics_observer_juror_tab',
|
||||
'analytics_observer_comparison_tab',
|
||||
'analytics_pdf_enabled',
|
||||
'analytics_pdf_sections',
|
||||
])
|
||||
|
||||
const auditSecuritySettings = getSettingsByKeys([
|
||||
'audit_retention_days',
|
||||
'anomaly_detection_enabled',
|
||||
'anomaly_rapid_actions_threshold',
|
||||
'anomaly_off_hours_start',
|
||||
'anomaly_off_hours_end',
|
||||
])
|
||||
|
||||
const localizationSettings = getSettingsByKeys([
|
||||
'localization_enabled_locales',
|
||||
'localization_default_locale',
|
||||
])
|
||||
|
||||
return (
|
||||
<>
|
||||
<Tabs defaultValue="ai" className="space-y-6">
|
||||
<TabsList className="grid w-full grid-cols-4 lg:grid-cols-8">
|
||||
<TabsList className="flex flex-wrap h-auto gap-1">
|
||||
<TabsTrigger value="ai" className="gap-2">
|
||||
<Bot className="h-4 w-4" />
|
||||
<span className="hidden sm:inline">AI</span>
|
||||
|
|
@ -147,6 +186,22 @@ export function SettingsContent({ initialSettings }: SettingsContentProps) {
|
|||
<SettingsIcon className="h-4 w-4" />
|
||||
<span className="hidden sm:inline">Defaults</span>
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="digest" className="gap-2">
|
||||
<Newspaper className="h-4 w-4" />
|
||||
<span className="hidden sm:inline">Digest</span>
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="analytics" className="gap-2">
|
||||
<BarChart3 className="h-4 w-4" />
|
||||
<span className="hidden sm:inline">Analytics</span>
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="audit" className="gap-2">
|
||||
<ShieldAlert className="h-4 w-4" />
|
||||
<span className="hidden sm:inline">Audit</span>
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="localization" className="gap-2">
|
||||
<Globe className="h-4 w-4" />
|
||||
<span className="hidden sm:inline">Locale</span>
|
||||
</TabsTrigger>
|
||||
</TabsList>
|
||||
|
||||
<TabsContent value="ai" className="space-y-6">
|
||||
|
|
@ -279,8 +334,456 @@ export function SettingsContent({ initialSettings }: SettingsContentProps) {
|
|||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="digest" className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Digest Configuration</CardTitle>
|
||||
<CardDescription>
|
||||
Configure automated digest emails sent to users
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<DigestSettingsSection settings={digestSettings} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="analytics" className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Analytics & Reports</CardTitle>
|
||||
<CardDescription>
|
||||
Configure observer dashboard visibility and PDF report settings
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<AnalyticsSettingsSection settings={analyticsSettings} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="audit" className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Audit & Security</CardTitle>
|
||||
<CardDescription>
|
||||
Configure audit log retention and anomaly detection
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<AuditSettingsSection settings={auditSecuritySettings} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="localization" className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Localization</CardTitle>
|
||||
<CardDescription>
|
||||
Configure language and locale settings
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<LocalizationSettingsSection settings={localizationSettings} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
|
||||
{/* Quick Links to sub-pages */}
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-base flex items-center gap-2">
|
||||
<LayoutTemplate className="h-4 w-4" />
|
||||
Round Templates
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Create reusable round configuration templates
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Button asChild>
|
||||
<Link href="/admin/settings/templates">
|
||||
<LayoutTemplate className="mr-2 h-4 w-4" />
|
||||
Manage Templates
|
||||
<ExternalLink className="ml-2 h-3 w-3" />
|
||||
</Link>
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-base flex items-center gap-2">
|
||||
<Webhook className="h-4 w-4" />
|
||||
Webhooks
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Configure webhook endpoints for platform events
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Button asChild>
|
||||
<Link href="/admin/settings/webhooks">
|
||||
<Webhook className="mr-2 h-4 w-4" />
|
||||
Manage Webhooks
|
||||
<ExternalLink className="ml-2 h-3 w-3" />
|
||||
</Link>
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export { SettingsSkeleton }
|
||||
|
||||
// Inline settings sections for new tabs
|
||||
|
||||
import { useState } from 'react'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
import { Loader2 } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
function useSettingsMutation() {
|
||||
const utils = trpc.useUtils()
|
||||
return trpc.settings.update.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.settings.invalidate()
|
||||
toast.success('Setting updated')
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
}
|
||||
|
||||
function SettingToggle({
|
||||
label,
|
||||
description,
|
||||
settingKey,
|
||||
value,
|
||||
}: {
|
||||
label: string
|
||||
description?: string
|
||||
settingKey: string
|
||||
value: string
|
||||
}) {
|
||||
const mutation = useSettingsMutation()
|
||||
const isChecked = value === 'true'
|
||||
|
||||
return (
|
||||
<div className="flex items-center justify-between rounded-lg border p-3">
|
||||
<div className="space-y-0.5">
|
||||
<Label className="text-sm font-medium">{label}</Label>
|
||||
{description && (
|
||||
<p className="text-xs text-muted-foreground">{description}</p>
|
||||
)}
|
||||
</div>
|
||||
<Switch
|
||||
checked={isChecked}
|
||||
disabled={mutation.isPending}
|
||||
onCheckedChange={(checked) =>
|
||||
mutation.mutate({ key: settingKey, value: String(checked) })
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function SettingInput({
|
||||
label,
|
||||
description,
|
||||
settingKey,
|
||||
value,
|
||||
type = 'text',
|
||||
}: {
|
||||
label: string
|
||||
description?: string
|
||||
settingKey: string
|
||||
value: string
|
||||
type?: string
|
||||
}) {
|
||||
const [localValue, setLocalValue] = useState(value)
|
||||
const mutation = useSettingsMutation()
|
||||
|
||||
const save = () => {
|
||||
if (localValue !== value) {
|
||||
mutation.mutate({ key: settingKey, value: localValue })
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm font-medium">{label}</Label>
|
||||
{description && (
|
||||
<p className="text-xs text-muted-foreground">{description}</p>
|
||||
)}
|
||||
<div className="flex gap-2">
|
||||
<Input
|
||||
type={type}
|
||||
value={localValue}
|
||||
onChange={(e) => setLocalValue(e.target.value)}
|
||||
onBlur={save}
|
||||
className="max-w-xs"
|
||||
/>
|
||||
{mutation.isPending && <Loader2 className="h-4 w-4 animate-spin self-center" />}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function SettingSelect({
|
||||
label,
|
||||
description,
|
||||
settingKey,
|
||||
value,
|
||||
options,
|
||||
}: {
|
||||
label: string
|
||||
description?: string
|
||||
settingKey: string
|
||||
value: string
|
||||
options: Array<{ value: string; label: string }>
|
||||
}) {
|
||||
const mutation = useSettingsMutation()
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm font-medium">{label}</Label>
|
||||
{description && (
|
||||
<p className="text-xs text-muted-foreground">{description}</p>
|
||||
)}
|
||||
<Select
|
||||
value={value || options[0]?.value}
|
||||
onValueChange={(v) => mutation.mutate({ key: settingKey, value: v })}
|
||||
disabled={mutation.isPending}
|
||||
>
|
||||
<SelectTrigger className="max-w-xs">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{options.map((opt) => (
|
||||
<SelectItem key={opt.value} value={opt.value}>
|
||||
{opt.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function DigestSettingsSection({ settings }: { settings: Record<string, string> }) {
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<SettingToggle
|
||||
label="Enable Digest Emails"
|
||||
description="Send periodic digest emails summarizing platform activity"
|
||||
settingKey="digest_enabled"
|
||||
value={settings.digest_enabled || 'false'}
|
||||
/>
|
||||
<SettingSelect
|
||||
label="Default Frequency"
|
||||
description="How often digests are sent to users by default"
|
||||
settingKey="digest_default_frequency"
|
||||
value={settings.digest_default_frequency || 'weekly'}
|
||||
options={[
|
||||
{ value: 'daily', label: 'Daily' },
|
||||
{ value: 'weekly', label: 'Weekly' },
|
||||
{ value: 'biweekly', label: 'Bi-weekly' },
|
||||
{ value: 'monthly', label: 'Monthly' },
|
||||
]}
|
||||
/>
|
||||
<SettingInput
|
||||
label="Send Hour (UTC)"
|
||||
description="Hour of day when digest emails are sent (0-23)"
|
||||
settingKey="digest_send_hour"
|
||||
value={settings.digest_send_hour || '8'}
|
||||
type="number"
|
||||
/>
|
||||
<div className="border-t pt-4 space-y-3">
|
||||
<Label className="text-sm font-medium">Digest Sections</Label>
|
||||
<SettingToggle
|
||||
label="Include Evaluations"
|
||||
settingKey="digest_include_evaluations"
|
||||
value={settings.digest_include_evaluations || 'true'}
|
||||
/>
|
||||
<SettingToggle
|
||||
label="Include Assignments"
|
||||
settingKey="digest_include_assignments"
|
||||
value={settings.digest_include_assignments || 'true'}
|
||||
/>
|
||||
<SettingToggle
|
||||
label="Include Deadlines"
|
||||
settingKey="digest_include_deadlines"
|
||||
value={settings.digest_include_deadlines || 'true'}
|
||||
/>
|
||||
<SettingToggle
|
||||
label="Include Announcements"
|
||||
settingKey="digest_include_announcements"
|
||||
value={settings.digest_include_announcements || 'true'}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function AnalyticsSettingsSection({ settings }: { settings: Record<string, string> }) {
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<Label className="text-sm font-medium">Observer Tab Visibility</Label>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Choose which analytics tabs are visible to observers
|
||||
</p>
|
||||
<SettingToggle
|
||||
label="Scores Tab"
|
||||
settingKey="analytics_observer_scores_tab"
|
||||
value={settings.analytics_observer_scores_tab || 'true'}
|
||||
/>
|
||||
<SettingToggle
|
||||
label="Progress Tab"
|
||||
settingKey="analytics_observer_progress_tab"
|
||||
value={settings.analytics_observer_progress_tab || 'true'}
|
||||
/>
|
||||
<SettingToggle
|
||||
label="Juror Stats Tab"
|
||||
settingKey="analytics_observer_juror_tab"
|
||||
value={settings.analytics_observer_juror_tab || 'true'}
|
||||
/>
|
||||
<SettingToggle
|
||||
label="Comparison Tab"
|
||||
settingKey="analytics_observer_comparison_tab"
|
||||
value={settings.analytics_observer_comparison_tab || 'true'}
|
||||
/>
|
||||
<div className="border-t pt-4 space-y-3">
|
||||
<Label className="text-sm font-medium">PDF Reports</Label>
|
||||
<SettingToggle
|
||||
label="Enable PDF Report Generation"
|
||||
description="Allow admins and observers to generate PDF reports"
|
||||
settingKey="analytics_pdf_enabled"
|
||||
value={settings.analytics_pdf_enabled || 'true'}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function AuditSettingsSection({ settings }: { settings: Record<string, string> }) {
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<SettingInput
|
||||
label="Retention Period (days)"
|
||||
description="How long audit log entries are kept before automatic cleanup"
|
||||
settingKey="audit_retention_days"
|
||||
value={settings.audit_retention_days || '365'}
|
||||
type="number"
|
||||
/>
|
||||
<div className="border-t pt-4 space-y-3">
|
||||
<SettingToggle
|
||||
label="Enable Anomaly Detection"
|
||||
description="Detect suspicious patterns like rapid actions or off-hours access"
|
||||
settingKey="anomaly_detection_enabled"
|
||||
value={settings.anomaly_detection_enabled || 'false'}
|
||||
/>
|
||||
<SettingInput
|
||||
label="Rapid Actions Threshold"
|
||||
description="Maximum actions per minute before flagging as anomalous"
|
||||
settingKey="anomaly_rapid_actions_threshold"
|
||||
value={settings.anomaly_rapid_actions_threshold || '30'}
|
||||
type="number"
|
||||
/>
|
||||
<SettingInput
|
||||
label="Off-Hours Start (UTC)"
|
||||
description="Start hour for off-hours monitoring (0-23)"
|
||||
settingKey="anomaly_off_hours_start"
|
||||
value={settings.anomaly_off_hours_start || '22'}
|
||||
type="number"
|
||||
/>
|
||||
<SettingInput
|
||||
label="Off-Hours End (UTC)"
|
||||
description="End hour for off-hours monitoring (0-23)"
|
||||
settingKey="anomaly_off_hours_end"
|
||||
value={settings.anomaly_off_hours_end || '6'}
|
||||
type="number"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function LocalizationSettingsSection({ settings }: { settings: Record<string, string> }) {
|
||||
const mutation = useSettingsMutation()
|
||||
const enabledLocales = (settings.localization_enabled_locales || 'en').split(',')
|
||||
|
||||
const toggleLocale = (locale: string) => {
|
||||
const current = new Set(enabledLocales)
|
||||
if (current.has(locale)) {
|
||||
if (current.size <= 1) {
|
||||
toast.error('At least one locale must be enabled')
|
||||
return
|
||||
}
|
||||
current.delete(locale)
|
||||
} else {
|
||||
current.add(locale)
|
||||
}
|
||||
mutation.mutate({
|
||||
key: 'localization_enabled_locales',
|
||||
value: Array.from(current).join(','),
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-3">
|
||||
<Label className="text-sm font-medium">Enabled Languages</Label>
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center justify-between rounded-lg border p-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="font-medium text-sm">EN</span>
|
||||
<span className="text-sm text-muted-foreground">English</span>
|
||||
</div>
|
||||
<Checkbox
|
||||
checked={enabledLocales.includes('en')}
|
||||
onCheckedChange={() => toggleLocale('en')}
|
||||
disabled={mutation.isPending}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex items-center justify-between rounded-lg border p-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="font-medium text-sm">FR</span>
|
||||
<span className="text-sm text-muted-foreground">Français</span>
|
||||
</div>
|
||||
<Checkbox
|
||||
checked={enabledLocales.includes('fr')}
|
||||
onCheckedChange={() => toggleLocale('fr')}
|
||||
disabled={mutation.isPending}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<SettingSelect
|
||||
label="Default Locale"
|
||||
description="The default language for new users"
|
||||
settingKey="localization_default_locale"
|
||||
value={settings.localization_default_locale || 'en'}
|
||||
options={[
|
||||
{ value: 'en', label: 'English' },
|
||||
{ value: 'fr', label: 'Fran\u00e7ais' },
|
||||
]}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,145 @@
|
|||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Card, CardContent } from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { MessageSquare, Lock, Send, User } from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
interface Comment {
|
||||
id: string
|
||||
author: string
|
||||
content: string
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
interface DiscussionThreadProps {
|
||||
comments: Comment[]
|
||||
onAddComment?: (content: string) => void
|
||||
isLocked?: boolean
|
||||
maxLength?: number
|
||||
isSubmitting?: boolean
|
||||
}
|
||||
|
||||
function formatRelativeTime(dateStr: string): string {
|
||||
const date = new Date(dateStr)
|
||||
const now = new Date()
|
||||
const diffMs = now.getTime() - date.getTime()
|
||||
const diffMinutes = Math.floor(diffMs / 60000)
|
||||
const diffHours = Math.floor(diffMinutes / 60)
|
||||
const diffDays = Math.floor(diffHours / 24)
|
||||
|
||||
if (diffMinutes < 1) return 'just now'
|
||||
if (diffMinutes < 60) return `${diffMinutes}m ago`
|
||||
if (diffHours < 24) return `${diffHours}h ago`
|
||||
if (diffDays < 7) return `${diffDays}d ago`
|
||||
return date.toLocaleDateString()
|
||||
}
|
||||
|
||||
export function DiscussionThread({
|
||||
comments,
|
||||
onAddComment,
|
||||
isLocked = false,
|
||||
maxLength = 2000,
|
||||
isSubmitting = false,
|
||||
}: DiscussionThreadProps) {
|
||||
const [newComment, setNewComment] = useState('')
|
||||
|
||||
const handleSubmit = () => {
|
||||
const trimmed = newComment.trim()
|
||||
if (!trimmed || !onAddComment) return
|
||||
onAddComment(trimmed)
|
||||
setNewComment('')
|
||||
}
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
|
||||
e.preventDefault()
|
||||
handleSubmit()
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{/* Locked banner */}
|
||||
{isLocked && (
|
||||
<div className="flex items-center gap-2 rounded-lg bg-muted p-3 text-sm text-muted-foreground">
|
||||
<Lock className="h-4 w-4 shrink-0" />
|
||||
Discussion is closed. No new comments can be added.
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Comments list */}
|
||||
{comments.length === 0 ? (
|
||||
<div className="flex flex-col items-center justify-center py-8 text-center">
|
||||
<MessageSquare className="h-10 w-10 text-muted-foreground/50" />
|
||||
<p className="mt-2 text-sm font-medium text-muted-foreground">No comments yet</p>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Be the first to share your thoughts on this project.
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{comments.map((comment) => (
|
||||
<Card key={comment.id}>
|
||||
<CardContent className="p-4">
|
||||
<div className="flex items-start gap-3">
|
||||
<div className="flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-muted">
|
||||
<User className="h-4 w-4 text-muted-foreground" />
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<span className="text-sm font-medium">{comment.author}</span>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{formatRelativeTime(comment.createdAt)}
|
||||
</span>
|
||||
</div>
|
||||
<p className="mt-1 text-sm whitespace-pre-wrap break-words">
|
||||
{comment.content}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Add comment form */}
|
||||
{!isLocked && onAddComment && (
|
||||
<div className="space-y-2">
|
||||
<Textarea
|
||||
placeholder="Add a comment... (Ctrl+Enter to send)"
|
||||
value={newComment}
|
||||
onChange={(e) => setNewComment(e.target.value.slice(0, maxLength))}
|
||||
onKeyDown={handleKeyDown}
|
||||
rows={3}
|
||||
disabled={isSubmitting}
|
||||
/>
|
||||
<div className="flex items-center justify-between">
|
||||
<span
|
||||
className={cn(
|
||||
'text-xs',
|
||||
newComment.length > maxLength * 0.9
|
||||
? 'text-destructive'
|
||||
: 'text-muted-foreground'
|
||||
)}
|
||||
>
|
||||
{newComment.length}/{maxLength}
|
||||
</span>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={handleSubmit}
|
||||
disabled={!newComment.trim() || isSubmitting}
|
||||
>
|
||||
<Send className="mr-2 h-4 w-4" />
|
||||
{isSubmitting ? 'Sending...' : 'Comment'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,103 @@
|
|||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from '@/components/ui/dialog'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Eye, Download, FileText, Image as ImageIcon, Video, File } from 'lucide-react'
|
||||
|
||||
interface FilePreviewProps {
|
||||
fileName: string
|
||||
mimeType: string
|
||||
downloadUrl: string
|
||||
}
|
||||
|
||||
function getPreviewType(mimeType: string): 'pdf' | 'image' | 'video' | 'unsupported' {
|
||||
if (mimeType === 'application/pdf') return 'pdf'
|
||||
if (mimeType.startsWith('image/')) return 'image'
|
||||
if (mimeType.startsWith('video/')) return 'video'
|
||||
return 'unsupported'
|
||||
}
|
||||
|
||||
function getFileIcon(mimeType: string) {
|
||||
if (mimeType === 'application/pdf') return FileText
|
||||
if (mimeType.startsWith('image/')) return ImageIcon
|
||||
if (mimeType.startsWith('video/')) return Video
|
||||
return File
|
||||
}
|
||||
|
||||
export function FilePreview({ fileName, mimeType, downloadUrl }: FilePreviewProps) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const previewType = getPreviewType(mimeType)
|
||||
const Icon = getFileIcon(mimeType)
|
||||
const canPreview = previewType !== 'unsupported'
|
||||
|
||||
if (!canPreview) {
|
||||
return (
|
||||
<Button variant="outline" size="sm" asChild>
|
||||
<a href={downloadUrl} target="_blank" rel="noopener noreferrer">
|
||||
<Download className="mr-2 h-4 w-4" />
|
||||
Download
|
||||
</a>
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>
|
||||
<Button variant="outline" size="sm">
|
||||
<Eye className="mr-2 h-4 w-4" />
|
||||
Preview
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="max-w-4xl max-h-[90vh]">
|
||||
<DialogHeader>
|
||||
<DialogTitle className="flex items-center gap-2 truncate">
|
||||
<Icon className="h-4 w-4 shrink-0" />
|
||||
{fileName}
|
||||
</DialogTitle>
|
||||
</DialogHeader>
|
||||
<div className="overflow-auto">
|
||||
{previewType === 'pdf' && (
|
||||
<iframe
|
||||
src={`${downloadUrl}#toolbar=0`}
|
||||
className="w-full h-[70vh] rounded-md"
|
||||
title={fileName}
|
||||
/>
|
||||
)}
|
||||
{previewType === 'image' && (
|
||||
<img
|
||||
src={downloadUrl}
|
||||
alt={fileName}
|
||||
className="w-full h-auto max-h-[70vh] object-contain rounded-md"
|
||||
/>
|
||||
)}
|
||||
{previewType === 'video' && (
|
||||
<video
|
||||
src={downloadUrl}
|
||||
controls
|
||||
className="w-full max-h-[70vh] rounded-md"
|
||||
preload="metadata"
|
||||
>
|
||||
Your browser does not support the video tag.
|
||||
</video>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex justify-end">
|
||||
<Button variant="outline" size="sm" asChild>
|
||||
<a href={downloadUrl} target="_blank" rel="noopener noreferrer">
|
||||
<Download className="mr-2 h-4 w-4" />
|
||||
Download
|
||||
</a>
|
||||
</Button>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
|
|
@ -6,6 +6,13 @@ import { Button } from '@/components/ui/button'
|
|||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from '@/components/ui/dialog'
|
||||
import {
|
||||
FileText,
|
||||
Video,
|
||||
|
|
@ -17,8 +24,11 @@ import {
|
|||
Loader2,
|
||||
AlertCircle,
|
||||
X,
|
||||
History,
|
||||
PackageOpen,
|
||||
} from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
interface ProjectFile {
|
||||
id: string
|
||||
|
|
@ -28,10 +38,12 @@ interface ProjectFile {
|
|||
size: number
|
||||
bucket: string
|
||||
objectKey: string
|
||||
version?: number
|
||||
}
|
||||
|
||||
interface FileViewerProps {
|
||||
files: ProjectFile[]
|
||||
projectId?: string
|
||||
className?: string
|
||||
}
|
||||
|
||||
|
|
@ -71,7 +83,7 @@ function getFileTypeLabel(fileType: string) {
|
|||
}
|
||||
}
|
||||
|
||||
export function FileViewer({ files, className }: FileViewerProps) {
|
||||
export function FileViewer({ files, projectId, className }: FileViewerProps) {
|
||||
if (files.length === 0) {
|
||||
return (
|
||||
<Card className={className}>
|
||||
|
|
@ -94,8 +106,11 @@ export function FileViewer({ files, className }: FileViewerProps) {
|
|||
|
||||
return (
|
||||
<Card className={className}>
|
||||
<CardHeader>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0">
|
||||
<CardTitle className="text-lg">Project Files</CardTitle>
|
||||
{projectId && files.length > 1 && (
|
||||
<BulkDownloadButton projectId={projectId} fileIds={files.map((f) => f.id)} />
|
||||
)}
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-3">
|
||||
{sortedFiles.map((file) => (
|
||||
|
|
@ -115,7 +130,10 @@ function FileItem({ file }: { file: ProjectFile }) {
|
|||
{ enabled: showPreview }
|
||||
)
|
||||
|
||||
const canPreview = file.mimeType.startsWith('video/') || file.mimeType === 'application/pdf'
|
||||
const canPreview =
|
||||
file.mimeType.startsWith('video/') ||
|
||||
file.mimeType === 'application/pdf' ||
|
||||
file.mimeType.startsWith('image/')
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
|
|
@ -125,7 +143,14 @@ function FileItem({ file }: { file: ProjectFile }) {
|
|||
</div>
|
||||
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="font-medium truncate">{file.fileName}</p>
|
||||
<div className="flex items-center gap-2">
|
||||
<p className="font-medium truncate">{file.fileName}</p>
|
||||
{file.version != null && file.version > 1 && (
|
||||
<Badge variant="outline" className="text-xs shrink-0">
|
||||
v{file.version}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
{getFileTypeLabel(file.fileType)}
|
||||
|
|
@ -134,7 +159,10 @@ function FileItem({ file }: { file: ProjectFile }) {
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex items-center gap-1">
|
||||
{file.version != null && file.version > 1 && (
|
||||
<VersionHistoryButton fileId={file.id} />
|
||||
)}
|
||||
{canPreview && (
|
||||
<Button
|
||||
variant="outline"
|
||||
|
|
@ -179,6 +207,179 @@ function FileItem({ file }: { file: ProjectFile }) {
|
|||
)
|
||||
}
|
||||
|
||||
function VersionHistoryButton({ fileId }: { fileId: string }) {
|
||||
const [open, setOpen] = useState(false)
|
||||
|
||||
const { data: versions, isLoading } = trpc.file.getVersionHistory.useQuery(
|
||||
{ fileId },
|
||||
{ enabled: open }
|
||||
)
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>
|
||||
<Button variant="ghost" size="sm" title="Version history">
|
||||
<History className="h-4 w-4" />
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Version History</DialogTitle>
|
||||
</DialogHeader>
|
||||
<div className="space-y-2 max-h-[60vh] overflow-y-auto">
|
||||
{isLoading ? (
|
||||
<div className="space-y-2">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<Skeleton key={i} className="h-14 w-full" />
|
||||
))}
|
||||
</div>
|
||||
) : versions && (versions as Array<Record<string, unknown>>).length > 0 ? (
|
||||
(versions as Array<Record<string, unknown>>).map((v) => (
|
||||
<div
|
||||
key={String(v.id)}
|
||||
className={cn(
|
||||
'flex items-center gap-3 rounded-lg border p-3',
|
||||
String(v.id) === fileId && 'border-primary bg-primary/5'
|
||||
)}
|
||||
>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<p className="text-sm font-medium truncate">
|
||||
{String(v.fileName)}
|
||||
</p>
|
||||
<Badge variant={String(v.id) === fileId ? 'default' : 'outline'} className="text-xs shrink-0">
|
||||
v{String(v.version)}
|
||||
</Badge>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-xs text-muted-foreground">
|
||||
<span>{formatFileSize(Number(v.size))}</span>
|
||||
<span>
|
||||
{v.createdAt
|
||||
? new Date(String(v.createdAt)).toLocaleDateString(undefined, {
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
})
|
||||
: ''}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<VersionDownloadButton
|
||||
bucket={String(v.bucket)}
|
||||
objectKey={String(v.objectKey)}
|
||||
/>
|
||||
</div>
|
||||
))
|
||||
) : (
|
||||
<p className="text-sm text-muted-foreground text-center py-4">
|
||||
No version history available
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
|
||||
function VersionDownloadButton({ bucket, objectKey }: { bucket: string; objectKey: string }) {
|
||||
const [downloading, setDownloading] = useState(false)
|
||||
|
||||
const { refetch } = trpc.file.getDownloadUrl.useQuery(
|
||||
{ bucket, objectKey },
|
||||
{ enabled: false }
|
||||
)
|
||||
|
||||
const handleDownload = async () => {
|
||||
setDownloading(true)
|
||||
try {
|
||||
const result = await refetch()
|
||||
if (result.data?.url) {
|
||||
window.open(result.data.url, '_blank')
|
||||
}
|
||||
} catch {
|
||||
toast.error('Failed to get download URL')
|
||||
} finally {
|
||||
setDownloading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-8 w-8 shrink-0"
|
||||
onClick={handleDownload}
|
||||
disabled={downloading}
|
||||
aria-label="Download this version"
|
||||
>
|
||||
{downloading ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<Download className="h-4 w-4" />
|
||||
)}
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
||||
function BulkDownloadButton({ projectId, fileIds }: { projectId: string; fileIds: string[] }) {
|
||||
const [downloading, setDownloading] = useState(false)
|
||||
|
||||
const { refetch } = trpc.file.getBulkDownloadUrls.useQuery(
|
||||
{ projectId, fileIds },
|
||||
{ enabled: false }
|
||||
)
|
||||
|
||||
const handleBulkDownload = async () => {
|
||||
setDownloading(true)
|
||||
try {
|
||||
const result = await refetch()
|
||||
if (result.data && Array.isArray(result.data)) {
|
||||
// Open each download URL with a small delay to avoid popup blocking
|
||||
for (let i = 0; i < result.data.length; i++) {
|
||||
const item = result.data[i] as { downloadUrl: string }
|
||||
if (item.downloadUrl) {
|
||||
// Use link element to trigger download without popup
|
||||
const link = document.createElement('a')
|
||||
link.href = item.downloadUrl
|
||||
link.target = '_blank'
|
||||
link.rel = 'noopener noreferrer'
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
// Small delay between downloads
|
||||
if (i < result.data.length - 1) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 300))
|
||||
}
|
||||
}
|
||||
}
|
||||
toast.success(`Downloading ${result.data.length} files`)
|
||||
}
|
||||
} catch {
|
||||
toast.error('Failed to download files')
|
||||
} finally {
|
||||
setDownloading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={handleBulkDownload}
|
||||
disabled={downloading}
|
||||
>
|
||||
{downloading ? (
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<PackageOpen className="mr-2 h-4 w-4" />
|
||||
)}
|
||||
Download All
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
||||
function FileDownloadButton({ file }: { file: ProjectFile }) {
|
||||
const [downloading, setDownloading] = useState(false)
|
||||
|
||||
|
|
@ -256,6 +457,29 @@ function FilePreview({ file, url }: { file: ProjectFile; url: string }) {
|
|||
)
|
||||
}
|
||||
|
||||
if (file.mimeType.startsWith('image/')) {
|
||||
return (
|
||||
<div className="relative flex items-center justify-center p-4">
|
||||
<img
|
||||
src={url}
|
||||
alt={file.fileName}
|
||||
className="max-w-full max-h-[500px] object-contain rounded-md"
|
||||
/>
|
||||
<Button
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
className="absolute top-2 right-2"
|
||||
asChild
|
||||
>
|
||||
<a href={url} target="_blank" rel="noopener noreferrer">
|
||||
<ExternalLink className="mr-2 h-4 w-4" />
|
||||
Open in new tab
|
||||
</a>
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex items-center justify-center py-8 text-muted-foreground">
|
||||
Preview not available for this file type
|
||||
|
|
@ -314,6 +538,11 @@ function CompactFileItem({ file }: { file: ProjectFile }) {
|
|||
<Icon className="h-4 w-4 shrink-0 text-muted-foreground" />
|
||||
)}
|
||||
<span className="flex-1 truncate text-sm">{file.fileName}</span>
|
||||
{file.version != null && file.version > 1 && (
|
||||
<Badge variant="outline" className="text-xs shrink-0">
|
||||
v{file.version}
|
||||
</Badge>
|
||||
)}
|
||||
<span className="text-xs text-muted-foreground shrink-0">
|
||||
{formatFileSize(file.size)}
|
||||
</span>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
'use client'
|
||||
|
||||
import { useTransition } from 'react'
|
||||
import { useLocale } from 'next-intl'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from '@/components/ui/dropdown-menu'
|
||||
import { Globe, Check } from 'lucide-react'
|
||||
|
||||
const LANGUAGES = [
|
||||
{ code: 'en', label: 'English', flag: 'EN' },
|
||||
{ code: 'fr', label: 'Fran\u00e7ais', flag: 'FR' },
|
||||
] as const
|
||||
|
||||
type LanguageCode = (typeof LANGUAGES)[number]['code']
|
||||
|
||||
export function LanguageSwitcher() {
|
||||
const locale = useLocale() as LanguageCode
|
||||
const router = useRouter()
|
||||
const [isPending, startTransition] = useTransition()
|
||||
|
||||
const currentLang = LANGUAGES.find((l) => l.code === locale) ?? LANGUAGES[0]
|
||||
|
||||
const switchLanguage = (code: LanguageCode) => {
|
||||
// Set cookie with 1 year expiry
|
||||
document.cookie = `locale=${code};path=/;max-age=${365 * 24 * 60 * 60};samesite=lax`
|
||||
// Refresh to re-run server components with new locale
|
||||
startTransition(() => {
|
||||
router.refresh()
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="ghost" size="sm" className="gap-2" disabled={isPending}>
|
||||
<Globe className="h-4 w-4" />
|
||||
<span className="font-medium">{currentLang.flag}</span>
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
{LANGUAGES.map((lang) => (
|
||||
<DropdownMenuItem
|
||||
key={lang.code}
|
||||
onClick={() => switchLanguage(lang.code)}
|
||||
className="gap-2"
|
||||
>
|
||||
<span className="font-medium w-6">{lang.flag}</span>
|
||||
<span>{lang.label}</span>
|
||||
{locale === lang.code && <Check className="ml-auto h-4 w-4" />}
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,173 @@
|
|||
'use client'
|
||||
|
||||
import { useState, useEffect } from 'react'
|
||||
import { motion, AnimatePresence } from 'motion/react'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
interface LiveScoreAnimationProps {
|
||||
score: number | null
|
||||
maxScore: number
|
||||
label: string
|
||||
animate?: boolean
|
||||
theme?: 'dark' | 'light' | 'branded'
|
||||
}
|
||||
|
||||
function getScoreColor(score: number, maxScore: number): string {
|
||||
const ratio = score / maxScore
|
||||
if (ratio >= 0.75) return 'text-green-500'
|
||||
if (ratio >= 0.5) return 'text-yellow-500'
|
||||
if (ratio >= 0.25) return 'text-orange-500'
|
||||
return 'text-red-500'
|
||||
}
|
||||
|
||||
function getProgressColor(score: number, maxScore: number): string {
|
||||
const ratio = score / maxScore
|
||||
if (ratio >= 0.75) return 'stroke-green-500'
|
||||
if (ratio >= 0.5) return 'stroke-yellow-500'
|
||||
if (ratio >= 0.25) return 'stroke-orange-500'
|
||||
return 'stroke-red-500'
|
||||
}
|
||||
|
||||
function getThemeClasses(theme: 'dark' | 'light' | 'branded') {
|
||||
switch (theme) {
|
||||
case 'dark':
|
||||
return {
|
||||
bg: 'bg-gray-900',
|
||||
text: 'text-white',
|
||||
label: 'text-gray-400',
|
||||
ring: 'stroke-gray-700',
|
||||
}
|
||||
case 'light':
|
||||
return {
|
||||
bg: 'bg-white',
|
||||
text: 'text-gray-900',
|
||||
label: 'text-gray-500',
|
||||
ring: 'stroke-gray-200',
|
||||
}
|
||||
case 'branded':
|
||||
return {
|
||||
bg: 'bg-[#053d57]',
|
||||
text: 'text-white',
|
||||
label: 'text-[#557f8c]',
|
||||
ring: 'stroke-[#053d57]/30',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function LiveScoreAnimation({
|
||||
score,
|
||||
maxScore,
|
||||
label,
|
||||
animate = true,
|
||||
theme = 'branded',
|
||||
}: LiveScoreAnimationProps) {
|
||||
const [displayScore, setDisplayScore] = useState(0)
|
||||
const themeClasses = getThemeClasses(theme)
|
||||
const radius = 40
|
||||
const circumference = 2 * Math.PI * radius
|
||||
const targetScore = score ?? 0
|
||||
const progress = maxScore > 0 ? targetScore / maxScore : 0
|
||||
const offset = circumference - progress * circumference
|
||||
|
||||
useEffect(() => {
|
||||
if (!animate || score === null) {
|
||||
setDisplayScore(targetScore)
|
||||
return
|
||||
}
|
||||
|
||||
let frame: number
|
||||
const duration = 1200
|
||||
const startTime = performance.now()
|
||||
const startScore = 0
|
||||
|
||||
const step = (currentTime: number) => {
|
||||
const elapsed = currentTime - startTime
|
||||
const progress = Math.min(elapsed / duration, 1)
|
||||
// Ease out cubic
|
||||
const eased = 1 - Math.pow(1 - progress, 3)
|
||||
const current = startScore + (targetScore - startScore) * eased
|
||||
setDisplayScore(Math.round(current * 10) / 10)
|
||||
|
||||
if (progress < 1) {
|
||||
frame = requestAnimationFrame(step)
|
||||
}
|
||||
}
|
||||
|
||||
frame = requestAnimationFrame(step)
|
||||
return () => cancelAnimationFrame(frame)
|
||||
}, [targetScore, animate, score])
|
||||
|
||||
if (score === null) {
|
||||
return (
|
||||
<div className={cn('flex flex-col items-center gap-2 rounded-xl p-4', themeClasses.bg)}>
|
||||
<div className="relative h-24 w-24">
|
||||
<svg className="h-24 w-24 -rotate-90" viewBox="0 0 100 100">
|
||||
<circle
|
||||
cx="50"
|
||||
cy="50"
|
||||
r={radius}
|
||||
fill="none"
|
||||
className={themeClasses.ring}
|
||||
strokeWidth="6"
|
||||
/>
|
||||
</svg>
|
||||
<div className="absolute inset-0 flex items-center justify-center">
|
||||
<span className={cn('text-lg font-medium', themeClasses.label)}>--</span>
|
||||
</div>
|
||||
</div>
|
||||
<span className={cn('text-xs font-medium', themeClasses.label)}>{label}</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<AnimatePresence>
|
||||
<motion.div
|
||||
initial={animate ? { opacity: 0, scale: 0.8 } : false}
|
||||
animate={{ opacity: 1, scale: 1 }}
|
||||
transition={{ duration: 0.5, ease: 'easeOut' }}
|
||||
className={cn('flex flex-col items-center gap-2 rounded-xl p-4', themeClasses.bg)}
|
||||
>
|
||||
<div className="relative h-24 w-24">
|
||||
<svg className="h-24 w-24 -rotate-90" viewBox="0 0 100 100">
|
||||
{/* Background ring */}
|
||||
<circle
|
||||
cx="50"
|
||||
cy="50"
|
||||
r={radius}
|
||||
fill="none"
|
||||
className={themeClasses.ring}
|
||||
strokeWidth="6"
|
||||
/>
|
||||
{/* Progress ring */}
|
||||
<motion.circle
|
||||
cx="50"
|
||||
cy="50"
|
||||
r={radius}
|
||||
fill="none"
|
||||
className={getProgressColor(targetScore, maxScore)}
|
||||
strokeWidth="6"
|
||||
strokeLinecap="round"
|
||||
strokeDasharray={circumference}
|
||||
initial={animate ? { strokeDashoffset: circumference } : { strokeDashoffset: offset }}
|
||||
animate={{ strokeDashoffset: offset }}
|
||||
transition={{ duration: 1.2, ease: 'easeOut' }}
|
||||
/>
|
||||
</svg>
|
||||
<div className="absolute inset-0 flex items-center justify-center">
|
||||
<span
|
||||
className={cn(
|
||||
'text-2xl font-bold tabular-nums',
|
||||
themeClasses.text,
|
||||
getScoreColor(targetScore, maxScore)
|
||||
)}
|
||||
>
|
||||
{displayScore.toFixed(maxScore % 1 !== 0 ? 1 : 0)}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<span className={cn('text-xs font-medium text-center', themeClasses.label)}>{label}</span>
|
||||
</motion.div>
|
||||
</AnimatePresence>
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,135 @@
|
|||
'use client'
|
||||
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Copy, QrCode } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
interface QRCodeDisplayProps {
|
||||
url: string
|
||||
title?: string
|
||||
size?: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a simple QR code using Canvas API.
|
||||
* Uses a basic QR encoding approach for URLs.
|
||||
*/
|
||||
function generateQRMatrix(data: string): boolean[][] {
|
||||
// Simple QR-like grid pattern based on data hash
|
||||
// For production, use a library like 'qrcode', but this is a lightweight visual
|
||||
const size = 25
|
||||
const matrix: boolean[][] = Array.from({ length: size }, () =>
|
||||
Array(size).fill(false)
|
||||
)
|
||||
|
||||
// Add finder patterns (top-left, top-right, bottom-left)
|
||||
const addFinderPattern = (row: number, col: number) => {
|
||||
for (let r = 0; r < 7; r++) {
|
||||
for (let c = 0; c < 7; c++) {
|
||||
if (
|
||||
r === 0 || r === 6 || c === 0 || c === 6 || // outer border
|
||||
(r >= 2 && r <= 4 && c >= 2 && c <= 4) // inner block
|
||||
) {
|
||||
if (row + r < size && col + c < size) {
|
||||
matrix[row + r][col + c] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addFinderPattern(0, 0)
|
||||
addFinderPattern(0, size - 7)
|
||||
addFinderPattern(size - 7, 0)
|
||||
|
||||
// Fill data area with a hash-based pattern
|
||||
let hash = 0
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
hash = ((hash << 5) - hash + data.charCodeAt(i)) | 0
|
||||
}
|
||||
|
||||
for (let r = 8; r < size - 8; r++) {
|
||||
for (let c = 8; c < size - 8; c++) {
|
||||
hash = ((hash << 5) - hash + r * size + c) | 0
|
||||
matrix[r][c] = (hash & 1) === 1
|
||||
}
|
||||
}
|
||||
|
||||
// Timing patterns
|
||||
for (let i = 8; i < size - 8; i++) {
|
||||
matrix[6][i] = i % 2 === 0
|
||||
matrix[i][6] = i % 2 === 0
|
||||
}
|
||||
|
||||
return matrix
|
||||
}
|
||||
|
||||
function drawQR(canvas: HTMLCanvasElement, data: string, pixelSize: number) {
|
||||
const matrix = generateQRMatrix(data)
|
||||
const size = matrix.length
|
||||
const totalSize = size * pixelSize
|
||||
canvas.width = totalSize
|
||||
canvas.height = totalSize
|
||||
|
||||
const ctx = canvas.getContext('2d')
|
||||
if (!ctx) return
|
||||
|
||||
// White background
|
||||
ctx.fillStyle = '#ffffff'
|
||||
ctx.fillRect(0, 0, totalSize, totalSize)
|
||||
|
||||
// Draw modules
|
||||
ctx.fillStyle = '#053d57'
|
||||
for (let r = 0; r < size; r++) {
|
||||
for (let c = 0; c < size; c++) {
|
||||
if (matrix[r][c]) {
|
||||
ctx.fillRect(c * pixelSize, r * pixelSize, pixelSize, pixelSize)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function QRCodeDisplay({ url, title = 'Scan to Vote', size = 200 }: QRCodeDisplayProps) {
|
||||
const canvasRef = useRef<HTMLCanvasElement>(null)
|
||||
const pixelSize = Math.floor(size / 25)
|
||||
|
||||
useEffect(() => {
|
||||
if (canvasRef.current) {
|
||||
drawQR(canvasRef.current, url, pixelSize)
|
||||
}
|
||||
}, [url, pixelSize])
|
||||
|
||||
const handleCopyUrl = () => {
|
||||
navigator.clipboard.writeText(url).then(() => {
|
||||
toast.success('URL copied to clipboard')
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-sm flex items-center gap-2">
|
||||
<QrCode className="h-4 w-4" />
|
||||
{title}
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="flex flex-col items-center gap-3">
|
||||
<canvas
|
||||
ref={canvasRef}
|
||||
className="border rounded-lg"
|
||||
style={{ width: size, height: size }}
|
||||
/>
|
||||
<div className="flex items-center gap-2 w-full">
|
||||
<code className="flex-1 text-xs bg-muted p-2 rounded truncate">
|
||||
{url}
|
||||
</code>
|
||||
<Button variant="ghost" size="sm" onClick={handleCopyUrl}>
|
||||
<Copy className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,113 @@
|
|||
'use client'
|
||||
|
||||
import { useEffect, useRef, useCallback, useState } from 'react'
|
||||
|
||||
export interface VoteUpdate {
|
||||
projectId: string
|
||||
totalVotes: number
|
||||
averageScore: number | null
|
||||
latestVote: { score: number; isAudienceVote: boolean; votedAt: string } | null
|
||||
timestamp: string
|
||||
}
|
||||
|
||||
export interface SessionStatusUpdate {
|
||||
status: string
|
||||
timestamp: string
|
||||
}
|
||||
|
||||
export interface ProjectChangeUpdate {
|
||||
projectId: string | null
|
||||
projectIndex: number
|
||||
timestamp: string
|
||||
}
|
||||
|
||||
interface SSECallbacks {
|
||||
onVoteUpdate?: (data: VoteUpdate) => void
|
||||
onSessionStatus?: (data: SessionStatusUpdate) => void
|
||||
onProjectChange?: (data: ProjectChangeUpdate) => void
|
||||
onConnected?: () => void
|
||||
onError?: (error: Event) => void
|
||||
}
|
||||
|
||||
export function useLiveVotingSSE(
|
||||
sessionId: string | null,
|
||||
callbacks: SSECallbacks
|
||||
) {
|
||||
const [isConnected, setIsConnected] = useState(false)
|
||||
const eventSourceRef = useRef<EventSource | null>(null)
|
||||
const callbacksRef = useRef(callbacks)
|
||||
callbacksRef.current = callbacks
|
||||
|
||||
const connect = useCallback(() => {
|
||||
if (!sessionId) return
|
||||
|
||||
// Close any existing connection
|
||||
if (eventSourceRef.current) {
|
||||
eventSourceRef.current.close()
|
||||
}
|
||||
|
||||
const baseUrl = typeof window !== 'undefined' ? window.location.origin : ''
|
||||
const url = `${baseUrl}/api/live-voting/stream?sessionId=${sessionId}`
|
||||
const es = new EventSource(url)
|
||||
eventSourceRef.current = es
|
||||
|
||||
es.addEventListener('connected', () => {
|
||||
setIsConnected(true)
|
||||
callbacksRef.current.onConnected?.()
|
||||
})
|
||||
|
||||
es.addEventListener('vote_update', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data) as VoteUpdate
|
||||
callbacksRef.current.onVoteUpdate?.(data)
|
||||
} catch {
|
||||
// Ignore parse errors
|
||||
}
|
||||
})
|
||||
|
||||
es.addEventListener('session_status', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data) as SessionStatusUpdate
|
||||
callbacksRef.current.onSessionStatus?.(data)
|
||||
} catch {
|
||||
// Ignore parse errors
|
||||
}
|
||||
})
|
||||
|
||||
es.addEventListener('project_change', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data) as ProjectChangeUpdate
|
||||
callbacksRef.current.onProjectChange?.(data)
|
||||
} catch {
|
||||
// Ignore parse errors
|
||||
}
|
||||
})
|
||||
|
||||
es.onerror = (event) => {
|
||||
setIsConnected(false)
|
||||
callbacksRef.current.onError?.(event)
|
||||
|
||||
// Auto-reconnect after 3 seconds
|
||||
setTimeout(() => {
|
||||
if (eventSourceRef.current === es) {
|
||||
connect()
|
||||
}
|
||||
}, 3000)
|
||||
}
|
||||
}, [sessionId])
|
||||
|
||||
const disconnect = useCallback(() => {
|
||||
if (eventSourceRef.current) {
|
||||
eventSourceRef.current.close()
|
||||
eventSourceRef.current = null
|
||||
setIsConnected(false)
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
connect()
|
||||
return () => disconnect()
|
||||
}, [connect, disconnect])
|
||||
|
||||
return { isConnected, reconnect: connect, disconnect }
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
import { getRequestConfig } from 'next-intl/server'
|
||||
import { cookies } from 'next/headers'
|
||||
|
||||
export const supportedLocales = ['en', 'fr'] as const
|
||||
export type SupportedLocale = (typeof supportedLocales)[number]
|
||||
|
||||
export const defaultLocale: SupportedLocale = 'en'
|
||||
|
||||
export default getRequestConfig(async () => {
|
||||
const store = await cookies()
|
||||
const cookieLocale = store.get('locale')?.value
|
||||
|
||||
// Validate the locale from cookie
|
||||
const locale = supportedLocales.includes(cookieLocale as SupportedLocale)
|
||||
? (cookieLocale as SupportedLocale)
|
||||
: defaultLocale
|
||||
|
||||
return {
|
||||
locale,
|
||||
messages: (await import(`../../messages/${locale}.json`)).default,
|
||||
}
|
||||
})
|
||||
|
|
@ -29,6 +29,10 @@ import { mentorRouter } from './mentor'
|
|||
import { filteringRouter } from './filtering'
|
||||
import { specialAwardRouter } from './specialAward'
|
||||
import { notificationRouter } from './notification'
|
||||
// Feature expansion routers
|
||||
import { roundTemplateRouter } from './roundTemplate'
|
||||
import { messageRouter } from './message'
|
||||
import { webhookRouter } from './webhook'
|
||||
|
||||
/**
|
||||
* Root tRPC router that combines all domain routers
|
||||
|
|
@ -64,6 +68,10 @@ export const appRouter = router({
|
|||
filtering: filteringRouter,
|
||||
specialAward: specialAwardRouter,
|
||||
notification: notificationRouter,
|
||||
// Feature expansion routers
|
||||
roundTemplate: roundTemplateRouter,
|
||||
message: messageRouter,
|
||||
webhook: webhookRouter,
|
||||
})
|
||||
|
||||
export type AppRouter = typeof appRouter
|
||||
|
|
|
|||
|
|
@ -366,4 +366,272 @@ export const analyticsRouter = router({
|
|||
count: d._count.id,
|
||||
}))
|
||||
}),
|
||||
|
||||
// =========================================================================
|
||||
// Advanced Analytics (F10)
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Compare metrics across multiple rounds
|
||||
*/
|
||||
getCrossRoundComparison: observerProcedure
|
||||
.input(z.object({ roundIds: z.array(z.string()).min(2) }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const comparisons = await Promise.all(
|
||||
input.roundIds.map(async (roundId) => {
|
||||
const round = await ctx.prisma.round.findUniqueOrThrow({
|
||||
where: { id: roundId },
|
||||
select: { id: true, name: true },
|
||||
})
|
||||
|
||||
const [projectCount, assignmentCount, evaluationCount] = await Promise.all([
|
||||
ctx.prisma.project.count({ where: { roundId } }),
|
||||
ctx.prisma.assignment.count({ where: { roundId } }),
|
||||
ctx.prisma.evaluation.count({
|
||||
where: {
|
||||
assignment: { roundId },
|
||||
status: 'SUBMITTED',
|
||||
},
|
||||
}),
|
||||
])
|
||||
|
||||
const completionRate = assignmentCount > 0
|
||||
? Math.round((evaluationCount / assignmentCount) * 100)
|
||||
: 0
|
||||
|
||||
// Get average scores
|
||||
const evaluations = await ctx.prisma.evaluation.findMany({
|
||||
where: {
|
||||
assignment: { roundId },
|
||||
status: 'SUBMITTED',
|
||||
},
|
||||
select: { globalScore: true },
|
||||
})
|
||||
|
||||
const globalScores = evaluations
|
||||
.map((e) => e.globalScore)
|
||||
.filter((s): s is number => s !== null)
|
||||
|
||||
const averageScore = globalScores.length > 0
|
||||
? globalScores.reduce((a, b) => a + b, 0) / globalScores.length
|
||||
: null
|
||||
|
||||
// Score distribution
|
||||
const distribution = Array.from({ length: 10 }, (_, i) => ({
|
||||
score: i + 1,
|
||||
count: globalScores.filter((s) => Math.round(s) === i + 1).length,
|
||||
}))
|
||||
|
||||
return {
|
||||
roundId,
|
||||
roundName: round.name,
|
||||
projectCount,
|
||||
evaluationCount,
|
||||
completionRate,
|
||||
averageScore,
|
||||
scoreDistribution: distribution,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return comparisons
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get juror consistency metrics for a round
|
||||
*/
|
||||
getJurorConsistency: observerProcedure
|
||||
.input(z.object({ roundId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const evaluations = await ctx.prisma.evaluation.findMany({
|
||||
where: {
|
||||
assignment: { roundId: input.roundId },
|
||||
status: 'SUBMITTED',
|
||||
},
|
||||
include: {
|
||||
assignment: {
|
||||
include: {
|
||||
user: { select: { id: true, name: true, email: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// Group scores by juror
|
||||
const jurorScores: Record<string, { name: string; email: string; scores: number[] }> = {}
|
||||
|
||||
evaluations.forEach((e) => {
|
||||
const userId = e.assignment.userId
|
||||
if (!jurorScores[userId]) {
|
||||
jurorScores[userId] = {
|
||||
name: e.assignment.user.name || e.assignment.user.email || 'Unknown',
|
||||
email: e.assignment.user.email || '',
|
||||
scores: [],
|
||||
}
|
||||
}
|
||||
if (e.globalScore !== null) {
|
||||
jurorScores[userId].scores.push(e.globalScore)
|
||||
}
|
||||
})
|
||||
|
||||
// Calculate overall average
|
||||
const allScores = Object.values(jurorScores).flatMap((j) => j.scores)
|
||||
const overallAverage = allScores.length > 0
|
||||
? allScores.reduce((a, b) => a + b, 0) / allScores.length
|
||||
: 0
|
||||
|
||||
// Calculate per-juror metrics
|
||||
const metrics = Object.entries(jurorScores).map(([userId, data]) => {
|
||||
const avg = data.scores.length > 0
|
||||
? data.scores.reduce((a, b) => a + b, 0) / data.scores.length
|
||||
: 0
|
||||
const variance = data.scores.length > 1
|
||||
? data.scores.reduce((sum, s) => sum + Math.pow(s - avg, 2), 0) / data.scores.length
|
||||
: 0
|
||||
const stddev = Math.sqrt(variance)
|
||||
const deviationFromOverall = Math.abs(avg - overallAverage)
|
||||
|
||||
return {
|
||||
userId,
|
||||
name: data.name,
|
||||
email: data.email,
|
||||
evaluationCount: data.scores.length,
|
||||
averageScore: avg,
|
||||
stddev,
|
||||
deviationFromOverall,
|
||||
isOutlier: deviationFromOverall > 2, // Flag if 2+ points from mean
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
overallAverage,
|
||||
jurors: metrics.sort((a, b) => b.deviationFromOverall - a.deviationFromOverall),
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get diversity metrics for projects in a round
|
||||
*/
|
||||
getDiversityMetrics: observerProcedure
|
||||
.input(z.object({ roundId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const projects = await ctx.prisma.project.findMany({
|
||||
where: { roundId: input.roundId },
|
||||
select: {
|
||||
country: true,
|
||||
competitionCategory: true,
|
||||
oceanIssue: true,
|
||||
tags: true,
|
||||
},
|
||||
})
|
||||
|
||||
const total = projects.length
|
||||
if (total === 0) {
|
||||
return { total: 0, byCountry: [], byCategory: [], byOceanIssue: [], byTag: [] }
|
||||
}
|
||||
|
||||
// By country
|
||||
const countryCounts: Record<string, number> = {}
|
||||
projects.forEach((p) => {
|
||||
const key = p.country || 'Unknown'
|
||||
countryCounts[key] = (countryCounts[key] || 0) + 1
|
||||
})
|
||||
const byCountry = Object.entries(countryCounts)
|
||||
.map(([country, count]) => ({ country, count, percentage: (count / total) * 100 }))
|
||||
.sort((a, b) => b.count - a.count)
|
||||
|
||||
// By competition category
|
||||
const categoryCounts: Record<string, number> = {}
|
||||
projects.forEach((p) => {
|
||||
const key = p.competitionCategory || 'Uncategorized'
|
||||
categoryCounts[key] = (categoryCounts[key] || 0) + 1
|
||||
})
|
||||
const byCategory = Object.entries(categoryCounts)
|
||||
.map(([category, count]) => ({ category, count, percentage: (count / total) * 100 }))
|
||||
.sort((a, b) => b.count - a.count)
|
||||
|
||||
// By ocean issue
|
||||
const issueCounts: Record<string, number> = {}
|
||||
projects.forEach((p) => {
|
||||
const key = p.oceanIssue || 'Unspecified'
|
||||
issueCounts[key] = (issueCounts[key] || 0) + 1
|
||||
})
|
||||
const byOceanIssue = Object.entries(issueCounts)
|
||||
.map(([issue, count]) => ({ issue, count, percentage: (count / total) * 100 }))
|
||||
.sort((a, b) => b.count - a.count)
|
||||
|
||||
// By tag
|
||||
const tagCounts: Record<string, number> = {}
|
||||
projects.forEach((p) => {
|
||||
p.tags.forEach((tag) => {
|
||||
tagCounts[tag] = (tagCounts[tag] || 0) + 1
|
||||
})
|
||||
})
|
||||
const byTag = Object.entries(tagCounts)
|
||||
.map(([tag, count]) => ({ tag, count, percentage: (count / total) * 100 }))
|
||||
.sort((a, b) => b.count - a.count)
|
||||
|
||||
return { total, byCountry, byCategory, byOceanIssue, byTag }
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get year-over-year stats across all rounds in a program
|
||||
*/
|
||||
getYearOverYear: observerProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const rounds = await ctx.prisma.round.findMany({
|
||||
where: { programId: input.programId },
|
||||
select: { id: true, name: true, createdAt: true },
|
||||
orderBy: { createdAt: 'asc' },
|
||||
})
|
||||
|
||||
const stats = await Promise.all(
|
||||
rounds.map(async (round) => {
|
||||
const [projectCount, evaluationCount, assignmentCount] = await Promise.all([
|
||||
ctx.prisma.project.count({ where: { roundId: round.id } }),
|
||||
ctx.prisma.evaluation.count({
|
||||
where: {
|
||||
assignment: { roundId: round.id },
|
||||
status: 'SUBMITTED',
|
||||
},
|
||||
}),
|
||||
ctx.prisma.assignment.count({ where: { roundId: round.id } }),
|
||||
])
|
||||
|
||||
const completionRate = assignmentCount > 0
|
||||
? Math.round((evaluationCount / assignmentCount) * 100)
|
||||
: 0
|
||||
|
||||
// Average score
|
||||
const evaluations = await ctx.prisma.evaluation.findMany({
|
||||
where: {
|
||||
assignment: { roundId: round.id },
|
||||
status: 'SUBMITTED',
|
||||
},
|
||||
select: { globalScore: true },
|
||||
})
|
||||
|
||||
const scores = evaluations
|
||||
.map((e) => e.globalScore)
|
||||
.filter((s): s is number => s !== null)
|
||||
|
||||
const averageScore = scores.length > 0
|
||||
? scores.reduce((a, b) => a + b, 0) / scores.length
|
||||
: null
|
||||
|
||||
return {
|
||||
roundId: round.id,
|
||||
roundName: round.name,
|
||||
createdAt: round.createdAt,
|
||||
projectCount,
|
||||
evaluationCount,
|
||||
completionRate,
|
||||
averageScore,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return stats
|
||||
}),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { z } from 'zod'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { router, publicProcedure } from '../trpc'
|
||||
import { CompetitionCategory, OceanIssue, TeamMemberRole } from '@prisma/client'
|
||||
import { Prisma, CompetitionCategory, OceanIssue, TeamMemberRole } from '@prisma/client'
|
||||
import {
|
||||
createNotification,
|
||||
notifyAdmins,
|
||||
|
|
@ -386,4 +386,278 @@ export const applicationRouter = router({
|
|||
: null,
|
||||
}
|
||||
}),
|
||||
|
||||
// =========================================================================
|
||||
// Draft Saving & Resume (F11)
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Save application as draft with resume token
|
||||
*/
|
||||
saveDraft: publicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
roundSlug: z.string(),
|
||||
email: z.string().email(),
|
||||
draftDataJson: z.record(z.unknown()),
|
||||
title: z.string().optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Find round by slug
|
||||
const round = await ctx.prisma.round.findFirst({
|
||||
where: { slug: input.roundSlug },
|
||||
})
|
||||
|
||||
if (!round) {
|
||||
throw new TRPCError({
|
||||
code: 'NOT_FOUND',
|
||||
message: 'Round not found',
|
||||
})
|
||||
}
|
||||
|
||||
// Check if drafts are enabled
|
||||
const settings = (round.settingsJson as Record<string, unknown>) || {}
|
||||
if (settings.drafts_enabled === false) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Draft saving is not enabled for this round',
|
||||
})
|
||||
}
|
||||
|
||||
// Calculate draft expiry
|
||||
const draftExpiryDays = (settings.draft_expiry_days as number) || 30
|
||||
const draftExpiresAt = new Date()
|
||||
draftExpiresAt.setDate(draftExpiresAt.getDate() + draftExpiryDays)
|
||||
|
||||
// Generate resume token
|
||||
const draftToken = `draft_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`
|
||||
|
||||
// Find or create draft project for this email+round
|
||||
const existingDraft = await ctx.prisma.project.findFirst({
|
||||
where: {
|
||||
roundId: round.id,
|
||||
submittedByEmail: input.email,
|
||||
isDraft: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (existingDraft) {
|
||||
// Update existing draft
|
||||
const updated = await ctx.prisma.project.update({
|
||||
where: { id: existingDraft.id },
|
||||
data: {
|
||||
title: input.title || existingDraft.title,
|
||||
draftDataJson: input.draftDataJson as Prisma.InputJsonValue,
|
||||
draftExpiresAt,
|
||||
metadataJson: {
|
||||
...((existingDraft.metadataJson as Record<string, unknown>) || {}),
|
||||
draftToken,
|
||||
} as Prisma.InputJsonValue,
|
||||
},
|
||||
})
|
||||
|
||||
return { projectId: updated.id, draftToken }
|
||||
}
|
||||
|
||||
// Create new draft project
|
||||
const project = await ctx.prisma.project.create({
|
||||
data: {
|
||||
roundId: round.id,
|
||||
title: input.title || 'Untitled Draft',
|
||||
isDraft: true,
|
||||
draftDataJson: input.draftDataJson as Prisma.InputJsonValue,
|
||||
draftExpiresAt,
|
||||
submittedByEmail: input.email,
|
||||
metadataJson: {
|
||||
draftToken,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return { projectId: project.id, draftToken }
|
||||
}),
|
||||
|
||||
/**
|
||||
* Resume a draft application using a token
|
||||
*/
|
||||
resumeDraft: publicProcedure
|
||||
.input(z.object({ draftToken: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const projects = await ctx.prisma.project.findMany({
|
||||
where: {
|
||||
isDraft: true,
|
||||
},
|
||||
})
|
||||
|
||||
// Find project with matching token in metadataJson
|
||||
const project = projects.find((p) => {
|
||||
const metadata = p.metadataJson as Record<string, unknown> | null
|
||||
return metadata?.draftToken === input.draftToken
|
||||
})
|
||||
|
||||
if (!project) {
|
||||
throw new TRPCError({
|
||||
code: 'NOT_FOUND',
|
||||
message: 'Draft not found or invalid token',
|
||||
})
|
||||
}
|
||||
|
||||
// Check expiry
|
||||
if (project.draftExpiresAt && new Date() > project.draftExpiresAt) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'This draft has expired',
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
projectId: project.id,
|
||||
draftDataJson: project.draftDataJson,
|
||||
title: project.title,
|
||||
roundId: project.roundId,
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Submit a saved draft as a final application
|
||||
*/
|
||||
submitDraft: publicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
projectId: z.string(),
|
||||
draftToken: z.string(),
|
||||
data: applicationSchema,
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const project = await ctx.prisma.project.findUniqueOrThrow({
|
||||
where: { id: input.projectId },
|
||||
include: { round: { include: { program: true } } },
|
||||
})
|
||||
|
||||
// Verify token
|
||||
const metadata = (project.metadataJson as Record<string, unknown>) || {}
|
||||
if (metadata.draftToken !== input.draftToken) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Invalid draft token',
|
||||
})
|
||||
}
|
||||
|
||||
if (!project.isDraft) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'This project has already been submitted',
|
||||
})
|
||||
}
|
||||
|
||||
const now = new Date()
|
||||
const { data } = input
|
||||
|
||||
// Find or create user
|
||||
let user = await ctx.prisma.user.findUnique({
|
||||
where: { email: data.contactEmail },
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
user = await ctx.prisma.user.create({
|
||||
data: {
|
||||
email: data.contactEmail,
|
||||
name: data.contactName,
|
||||
role: 'APPLICANT',
|
||||
status: 'ACTIVE',
|
||||
phoneNumber: data.contactPhone,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Update project with final data
|
||||
const updated = await ctx.prisma.project.update({
|
||||
where: { id: input.projectId },
|
||||
data: {
|
||||
isDraft: false,
|
||||
draftDataJson: Prisma.DbNull,
|
||||
draftExpiresAt: null,
|
||||
title: data.projectName,
|
||||
teamName: data.teamName,
|
||||
description: data.description,
|
||||
competitionCategory: data.competitionCategory,
|
||||
oceanIssue: data.oceanIssue,
|
||||
country: data.country,
|
||||
geographicZone: data.city ? `${data.city}, ${data.country}` : data.country,
|
||||
institution: data.institution,
|
||||
wantsMentorship: data.wantsMentorship,
|
||||
referralSource: data.referralSource,
|
||||
submissionSource: 'PUBLIC_FORM',
|
||||
submittedByEmail: data.contactEmail,
|
||||
submittedByUserId: user.id,
|
||||
submittedAt: now,
|
||||
status: 'SUBMITTED',
|
||||
metadataJson: {
|
||||
contactPhone: data.contactPhone,
|
||||
startupCreatedDate: data.startupCreatedDate,
|
||||
gdprConsentAt: now.toISOString(),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// Audit log
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: user.id,
|
||||
action: 'DRAFT_SUBMITTED',
|
||||
entityType: 'Project',
|
||||
entityId: updated.id,
|
||||
detailsJson: {
|
||||
source: 'draft_submission',
|
||||
title: data.projectName,
|
||||
category: data.competitionCategory,
|
||||
},
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
})
|
||||
} catch {
|
||||
// Never throw on audit failure
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
projectId: updated.id,
|
||||
message: `Thank you for applying to ${project.round.program.name}!`,
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get a read-only preview of draft data
|
||||
*/
|
||||
getPreview: publicProcedure
|
||||
.input(z.object({ draftToken: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const projects = await ctx.prisma.project.findMany({
|
||||
where: {
|
||||
isDraft: true,
|
||||
},
|
||||
})
|
||||
|
||||
const project = projects.find((p) => {
|
||||
const metadata = p.metadataJson as Record<string, unknown> | null
|
||||
return metadata?.draftToken === input.draftToken
|
||||
})
|
||||
|
||||
if (!project) {
|
||||
throw new TRPCError({
|
||||
code: 'NOT_FOUND',
|
||||
message: 'Draft not found or invalid token',
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
title: project.title,
|
||||
draftDataJson: project.draftDataJson,
|
||||
createdAt: project.createdAt,
|
||||
expiresAt: project.draftExpiresAt,
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { z } from 'zod'
|
||||
import { router, adminProcedure } from '../trpc'
|
||||
import { router, adminProcedure, superAdminProcedure } from '../trpc'
|
||||
import { logAudit } from '../utils/audit'
|
||||
|
||||
export const auditRouter = router({
|
||||
/**
|
||||
|
|
@ -181,4 +182,158 @@ export const auditRouter = router({
|
|||
})),
|
||||
}
|
||||
}),
|
||||
|
||||
// =========================================================================
|
||||
// Anomaly Detection & Session Tracking (F14)
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Detect anomalous activity patterns within a time window
|
||||
*/
|
||||
getAnomalies: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
timeWindowMinutes: z.number().int().min(1).max(1440).default(60),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
// Load anomaly rules from settings
|
||||
const rulesSetting = await ctx.prisma.systemSettings.findUnique({
|
||||
where: { key: 'audit_anomaly_rules' },
|
||||
})
|
||||
|
||||
const rules = rulesSetting?.value
|
||||
? JSON.parse(rulesSetting.value) as {
|
||||
rapid_changes_per_minute?: number
|
||||
bulk_operations_threshold?: number
|
||||
}
|
||||
: { rapid_changes_per_minute: 30, bulk_operations_threshold: 50 }
|
||||
|
||||
const rapidThreshold = rules.rapid_changes_per_minute || 30
|
||||
const bulkThreshold = rules.bulk_operations_threshold || 50
|
||||
|
||||
const windowStart = new Date()
|
||||
windowStart.setMinutes(windowStart.getMinutes() - input.timeWindowMinutes)
|
||||
|
||||
// Get action counts per user in the time window
|
||||
const userActivity = await ctx.prisma.auditLog.groupBy({
|
||||
by: ['userId'],
|
||||
where: {
|
||||
timestamp: { gte: windowStart },
|
||||
userId: { not: null },
|
||||
},
|
||||
_count: true,
|
||||
})
|
||||
|
||||
// Filter for users exceeding thresholds
|
||||
const suspiciousUserIds = userActivity
|
||||
.filter((u) => u._count >= bulkThreshold)
|
||||
.map((u) => u.userId)
|
||||
.filter((id): id is string => id !== null)
|
||||
|
||||
// Get user details
|
||||
const users = suspiciousUserIds.length > 0
|
||||
? await ctx.prisma.user.findMany({
|
||||
where: { id: { in: suspiciousUserIds } },
|
||||
select: { id: true, name: true, email: true, role: true },
|
||||
})
|
||||
: []
|
||||
|
||||
const userMap = new Map(users.map((u) => [u.id, u]))
|
||||
|
||||
const anomalies = userActivity
|
||||
.filter((u) => u._count >= bulkThreshold)
|
||||
.map((u) => ({
|
||||
userId: u.userId,
|
||||
user: u.userId ? userMap.get(u.userId) || null : null,
|
||||
actionCount: u._count,
|
||||
timeWindowMinutes: input.timeWindowMinutes,
|
||||
actionsPerMinute: u._count / input.timeWindowMinutes,
|
||||
isRapid: (u._count / input.timeWindowMinutes) >= rapidThreshold,
|
||||
isBulk: u._count >= bulkThreshold,
|
||||
}))
|
||||
.sort((a, b) => b.actionCount - a.actionCount)
|
||||
|
||||
return {
|
||||
anomalies,
|
||||
thresholds: {
|
||||
rapidChangesPerMinute: rapidThreshold,
|
||||
bulkOperationsThreshold: bulkThreshold,
|
||||
},
|
||||
timeWindow: {
|
||||
start: windowStart,
|
||||
end: new Date(),
|
||||
minutes: input.timeWindowMinutes,
|
||||
},
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get all audit logs for a specific session
|
||||
*/
|
||||
getSessionTimeline: adminProcedure
|
||||
.input(z.object({ sessionId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const logs = await ctx.prisma.auditLog.findMany({
|
||||
where: { sessionId: input.sessionId },
|
||||
orderBy: { timestamp: 'asc' },
|
||||
include: {
|
||||
user: { select: { id: true, name: true, email: true } },
|
||||
},
|
||||
})
|
||||
|
||||
return logs
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get current audit retention configuration
|
||||
*/
|
||||
getRetentionConfig: adminProcedure.query(async ({ ctx }) => {
|
||||
const setting = await ctx.prisma.systemSettings.findUnique({
|
||||
where: { key: 'audit_retention_days' },
|
||||
})
|
||||
|
||||
return {
|
||||
retentionDays: setting?.value ? parseInt(setting.value, 10) : 365,
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Update audit retention configuration (super admin only)
|
||||
*/
|
||||
updateRetentionConfig: superAdminProcedure
|
||||
.input(z.object({ retentionDays: z.number().int().min(30) }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const setting = await ctx.prisma.systemSettings.upsert({
|
||||
where: { key: 'audit_retention_days' },
|
||||
update: {
|
||||
value: input.retentionDays.toString(),
|
||||
updatedBy: ctx.user.id,
|
||||
},
|
||||
create: {
|
||||
key: 'audit_retention_days',
|
||||
value: input.retentionDays.toString(),
|
||||
category: 'AUDIT_CONFIG',
|
||||
updatedBy: ctx.user.id,
|
||||
},
|
||||
})
|
||||
|
||||
// Audit log
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'UPDATE_RETENTION_CONFIG',
|
||||
entityType: 'SystemSettings',
|
||||
entityId: setting.id,
|
||||
detailsJson: { retentionDays: input.retentionDays },
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
})
|
||||
} catch {
|
||||
// Never throw on audit failure
|
||||
}
|
||||
|
||||
return { retentionDays: input.retentionDays }
|
||||
}),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { z } from 'zod'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { router, protectedProcedure, adminProcedure } from '../trpc'
|
||||
import { router, protectedProcedure, adminProcedure, juryProcedure } from '../trpc'
|
||||
import { logAudit } from '@/server/utils/audit'
|
||||
import { notifyAdmins, NotificationTypes } from '../services/in-app-notification'
|
||||
import { processEvaluationReminders } from '../services/evaluation-reminders'
|
||||
|
|
@ -621,4 +621,397 @@ export const evaluationRouter = router({
|
|||
errors,
|
||||
}
|
||||
}),
|
||||
|
||||
// =========================================================================
|
||||
// Side-by-Side Comparison (F4)
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Get multiple projects with evaluations for side-by-side comparison
|
||||
*/
|
||||
getMultipleForComparison: juryProcedure
|
||||
.input(
|
||||
z.object({
|
||||
projectIds: z.array(z.string()).min(2).max(3),
|
||||
roundId: z.string(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
// Verify all projects are assigned to current user in this round
|
||||
const assignments = await ctx.prisma.assignment.findMany({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
roundId: input.roundId,
|
||||
projectId: { in: input.projectIds },
|
||||
},
|
||||
include: {
|
||||
project: {
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
teamName: true,
|
||||
description: true,
|
||||
country: true,
|
||||
tags: true,
|
||||
files: {
|
||||
select: {
|
||||
id: true,
|
||||
fileName: true,
|
||||
fileType: true,
|
||||
size: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
evaluation: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (assignments.length !== input.projectIds.length) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You are not assigned to all requested projects in this round',
|
||||
})
|
||||
}
|
||||
|
||||
return assignments.map((a) => ({
|
||||
project: a.project,
|
||||
evaluation: a.evaluation,
|
||||
assignmentId: a.id,
|
||||
}))
|
||||
}),
|
||||
|
||||
// =========================================================================
|
||||
// Peer Review & Discussion (F13)
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Get anonymized peer evaluation summary for a project
|
||||
*/
|
||||
getPeerSummary: juryProcedure
|
||||
.input(
|
||||
z.object({
|
||||
projectId: z.string(),
|
||||
roundId: z.string(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
// Verify user has submitted their own evaluation first
|
||||
const userAssignment = await ctx.prisma.assignment.findFirst({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
},
|
||||
include: { evaluation: true },
|
||||
})
|
||||
|
||||
if (!userAssignment || userAssignment.evaluation?.status !== 'SUBMITTED') {
|
||||
throw new TRPCError({
|
||||
code: 'PRECONDITION_FAILED',
|
||||
message: 'You must submit your own evaluation before viewing peer summaries',
|
||||
})
|
||||
}
|
||||
|
||||
// Check round settings for peer review
|
||||
const round = await ctx.prisma.round.findUniqueOrThrow({
|
||||
where: { id: input.roundId },
|
||||
})
|
||||
|
||||
const settings = (round.settingsJson as Record<string, unknown>) || {}
|
||||
if (!settings.peer_review_enabled) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Peer review is not enabled for this round',
|
||||
})
|
||||
}
|
||||
|
||||
// Get all submitted evaluations for this project
|
||||
const evaluations = await ctx.prisma.evaluation.findMany({
|
||||
where: {
|
||||
status: 'SUBMITTED',
|
||||
assignment: {
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
assignment: {
|
||||
include: {
|
||||
user: { select: { id: true, name: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (evaluations.length === 0) {
|
||||
return { aggregated: null, individualScores: [], totalEvaluations: 0 }
|
||||
}
|
||||
|
||||
// Calculate average and stddev per criterion
|
||||
const criterionData: Record<string, number[]> = {}
|
||||
evaluations.forEach((e) => {
|
||||
const scores = e.criterionScoresJson as Record<string, number> | null
|
||||
if (scores) {
|
||||
Object.entries(scores).forEach(([key, val]) => {
|
||||
if (typeof val === 'number') {
|
||||
if (!criterionData[key]) criterionData[key] = []
|
||||
criterionData[key].push(val)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
const aggregated: Record<string, { average: number; stddev: number; count: number; distribution: Record<number, number> }> = {}
|
||||
Object.entries(criterionData).forEach(([key, scores]) => {
|
||||
const avg = scores.reduce((a, b) => a + b, 0) / scores.length
|
||||
const variance = scores.reduce((sum, s) => sum + Math.pow(s - avg, 2), 0) / scores.length
|
||||
const stddev = Math.sqrt(variance)
|
||||
|
||||
const distribution: Record<number, number> = {}
|
||||
scores.forEach((s) => {
|
||||
const bucket = Math.round(s)
|
||||
distribution[bucket] = (distribution[bucket] || 0) + 1
|
||||
})
|
||||
|
||||
aggregated[key] = { average: avg, stddev, count: scores.length, distribution }
|
||||
})
|
||||
|
||||
// Anonymize individual scores based on round settings
|
||||
const anonymizationLevel = (settings.anonymization_level as string) || 'fully_anonymous'
|
||||
|
||||
const individualScores = evaluations.map((e) => {
|
||||
let jurorLabel: string
|
||||
if (anonymizationLevel === 'named') {
|
||||
jurorLabel = e.assignment.user.name || 'Juror'
|
||||
} else if (anonymizationLevel === 'show_initials') {
|
||||
const name = e.assignment.user.name || ''
|
||||
jurorLabel = name
|
||||
.split(' ')
|
||||
.map((n) => n[0])
|
||||
.join('')
|
||||
.toUpperCase() || 'J'
|
||||
} else {
|
||||
jurorLabel = `Juror ${evaluations.indexOf(e) + 1}`
|
||||
}
|
||||
|
||||
return {
|
||||
jurorLabel,
|
||||
globalScore: e.globalScore,
|
||||
binaryDecision: e.binaryDecision,
|
||||
criterionScoresJson: e.criterionScoresJson,
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
aggregated,
|
||||
individualScores,
|
||||
totalEvaluations: evaluations.length,
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get or create a discussion for a project evaluation
|
||||
*/
|
||||
getDiscussion: juryProcedure
|
||||
.input(
|
||||
z.object({
|
||||
projectId: z.string(),
|
||||
roundId: z.string(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
// Get or create discussion
|
||||
let discussion = await ctx.prisma.evaluationDiscussion.findUnique({
|
||||
where: {
|
||||
projectId_roundId: {
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
comments: {
|
||||
include: {
|
||||
user: { select: { id: true, name: true } },
|
||||
},
|
||||
orderBy: { createdAt: 'asc' },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!discussion) {
|
||||
discussion = await ctx.prisma.evaluationDiscussion.create({
|
||||
data: {
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
},
|
||||
include: {
|
||||
comments: {
|
||||
include: {
|
||||
user: { select: { id: true, name: true } },
|
||||
},
|
||||
orderBy: { createdAt: 'asc' },
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Anonymize comments based on round settings
|
||||
const round = await ctx.prisma.round.findUniqueOrThrow({
|
||||
where: { id: input.roundId },
|
||||
})
|
||||
const settings = (round.settingsJson as Record<string, unknown>) || {}
|
||||
const anonymizationLevel = (settings.anonymization_level as string) || 'fully_anonymous'
|
||||
|
||||
const anonymizedComments = discussion.comments.map((c, idx) => {
|
||||
let authorLabel: string
|
||||
if (anonymizationLevel === 'named' || c.userId === ctx.user.id) {
|
||||
authorLabel = c.user.name || 'Juror'
|
||||
} else if (anonymizationLevel === 'show_initials') {
|
||||
const name = c.user.name || ''
|
||||
authorLabel = name
|
||||
.split(' ')
|
||||
.map((n) => n[0])
|
||||
.join('')
|
||||
.toUpperCase() || 'J'
|
||||
} else {
|
||||
authorLabel = `Juror ${idx + 1}`
|
||||
}
|
||||
|
||||
return {
|
||||
id: c.id,
|
||||
authorLabel,
|
||||
isOwn: c.userId === ctx.user.id,
|
||||
content: c.content,
|
||||
createdAt: c.createdAt,
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
id: discussion.id,
|
||||
status: discussion.status,
|
||||
createdAt: discussion.createdAt,
|
||||
closedAt: discussion.closedAt,
|
||||
comments: anonymizedComments,
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Add a comment to a project evaluation discussion
|
||||
*/
|
||||
addComment: juryProcedure
|
||||
.input(
|
||||
z.object({
|
||||
projectId: z.string(),
|
||||
roundId: z.string(),
|
||||
content: z.string().min(1).max(2000),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Check max comment length from round settings
|
||||
const round = await ctx.prisma.round.findUniqueOrThrow({
|
||||
where: { id: input.roundId },
|
||||
})
|
||||
const settings = (round.settingsJson as Record<string, unknown>) || {}
|
||||
const maxLength = (settings.max_comment_length as number) || 2000
|
||||
if (input.content.length > maxLength) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: `Comment exceeds maximum length of ${maxLength} characters`,
|
||||
})
|
||||
}
|
||||
|
||||
// Get or create discussion
|
||||
let discussion = await ctx.prisma.evaluationDiscussion.findUnique({
|
||||
where: {
|
||||
projectId_roundId: {
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!discussion) {
|
||||
discussion = await ctx.prisma.evaluationDiscussion.create({
|
||||
data: {
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
if (discussion.status === 'closed') {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'This discussion has been closed',
|
||||
})
|
||||
}
|
||||
|
||||
const comment = await ctx.prisma.discussionComment.create({
|
||||
data: {
|
||||
discussionId: discussion.id,
|
||||
userId: ctx.user.id,
|
||||
content: input.content,
|
||||
},
|
||||
})
|
||||
|
||||
// Audit log
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'DISCUSSION_COMMENT_ADDED',
|
||||
entityType: 'DiscussionComment',
|
||||
entityId: comment.id,
|
||||
detailsJson: {
|
||||
discussionId: discussion.id,
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
},
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
})
|
||||
} catch {
|
||||
// Never throw on audit failure
|
||||
}
|
||||
|
||||
return comment
|
||||
}),
|
||||
|
||||
/**
|
||||
* Close a discussion (admin only)
|
||||
*/
|
||||
closeDiscussion: adminProcedure
|
||||
.input(z.object({ discussionId: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const discussion = await ctx.prisma.evaluationDiscussion.update({
|
||||
where: { id: input.discussionId },
|
||||
data: {
|
||||
status: 'closed',
|
||||
closedAt: new Date(),
|
||||
closedById: ctx.user.id,
|
||||
},
|
||||
})
|
||||
|
||||
// Audit log
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'DISCUSSION_CLOSED',
|
||||
entityType: 'EvaluationDiscussion',
|
||||
entityId: input.discussionId,
|
||||
detailsJson: {
|
||||
projectId: discussion.projectId,
|
||||
roundId: discussion.roundId,
|
||||
},
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
})
|
||||
} catch {
|
||||
// Never throw on audit failure
|
||||
}
|
||||
|
||||
return discussion
|
||||
}),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { z } from 'zod'
|
||||
import { router, adminProcedure } from '../trpc'
|
||||
import { router, adminProcedure, observerProcedure } from '../trpc'
|
||||
import { logAudit } from '../utils/audit'
|
||||
|
||||
export const exportRouter = router({
|
||||
|
|
@ -388,4 +388,234 @@ export const exportRouter = router({
|
|||
],
|
||||
}
|
||||
}),
|
||||
|
||||
// =========================================================================
|
||||
// PDF Report Data (F10)
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Compile structured data for PDF report generation
|
||||
*/
|
||||
getReportData: observerProcedure
|
||||
.input(
|
||||
z.object({
|
||||
roundId: z.string(),
|
||||
sections: z.array(z.string()).optional(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const includeSection = (name: string) =>
|
||||
!input.sections || input.sections.length === 0 || input.sections.includes(name)
|
||||
|
||||
const round = await ctx.prisma.round.findUniqueOrThrow({
|
||||
where: { id: input.roundId },
|
||||
include: {
|
||||
program: { select: { name: true, year: true } },
|
||||
},
|
||||
})
|
||||
|
||||
const result: Record<string, unknown> = {
|
||||
roundName: round.name,
|
||||
programName: round.program.name,
|
||||
programYear: round.program.year,
|
||||
generatedAt: new Date().toISOString(),
|
||||
}
|
||||
|
||||
// Summary stats
|
||||
if (includeSection('summary')) {
|
||||
const [projectCount, assignmentCount, evaluationCount, jurorCount] = await Promise.all([
|
||||
ctx.prisma.project.count({ where: { roundId: input.roundId } }),
|
||||
ctx.prisma.assignment.count({ where: { roundId: input.roundId } }),
|
||||
ctx.prisma.evaluation.count({
|
||||
where: {
|
||||
assignment: { roundId: input.roundId },
|
||||
status: 'SUBMITTED',
|
||||
},
|
||||
}),
|
||||
ctx.prisma.assignment.groupBy({
|
||||
by: ['userId'],
|
||||
where: { roundId: input.roundId },
|
||||
}),
|
||||
])
|
||||
|
||||
result.summary = {
|
||||
projectCount,
|
||||
assignmentCount,
|
||||
evaluationCount,
|
||||
jurorCount: jurorCount.length,
|
||||
completionRate: assignmentCount > 0
|
||||
? Math.round((evaluationCount / assignmentCount) * 100)
|
||||
: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// Score distributions
|
||||
if (includeSection('scoreDistribution')) {
|
||||
const evaluations = await ctx.prisma.evaluation.findMany({
|
||||
where: {
|
||||
assignment: { roundId: input.roundId },
|
||||
status: 'SUBMITTED',
|
||||
},
|
||||
select: { globalScore: true },
|
||||
})
|
||||
|
||||
const scores = evaluations
|
||||
.map((e) => e.globalScore)
|
||||
.filter((s): s is number => s !== null)
|
||||
|
||||
result.scoreDistribution = {
|
||||
distribution: Array.from({ length: 10 }, (_, i) => ({
|
||||
score: i + 1,
|
||||
count: scores.filter((s) => Math.round(s) === i + 1).length,
|
||||
})),
|
||||
average: scores.length > 0
|
||||
? scores.reduce((a, b) => a + b, 0) / scores.length
|
||||
: null,
|
||||
total: scores.length,
|
||||
}
|
||||
}
|
||||
|
||||
// Rankings
|
||||
if (includeSection('rankings')) {
|
||||
const projects = await ctx.prisma.project.findMany({
|
||||
where: { roundId: input.roundId },
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
teamName: true,
|
||||
status: true,
|
||||
assignments: {
|
||||
select: {
|
||||
evaluation: {
|
||||
select: { globalScore: true, binaryDecision: true, status: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const rankings = projects
|
||||
.map((p) => {
|
||||
const submitted = p.assignments
|
||||
.map((a) => a.evaluation)
|
||||
.filter((e) => e?.status === 'SUBMITTED')
|
||||
const scores = submitted
|
||||
.map((e) => e?.globalScore)
|
||||
.filter((s): s is number => s !== null)
|
||||
const yesVotes = submitted.filter((e) => e?.binaryDecision === true).length
|
||||
|
||||
return {
|
||||
title: p.title,
|
||||
teamName: p.teamName,
|
||||
status: p.status,
|
||||
evaluationCount: submitted.length,
|
||||
averageScore: scores.length > 0
|
||||
? scores.reduce((a, b) => a + b, 0) / scores.length
|
||||
: null,
|
||||
yesPercentage: submitted.length > 0
|
||||
? (yesVotes / submitted.length) * 100
|
||||
: null,
|
||||
}
|
||||
})
|
||||
.filter((r) => r.averageScore !== null)
|
||||
.sort((a, b) => (b.averageScore || 0) - (a.averageScore || 0))
|
||||
|
||||
result.rankings = rankings
|
||||
}
|
||||
|
||||
// Juror stats
|
||||
if (includeSection('jurorStats')) {
|
||||
const assignments = await ctx.prisma.assignment.findMany({
|
||||
where: { roundId: input.roundId },
|
||||
include: {
|
||||
user: { select: { name: true, email: true } },
|
||||
evaluation: { select: { status: true, globalScore: true } },
|
||||
},
|
||||
})
|
||||
|
||||
const byUser: Record<string, { name: string; assigned: number; completed: number; scores: number[] }> = {}
|
||||
assignments.forEach((a) => {
|
||||
if (!byUser[a.userId]) {
|
||||
byUser[a.userId] = {
|
||||
name: a.user.name || a.user.email || 'Unknown',
|
||||
assigned: 0,
|
||||
completed: 0,
|
||||
scores: [],
|
||||
}
|
||||
}
|
||||
byUser[a.userId].assigned++
|
||||
if (a.evaluation?.status === 'SUBMITTED') {
|
||||
byUser[a.userId].completed++
|
||||
if (a.evaluation.globalScore !== null) {
|
||||
byUser[a.userId].scores.push(a.evaluation.globalScore)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
result.jurorStats = Object.values(byUser).map((u) => ({
|
||||
name: u.name,
|
||||
assigned: u.assigned,
|
||||
completed: u.completed,
|
||||
completionRate: u.assigned > 0 ? Math.round((u.completed / u.assigned) * 100) : 0,
|
||||
averageScore: u.scores.length > 0
|
||||
? u.scores.reduce((a, b) => a + b, 0) / u.scores.length
|
||||
: null,
|
||||
}))
|
||||
}
|
||||
|
||||
// Criteria breakdown
|
||||
if (includeSection('criteriaBreakdown')) {
|
||||
const form = await ctx.prisma.evaluationForm.findFirst({
|
||||
where: { roundId: input.roundId, isActive: true },
|
||||
})
|
||||
|
||||
if (form?.criteriaJson) {
|
||||
const criteria = form.criteriaJson as Array<{ id: string; label: string }>
|
||||
const evaluations = await ctx.prisma.evaluation.findMany({
|
||||
where: {
|
||||
assignment: { roundId: input.roundId },
|
||||
status: 'SUBMITTED',
|
||||
},
|
||||
select: { criterionScoresJson: true },
|
||||
})
|
||||
|
||||
result.criteriaBreakdown = criteria.map((c) => {
|
||||
const scores: number[] = []
|
||||
evaluations.forEach((e) => {
|
||||
const cs = e.criterionScoresJson as Record<string, number> | null
|
||||
if (cs && typeof cs[c.id] === 'number') {
|
||||
scores.push(cs[c.id])
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
id: c.id,
|
||||
label: c.label,
|
||||
averageScore: scores.length > 0
|
||||
? scores.reduce((a, b) => a + b, 0) / scores.length
|
||||
: null,
|
||||
count: scores.length,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Audit log for report generation
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'REPORT_GENERATED',
|
||||
entityType: 'Round',
|
||||
entityId: input.roundId,
|
||||
detailsJson: { sections: input.sections },
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
})
|
||||
} catch {
|
||||
// Never throw on audit failure
|
||||
}
|
||||
|
||||
return result
|
||||
}),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -384,4 +384,267 @@ export const fileRouter = router({
|
|||
|
||||
return grouped
|
||||
}),
|
||||
|
||||
/**
|
||||
* Replace a file with a new version
|
||||
*/
|
||||
replaceFile: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
projectId: z.string(),
|
||||
oldFileId: z.string(),
|
||||
fileName: z.string(),
|
||||
fileType: z.enum(['EXEC_SUMMARY', 'PRESENTATION', 'VIDEO', 'OTHER']),
|
||||
mimeType: z.string(),
|
||||
size: z.number().int().positive(),
|
||||
bucket: z.string(),
|
||||
objectKey: z.string(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const isAdmin = ['SUPER_ADMIN', 'PROGRAM_ADMIN'].includes(ctx.user.role)
|
||||
|
||||
if (!isAdmin) {
|
||||
// Check user has access to the project (assigned or team member)
|
||||
const [assignment, mentorAssignment, teamMembership] = await Promise.all([
|
||||
ctx.prisma.assignment.findFirst({
|
||||
where: { userId: ctx.user.id, projectId: input.projectId },
|
||||
select: { id: true },
|
||||
}),
|
||||
ctx.prisma.mentorAssignment.findFirst({
|
||||
where: { mentorId: ctx.user.id, projectId: input.projectId },
|
||||
select: { id: true },
|
||||
}),
|
||||
ctx.prisma.project.findFirst({
|
||||
where: {
|
||||
id: input.projectId,
|
||||
OR: [
|
||||
{ submittedByUserId: ctx.user.id },
|
||||
{ teamMembers: { some: { userId: ctx.user.id } } },
|
||||
],
|
||||
},
|
||||
select: { id: true },
|
||||
}),
|
||||
])
|
||||
|
||||
if (!assignment && !mentorAssignment && !teamMembership) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You do not have access to replace files for this project',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Get the old file to read its version
|
||||
const oldFile = await ctx.prisma.projectFile.findUniqueOrThrow({
|
||||
where: { id: input.oldFileId },
|
||||
select: { id: true, version: true, projectId: true },
|
||||
})
|
||||
|
||||
if (oldFile.projectId !== input.projectId) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'File does not belong to the specified project',
|
||||
})
|
||||
}
|
||||
|
||||
// Create new file and update old file in a transaction
|
||||
const result = await ctx.prisma.$transaction(async (tx) => {
|
||||
const newFile = await tx.projectFile.create({
|
||||
data: {
|
||||
projectId: input.projectId,
|
||||
fileName: input.fileName,
|
||||
fileType: input.fileType,
|
||||
mimeType: input.mimeType,
|
||||
size: input.size,
|
||||
bucket: input.bucket,
|
||||
objectKey: input.objectKey,
|
||||
version: oldFile.version + 1,
|
||||
},
|
||||
})
|
||||
|
||||
// Link old file to new file
|
||||
await tx.projectFile.update({
|
||||
where: { id: input.oldFileId },
|
||||
data: { replacedById: newFile.id },
|
||||
})
|
||||
|
||||
await logAudit({
|
||||
prisma: tx,
|
||||
userId: ctx.user.id,
|
||||
action: 'REPLACE_FILE',
|
||||
entityType: 'ProjectFile',
|
||||
entityId: newFile.id,
|
||||
detailsJson: {
|
||||
projectId: input.projectId,
|
||||
oldFileId: input.oldFileId,
|
||||
oldVersion: oldFile.version,
|
||||
newVersion: newFile.version,
|
||||
fileName: input.fileName,
|
||||
},
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
})
|
||||
|
||||
return newFile
|
||||
})
|
||||
|
||||
return result
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get version history for a file
|
||||
*/
|
||||
getVersionHistory: protectedProcedure
|
||||
.input(z.object({ fileId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
// Find the requested file
|
||||
const file = await ctx.prisma.projectFile.findUniqueOrThrow({
|
||||
where: { id: input.fileId },
|
||||
select: {
|
||||
id: true,
|
||||
projectId: true,
|
||||
fileName: true,
|
||||
fileType: true,
|
||||
mimeType: true,
|
||||
size: true,
|
||||
bucket: true,
|
||||
objectKey: true,
|
||||
version: true,
|
||||
replacedById: true,
|
||||
createdAt: true,
|
||||
},
|
||||
})
|
||||
|
||||
// Walk backwards: find all prior versions by following replacedById chains
|
||||
// First, collect ALL files for this project with the same fileType to find the chain
|
||||
const allRelatedFiles = await ctx.prisma.projectFile.findMany({
|
||||
where: { projectId: file.projectId },
|
||||
select: {
|
||||
id: true,
|
||||
fileName: true,
|
||||
fileType: true,
|
||||
mimeType: true,
|
||||
size: true,
|
||||
bucket: true,
|
||||
objectKey: true,
|
||||
version: true,
|
||||
replacedById: true,
|
||||
createdAt: true,
|
||||
},
|
||||
orderBy: { version: 'asc' },
|
||||
})
|
||||
|
||||
// Build a chain map: fileId -> file that replaced it
|
||||
const replacedByMap = new Map(
|
||||
allRelatedFiles.filter((f) => f.replacedById).map((f) => [f.replacedById!, f.id])
|
||||
)
|
||||
|
||||
// Walk from the current file backwards through replacedById to find all versions in chain
|
||||
const versions: typeof allRelatedFiles = []
|
||||
|
||||
// Find the root of this version chain (walk backwards)
|
||||
let currentId: string | undefined = input.fileId
|
||||
const visited = new Set<string>()
|
||||
while (currentId && !visited.has(currentId)) {
|
||||
visited.add(currentId)
|
||||
const prevId = replacedByMap.get(currentId)
|
||||
if (prevId) {
|
||||
currentId = prevId
|
||||
} else {
|
||||
break // reached root
|
||||
}
|
||||
}
|
||||
|
||||
// Now walk forward from root
|
||||
let walkId: string | undefined = currentId
|
||||
const fileMap = new Map(allRelatedFiles.map((f) => [f.id, f]))
|
||||
const forwardVisited = new Set<string>()
|
||||
while (walkId && !forwardVisited.has(walkId)) {
|
||||
forwardVisited.add(walkId)
|
||||
const f = fileMap.get(walkId)
|
||||
if (f) {
|
||||
versions.push(f)
|
||||
walkId = f.replacedById ?? undefined
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return versions
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get bulk download URLs for project files
|
||||
*/
|
||||
getBulkDownloadUrls: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
projectId: z.string(),
|
||||
fileIds: z.array(z.string()).optional(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const isAdmin = ['SUPER_ADMIN', 'PROGRAM_ADMIN'].includes(ctx.user.role)
|
||||
|
||||
if (!isAdmin) {
|
||||
const [assignment, mentorAssignment, teamMembership] = await Promise.all([
|
||||
ctx.prisma.assignment.findFirst({
|
||||
where: { userId: ctx.user.id, projectId: input.projectId },
|
||||
select: { id: true },
|
||||
}),
|
||||
ctx.prisma.mentorAssignment.findFirst({
|
||||
where: { mentorId: ctx.user.id, projectId: input.projectId },
|
||||
select: { id: true },
|
||||
}),
|
||||
ctx.prisma.project.findFirst({
|
||||
where: {
|
||||
id: input.projectId,
|
||||
OR: [
|
||||
{ submittedByUserId: ctx.user.id },
|
||||
{ teamMembers: { some: { userId: ctx.user.id } } },
|
||||
],
|
||||
},
|
||||
select: { id: true },
|
||||
}),
|
||||
])
|
||||
|
||||
if (!assignment && !mentorAssignment && !teamMembership) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You do not have access to this project\'s files',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Get files
|
||||
const where: Record<string, unknown> = { projectId: input.projectId }
|
||||
if (input.fileIds && input.fileIds.length > 0) {
|
||||
where.id = { in: input.fileIds }
|
||||
}
|
||||
|
||||
const files = await ctx.prisma.projectFile.findMany({
|
||||
where,
|
||||
select: {
|
||||
id: true,
|
||||
fileName: true,
|
||||
bucket: true,
|
||||
objectKey: true,
|
||||
},
|
||||
})
|
||||
|
||||
// Generate signed URLs for each file
|
||||
const results = await Promise.all(
|
||||
files.map(async (file) => {
|
||||
const downloadUrl = await getPresignedUrl(file.bucket, file.objectKey, 'GET', 900)
|
||||
return {
|
||||
fileId: file.id,
|
||||
fileName: file.fileName,
|
||||
downloadUrl,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return results
|
||||
}),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { z } from 'zod'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { router, protectedProcedure, adminProcedure } from '../trpc'
|
||||
import { router, protectedProcedure, adminProcedure, publicProcedure } from '../trpc'
|
||||
import { logAudit } from '../utils/audit'
|
||||
|
||||
export const liveVotingRouter = router({
|
||||
|
|
@ -351,7 +351,7 @@ export const liveVotingRouter = router({
|
|||
}),
|
||||
|
||||
/**
|
||||
* Get results for a session
|
||||
* Get results for a session (with weighted jury + audience scoring)
|
||||
*/
|
||||
getResults: protectedProcedure
|
||||
.input(z.object({ sessionId: z.string() }))
|
||||
|
|
@ -367,36 +367,281 @@ export const liveVotingRouter = router({
|
|||
},
|
||||
})
|
||||
|
||||
// Get all votes grouped by project
|
||||
const projectScores = await ctx.prisma.liveVote.groupBy({
|
||||
const audienceWeight = session.audienceVoteWeight || 0
|
||||
const juryWeight = 1 - audienceWeight
|
||||
|
||||
// Get jury votes grouped by project
|
||||
const juryScores = await ctx.prisma.liveVote.groupBy({
|
||||
by: ['projectId'],
|
||||
where: { sessionId: input.sessionId, isAudienceVote: false },
|
||||
_avg: { score: true },
|
||||
_count: true,
|
||||
})
|
||||
|
||||
// Get audience votes grouped by project
|
||||
const audienceScores = await ctx.prisma.liveVote.groupBy({
|
||||
by: ['projectId'],
|
||||
where: { sessionId: input.sessionId, isAudienceVote: true },
|
||||
_avg: { score: true },
|
||||
_count: true,
|
||||
})
|
||||
|
||||
// Get project details
|
||||
const allProjectIds = [
|
||||
...new Set([
|
||||
...juryScores.map((s) => s.projectId),
|
||||
...audienceScores.map((s) => s.projectId),
|
||||
]),
|
||||
]
|
||||
const projects = await ctx.prisma.project.findMany({
|
||||
where: { id: { in: allProjectIds } },
|
||||
select: { id: true, title: true, teamName: true },
|
||||
})
|
||||
|
||||
const audienceMap = new Map(audienceScores.map((s) => [s.projectId, s]))
|
||||
|
||||
// Combine and calculate weighted scores
|
||||
const results = juryScores
|
||||
.map((jurySc) => {
|
||||
const project = projects.find((p) => p.id === jurySc.projectId)
|
||||
const audienceSc = audienceMap.get(jurySc.projectId)
|
||||
const juryAvg = jurySc._avg.score || 0
|
||||
const audienceAvg = audienceSc?._avg.score || 0
|
||||
const weightedTotal = audienceWeight > 0 && audienceSc
|
||||
? juryAvg * juryWeight + audienceAvg * audienceWeight
|
||||
: juryAvg
|
||||
|
||||
return {
|
||||
project,
|
||||
juryAverage: juryAvg,
|
||||
juryVoteCount: jurySc._count,
|
||||
audienceAverage: audienceAvg,
|
||||
audienceVoteCount: audienceSc?._count || 0,
|
||||
weightedTotal,
|
||||
}
|
||||
})
|
||||
.sort((a, b) => b.weightedTotal - a.weightedTotal)
|
||||
|
||||
// Detect ties
|
||||
const ties: string[][] = []
|
||||
for (let i = 0; i < results.length - 1; i++) {
|
||||
if (Math.abs(results[i].weightedTotal - results[i + 1].weightedTotal) < 0.001) {
|
||||
const tieGroup = [results[i].project?.id, results[i + 1].project?.id].filter(Boolean) as string[]
|
||||
ties.push(tieGroup)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
session,
|
||||
results,
|
||||
ties,
|
||||
tieBreakerMethod: session.tieBreakerMethod,
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Update presentation settings for a live voting session
|
||||
*/
|
||||
updatePresentationSettings: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
sessionId: z.string(),
|
||||
presentationSettingsJson: z.object({
|
||||
theme: z.string().optional(),
|
||||
autoAdvance: z.boolean().optional(),
|
||||
autoAdvanceDelay: z.number().int().min(5).max(120).optional(),
|
||||
scoreDisplayFormat: z.enum(['bar', 'number', 'radial']).optional(),
|
||||
showVoteCount: z.boolean().optional(),
|
||||
brandingOverlay: z.string().optional(),
|
||||
}),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const session = await ctx.prisma.liveVotingSession.update({
|
||||
where: { id: input.sessionId },
|
||||
data: {
|
||||
presentationSettingsJson: input.presentationSettingsJson,
|
||||
},
|
||||
})
|
||||
|
||||
return session
|
||||
}),
|
||||
|
||||
/**
|
||||
* Update session config (audience voting, tie-breaker)
|
||||
*/
|
||||
updateSessionConfig: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
sessionId: z.string(),
|
||||
allowAudienceVotes: z.boolean().optional(),
|
||||
audienceVoteWeight: z.number().min(0).max(1).optional(),
|
||||
tieBreakerMethod: z.enum(['admin_decides', 'highest_individual', 'revote']).optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { sessionId, ...data } = input
|
||||
|
||||
const session = await ctx.prisma.liveVotingSession.update({
|
||||
where: { id: sessionId },
|
||||
data,
|
||||
})
|
||||
|
||||
// Audit log
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'UPDATE_SESSION_CONFIG',
|
||||
entityType: 'LiveVotingSession',
|
||||
entityId: sessionId,
|
||||
detailsJson: data,
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
})
|
||||
} catch {
|
||||
// Audit log errors should never break the operation
|
||||
}
|
||||
|
||||
return session
|
||||
}),
|
||||
|
||||
/**
|
||||
* Cast an audience vote
|
||||
*/
|
||||
castAudienceVote: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
sessionId: z.string(),
|
||||
projectId: z.string(),
|
||||
score: z.number().int().min(1).max(10),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Verify session is in progress and allows audience votes
|
||||
const session = await ctx.prisma.liveVotingSession.findUniqueOrThrow({
|
||||
where: { id: input.sessionId },
|
||||
})
|
||||
|
||||
if (session.status !== 'IN_PROGRESS') {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Voting is not currently active',
|
||||
})
|
||||
}
|
||||
|
||||
if (!session.allowAudienceVotes) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Audience voting is not enabled for this session',
|
||||
})
|
||||
}
|
||||
|
||||
if (session.currentProjectId !== input.projectId) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Cannot vote for this project right now',
|
||||
})
|
||||
}
|
||||
|
||||
if (session.votingEndsAt && new Date() > session.votingEndsAt) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Voting window has closed',
|
||||
})
|
||||
}
|
||||
|
||||
// Upsert audience vote
|
||||
const vote = await ctx.prisma.liveVote.upsert({
|
||||
where: {
|
||||
sessionId_projectId_userId: {
|
||||
sessionId: input.sessionId,
|
||||
projectId: input.projectId,
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
sessionId: input.sessionId,
|
||||
projectId: input.projectId,
|
||||
userId: ctx.user.id,
|
||||
score: input.score,
|
||||
isAudienceVote: true,
|
||||
},
|
||||
update: {
|
||||
score: input.score,
|
||||
votedAt: new Date(),
|
||||
},
|
||||
})
|
||||
|
||||
return vote
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get public results for a live voting session (no auth required)
|
||||
*/
|
||||
getPublicResults: publicProcedure
|
||||
.input(z.object({ sessionId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const session = await ctx.prisma.liveVotingSession.findUniqueOrThrow({
|
||||
where: { id: input.sessionId },
|
||||
select: {
|
||||
id: true,
|
||||
status: true,
|
||||
currentProjectId: true,
|
||||
votingEndsAt: true,
|
||||
presentationSettingsJson: true,
|
||||
allowAudienceVotes: true,
|
||||
audienceVoteWeight: true,
|
||||
},
|
||||
})
|
||||
|
||||
// Only return data if session is in progress or completed
|
||||
if (session.status !== 'IN_PROGRESS' && session.status !== 'COMPLETED') {
|
||||
return {
|
||||
session: {
|
||||
id: session.id,
|
||||
status: session.status,
|
||||
presentationSettings: session.presentationSettingsJson,
|
||||
},
|
||||
projects: [],
|
||||
}
|
||||
}
|
||||
|
||||
// Get all votes grouped by project (anonymized - no user data)
|
||||
const scores = await ctx.prisma.liveVote.groupBy({
|
||||
by: ['projectId'],
|
||||
where: { sessionId: input.sessionId },
|
||||
_avg: { score: true },
|
||||
_count: true,
|
||||
})
|
||||
|
||||
// Get project details
|
||||
const projectIds = projectScores.map((s) => s.projectId)
|
||||
const projectIds = scores.map((s) => s.projectId)
|
||||
const projects = await ctx.prisma.project.findMany({
|
||||
where: { id: { in: projectIds } },
|
||||
select: { id: true, title: true, teamName: true },
|
||||
})
|
||||
|
||||
// Combine and sort by average score
|
||||
const results = projectScores
|
||||
.map((score) => {
|
||||
const project = projects.find((p) => p.id === score.projectId)
|
||||
return {
|
||||
project,
|
||||
averageScore: score._avg.score || 0,
|
||||
voteCount: score._count,
|
||||
}
|
||||
})
|
||||
.sort((a, b) => b.averageScore - a.averageScore)
|
||||
const projectsWithScores = scores.map((score) => {
|
||||
const project = projects.find((p) => p.id === score.projectId)
|
||||
return {
|
||||
id: project?.id,
|
||||
title: project?.title,
|
||||
teamName: project?.teamName,
|
||||
averageScore: score._avg.score || 0,
|
||||
voteCount: score._count,
|
||||
}
|
||||
}).sort((a, b) => b.averageScore - a.averageScore)
|
||||
|
||||
return {
|
||||
session,
|
||||
results,
|
||||
session: {
|
||||
id: session.id,
|
||||
status: session.status,
|
||||
currentProjectId: session.currentProjectId,
|
||||
votingEndsAt: session.votingEndsAt,
|
||||
presentationSettings: session.presentationSettingsJson,
|
||||
allowAudienceVotes: session.allowAudienceVotes,
|
||||
},
|
||||
projects: projectsWithScores,
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -794,4 +794,502 @@ export const mentorRouter = router({
|
|||
totalPages: Math.ceil(total / input.perPage),
|
||||
}
|
||||
}),
|
||||
|
||||
// =========================================================================
|
||||
// Mentor Notes CRUD (F8)
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Create a mentor note for an assignment
|
||||
*/
|
||||
createNote: mentorProcedure
|
||||
.input(
|
||||
z.object({
|
||||
mentorAssignmentId: z.string(),
|
||||
content: z.string().min(1).max(10000),
|
||||
isVisibleToAdmin: z.boolean().default(true),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Verify the user owns this assignment or is admin
|
||||
const assignment = await ctx.prisma.mentorAssignment.findUniqueOrThrow({
|
||||
where: { id: input.mentorAssignmentId },
|
||||
select: { mentorId: true, projectId: true },
|
||||
})
|
||||
|
||||
const isAdmin = ['SUPER_ADMIN', 'PROGRAM_ADMIN'].includes(ctx.user.role)
|
||||
if (assignment.mentorId !== ctx.user.id && !isAdmin) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You are not assigned to this mentorship',
|
||||
})
|
||||
}
|
||||
|
||||
const note = await ctx.prisma.mentorNote.create({
|
||||
data: {
|
||||
mentorAssignmentId: input.mentorAssignmentId,
|
||||
authorId: ctx.user.id,
|
||||
content: input.content,
|
||||
isVisibleToAdmin: input.isVisibleToAdmin,
|
||||
},
|
||||
include: {
|
||||
author: { select: { id: true, name: true, email: true } },
|
||||
},
|
||||
})
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'CREATE_MENTOR_NOTE',
|
||||
entityType: 'MentorNote',
|
||||
entityId: note.id,
|
||||
detailsJson: {
|
||||
mentorAssignmentId: input.mentorAssignmentId,
|
||||
projectId: assignment.projectId,
|
||||
},
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
})
|
||||
} catch {
|
||||
// Audit log errors should never break the operation
|
||||
}
|
||||
|
||||
return note
|
||||
}),
|
||||
|
||||
/**
|
||||
* Update a mentor note
|
||||
*/
|
||||
updateNote: mentorProcedure
|
||||
.input(
|
||||
z.object({
|
||||
noteId: z.string(),
|
||||
content: z.string().min(1).max(10000),
|
||||
isVisibleToAdmin: z.boolean().optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const note = await ctx.prisma.mentorNote.findUniqueOrThrow({
|
||||
where: { id: input.noteId },
|
||||
select: { authorId: true },
|
||||
})
|
||||
|
||||
if (note.authorId !== ctx.user.id) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You can only edit your own notes',
|
||||
})
|
||||
}
|
||||
|
||||
return ctx.prisma.mentorNote.update({
|
||||
where: { id: input.noteId },
|
||||
data: {
|
||||
content: input.content,
|
||||
...(input.isVisibleToAdmin !== undefined && { isVisibleToAdmin: input.isVisibleToAdmin }),
|
||||
},
|
||||
include: {
|
||||
author: { select: { id: true, name: true, email: true } },
|
||||
},
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Delete a mentor note
|
||||
*/
|
||||
deleteNote: mentorProcedure
|
||||
.input(z.object({ noteId: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const note = await ctx.prisma.mentorNote.findUniqueOrThrow({
|
||||
where: { id: input.noteId },
|
||||
select: { authorId: true },
|
||||
})
|
||||
|
||||
if (note.authorId !== ctx.user.id) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You can only delete your own notes',
|
||||
})
|
||||
}
|
||||
|
||||
return ctx.prisma.mentorNote.delete({
|
||||
where: { id: input.noteId },
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get notes for a mentor assignment
|
||||
*/
|
||||
getNotes: mentorProcedure
|
||||
.input(z.object({ mentorAssignmentId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const assignment = await ctx.prisma.mentorAssignment.findUniqueOrThrow({
|
||||
where: { id: input.mentorAssignmentId },
|
||||
select: { mentorId: true },
|
||||
})
|
||||
|
||||
const isAdmin = ['SUPER_ADMIN', 'PROGRAM_ADMIN'].includes(ctx.user.role)
|
||||
|
||||
if (assignment.mentorId !== ctx.user.id && !isAdmin) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You are not assigned to this mentorship',
|
||||
})
|
||||
}
|
||||
|
||||
// Admins see all notes; mentors see only their own
|
||||
const where: Record<string, unknown> = { mentorAssignmentId: input.mentorAssignmentId }
|
||||
if (!isAdmin) {
|
||||
where.authorId = ctx.user.id
|
||||
}
|
||||
|
||||
return ctx.prisma.mentorNote.findMany({
|
||||
where,
|
||||
include: {
|
||||
author: { select: { id: true, name: true, email: true } },
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
})
|
||||
}),
|
||||
|
||||
// =========================================================================
|
||||
// Milestone Operations (F8)
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Get milestones for a program with completion status
|
||||
*/
|
||||
getMilestones: mentorProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const milestones = await ctx.prisma.mentorMilestone.findMany({
|
||||
where: { programId: input.programId },
|
||||
include: {
|
||||
completions: {
|
||||
include: {
|
||||
mentorAssignment: { select: { id: true, projectId: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { sortOrder: 'asc' },
|
||||
})
|
||||
|
||||
// Get current user's assignments for completion status context
|
||||
const myAssignments = await ctx.prisma.mentorAssignment.findMany({
|
||||
where: { mentorId: ctx.user.id },
|
||||
select: { id: true, projectId: true },
|
||||
})
|
||||
const myAssignmentIds = new Set(myAssignments.map((a) => a.id))
|
||||
|
||||
return milestones.map((milestone) => ({
|
||||
...milestone,
|
||||
myCompletions: milestone.completions.filter((c) =>
|
||||
myAssignmentIds.has(c.mentorAssignmentId)
|
||||
),
|
||||
}))
|
||||
}),
|
||||
|
||||
/**
|
||||
* Mark a milestone as completed for an assignment
|
||||
*/
|
||||
completeMilestone: mentorProcedure
|
||||
.input(
|
||||
z.object({
|
||||
milestoneId: z.string(),
|
||||
mentorAssignmentId: z.string(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Verify the user owns this assignment
|
||||
const assignment = await ctx.prisma.mentorAssignment.findUniqueOrThrow({
|
||||
where: { id: input.mentorAssignmentId },
|
||||
select: { mentorId: true, projectId: true },
|
||||
})
|
||||
|
||||
const isAdmin = ['SUPER_ADMIN', 'PROGRAM_ADMIN'].includes(ctx.user.role)
|
||||
if (assignment.mentorId !== ctx.user.id && !isAdmin) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You are not assigned to this mentorship',
|
||||
})
|
||||
}
|
||||
|
||||
const completion = await ctx.prisma.mentorMilestoneCompletion.create({
|
||||
data: {
|
||||
milestoneId: input.milestoneId,
|
||||
mentorAssignmentId: input.mentorAssignmentId,
|
||||
completedById: ctx.user.id,
|
||||
},
|
||||
})
|
||||
|
||||
// Check if all required milestones are now completed
|
||||
const milestone = await ctx.prisma.mentorMilestone.findUniqueOrThrow({
|
||||
where: { id: input.milestoneId },
|
||||
select: { programId: true },
|
||||
})
|
||||
|
||||
const requiredMilestones = await ctx.prisma.mentorMilestone.findMany({
|
||||
where: { programId: milestone.programId, isRequired: true },
|
||||
select: { id: true },
|
||||
})
|
||||
|
||||
const completedMilestones = await ctx.prisma.mentorMilestoneCompletion.findMany({
|
||||
where: {
|
||||
mentorAssignmentId: input.mentorAssignmentId,
|
||||
milestoneId: { in: requiredMilestones.map((m) => m.id) },
|
||||
},
|
||||
select: { milestoneId: true },
|
||||
})
|
||||
|
||||
const allRequiredDone = requiredMilestones.length > 0 &&
|
||||
completedMilestones.length >= requiredMilestones.length
|
||||
|
||||
if (allRequiredDone) {
|
||||
await ctx.prisma.mentorAssignment.update({
|
||||
where: { id: input.mentorAssignmentId },
|
||||
data: { completionStatus: 'completed' },
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'COMPLETE_MILESTONE',
|
||||
entityType: 'MentorMilestoneCompletion',
|
||||
entityId: completion.id,
|
||||
detailsJson: {
|
||||
milestoneId: input.milestoneId,
|
||||
mentorAssignmentId: input.mentorAssignmentId,
|
||||
allRequiredDone,
|
||||
},
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
})
|
||||
} catch {
|
||||
// Audit log errors should never break the operation
|
||||
}
|
||||
|
||||
return { completion, allRequiredDone }
|
||||
}),
|
||||
|
||||
/**
|
||||
* Uncomplete a milestone for an assignment
|
||||
*/
|
||||
uncompleteMilestone: mentorProcedure
|
||||
.input(
|
||||
z.object({
|
||||
milestoneId: z.string(),
|
||||
mentorAssignmentId: z.string(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const assignment = await ctx.prisma.mentorAssignment.findUniqueOrThrow({
|
||||
where: { id: input.mentorAssignmentId },
|
||||
select: { mentorId: true },
|
||||
})
|
||||
|
||||
const isAdmin = ['SUPER_ADMIN', 'PROGRAM_ADMIN'].includes(ctx.user.role)
|
||||
if (assignment.mentorId !== ctx.user.id && !isAdmin) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You are not assigned to this mentorship',
|
||||
})
|
||||
}
|
||||
|
||||
await ctx.prisma.mentorMilestoneCompletion.delete({
|
||||
where: {
|
||||
milestoneId_mentorAssignmentId: {
|
||||
milestoneId: input.milestoneId,
|
||||
mentorAssignmentId: input.mentorAssignmentId,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// Revert completion status if it was completed
|
||||
await ctx.prisma.mentorAssignment.update({
|
||||
where: { id: input.mentorAssignmentId },
|
||||
data: { completionStatus: 'in_progress' },
|
||||
})
|
||||
|
||||
return { success: true }
|
||||
}),
|
||||
|
||||
// =========================================================================
|
||||
// Admin Milestone Management (F8)
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Create a milestone for a program
|
||||
*/
|
||||
createMilestone: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
programId: z.string(),
|
||||
name: z.string().min(1).max(255),
|
||||
description: z.string().max(2000).optional(),
|
||||
isRequired: z.boolean().default(false),
|
||||
deadlineOffsetDays: z.number().int().optional().nullable(),
|
||||
sortOrder: z.number().int().default(0),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
return ctx.prisma.mentorMilestone.create({
|
||||
data: input,
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Update a milestone
|
||||
*/
|
||||
updateMilestone: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
milestoneId: z.string(),
|
||||
name: z.string().min(1).max(255).optional(),
|
||||
description: z.string().max(2000).optional().nullable(),
|
||||
isRequired: z.boolean().optional(),
|
||||
deadlineOffsetDays: z.number().int().optional().nullable(),
|
||||
sortOrder: z.number().int().optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { milestoneId, ...data } = input
|
||||
return ctx.prisma.mentorMilestone.update({
|
||||
where: { id: milestoneId },
|
||||
data,
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Delete a milestone (cascades completions)
|
||||
*/
|
||||
deleteMilestone: adminProcedure
|
||||
.input(z.object({ milestoneId: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
return ctx.prisma.mentorMilestone.delete({
|
||||
where: { id: input.milestoneId },
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Reorder milestones
|
||||
*/
|
||||
reorderMilestones: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
milestoneIds: z.array(z.string()),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
await ctx.prisma.$transaction(
|
||||
input.milestoneIds.map((id, index) =>
|
||||
ctx.prisma.mentorMilestone.update({
|
||||
where: { id },
|
||||
data: { sortOrder: index },
|
||||
})
|
||||
)
|
||||
)
|
||||
|
||||
return { success: true }
|
||||
}),
|
||||
|
||||
// =========================================================================
|
||||
// Activity Tracking (F8)
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Track a mentor's view of an assignment
|
||||
*/
|
||||
trackView: mentorProcedure
|
||||
.input(z.object({ mentorAssignmentId: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const assignment = await ctx.prisma.mentorAssignment.findUniqueOrThrow({
|
||||
where: { id: input.mentorAssignmentId },
|
||||
select: { mentorId: true },
|
||||
})
|
||||
|
||||
const isAdmin = ['SUPER_ADMIN', 'PROGRAM_ADMIN'].includes(ctx.user.role)
|
||||
if (assignment.mentorId !== ctx.user.id && !isAdmin) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You are not assigned to this mentorship',
|
||||
})
|
||||
}
|
||||
|
||||
return ctx.prisma.mentorAssignment.update({
|
||||
where: { id: input.mentorAssignmentId },
|
||||
data: { lastViewedAt: new Date() },
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get activity stats for all mentors (admin)
|
||||
*/
|
||||
getActivityStats: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
roundId: z.string().optional(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const where = input.roundId
|
||||
? { project: { roundId: input.roundId } }
|
||||
: {}
|
||||
|
||||
const assignments = await ctx.prisma.mentorAssignment.findMany({
|
||||
where,
|
||||
include: {
|
||||
mentor: { select: { id: true, name: true, email: true } },
|
||||
project: { select: { id: true, title: true } },
|
||||
notes: { select: { id: true } },
|
||||
milestoneCompletions: { select: { id: true } },
|
||||
},
|
||||
})
|
||||
|
||||
// Get message counts per mentor
|
||||
const mentorIds = [...new Set(assignments.map((a) => a.mentorId))]
|
||||
const messageCounts = await ctx.prisma.mentorMessage.groupBy({
|
||||
by: ['senderId'],
|
||||
where: { senderId: { in: mentorIds } },
|
||||
_count: true,
|
||||
})
|
||||
const messageCountMap = new Map(messageCounts.map((m) => [m.senderId, m._count]))
|
||||
|
||||
// Build per-mentor stats
|
||||
const mentorStats = new Map<string, {
|
||||
mentor: { id: string; name: string | null; email: string }
|
||||
assignments: number
|
||||
lastViewedAt: Date | null
|
||||
notesCount: number
|
||||
milestonesCompleted: number
|
||||
messagesSent: number
|
||||
completionStatuses: string[]
|
||||
}>()
|
||||
|
||||
for (const assignment of assignments) {
|
||||
const existing = mentorStats.get(assignment.mentorId)
|
||||
if (existing) {
|
||||
existing.assignments++
|
||||
existing.notesCount += assignment.notes.length
|
||||
existing.milestonesCompleted += assignment.milestoneCompletions.length
|
||||
existing.completionStatuses.push(assignment.completionStatus)
|
||||
if (assignment.lastViewedAt && (!existing.lastViewedAt || assignment.lastViewedAt > existing.lastViewedAt)) {
|
||||
existing.lastViewedAt = assignment.lastViewedAt
|
||||
}
|
||||
} else {
|
||||
mentorStats.set(assignment.mentorId, {
|
||||
mentor: assignment.mentor,
|
||||
assignments: 1,
|
||||
lastViewedAt: assignment.lastViewedAt,
|
||||
notesCount: assignment.notes.length,
|
||||
milestonesCompleted: assignment.milestoneCompletions.length,
|
||||
messagesSent: messageCountMap.get(assignment.mentorId) || 0,
|
||||
completionStatuses: [assignment.completionStatus],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(mentorStats.values())
|
||||
}),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -0,0 +1,406 @@
|
|||
import { z } from 'zod'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { router, protectedProcedure, adminProcedure } from '../trpc'
|
||||
import { logAudit } from '@/server/utils/audit'
|
||||
import { sendStyledNotificationEmail } from '@/lib/email'
|
||||
|
||||
export const messageRouter = router({
|
||||
/**
|
||||
* Send a message to recipients.
|
||||
* Resolves recipient list based on recipientType and delivers via specified channels.
|
||||
*/
|
||||
send: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
recipientType: z.enum(['USER', 'ROLE', 'ROUND_JURY', 'PROGRAM_TEAM', 'ALL']),
|
||||
recipientFilter: z.any().optional(),
|
||||
roundId: z.string().optional(),
|
||||
subject: z.string().min(1).max(500),
|
||||
body: z.string().min(1),
|
||||
deliveryChannels: z.array(z.string()).min(1),
|
||||
scheduledAt: z.string().datetime().optional(),
|
||||
templateId: z.string().optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Resolve recipients based on type
|
||||
const recipientUserIds = await resolveRecipients(
|
||||
ctx.prisma,
|
||||
input.recipientType,
|
||||
input.recipientFilter,
|
||||
input.roundId
|
||||
)
|
||||
|
||||
if (recipientUserIds.length === 0) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'No recipients found for the given criteria',
|
||||
})
|
||||
}
|
||||
|
||||
const isScheduled = !!input.scheduledAt
|
||||
const now = new Date()
|
||||
|
||||
// Create message
|
||||
const message = await ctx.prisma.message.create({
|
||||
data: {
|
||||
senderId: ctx.user.id,
|
||||
recipientType: input.recipientType,
|
||||
recipientFilter: input.recipientFilter ?? undefined,
|
||||
roundId: input.roundId,
|
||||
templateId: input.templateId,
|
||||
subject: input.subject,
|
||||
body: input.body,
|
||||
deliveryChannels: input.deliveryChannels,
|
||||
scheduledAt: input.scheduledAt ? new Date(input.scheduledAt) : undefined,
|
||||
sentAt: isScheduled ? undefined : now,
|
||||
recipients: {
|
||||
create: recipientUserIds.flatMap((userId) =>
|
||||
input.deliveryChannels.map((channel) => ({
|
||||
userId,
|
||||
channel,
|
||||
}))
|
||||
),
|
||||
},
|
||||
},
|
||||
include: {
|
||||
recipients: true,
|
||||
},
|
||||
})
|
||||
|
||||
// If not scheduled, deliver immediately for EMAIL channel
|
||||
if (!isScheduled && input.deliveryChannels.includes('EMAIL')) {
|
||||
const users = await ctx.prisma.user.findMany({
|
||||
where: { id: { in: recipientUserIds } },
|
||||
select: { id: true, name: true, email: true },
|
||||
})
|
||||
|
||||
const baseUrl = process.env.NEXTAUTH_URL || 'https://monaco-opc.com'
|
||||
|
||||
for (const user of users) {
|
||||
try {
|
||||
await sendStyledNotificationEmail(
|
||||
user.email,
|
||||
user.name || '',
|
||||
'MESSAGE',
|
||||
{
|
||||
name: user.name || undefined,
|
||||
title: input.subject,
|
||||
message: input.body,
|
||||
linkUrl: `${baseUrl}/messages`,
|
||||
}
|
||||
)
|
||||
} catch (error) {
|
||||
console.error(`[Message] Failed to send email to ${user.email}:`, error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'SEND_MESSAGE',
|
||||
entityType: 'Message',
|
||||
entityId: message.id,
|
||||
detailsJson: {
|
||||
recipientType: input.recipientType,
|
||||
recipientCount: recipientUserIds.length,
|
||||
channels: input.deliveryChannels,
|
||||
scheduled: isScheduled,
|
||||
},
|
||||
})
|
||||
} catch {}
|
||||
|
||||
return {
|
||||
...message,
|
||||
recipientCount: recipientUserIds.length,
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get the current user's inbox (messages sent to them).
|
||||
*/
|
||||
inbox: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
page: z.number().int().min(1).default(1),
|
||||
pageSize: z.number().int().min(1).max(100).default(20),
|
||||
}).optional()
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const page = input?.page ?? 1
|
||||
const pageSize = input?.pageSize ?? 20
|
||||
const skip = (page - 1) * pageSize
|
||||
|
||||
const [items, total] = await Promise.all([
|
||||
ctx.prisma.messageRecipient.findMany({
|
||||
where: { userId: ctx.user.id },
|
||||
include: {
|
||||
message: {
|
||||
include: {
|
||||
sender: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { message: { createdAt: 'desc' } },
|
||||
skip,
|
||||
take: pageSize,
|
||||
}),
|
||||
ctx.prisma.messageRecipient.count({
|
||||
where: { userId: ctx.user.id },
|
||||
}),
|
||||
])
|
||||
|
||||
return {
|
||||
items,
|
||||
total,
|
||||
page,
|
||||
pageSize,
|
||||
totalPages: Math.ceil(total / pageSize),
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Mark a message as read.
|
||||
*/
|
||||
markRead: protectedProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const recipient = await ctx.prisma.messageRecipient.findUnique({
|
||||
where: { id: input.id },
|
||||
})
|
||||
|
||||
if (!recipient || recipient.userId !== ctx.user.id) {
|
||||
throw new TRPCError({
|
||||
code: 'NOT_FOUND',
|
||||
message: 'Message not found',
|
||||
})
|
||||
}
|
||||
|
||||
return ctx.prisma.messageRecipient.update({
|
||||
where: { id: input.id },
|
||||
data: {
|
||||
isRead: true,
|
||||
readAt: new Date(),
|
||||
},
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get unread message count for the current user.
|
||||
*/
|
||||
getUnreadCount: protectedProcedure.query(async ({ ctx }) => {
|
||||
const count = await ctx.prisma.messageRecipient.count({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
isRead: false,
|
||||
},
|
||||
})
|
||||
return { count }
|
||||
}),
|
||||
|
||||
// =========================================================================
|
||||
// Template procedures
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* List all message templates.
|
||||
*/
|
||||
listTemplates: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
category: z.string().optional(),
|
||||
activeOnly: z.boolean().default(true),
|
||||
}).optional()
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
return ctx.prisma.messageTemplate.findMany({
|
||||
where: {
|
||||
...(input?.category ? { category: input.category } : {}),
|
||||
...(input?.activeOnly !== false ? { isActive: true } : {}),
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Create a message template.
|
||||
*/
|
||||
createTemplate: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
name: z.string().min(1).max(200),
|
||||
category: z.string().min(1).max(100),
|
||||
subject: z.string().min(1).max(500),
|
||||
body: z.string().min(1),
|
||||
variables: z.any().optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const template = await ctx.prisma.messageTemplate.create({
|
||||
data: {
|
||||
name: input.name,
|
||||
category: input.category,
|
||||
subject: input.subject,
|
||||
body: input.body,
|
||||
variables: input.variables ?? undefined,
|
||||
createdBy: ctx.user.id,
|
||||
},
|
||||
})
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'CREATE_MESSAGE_TEMPLATE',
|
||||
entityType: 'MessageTemplate',
|
||||
entityId: template.id,
|
||||
detailsJson: { name: input.name, category: input.category },
|
||||
})
|
||||
} catch {}
|
||||
|
||||
return template
|
||||
}),
|
||||
|
||||
/**
|
||||
* Update a message template.
|
||||
*/
|
||||
updateTemplate: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string().min(1).max(200).optional(),
|
||||
category: z.string().min(1).max(100).optional(),
|
||||
subject: z.string().min(1).max(500).optional(),
|
||||
body: z.string().min(1).optional(),
|
||||
variables: z.any().optional(),
|
||||
isActive: z.boolean().optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { id, ...data } = input
|
||||
|
||||
const template = await ctx.prisma.messageTemplate.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...(data.name !== undefined ? { name: data.name } : {}),
|
||||
...(data.category !== undefined ? { category: data.category } : {}),
|
||||
...(data.subject !== undefined ? { subject: data.subject } : {}),
|
||||
...(data.body !== undefined ? { body: data.body } : {}),
|
||||
...(data.variables !== undefined ? { variables: data.variables } : {}),
|
||||
...(data.isActive !== undefined ? { isActive: data.isActive } : {}),
|
||||
},
|
||||
})
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'UPDATE_MESSAGE_TEMPLATE',
|
||||
entityType: 'MessageTemplate',
|
||||
entityId: id,
|
||||
detailsJson: { updatedFields: Object.keys(data) },
|
||||
})
|
||||
} catch {}
|
||||
|
||||
return template
|
||||
}),
|
||||
|
||||
/**
|
||||
* Soft-delete a message template (set isActive=false).
|
||||
*/
|
||||
deleteTemplate: adminProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const template = await ctx.prisma.messageTemplate.update({
|
||||
where: { id: input.id },
|
||||
data: { isActive: false },
|
||||
})
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'DELETE_MESSAGE_TEMPLATE',
|
||||
entityType: 'MessageTemplate',
|
||||
entityId: input.id,
|
||||
})
|
||||
} catch {}
|
||||
|
||||
return template
|
||||
}),
|
||||
})
|
||||
|
||||
// =============================================================================
|
||||
// Helper: Resolve recipient user IDs based on recipientType
|
||||
// =============================================================================
|
||||
|
||||
type PrismaClient = Parameters<Parameters<typeof adminProcedure.mutation>[0]>[0]['ctx']['prisma']
|
||||
|
||||
async function resolveRecipients(
|
||||
prisma: PrismaClient,
|
||||
recipientType: string,
|
||||
recipientFilter: unknown,
|
||||
roundId?: string
|
||||
): Promise<string[]> {
|
||||
const filter = recipientFilter as Record<string, unknown> | undefined
|
||||
|
||||
switch (recipientType) {
|
||||
case 'USER': {
|
||||
const userId = filter?.userId as string
|
||||
if (!userId) return []
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { id: true },
|
||||
})
|
||||
return user ? [user.id] : []
|
||||
}
|
||||
|
||||
case 'ROLE': {
|
||||
const role = filter?.role as string
|
||||
if (!role) return []
|
||||
const users = await prisma.user.findMany({
|
||||
where: { role: role as any, status: 'ACTIVE' },
|
||||
select: { id: true },
|
||||
})
|
||||
return users.map((u) => u.id)
|
||||
}
|
||||
|
||||
case 'ROUND_JURY': {
|
||||
const targetRoundId = roundId || (filter?.roundId as string)
|
||||
if (!targetRoundId) return []
|
||||
const assignments = await prisma.assignment.findMany({
|
||||
where: { roundId: targetRoundId },
|
||||
select: { userId: true },
|
||||
distinct: ['userId'],
|
||||
})
|
||||
return assignments.map((a) => a.userId)
|
||||
}
|
||||
|
||||
case 'PROGRAM_TEAM': {
|
||||
const programId = filter?.programId as string
|
||||
if (!programId) return []
|
||||
// Get all applicants with projects in rounds of this program
|
||||
const projects = await prisma.project.findMany({
|
||||
where: { round: { programId } },
|
||||
select: { submittedByUserId: true },
|
||||
})
|
||||
const ids = new Set(projects.map((p) => p.submittedByUserId).filter(Boolean) as string[])
|
||||
return [...ids]
|
||||
}
|
||||
|
||||
case 'ALL': {
|
||||
const users = await prisma.user.findMany({
|
||||
where: { status: 'ACTIVE' },
|
||||
select: { id: true },
|
||||
})
|
||||
return users.map((u) => u.id)
|
||||
}
|
||||
|
||||
default:
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
|
@ -26,6 +26,22 @@ export const roundRouter = router({
|
|||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* List all rounds across all programs (admin only, for messaging/filtering)
|
||||
*/
|
||||
listAll: adminProcedure
|
||||
.query(async ({ ctx }) => {
|
||||
return ctx.prisma.round.findMany({
|
||||
orderBy: [{ program: { name: 'asc' } }, { sortOrder: 'asc' }],
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
programId: true,
|
||||
program: { select: { name: true } },
|
||||
},
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get a single round with stats
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -0,0 +1,209 @@
|
|||
import { z } from 'zod'
|
||||
import { RoundType } from '@prisma/client'
|
||||
import { router, adminProcedure } from '../trpc'
|
||||
import { logAudit } from '@/server/utils/audit'
|
||||
|
||||
export const roundTemplateRouter = router({
|
||||
/**
|
||||
* List all round templates, optionally filtered by programId.
|
||||
*/
|
||||
list: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
programId: z.string().optional(),
|
||||
}).optional()
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
return ctx.prisma.roundTemplate.findMany({
|
||||
where: {
|
||||
...(input?.programId ? { programId: input.programId } : {}),
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get a single template by ID.
|
||||
*/
|
||||
getById: adminProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const template = await ctx.prisma.roundTemplate.findUnique({
|
||||
where: { id: input.id },
|
||||
})
|
||||
|
||||
if (!template) {
|
||||
throw new Error('Template not found')
|
||||
}
|
||||
|
||||
return template
|
||||
}),
|
||||
|
||||
/**
|
||||
* Create a new round template from scratch.
|
||||
*/
|
||||
create: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
name: z.string().min(1).max(200),
|
||||
description: z.string().optional(),
|
||||
programId: z.string().optional(),
|
||||
roundType: z.nativeEnum(RoundType).default('EVALUATION'),
|
||||
criteriaJson: z.any(),
|
||||
settingsJson: z.any().optional(),
|
||||
assignmentConfig: z.any().optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const template = await ctx.prisma.roundTemplate.create({
|
||||
data: {
|
||||
name: input.name,
|
||||
description: input.description,
|
||||
programId: input.programId,
|
||||
roundType: input.roundType,
|
||||
criteriaJson: input.criteriaJson,
|
||||
settingsJson: input.settingsJson ?? undefined,
|
||||
assignmentConfig: input.assignmentConfig ?? undefined,
|
||||
createdBy: ctx.user.id,
|
||||
},
|
||||
})
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'CREATE_ROUND_TEMPLATE',
|
||||
entityType: 'RoundTemplate',
|
||||
entityId: template.id,
|
||||
detailsJson: { name: input.name },
|
||||
})
|
||||
} catch {}
|
||||
|
||||
return template
|
||||
}),
|
||||
|
||||
/**
|
||||
* Create a template from an existing round (snapshot).
|
||||
*/
|
||||
createFromRound: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
roundId: z.string(),
|
||||
name: z.string().min(1).max(200),
|
||||
description: z.string().optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Fetch the round and its active evaluation form
|
||||
const round = await ctx.prisma.round.findUnique({
|
||||
where: { id: input.roundId },
|
||||
include: {
|
||||
evaluationForms: {
|
||||
where: { isActive: true },
|
||||
take: 1,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!round) {
|
||||
throw new Error('Round not found')
|
||||
}
|
||||
|
||||
const form = round.evaluationForms[0]
|
||||
const criteriaJson = form?.criteriaJson ?? []
|
||||
|
||||
const template = await ctx.prisma.roundTemplate.create({
|
||||
data: {
|
||||
name: input.name,
|
||||
description: input.description || `Snapshot of ${round.name}`,
|
||||
programId: round.programId,
|
||||
roundType: round.roundType,
|
||||
criteriaJson,
|
||||
settingsJson: round.settingsJson ?? undefined,
|
||||
createdBy: ctx.user.id,
|
||||
},
|
||||
})
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'CREATE_ROUND_TEMPLATE_FROM_ROUND',
|
||||
entityType: 'RoundTemplate',
|
||||
entityId: template.id,
|
||||
detailsJson: { name: input.name, sourceRoundId: input.roundId },
|
||||
})
|
||||
} catch {}
|
||||
|
||||
return template
|
||||
}),
|
||||
|
||||
/**
|
||||
* Update a template.
|
||||
*/
|
||||
update: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string().min(1).max(200).optional(),
|
||||
description: z.string().optional(),
|
||||
programId: z.string().nullable().optional(),
|
||||
roundType: z.nativeEnum(RoundType).optional(),
|
||||
criteriaJson: z.any().optional(),
|
||||
settingsJson: z.any().optional(),
|
||||
assignmentConfig: z.any().optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { id, ...data } = input
|
||||
|
||||
const template = await ctx.prisma.roundTemplate.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...(data.name !== undefined ? { name: data.name } : {}),
|
||||
...(data.description !== undefined ? { description: data.description } : {}),
|
||||
...(data.programId !== undefined ? { programId: data.programId } : {}),
|
||||
...(data.roundType !== undefined ? { roundType: data.roundType } : {}),
|
||||
...(data.criteriaJson !== undefined ? { criteriaJson: data.criteriaJson } : {}),
|
||||
...(data.settingsJson !== undefined ? { settingsJson: data.settingsJson } : {}),
|
||||
...(data.assignmentConfig !== undefined ? { assignmentConfig: data.assignmentConfig } : {}),
|
||||
},
|
||||
})
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'UPDATE_ROUND_TEMPLATE',
|
||||
entityType: 'RoundTemplate',
|
||||
entityId: id,
|
||||
detailsJson: { updatedFields: Object.keys(data) },
|
||||
})
|
||||
} catch {}
|
||||
|
||||
return template
|
||||
}),
|
||||
|
||||
/**
|
||||
* Delete a template.
|
||||
*/
|
||||
delete: adminProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
await ctx.prisma.roundTemplate.delete({
|
||||
where: { id: input.id },
|
||||
})
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'DELETE_ROUND_TEMPLATE',
|
||||
entityType: 'RoundTemplate',
|
||||
entityId: input.id,
|
||||
})
|
||||
} catch {}
|
||||
|
||||
return { success: true }
|
||||
}),
|
||||
})
|
||||
|
|
@ -26,14 +26,22 @@ export const settingsRouter = router({
|
|||
* These are non-sensitive settings that can be exposed to any user
|
||||
*/
|
||||
getFeatureFlags: protectedProcedure.query(async ({ ctx }) => {
|
||||
const [whatsappEnabled] = await Promise.all([
|
||||
const [whatsappEnabled, defaultLocale, availableLocales] = await Promise.all([
|
||||
ctx.prisma.systemSettings.findUnique({
|
||||
where: { key: 'whatsapp_enabled' },
|
||||
}),
|
||||
ctx.prisma.systemSettings.findUnique({
|
||||
where: { key: 'i18n_default_locale' },
|
||||
}),
|
||||
ctx.prisma.systemSettings.findUnique({
|
||||
where: { key: 'i18n_available_locales' },
|
||||
}),
|
||||
])
|
||||
|
||||
return {
|
||||
whatsappEnabled: whatsappEnabled?.value === 'true',
|
||||
defaultLocale: defaultLocale?.value || 'en',
|
||||
availableLocales: availableLocales?.value ? JSON.parse(availableLocales.value) : ['en', 'fr'],
|
||||
}
|
||||
}),
|
||||
|
||||
|
|
@ -159,13 +167,14 @@ export const settingsRouter = router({
|
|||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Infer category from key prefix if not provided
|
||||
const inferCategory = (key: string): 'AI' | 'BRANDING' | 'EMAIL' | 'STORAGE' | 'SECURITY' | 'DEFAULTS' | 'WHATSAPP' => {
|
||||
const inferCategory = (key: string): 'AI' | 'BRANDING' | 'EMAIL' | 'STORAGE' | 'SECURITY' | 'DEFAULTS' | 'WHATSAPP' | 'LOCALIZATION' => {
|
||||
if (key.startsWith('openai') || key.startsWith('ai_')) return 'AI'
|
||||
if (key.startsWith('smtp_') || key.startsWith('email_')) return 'EMAIL'
|
||||
if (key.startsWith('storage_') || key.startsWith('local_storage') || key.startsWith('max_file') || key.startsWith('avatar_') || key.startsWith('allowed_file')) return 'STORAGE'
|
||||
if (key.startsWith('brand_') || key.startsWith('logo_') || key.startsWith('primary_') || key.startsWith('theme_')) return 'BRANDING'
|
||||
if (key.startsWith('whatsapp_')) return 'WHATSAPP'
|
||||
if (key.startsWith('security_') || key.startsWith('session_')) return 'SECURITY'
|
||||
if (key.startsWith('i18n_') || key.startsWith('locale_')) return 'LOCALIZATION'
|
||||
return 'DEFAULTS'
|
||||
}
|
||||
|
||||
|
|
@ -529,4 +538,245 @@ export const settingsRouter = router({
|
|||
costFormatted: formatCost(day.cost),
|
||||
}))
|
||||
}),
|
||||
|
||||
// =========================================================================
|
||||
// Feature-specific settings categories
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Get digest notification settings
|
||||
*/
|
||||
getDigestSettings: adminProcedure.query(async ({ ctx }) => {
|
||||
const settings = await ctx.prisma.systemSettings.findMany({
|
||||
where: { category: 'DIGEST' },
|
||||
orderBy: { key: 'asc' },
|
||||
})
|
||||
|
||||
return settings
|
||||
}),
|
||||
|
||||
/**
|
||||
* Update digest notification settings
|
||||
*/
|
||||
updateDigestSettings: superAdminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
settings: z.array(
|
||||
z.object({
|
||||
key: z.string(),
|
||||
value: z.string(),
|
||||
})
|
||||
),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const results = await Promise.all(
|
||||
input.settings.map((s) =>
|
||||
ctx.prisma.systemSettings.upsert({
|
||||
where: { key: s.key },
|
||||
update: { value: s.value, updatedBy: ctx.user.id },
|
||||
create: {
|
||||
key: s.key,
|
||||
value: s.value,
|
||||
category: 'DIGEST',
|
||||
updatedBy: ctx.user.id,
|
||||
},
|
||||
})
|
||||
)
|
||||
)
|
||||
|
||||
// Audit log
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'UPDATE_DIGEST_SETTINGS',
|
||||
entityType: 'SystemSettings',
|
||||
detailsJson: { keys: input.settings.map((s) => s.key) },
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
})
|
||||
} catch {
|
||||
// Never throw on audit failure
|
||||
}
|
||||
|
||||
return results
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get analytics/reporting settings
|
||||
*/
|
||||
getAnalyticsSettings: adminProcedure.query(async ({ ctx }) => {
|
||||
const settings = await ctx.prisma.systemSettings.findMany({
|
||||
where: { category: 'ANALYTICS' },
|
||||
orderBy: { key: 'asc' },
|
||||
})
|
||||
|
||||
return settings
|
||||
}),
|
||||
|
||||
/**
|
||||
* Update analytics/reporting settings
|
||||
*/
|
||||
updateAnalyticsSettings: superAdminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
settings: z.array(
|
||||
z.object({
|
||||
key: z.string(),
|
||||
value: z.string(),
|
||||
})
|
||||
),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const results = await Promise.all(
|
||||
input.settings.map((s) =>
|
||||
ctx.prisma.systemSettings.upsert({
|
||||
where: { key: s.key },
|
||||
update: { value: s.value, updatedBy: ctx.user.id },
|
||||
create: {
|
||||
key: s.key,
|
||||
value: s.value,
|
||||
category: 'ANALYTICS',
|
||||
updatedBy: ctx.user.id,
|
||||
},
|
||||
})
|
||||
)
|
||||
)
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'UPDATE_ANALYTICS_SETTINGS',
|
||||
entityType: 'SystemSettings',
|
||||
detailsJson: { keys: input.settings.map((s) => s.key) },
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
})
|
||||
} catch {
|
||||
// Never throw on audit failure
|
||||
}
|
||||
|
||||
return results
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get audit configuration settings
|
||||
*/
|
||||
getAuditSettings: adminProcedure.query(async ({ ctx }) => {
|
||||
const settings = await ctx.prisma.systemSettings.findMany({
|
||||
where: { category: 'AUDIT_CONFIG' },
|
||||
orderBy: { key: 'asc' },
|
||||
})
|
||||
|
||||
return settings
|
||||
}),
|
||||
|
||||
/**
|
||||
* Update audit configuration settings
|
||||
*/
|
||||
updateAuditSettings: superAdminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
settings: z.array(
|
||||
z.object({
|
||||
key: z.string(),
|
||||
value: z.string(),
|
||||
})
|
||||
),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const results = await Promise.all(
|
||||
input.settings.map((s) =>
|
||||
ctx.prisma.systemSettings.upsert({
|
||||
where: { key: s.key },
|
||||
update: { value: s.value, updatedBy: ctx.user.id },
|
||||
create: {
|
||||
key: s.key,
|
||||
value: s.value,
|
||||
category: 'AUDIT_CONFIG',
|
||||
updatedBy: ctx.user.id,
|
||||
},
|
||||
})
|
||||
)
|
||||
)
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'UPDATE_AUDIT_SETTINGS',
|
||||
entityType: 'SystemSettings',
|
||||
detailsJson: { keys: input.settings.map((s) => s.key) },
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
})
|
||||
} catch {
|
||||
// Never throw on audit failure
|
||||
}
|
||||
|
||||
return results
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get localization settings
|
||||
*/
|
||||
getLocalizationSettings: adminProcedure.query(async ({ ctx }) => {
|
||||
const settings = await ctx.prisma.systemSettings.findMany({
|
||||
where: { category: 'LOCALIZATION' },
|
||||
orderBy: { key: 'asc' },
|
||||
})
|
||||
|
||||
return settings
|
||||
}),
|
||||
|
||||
/**
|
||||
* Update localization settings
|
||||
*/
|
||||
updateLocalizationSettings: superAdminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
settings: z.array(
|
||||
z.object({
|
||||
key: z.string(),
|
||||
value: z.string(),
|
||||
})
|
||||
),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const results = await Promise.all(
|
||||
input.settings.map((s) =>
|
||||
ctx.prisma.systemSettings.upsert({
|
||||
where: { key: s.key },
|
||||
update: { value: s.value, updatedBy: ctx.user.id },
|
||||
create: {
|
||||
key: s.key,
|
||||
value: s.value,
|
||||
category: 'LOCALIZATION',
|
||||
updatedBy: ctx.user.id,
|
||||
},
|
||||
})
|
||||
)
|
||||
)
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'UPDATE_LOCALIZATION_SETTINGS',
|
||||
entityType: 'SystemSettings',
|
||||
detailsJson: { keys: input.settings.map((s) => s.key) },
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
})
|
||||
} catch {
|
||||
// Never throw on audit failure
|
||||
}
|
||||
|
||||
return results
|
||||
}),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -34,6 +34,9 @@ export const userRouter = router({
|
|||
bio: true,
|
||||
notificationPreference: true,
|
||||
profileImageKey: true,
|
||||
digestFrequency: true,
|
||||
availabilityJson: true,
|
||||
preferredWorkload: true,
|
||||
createdAt: true,
|
||||
lastLoginAt: true,
|
||||
},
|
||||
|
|
@ -80,10 +83,13 @@ export const userRouter = router({
|
|||
phoneNumber: z.string().max(20).optional().nullable(),
|
||||
notificationPreference: z.enum(['EMAIL', 'WHATSAPP', 'BOTH', 'NONE']).optional(),
|
||||
expertiseTags: z.array(z.string()).max(15).optional(),
|
||||
digestFrequency: z.enum(['none', 'daily', 'weekly']).optional(),
|
||||
availabilityJson: z.any().optional(),
|
||||
preferredWorkload: z.number().int().min(1).max(100).optional().nullable(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { bio, expertiseTags, ...directFields } = input
|
||||
const { bio, expertiseTags, availabilityJson, preferredWorkload, digestFrequency, ...directFields } = input
|
||||
|
||||
// If bio is provided, merge it into metadataJson
|
||||
let metadataJson: Prisma.InputJsonValue | undefined
|
||||
|
|
@ -102,6 +108,9 @@ export const userRouter = router({
|
|||
...directFields,
|
||||
...(metadataJson !== undefined && { metadataJson }),
|
||||
...(expertiseTags !== undefined && { expertiseTags }),
|
||||
...(digestFrequency !== undefined && { digestFrequency }),
|
||||
...(availabilityJson !== undefined && { availabilityJson: availabilityJson as Prisma.InputJsonValue }),
|
||||
...(preferredWorkload !== undefined && { preferredWorkload }),
|
||||
},
|
||||
})
|
||||
}),
|
||||
|
|
@ -215,6 +224,8 @@ export const userRouter = router({
|
|||
status: true,
|
||||
expertiseTags: true,
|
||||
maxAssignments: true,
|
||||
availabilityJson: true,
|
||||
preferredWorkload: true,
|
||||
profileImageKey: true,
|
||||
profileImageProvider: true,
|
||||
createdAt: true,
|
||||
|
|
@ -326,6 +337,8 @@ export const userRouter = router({
|
|||
status: z.enum(['INVITED', 'ACTIVE', 'SUSPENDED']).optional(),
|
||||
expertiseTags: z.array(z.string()).optional(),
|
||||
maxAssignments: z.number().int().min(1).max(100).optional().nullable(),
|
||||
availabilityJson: z.any().optional(),
|
||||
preferredWorkload: z.number().int().min(1).max(100).optional().nullable(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
|
|
@ -630,6 +643,8 @@ export const userRouter = router({
|
|||
name: true,
|
||||
expertiseTags: true,
|
||||
maxAssignments: true,
|
||||
availabilityJson: true,
|
||||
preferredWorkload: true,
|
||||
profileImageKey: true,
|
||||
profileImageProvider: true,
|
||||
_count: {
|
||||
|
|
@ -1063,4 +1078,30 @@ export const userRouter = router({
|
|||
|
||||
return { success: true, message: 'If an account exists with this email, a password reset link will be sent.' }
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get current user's digest settings along with global digest config
|
||||
*/
|
||||
getDigestSettings: protectedProcedure.query(async ({ ctx }) => {
|
||||
const [user, digestEnabled, digestSections] = await Promise.all([
|
||||
ctx.prisma.user.findUniqueOrThrow({
|
||||
where: { id: ctx.user.id },
|
||||
select: { digestFrequency: true },
|
||||
}),
|
||||
ctx.prisma.systemSettings.findUnique({
|
||||
where: { key: 'digest_enabled' },
|
||||
select: { value: true },
|
||||
}),
|
||||
ctx.prisma.systemSettings.findUnique({
|
||||
where: { key: 'digest_sections' },
|
||||
select: { value: true },
|
||||
}),
|
||||
])
|
||||
|
||||
return {
|
||||
digestFrequency: user.digestFrequency,
|
||||
globalDigestEnabled: digestEnabled?.value === 'true',
|
||||
globalDigestSections: digestSections?.value ? JSON.parse(digestSections.value) : [],
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -0,0 +1,304 @@
|
|||
import { z } from 'zod'
|
||||
import { router, superAdminProcedure } from '../trpc'
|
||||
import { logAudit } from '@/server/utils/audit'
|
||||
import {
|
||||
generateWebhookSecret,
|
||||
deliverWebhook,
|
||||
} from '@/server/services/webhook-dispatcher'
|
||||
|
||||
export const WEBHOOK_EVENTS = [
|
||||
'evaluation.submitted',
|
||||
'evaluation.updated',
|
||||
'project.created',
|
||||
'project.statusChanged',
|
||||
'round.activated',
|
||||
'round.closed',
|
||||
'assignment.created',
|
||||
'assignment.completed',
|
||||
'user.invited',
|
||||
'user.activated',
|
||||
] as const
|
||||
|
||||
export const webhookRouter = router({
|
||||
/**
|
||||
* List all webhooks with delivery stats.
|
||||
*/
|
||||
list: superAdminProcedure.query(async ({ ctx }) => {
|
||||
const webhooks = await ctx.prisma.webhook.findMany({
|
||||
include: {
|
||||
_count: {
|
||||
select: { deliveries: true },
|
||||
},
|
||||
createdBy: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
})
|
||||
|
||||
// Compute recent delivery stats for each webhook
|
||||
const now = new Date()
|
||||
const twentyFourHoursAgo = new Date(now.getTime() - 24 * 60 * 60 * 1000)
|
||||
|
||||
const stats = await Promise.all(
|
||||
webhooks.map(async (wh) => {
|
||||
const [delivered, failed] = await Promise.all([
|
||||
ctx.prisma.webhookDelivery.count({
|
||||
where: {
|
||||
webhookId: wh.id,
|
||||
status: 'DELIVERED',
|
||||
createdAt: { gte: twentyFourHoursAgo },
|
||||
},
|
||||
}),
|
||||
ctx.prisma.webhookDelivery.count({
|
||||
where: {
|
||||
webhookId: wh.id,
|
||||
status: 'FAILED',
|
||||
createdAt: { gte: twentyFourHoursAgo },
|
||||
},
|
||||
}),
|
||||
])
|
||||
return {
|
||||
...wh,
|
||||
recentDelivered: delivered,
|
||||
recentFailed: failed,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return stats
|
||||
}),
|
||||
|
||||
/**
|
||||
* Create a new webhook.
|
||||
*/
|
||||
create: superAdminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
name: z.string().min(1).max(200),
|
||||
url: z.string().url(),
|
||||
events: z.array(z.string()).min(1),
|
||||
headers: z.any().optional(),
|
||||
maxRetries: z.number().int().min(0).max(10).default(3),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const secret = generateWebhookSecret()
|
||||
|
||||
const webhook = await ctx.prisma.webhook.create({
|
||||
data: {
|
||||
name: input.name,
|
||||
url: input.url,
|
||||
secret,
|
||||
events: input.events,
|
||||
headers: input.headers ?? undefined,
|
||||
maxRetries: input.maxRetries,
|
||||
createdById: ctx.user.id,
|
||||
},
|
||||
})
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'CREATE_WEBHOOK',
|
||||
entityType: 'Webhook',
|
||||
entityId: webhook.id,
|
||||
detailsJson: { name: input.name, url: input.url, events: input.events },
|
||||
})
|
||||
} catch {}
|
||||
|
||||
return webhook
|
||||
}),
|
||||
|
||||
/**
|
||||
* Update a webhook.
|
||||
*/
|
||||
update: superAdminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string().min(1).max(200).optional(),
|
||||
url: z.string().url().optional(),
|
||||
events: z.array(z.string()).min(1).optional(),
|
||||
headers: z.any().optional(),
|
||||
isActive: z.boolean().optional(),
|
||||
maxRetries: z.number().int().min(0).max(10).optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { id, ...data } = input
|
||||
|
||||
const webhook = await ctx.prisma.webhook.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...(data.name !== undefined ? { name: data.name } : {}),
|
||||
...(data.url !== undefined ? { url: data.url } : {}),
|
||||
...(data.events !== undefined ? { events: data.events } : {}),
|
||||
...(data.headers !== undefined ? { headers: data.headers } : {}),
|
||||
...(data.isActive !== undefined ? { isActive: data.isActive } : {}),
|
||||
...(data.maxRetries !== undefined ? { maxRetries: data.maxRetries } : {}),
|
||||
},
|
||||
})
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'UPDATE_WEBHOOK',
|
||||
entityType: 'Webhook',
|
||||
entityId: id,
|
||||
detailsJson: { updatedFields: Object.keys(data) },
|
||||
})
|
||||
} catch {}
|
||||
|
||||
return webhook
|
||||
}),
|
||||
|
||||
/**
|
||||
* Delete a webhook and its delivery history.
|
||||
*/
|
||||
delete: superAdminProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Cascade delete is defined in schema, so just delete the webhook
|
||||
await ctx.prisma.webhook.delete({
|
||||
where: { id: input.id },
|
||||
})
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'DELETE_WEBHOOK',
|
||||
entityType: 'Webhook',
|
||||
entityId: input.id,
|
||||
})
|
||||
} catch {}
|
||||
|
||||
return { success: true }
|
||||
}),
|
||||
|
||||
/**
|
||||
* Send a test payload to a webhook.
|
||||
*/
|
||||
test: superAdminProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const webhook = await ctx.prisma.webhook.findUnique({
|
||||
where: { id: input.id },
|
||||
})
|
||||
|
||||
if (!webhook) {
|
||||
throw new Error('Webhook not found')
|
||||
}
|
||||
|
||||
const testPayload = {
|
||||
event: 'test',
|
||||
timestamp: new Date().toISOString(),
|
||||
data: {
|
||||
message: 'This is a test webhook delivery from MOPC Platform.',
|
||||
webhookId: webhook.id,
|
||||
webhookName: webhook.name,
|
||||
},
|
||||
}
|
||||
|
||||
const delivery = await ctx.prisma.webhookDelivery.create({
|
||||
data: {
|
||||
webhookId: webhook.id,
|
||||
event: 'test',
|
||||
payload: testPayload,
|
||||
status: 'PENDING',
|
||||
attempts: 0,
|
||||
},
|
||||
})
|
||||
|
||||
await deliverWebhook(delivery.id)
|
||||
|
||||
// Fetch updated delivery to get the result
|
||||
const result = await ctx.prisma.webhookDelivery.findUnique({
|
||||
where: { id: delivery.id },
|
||||
})
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'TEST_WEBHOOK',
|
||||
entityType: 'Webhook',
|
||||
entityId: input.id,
|
||||
detailsJson: { deliveryStatus: result?.status },
|
||||
})
|
||||
} catch {}
|
||||
|
||||
return result
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get paginated delivery log for a webhook.
|
||||
*/
|
||||
getDeliveryLog: superAdminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
webhookId: z.string(),
|
||||
page: z.number().int().min(1).default(1),
|
||||
pageSize: z.number().int().min(1).max(100).default(20),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const skip = (input.page - 1) * input.pageSize
|
||||
|
||||
const [items, total] = await Promise.all([
|
||||
ctx.prisma.webhookDelivery.findMany({
|
||||
where: { webhookId: input.webhookId },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
skip,
|
||||
take: input.pageSize,
|
||||
}),
|
||||
ctx.prisma.webhookDelivery.count({
|
||||
where: { webhookId: input.webhookId },
|
||||
}),
|
||||
])
|
||||
|
||||
return {
|
||||
items,
|
||||
total,
|
||||
page: input.page,
|
||||
pageSize: input.pageSize,
|
||||
totalPages: Math.ceil(total / input.pageSize),
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Regenerate the HMAC secret for a webhook.
|
||||
*/
|
||||
regenerateSecret: superAdminProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const newSecret = generateWebhookSecret()
|
||||
|
||||
const webhook = await ctx.prisma.webhook.update({
|
||||
where: { id: input.id },
|
||||
data: { secret: newSecret },
|
||||
})
|
||||
|
||||
try {
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'REGENERATE_WEBHOOK_SECRET',
|
||||
entityType: 'Webhook',
|
||||
entityId: input.id,
|
||||
})
|
||||
} catch {}
|
||||
|
||||
return webhook
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get available webhook events.
|
||||
*/
|
||||
getAvailableEvents: superAdminProcedure.query(() => {
|
||||
return WEBHOOK_EVENTS
|
||||
}),
|
||||
})
|
||||
|
|
@ -0,0 +1,276 @@
|
|||
import { prisma } from '@/lib/prisma'
|
||||
import { sendStyledNotificationEmail } from '@/lib/email'
|
||||
|
||||
interface DigestResult {
|
||||
sent: number
|
||||
errors: number
|
||||
}
|
||||
|
||||
interface DigestSection {
|
||||
title: string
|
||||
items: string[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Process and send email digests for all opted-in users.
|
||||
* Called by cron endpoint.
|
||||
*/
|
||||
export async function processDigests(
|
||||
type: 'daily' | 'weekly'
|
||||
): Promise<DigestResult> {
|
||||
let sent = 0
|
||||
let errors = 0
|
||||
|
||||
// Check if digest feature is enabled
|
||||
const enabledSetting = await prisma.systemSettings.findUnique({
|
||||
where: { key: 'digest_enabled' },
|
||||
})
|
||||
if (enabledSetting?.value === 'false') {
|
||||
return { sent: 0, errors: 0 }
|
||||
}
|
||||
|
||||
// Find users who opted in for this digest frequency
|
||||
const users = await prisma.user.findMany({
|
||||
where: {
|
||||
digestFrequency: type,
|
||||
status: 'ACTIVE',
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (users.length === 0) {
|
||||
return { sent: 0, errors: 0 }
|
||||
}
|
||||
|
||||
// Load enabled sections from settings
|
||||
const sectionsSetting = await prisma.systemSettings.findUnique({
|
||||
where: { key: 'digest_sections' },
|
||||
})
|
||||
const enabledSections: string[] = sectionsSetting?.value
|
||||
? JSON.parse(sectionsSetting.value)
|
||||
: ['pending_evaluations', 'upcoming_deadlines', 'new_assignments', 'unread_notifications']
|
||||
|
||||
const baseUrl = process.env.NEXTAUTH_URL || 'https://monaco-opc.com'
|
||||
|
||||
for (const user of users) {
|
||||
try {
|
||||
const content = await getDigestContent(user.id, enabledSections)
|
||||
|
||||
// Skip if there's nothing to report
|
||||
if (content.sections.length === 0) continue
|
||||
|
||||
// Build email body from sections
|
||||
const bodyParts: string[] = []
|
||||
for (const section of content.sections) {
|
||||
bodyParts.push(`**${section.title}**`)
|
||||
for (const item of section.items) {
|
||||
bodyParts.push(`- ${item}`)
|
||||
}
|
||||
bodyParts.push('')
|
||||
}
|
||||
|
||||
await sendStyledNotificationEmail(
|
||||
user.email,
|
||||
user.name || '',
|
||||
'DIGEST',
|
||||
{
|
||||
name: user.name || undefined,
|
||||
title: `Your ${type === 'daily' ? 'Daily' : 'Weekly'} Digest`,
|
||||
message: bodyParts.join('\n'),
|
||||
linkUrl: `${baseUrl}/dashboard`,
|
||||
metadata: {
|
||||
digestType: type,
|
||||
pendingEvaluations: content.pendingEvaluations,
|
||||
upcomingDeadlines: content.upcomingDeadlines,
|
||||
newAssignments: content.newAssignments,
|
||||
unreadNotifications: content.unreadNotifications,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// Log the digest
|
||||
await prisma.digestLog.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
digestType: type,
|
||||
contentJson: {
|
||||
pendingEvaluations: content.pendingEvaluations,
|
||||
upcomingDeadlines: content.upcomingDeadlines,
|
||||
newAssignments: content.newAssignments,
|
||||
unreadNotifications: content.unreadNotifications,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
sent++
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`[Digest] Failed to send ${type} digest to ${user.email}:`,
|
||||
error
|
||||
)
|
||||
errors++
|
||||
}
|
||||
}
|
||||
|
||||
return { sent, errors }
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile digest content for a single user.
|
||||
*/
|
||||
async function getDigestContent(
|
||||
userId: string,
|
||||
enabledSections: string[]
|
||||
): Promise<{
|
||||
sections: DigestSection[]
|
||||
pendingEvaluations: number
|
||||
upcomingDeadlines: number
|
||||
newAssignments: number
|
||||
unreadNotifications: number
|
||||
}> {
|
||||
const now = new Date()
|
||||
const sections: DigestSection[] = []
|
||||
let pendingEvaluations = 0
|
||||
let upcomingDeadlines = 0
|
||||
let newAssignments = 0
|
||||
let unreadNotifications = 0
|
||||
|
||||
// 1. Pending evaluations
|
||||
if (enabledSections.includes('pending_evaluations')) {
|
||||
const pendingAssignments = await prisma.assignment.findMany({
|
||||
where: {
|
||||
userId,
|
||||
isCompleted: false,
|
||||
round: {
|
||||
status: 'ACTIVE',
|
||||
votingEndAt: { gt: now },
|
||||
},
|
||||
},
|
||||
include: {
|
||||
project: { select: { id: true, title: true } },
|
||||
round: { select: { name: true, votingEndAt: true } },
|
||||
},
|
||||
})
|
||||
|
||||
pendingEvaluations = pendingAssignments.length
|
||||
if (pendingAssignments.length > 0) {
|
||||
sections.push({
|
||||
title: `Pending Evaluations (${pendingAssignments.length})`,
|
||||
items: pendingAssignments.map(
|
||||
(a) =>
|
||||
`${a.project.title} - ${a.round.name}${
|
||||
a.round.votingEndAt
|
||||
? ` (due ${a.round.votingEndAt.toLocaleDateString('en-US', {
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
})})`
|
||||
: ''
|
||||
}`
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Upcoming deadlines (rounds ending within 7 days)
|
||||
if (enabledSections.includes('upcoming_deadlines')) {
|
||||
const sevenDaysFromNow = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000)
|
||||
const upcomingRounds = await prisma.round.findMany({
|
||||
where: {
|
||||
status: 'ACTIVE',
|
||||
votingEndAt: {
|
||||
gt: now,
|
||||
lte: sevenDaysFromNow,
|
||||
},
|
||||
assignments: {
|
||||
some: {
|
||||
userId,
|
||||
isCompleted: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
name: true,
|
||||
votingEndAt: true,
|
||||
},
|
||||
})
|
||||
|
||||
upcomingDeadlines = upcomingRounds.length
|
||||
if (upcomingRounds.length > 0) {
|
||||
sections.push({
|
||||
title: 'Upcoming Deadlines',
|
||||
items: upcomingRounds.map(
|
||||
(r) =>
|
||||
`${r.name} - ${r.votingEndAt?.toLocaleDateString('en-US', {
|
||||
weekday: 'short',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
})}`
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 3. New assignments since last digest
|
||||
if (enabledSections.includes('new_assignments')) {
|
||||
const lastDigest = await prisma.digestLog.findFirst({
|
||||
where: { userId },
|
||||
orderBy: { sentAt: 'desc' },
|
||||
select: { sentAt: true },
|
||||
})
|
||||
|
||||
const sinceDate = lastDigest?.sentAt || new Date(now.getTime() - 24 * 60 * 60 * 1000)
|
||||
|
||||
const recentAssignments = await prisma.assignment.findMany({
|
||||
where: {
|
||||
userId,
|
||||
createdAt: { gt: sinceDate },
|
||||
},
|
||||
include: {
|
||||
project: { select: { id: true, title: true } },
|
||||
round: { select: { name: true } },
|
||||
},
|
||||
})
|
||||
|
||||
newAssignments = recentAssignments.length
|
||||
if (recentAssignments.length > 0) {
|
||||
sections.push({
|
||||
title: `New Assignments (${recentAssignments.length})`,
|
||||
items: recentAssignments.map(
|
||||
(a) => `${a.project.title} - ${a.round.name}`
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Unread notifications count
|
||||
if (enabledSections.includes('unread_notifications')) {
|
||||
const unreadCount = await prisma.inAppNotification.count({
|
||||
where: {
|
||||
userId,
|
||||
isRead: false,
|
||||
},
|
||||
})
|
||||
|
||||
unreadNotifications = unreadCount
|
||||
if (unreadCount > 0) {
|
||||
sections.push({
|
||||
title: 'Notifications',
|
||||
items: [`You have ${unreadCount} unread notification${unreadCount !== 1 ? 's' : ''}`],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
sections,
|
||||
pendingEvaluations,
|
||||
upcomingDeadlines,
|
||||
newAssignments,
|
||||
unreadNotifications,
|
||||
}
|
||||
}
|
||||
|
|
@ -4,20 +4,22 @@
|
|||
* Calculates scores for jury/mentor-project matching based on:
|
||||
* - Tag overlap (expertise match)
|
||||
* - Bio/description match (text similarity)
|
||||
* - Workload balance
|
||||
* - Workload balance (respects preferredWorkload and maxAssignments)
|
||||
* - Country match (mentors only)
|
||||
* - Geographic diversity penalty (prevents clustering by country)
|
||||
* - Previous round familiarity bonus (continuity across rounds)
|
||||
* - COI penalty (conflict of interest hard-block)
|
||||
* - Availability window check (F2: penalizes jurors unavailable during voting)
|
||||
*
|
||||
* Score Breakdown:
|
||||
* - Tag overlap: 0-40 points (weighted by confidence)
|
||||
* - Bio match: 0-15 points (if bio exists)
|
||||
* - Workload balance: 0-25 points
|
||||
* - Workload balance: 0-25 points (uses preferredWorkload as soft target)
|
||||
* - Country match: 0-15 points (mentors only)
|
||||
* - Geo diversity: -15 per excess same-country assignment (threshold: 2)
|
||||
* - Previous round familiarity: +10 if reviewed in earlier round
|
||||
* - COI: juror skipped entirely if conflict declared
|
||||
* - Availability: -30 if unavailable during voting window
|
||||
*/
|
||||
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
|
@ -32,6 +34,7 @@ export interface ScoreBreakdown {
|
|||
geoDiversityPenalty: number
|
||||
previousRoundFamiliarity: number
|
||||
coiPenalty: number
|
||||
availabilityPenalty: number
|
||||
}
|
||||
|
||||
export interface AssignmentScore {
|
||||
|
|
@ -65,6 +68,7 @@ const GEO_DIVERSITY_THRESHOLD = 2
|
|||
const GEO_DIVERSITY_PENALTY_PER_EXCESS = -15
|
||||
const PREVIOUS_ROUND_FAMILIARITY_BONUS = 10
|
||||
// COI jurors are skipped entirely rather than penalized (effectively -Infinity)
|
||||
const AVAILABILITY_PENALTY = -30 // Heavy penalty for unavailable jurors
|
||||
|
||||
// Common words to exclude from bio matching
|
||||
const STOP_WORDS = new Set([
|
||||
|
|
@ -224,6 +228,45 @@ export function calculateCountryMatchScore(
|
|||
return 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a user is available during the round's voting window.
|
||||
* availabilityJson is an array of { start, end } date-range objects
|
||||
* representing when the user IS available.
|
||||
* Returns 0 (available) or AVAILABILITY_PENALTY (unavailable).
|
||||
*/
|
||||
export function calculateAvailabilityPenalty(
|
||||
availabilityJson: unknown,
|
||||
votingStartAt: Date | null | undefined,
|
||||
votingEndAt: Date | null | undefined
|
||||
): number {
|
||||
// If no availability windows set, user is always available
|
||||
if (!availabilityJson || !Array.isArray(availabilityJson) || availabilityJson.length === 0) {
|
||||
return 0
|
||||
}
|
||||
|
||||
// If no voting window defined, can't check availability
|
||||
if (!votingStartAt || !votingEndAt) {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Check if any availability window overlaps with the voting window
|
||||
for (const window of availabilityJson) {
|
||||
if (!window || typeof window !== 'object') continue
|
||||
const start = new Date((window as { start: string }).start)
|
||||
const end = new Date((window as { end: string }).end)
|
||||
|
||||
if (isNaN(start.getTime()) || isNaN(end.getTime())) continue
|
||||
|
||||
// Check overlap: user available window overlaps with voting window
|
||||
if (start <= votingEndAt && end >= votingStartAt) {
|
||||
return 0 // Available during at least part of the voting window
|
||||
}
|
||||
}
|
||||
|
||||
// No availability window overlaps with voting window
|
||||
return AVAILABILITY_PENALTY
|
||||
}
|
||||
|
||||
// ─── Main Scoring Function ───────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
|
@ -275,6 +318,8 @@ export async function getSmartSuggestions(options: {
|
|||
expertiseTags: true,
|
||||
maxAssignments: true,
|
||||
country: true,
|
||||
availabilityJson: true,
|
||||
preferredWorkload: true,
|
||||
_count: {
|
||||
select: {
|
||||
assignments: {
|
||||
|
|
@ -289,6 +334,12 @@ export async function getSmartSuggestions(options: {
|
|||
return []
|
||||
}
|
||||
|
||||
// Get round voting window for availability checking
|
||||
const roundForAvailability = await prisma.round.findUnique({
|
||||
where: { id: roundId },
|
||||
select: { votingStartAt: true, votingEndAt: true },
|
||||
})
|
||||
|
||||
// Get existing assignments to avoid duplicates
|
||||
const existingAssignments = await prisma.assignment.findMany({
|
||||
where: { roundId },
|
||||
|
|
@ -399,9 +450,12 @@ export async function getSmartSuggestions(options: {
|
|||
project.description
|
||||
)
|
||||
|
||||
// Use preferredWorkload as a soft target when available, fallback to calculated target
|
||||
const effectiveTarget = user.preferredWorkload ?? targetPerUser
|
||||
|
||||
const workloadScore = calculateWorkloadScore(
|
||||
currentCount,
|
||||
targetPerUser,
|
||||
effectiveTarget,
|
||||
user.maxAssignments
|
||||
)
|
||||
|
||||
|
|
@ -410,6 +464,13 @@ export async function getSmartSuggestions(options: {
|
|||
? calculateCountryMatchScore(user.country, project.country)
|
||||
: 0
|
||||
|
||||
// Availability check against the round's voting window
|
||||
const availabilityPenalty = calculateAvailabilityPenalty(
|
||||
user.availabilityJson,
|
||||
roundForAvailability?.votingStartAt,
|
||||
roundForAvailability?.votingEndAt
|
||||
)
|
||||
|
||||
// ── New scoring factors ─────────────────────────────────────────────
|
||||
|
||||
// Geographic diversity penalty
|
||||
|
|
@ -437,7 +498,8 @@ export async function getSmartSuggestions(options: {
|
|||
workloadScore +
|
||||
countryScore +
|
||||
geoDiversityPenalty +
|
||||
previousRoundFamiliarity
|
||||
previousRoundFamiliarity +
|
||||
availabilityPenalty
|
||||
|
||||
// Build reasoning
|
||||
const reasoning: string[] = []
|
||||
|
|
@ -452,6 +514,9 @@ export async function getSmartSuggestions(options: {
|
|||
} else if (workloadScore > 0) {
|
||||
reasoning.push('Moderate workload')
|
||||
}
|
||||
if (user.preferredWorkload) {
|
||||
reasoning.push(`Preferred workload: ${user.preferredWorkload}`)
|
||||
}
|
||||
if (countryScore > 0) {
|
||||
reasoning.push('Same country')
|
||||
}
|
||||
|
|
@ -461,6 +526,9 @@ export async function getSmartSuggestions(options: {
|
|||
if (previousRoundFamiliarity > 0) {
|
||||
reasoning.push('Reviewed in previous round (+10)')
|
||||
}
|
||||
if (availabilityPenalty < 0) {
|
||||
reasoning.push(`Unavailable during voting window (${availabilityPenalty})`)
|
||||
}
|
||||
|
||||
suggestions.push({
|
||||
userId: user.id,
|
||||
|
|
@ -477,6 +545,7 @@ export async function getSmartSuggestions(options: {
|
|||
geoDiversityPenalty,
|
||||
previousRoundFamiliarity,
|
||||
coiPenalty: 0, // COI jurors are skipped entirely
|
||||
availabilityPenalty,
|
||||
},
|
||||
reasoning,
|
||||
matchingTags,
|
||||
|
|
@ -602,6 +671,7 @@ export async function getMentorSuggestionsForProject(
|
|||
geoDiversityPenalty: 0,
|
||||
previousRoundFamiliarity: 0,
|
||||
coiPenalty: 0,
|
||||
availabilityPenalty: 0,
|
||||
},
|
||||
reasoning,
|
||||
matchingTags,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,174 @@
|
|||
import crypto from 'crypto'
|
||||
import { Prisma } from '@prisma/client'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
/**
|
||||
* Dispatch a webhook event to all active webhooks subscribed to this event.
|
||||
*/
|
||||
export async function dispatchWebhookEvent(
|
||||
event: string,
|
||||
payload: Record<string, unknown>
|
||||
): Promise<number> {
|
||||
const webhooks = await prisma.webhook.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
events: { has: event },
|
||||
},
|
||||
})
|
||||
|
||||
if (webhooks.length === 0) return 0
|
||||
|
||||
let deliveryCount = 0
|
||||
|
||||
for (const webhook of webhooks) {
|
||||
try {
|
||||
const delivery = await prisma.webhookDelivery.create({
|
||||
data: {
|
||||
webhookId: webhook.id,
|
||||
event,
|
||||
payload: payload as Prisma.InputJsonValue,
|
||||
status: 'PENDING',
|
||||
attempts: 0,
|
||||
},
|
||||
})
|
||||
|
||||
// Attempt delivery asynchronously (don't block the caller)
|
||||
deliverWebhook(delivery.id).catch((err) => {
|
||||
console.error(`[Webhook] Background delivery failed for ${delivery.id}:`, err)
|
||||
})
|
||||
|
||||
deliveryCount++
|
||||
} catch (error) {
|
||||
console.error(`[Webhook] Failed to create delivery for webhook ${webhook.id}:`, error)
|
||||
}
|
||||
}
|
||||
|
||||
return deliveryCount
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to deliver a single webhook.
|
||||
*/
|
||||
export async function deliverWebhook(deliveryId: string): Promise<void> {
|
||||
const delivery = await prisma.webhookDelivery.findUnique({
|
||||
where: { id: deliveryId },
|
||||
include: { webhook: true },
|
||||
})
|
||||
|
||||
if (!delivery || !delivery.webhook) {
|
||||
console.error(`[Webhook] Delivery ${deliveryId} not found`)
|
||||
return
|
||||
}
|
||||
|
||||
const { webhook } = delivery
|
||||
const payloadStr = JSON.stringify(delivery.payload)
|
||||
|
||||
// Sign payload with HMAC-SHA256
|
||||
const signature = crypto
|
||||
.createHmac('sha256', webhook.secret)
|
||||
.update(payloadStr)
|
||||
.digest('hex')
|
||||
|
||||
// Build headers
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Webhook-Signature': `sha256=${signature}`,
|
||||
'X-Webhook-Event': delivery.event,
|
||||
'X-Webhook-Delivery': delivery.id,
|
||||
}
|
||||
|
||||
// Merge custom headers from webhook config
|
||||
if (webhook.headers && typeof webhook.headers === 'object') {
|
||||
const customHeaders = webhook.headers as Record<string, string>
|
||||
for (const [key, value] of Object.entries(customHeaders)) {
|
||||
if (typeof value === 'string') {
|
||||
headers[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const controller = new AbortController()
|
||||
const timeout = setTimeout(() => controller.abort(), 30000) // 30s timeout
|
||||
|
||||
const response = await fetch(webhook.url, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: payloadStr,
|
||||
signal: controller.signal,
|
||||
})
|
||||
|
||||
clearTimeout(timeout)
|
||||
|
||||
const responseBody = await response.text().catch(() => '')
|
||||
|
||||
await prisma.webhookDelivery.update({
|
||||
where: { id: deliveryId },
|
||||
data: {
|
||||
status: response.ok ? 'DELIVERED' : 'FAILED',
|
||||
responseStatus: response.status,
|
||||
responseBody: responseBody.slice(0, 4000), // Truncate long responses
|
||||
attempts: delivery.attempts + 1,
|
||||
lastAttemptAt: new Date(),
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
||||
|
||||
await prisma.webhookDelivery.update({
|
||||
where: { id: deliveryId },
|
||||
data: {
|
||||
status: 'FAILED',
|
||||
responseBody: errorMessage.slice(0, 4000),
|
||||
attempts: delivery.attempts + 1,
|
||||
lastAttemptAt: new Date(),
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retry all failed webhook deliveries that haven't exceeded max retries.
|
||||
* Called by cron.
|
||||
*/
|
||||
export async function retryFailedDeliveries(): Promise<{
|
||||
retried: number
|
||||
errors: number
|
||||
}> {
|
||||
let retried = 0
|
||||
let errors = 0
|
||||
|
||||
const failedDeliveries = await prisma.webhookDelivery.findMany({
|
||||
where: {
|
||||
status: 'FAILED',
|
||||
},
|
||||
include: {
|
||||
webhook: {
|
||||
select: { maxRetries: true, isActive: true },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
for (const delivery of failedDeliveries) {
|
||||
// Skip if webhook is inactive or max retries exceeded
|
||||
if (!delivery.webhook.isActive) continue
|
||||
if (delivery.attempts >= delivery.webhook.maxRetries) continue
|
||||
|
||||
try {
|
||||
await deliverWebhook(delivery.id)
|
||||
retried++
|
||||
} catch (error) {
|
||||
console.error(`[Webhook] Retry failed for delivery ${delivery.id}:`, error)
|
||||
errors++
|
||||
}
|
||||
}
|
||||
|
||||
return { retried, errors }
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random HMAC secret for webhook signing.
|
||||
*/
|
||||
export function generateWebhookSecret(): string {
|
||||
return crypto.randomBytes(32).toString('hex')
|
||||
}
|
||||
Loading…
Reference in New Issue