Skip to content
Open
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
10 changes: 10 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@eslint/js": "^9.16.0",
"@types/cors": "^2.8.17",
"@types/express": "^5.0.0",
"@types/node-schedule": "^2.1.7",
"@typescript-eslint/eslint-plugin": "^8.18.0",
"concurrently": "^9.1.0",
"eslint": "^8.56.0",
Expand Down
13 changes: 13 additions & 0 deletions backend/src/models/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
name: string;
email: string;
uid: string;
hasCompletedWeeklyCheckin: boolean;
hasCompletedDailyCheckin: boolean;
};

type UserDoc = {
name: string;
email: string;
uid: string;
hasCompletedWeeklyCheckin: boolean;
hasCompletedDailyCheckin: boolean;
} & mongoose.Document;

type UserModelInterface = {
build(attr: UserInterface): UserDoc;

Check warning on line 20 in backend/src/models/users.ts

View workflow job for this annotation

GitHub Actions / Backend lint and style check

'attr' is defined but never used. Allowed unused args must match /^_/u
} & mongoose.Model<UserDoc>;

const userSchema = new mongoose.Schema({
Expand All @@ -29,6 +33,15 @@
type: String,
required: true,
},

hasCompletedWeeklyCheckin: {
type: Boolean,
required: true,
},
hasCompletedDailyCheckin: {
type: Boolean,
required: true,
},
});

userSchema.statics.build = (attr: UserInterface) => {
Expand Down
32 changes: 29 additions & 3 deletions backend/src/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ router.get(
// POST route to create a user profile
router.post("/users", async (req: PsychesRequest, res: Response): Promise<void> => {
try {
const { name, email, uid } = req.body;
console.log("BODY:", req.body);

const { name, email, uid, hasCompletedWeeklyCheckin } = req.body;

// Validate required fields
if (!name || !email || !uid) {
if (!name || !email || !uid || !hasCompletedWeeklyCheckin) {
res.status(400).json({ message: "All fields are required" });
return;
}
Expand All @@ -64,7 +66,7 @@ router.post("/users", async (req: PsychesRequest, res: Response): Promise<void>
}

// Create and save new user
const newUser = new User({ name, email, uid });
const newUser = new User({ name, email, uid, hasCompletedWeeklyCheckin });

await newUser.save();

Expand Down Expand Up @@ -103,4 +105,28 @@ router.put("/users/:uid", async (req: PsychesRequest, res: Response): Promise<vo
}
});

router.put("/users/:uid/checkin", async (req: PsychesRequest, res: Response): Promise<void> => {
try {
const { uid } = req.params;

const user = await User.findOneAndUpdate(
{ uid },
{ hasCompletedWeeklyCheckin: true },
{ new: true },
);

if (!user) {
res.status(404).json({ message: "User not found" });
return;
}

console.log("✅ User updated successfully:", user);

res.status(200).json({ message: "Weekly check-in marked as complete", user });
} catch (error) {
console.error("Error completing check-in:", error);
res.status(500).json({ message: "Server error", error: (error as Error).message });
}
});

export { router as userRouter };
3 changes: 3 additions & 0 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import "dotenv/config";
import express, { Express, Request, Response } from "express";
import mongoose from "mongoose";
import { resetWeeklyCheckin, resetDailyCheckin } from "./services/resetCheckin";

Check warning on line 6 in backend/src/server.ts

View workflow job for this annotation

GitHub Actions / Backend lint and style check

import statements should have an absolute path

import env from "../src/util/validateEnv";

Expand Down Expand Up @@ -35,4 +36,6 @@
console.log(`[server]: Server is running at http://localhost:${String(port)}`);
});

resetWeeklyCheckin();
resetDailyCheckin();
module.exports = app;
33 changes: 33 additions & 0 deletions backend/src/services/resetCheckin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { scheduleJob } from "node-schedule";
import { User } from "../models/users";

function resetWeeklyCheckin() {
scheduleJob("0 0 * * 0", async () => {
// Runs at 12:00 AM every Sunday (server time)
try {
const result = await User.updateMany(
{}, // all users
{ hasCompletedWeeklyCheckin: false },
);
console.log(`Weekly check-ins reset for ${result.modifiedCount} users.`);
} catch (error) {
console.error("Error resetting weekly check-ins:", error);
}
});
}
function resetDailyCheckin() {
scheduleJob("0 0 * * *", async () => {
// Runs at 12:00 AM every day (server time)
try {
const result = await User.updateMany(
{}, // all users
{ hasCompletedDailyCheckin: false },
);
console.log(`Daily check-ins reset for ${result.modifiedCount} users.`);
} catch (error) {
console.error("Error resetting Daily check-ins:", error);
}
});
}

export { resetWeeklyCheckin, resetDailyCheckin };
89 changes: 89 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies": {
"node-schedule": "^2.1.1"
},
"devDependencies": {
"@types/node-schedule": "^2.1.7"
}
}
Loading