You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. Go to https://aistudio.google.com
2. Sign in with Google account
3. Click "Get API Key" → "Create API key"
4. Copy the key and add it to server/.env as GEMINI_API_KEY
6.2 AI-Powered Features Implementation
Add AI Routes: server/src/routes/ai.js
// ====================================// AI Routes// ====================================constexpress=require('express');constrouter=express.Router();constaiService=require('../services/aiService');const{ authenticate }=require('../middleware/auth');const{ supabaseAdmin }=require('../config/database');// GET /api/ai/suggest-tasks — AI task suggestionsrouter.get('/suggest-tasks',authenticate,async(req,res)=>{try{const{data: profile}=awaitsupabaseAdmin.from('profiles').select('*, wards(*)').eq('id',req.userId).single();constsuggestions=awaitaiService.suggestTasks(profile,profile.wards);res.json({success: true,data: suggestions});}catch(error){console.error('AI suggest tasks error:',error);res.status(500).json({success: false,message: 'Failed to generate task suggestions.'});}});// POST /api/ai/analyze-issue — AI issue analysisrouter.post('/analyze-issue',authenticate,async(req,res)=>{try{const{ description, category }=req.body;constanalysis=awaitaiService.analyzeIssue(description,category);res.json({success: true,data: { analysis }});}catch(error){console.error('AI analyze error:',error);res.status(500).json({success: false,message: 'Failed to analyze issue.'});}});// GET /api/ai/ward-quiz/:wardId — Generate ward quizrouter.get('/ward-quiz/:wardId',authenticate,async(req,res)=>{try{const{data: ward}=awaitsupabaseAdmin.from('wards').select('*').eq('id',req.params.wardId).single();if(!ward){returnres.status(404).json({success: false,message: 'Ward not found.'});}constquestions=awaitaiService.generateWardQuiz(ward);res.json({success: true,data: questions});}catch(error){console.error('AI quiz error:',error);res.status(500).json({success: false,message: 'Failed to generate quiz.'});}});// POST /api/ai/portfolio-summary — Generate portfolio summaryrouter.post('/portfolio-summary',authenticate,async(req,res)=>{try{const{data: profile}=awaitsupabaseAdmin.from('profiles').select('*').eq('id',req.userId).single();constsummary=awaitaiService.generatePortfolioSummary(profile);res.json({success: true,data: { summary }});}catch(error){console.error('AI summary error:',error);res.status(500).json({success: false,message: 'Failed to generate summary.'});}});module.exports=router;
Register AI routes in server/server.js
// Add this with other route importsconstaiRoutes=require('./src/routes/ai');// Add this with other route mountingsapp.use('/api/ai',aiRoutes);