const{ OpenAI }=require("openai");constopenai=newOpenAI({apiKey: process.env.OPENAI_API_KEY});asyncfunctionanalyzeReviews(reviews){constcombinedReviews=reviews.join(" ");constprompt=`Analyze these Airbnb guest reviews and summarize:1. Overall sentiment (positive, neutral, negative)2. Common themes3. Three specific improvement suggestionsReviews:${combinedReviews}`;constres=awaitopenai.chat.completions.create({model: "gpt-4",messages: [{role: "user",content: prompt}],});returnres.choices[0].message.content;}module.exports={ analyzeReviews };
services/reportService.js
constWeeklyReport=require('../models/WeeklyReport');const{ analyzeReviews }=require('./openaiService');asyncfunctiongenerateWeeklyReport(){constdummyReviews=["The place was clean but the check-in was confusing.","Great location but the WiFi was slow.","Host was very helpful!"];constsummary=awaitanalyzeReviews(dummyReviews);constnewReport=newWeeklyReport({avgOccupancy: 72,avgRate: 145,guestReviewSummary: summary,recommendations: ["Clarify check-in instructions","Improve WiFi router placement","Keep up excellent communication"]});awaitnewReport.save();}module.exports={ generateWeeklyReport };
constexpress=require("express");constmongoose=require("mongoose");require("dotenv").config();constreportRoutes=require("./routes/reports");constapp=express();app.use(express.json());app.use("/api/reports",reportRoutes);mongoose.connect(process.env.MONGO_URI,()=>{console.log("Connected to MongoDB");});require("./schedulers/weeklyReport");constPORT=process.env.PORT||5000;app.listen(PORT,()=>console.log(`Server running on port ${PORT}`));