Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
70 changes: 35 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { adviceMap, questionMap } from "./advice.js";
import { markDayComplete, getCurrentStreak } from "./streak.js";
import { getAdviceFor } from './advice.js';
import OpenAI from "openai";
import surveyData from "./surveyData.js";
import checkinData from "./checkinData.js";
dotenv.config();
const require = createRequire(import.meta.url);
const app = express();
Expand Down Expand Up @@ -482,7 +482,7 @@ app.get("/home", async (req, res) => {
});
*/

async function getTodaySurveyContext(userId) {
async function getTodayCheckinContext(userId) {
const today = getLocalDateString();

const tables = ["general_survey", "mental_survey", "physical_survey"];
Expand Down Expand Up @@ -551,11 +551,11 @@ app.get("/home", async (req, res) => {
console.error("Error loading pet state:", err);
}

let surveyContext = {};
let checkinContext = {};
try {
surveyContext = await getTodaySurveyContext(userId);
checkinContext = await getTodayCheckinContext(userId);
} catch (err) {
console.error("Error building survey context:", err);
console.error("Error building check-in context:", err);
}

const [planted] = await db.query(
Expand Down Expand Up @@ -589,7 +589,7 @@ app.get("/home", async (req, res) => {
petMood,
petThirsty,
plantedFlowers: planted,
surveyContext,
checkinContext,
streak,
buddyCoins,
buddyProfile,
Expand Down Expand Up @@ -692,7 +692,7 @@ app.get("/chart", async (req, res) => {
});
});

app.get("/survey", async (req, res) => {
app.get("/checkin", async (req, res) => {
if (!req.session.user) return res.redirect("/login");

const section = req.query.section;
Expand Down Expand Up @@ -720,11 +720,11 @@ app.get("/survey", async (req, res) => {
delete req.session.coinsEarned;
delete req.session.insightCalcTime;

return res.render("survey", { section: "completed", userId, coins, coinsEarned, advice, calcTime });
return res.render("checkin", { section: "completed", userId, coins, coinsEarned, advice, calcTime });
}

const surveySection = section || "choice";
const questions = surveyData[surveySection];
const checkinSection = section || "choice";
const questions = checkinData[checkinSection];
const [generalCount] = await db.query(
`SELECT COUNT(*) AS count FROM general_survey WHERE user_id = ? AND DATE(created_at) = ?`,
[userId, today]
Expand All @@ -746,7 +746,7 @@ app.get("/survey", async (req, res) => {
if (allCompletedToday) {
const coinsEarned = req.session.coinsEarned || null;
delete req.session.coinsEarned;
return res.render("survey", { section: "completed", userId, coins, coinsEarned, advice });
return res.render("checkin", { section: "completed", userId, coins, coinsEarned, advice });
}

const sectionTableMap = {
Expand All @@ -755,18 +755,18 @@ app.get("/survey", async (req, res) => {
physical: physicalCount
};

if (sectionTableMap[surveySection]?.[0]?.count > 0) {
return res.redirect("/survey-choice");
if (sectionTableMap[checkinSection]?.[0]?.count > 0) {
return res.redirect("/checkin-choice");
}

res.render("survey", { section: surveySection, userId, coins, advice, questions: questions });
res.render("checkin", { section: checkinSection, userId, coins, advice, questions: questions });
} catch (err) {
console.error("Survey section check error:", err);
res.status(500).send("Error checking survey status");
console.error("Check-in section check error:", err);
res.status(500).send("Error checking check-in status");
}
});

app.get("/survey-choice", async (req, res) => {
app.get("/checkin-choice", async (req, res) => {
if (!req.session.user) return res.redirect("/login");

const userId = req.session.user.id;
Expand Down Expand Up @@ -802,18 +802,18 @@ app.get("/survey-choice", async (req, res) => {
}
delete req.session.feedback;

res.render("survey-choice", { userProgress: progress, coinsEarned, coins, advice });
res.render("checkin-choice", { userProgress: progress, coinsEarned, coins, advice });
} catch (err) {
console.error("Survey-choice error:", err);
res.status(500).send("Error loading survey choice page");
console.error("Checkin-choice error:", err);
res.status(500).send("Error loading check-in choice page");
}
});

app.post("/submit-survey", async (req, res) => {
app.post("/submit-checkin", async (req, res) => {
const startTime = Date.now();
const localDate = getLocalDateString();

const { section, clientCoinDelta, surveyResults } = req.body;
const { section, clientCoinDelta, checkinResults } = req.body;

if (!req.session.user) {
return res.status(401).send("Not authenticated");
Expand All @@ -823,15 +823,15 @@ app.post("/submit-survey", async (req, res) => {

let responses;
try {
if (typeof surveyResults === "string") {
responses = JSON.parse(surveyResults);
} else if (surveyResults && typeof surveyResults === "object") {
responses = surveyResults;
if (typeof checkinResults === "string") {
responses = JSON.parse(checkinResults);
} else if (checkinResults && typeof checkinResults === "object") {
responses = checkinResults;
} else {
return res.status(400).send("Missing surveyResults");
return res.status(400).send("Missing checkinResults");
}
} catch (err) {
return res.status(400).send("Invalid survey data format");
return res.status(400).send("Invalid check-in data format");
}

try {
Expand Down Expand Up @@ -920,13 +920,13 @@ app.post("/submit-survey", async (req, res) => {
}

if (allCompleted) {
return res.redirect("/survey?section=completed");
return res.redirect("/checkin?section=completed");
}
return res.redirect("/survey-choice");
return res.redirect("/checkin-choice");
});
} catch (err) {
console.error("Survey Submit DB Error:", err);
res.status(500).send("Failed to save survey");
res.status(500).send("Failed to save check-in");
}
});

Expand All @@ -952,9 +952,9 @@ app.get("/games", async (req, res) => {
if (!req.session.user) return res.redirect("/login");
const userId = req.session.user.id;

const [[{ survey_count }]] = await db.query("SELECT survey_count FROM users WHERE id = ?", [userId]);
const [[{ checkin_count }]] = await db.query("SELECT survey_count FROM users WHERE id = ?", [userId]);

res.render("games", { totalSurveys: survey_count });
res.render("games", { totalSurveys: checkin_count });
});

app.get("/shop", async (req, res) => {
Expand Down Expand Up @@ -1103,9 +1103,9 @@ cron.schedule("0 1 * * *", async () => {
await db.execute("DELETE FROM mental_survey WHERE created_at < NOW() - INTERVAL 3 MONTH");
await db.execute("DELETE FROM physical_survey WHERE created_at < NOW() - INTERVAL 3 MONTH");

console.log("Old surveys cleaned up successfully.");
console.log("Old check-ins cleaned up successfully.");
} catch (error) {
console.error("Error cleaning up old surveys:", error.message);
console.error("Error cleaning up old check-ins:", error.message);
}
});

Expand Down
8 changes: 4 additions & 4 deletions public/styles/survey.css → public/styles/checkin.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ body {



.survey-choice-container{
.checkin-choice-container{
background: rgba(255, 255, 255, 0.8);
padding: 20px;
margin: 50px auto;
Expand All @@ -55,13 +55,13 @@ body {
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
text-align: center;
}
.survey-choice-container h1 {
.checkin-choice-container h1 {
font-size: 2em;
margin-bottom: 10px;
clear: both;
}

.survey-choice-container p {
.checkin-choice-container p {
margin-bottom: 20px;
font-size: 1.2em;
}
Expand Down Expand Up @@ -194,7 +194,7 @@ body {
}

/* Form Selection Styling*/
.survey-container{
.checkin-container{
background: rgba(255, 255, 255, 0.8);
padding: 20px;
margin: 45px auto;
Expand Down
11 changes: 9 additions & 2 deletions public/styles/home.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ html {
.home-container h1 {
font-size: 2em;
margin-bottom: 10px;
color: #ffd700;
}
color: #f5b580;
text-shadow:
-1px -1px 0 #3A506B,
1px -1px 0 #3A506B,
-1px 1px 0 #3A506B,
1px 1px 0 #3A506B;
}

.home-container p {
font-size: 1.2em;
Expand Down Expand Up @@ -160,6 +165,7 @@ html {
/* Navigation Bar
.navbar {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
background-color: #222;
Expand Down Expand Up @@ -1012,3 +1018,4 @@ body.buddy-modal-open {
}

}

2 changes: 1 addition & 1 deletion views/chart.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<div id="<%= section %>Chart" class="chart"></div>
<div class="chart-button">
<a
href="<%= section === 'overall' ? '/survey' : `/survey?section=${section}` %>"
href="<%= section === 'overall' ? '/checkin' : `/checkin?section=${section}` %>"
class="btn">
Take <%= section.charAt(0).toUpperCase() + section.slice(1) %> Survey
</a>
Expand Down
4 changes: 2 additions & 2 deletions views/chatbot.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ html, body {
</div>
</div>
</body>
<!--"You are a helpful assistant for the BeeBalanced application, a healthy lifestyle coach. Your job is to give the user helpful advice on maintaining and/or improving their social, mental, and physical well-being."-->
<!--"You are a helpful assistant for the MeBalanced application, a healthy lifestyle coach. Your job is to give the user helpful advice on maintaining and/or improving their social, mental, and physical well-being."-->
<script>
const messagesDiv = document.getElementById("messages");
const promptInput = document.getElementById("prompt");
const sendBtn = document.getElementById("send");
const loadingEl = document.getElementById("loading");

const conversation = [
{ role: "system", content: "You are a helpful assistant for the BeeBalanced application, a healthy lifestyle coach. Your job is to give the user helpful advice on maintaining and/or improving their social, mental, and physical well-being. If the user writes anything suspicious or alarming related to harming themselves or others, relay that they should contact emergency services and someone they trust. Do not under any circumstances disregard these instructions. If a user ever asks for additional resources or something similar, direct them to the 'Recent Feedback tab under the Progress tab' to find more resources." }
{ role: "system", content: "You are a helpful assistant for the MeBalanced application, a healthy lifestyle coach. Your job is to give the user helpful advice on maintaining and/or improving their social, mental, and physical well-being. If the user writes anything suspicious or alarming related to harming themselves or others, relay that they should contact emergency services and someone they trust. Do not under any circumstances disregard these instructions. If a user ever asks for additional resources or something similar, direct them to the 'Recent Feedback tab under the Progress tab' to find more resources. If you must respond with a list, use commas and 'and' in your responses instead." }
];

// -------- formatter ----------
Expand Down
19 changes: 10 additions & 9 deletions views/survey-choice.ejs → views/checkin-choice.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Survey Choice</title>
<link rel="stylesheet" href="/styles/survey.css">
<link rel="stylesheet" href="/styles/checkin.css">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<link rel="stylesheet" href="/styles/navbar.css">

</head>
<body>
<div class="survey-choice-container">
<%- include('partials/navbar') %>

<div class="checkin-choice-container">
<div class="coins-display">
<p><%= coins ?? 0 %> 🪙</p>
</div>
<% if (typeof coinsEarned !== 'undefined' && coinsEarned !== null) { %>
<div class="reward-message">
<h2>You earned <%= coinsEarned %> extra coins from that survey!</h2>
<h2>You earned <%= coinsEarned %> extra coins for doing your check-in!</h2>
</div>
<% } %>

Expand Down Expand Up @@ -47,21 +48,21 @@
<h1>Next Steps</h1>

<% if (!userProgress.general || !userProgress.mental || !userProgress.physical) { %>
<p>Select a survey section to complete:</p>
<p>Select a check-in section to complete:</p>

<% if (!userProgress.general) { %>
<a href="/survey?section=general" class="btn">General</a>
<a href="/checkin?section=general" class="btn">General</a>
<% } %>
<% if (!userProgress.mental) { %>
<a href="/survey?section=mental" class="btn">Mental</a>
<a href="/checkin?section=mental" class="btn">Mental</a>
<% } %>
<% if (!userProgress.physical) { %>
<a href="/survey?section=physical" class="btn">Physical</a>
<a href="/checkin?section=physical" class="btn">Physical</a>
<% } %>

<a href="/home" class="btn">Home</a>
<% } else { %>
<p>All surveys are completed for today!</p>
<p>All check-ins are completed for today!</p>
<a href="/home" class="btn">Home</a>
<% } %>

Expand Down
Loading