Skip to content

ADD repeat cron, FIX individ task page, FIX css #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
9 changes: 5 additions & 4 deletions back-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.2",
"chai": "^4.3.7",
"chai-http": "^4.3.0",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"cron": "^2.3.0",
"dotenv": "^10.0.0",
"express": "^4.18.2",
"express-validator": "^7.0.0",
"istanbul": "^0.4.5",
"mongoose": "^7.0.3",
"cookie-parser": "^1.4.5",
"multer": "^1.4.5-lts.1",
"passport": "^0.5.0",
"passport-jwt": "^4.0.0",
"bcryptjs": "^2.4.3",
"multer": "^1.4.5-lts.1"
"passport-jwt": "^4.0.0"
},
"devDependencies": {
"mocha": "^10.2.0",
Expand Down
2 changes: 2 additions & 0 deletions back-end/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ if (process.env.NODE_ENV === "production") {
console.log("Testing mode activated.");
}

const job = require('./cron/task.update.js');

const taskRouter = require('./routes/task.route.js');
const profRouter = require('./routes/prof.route.js');
const financesRouter = require('./routes/finances.route.js');
Expand Down
5 changes: 2 additions & 3 deletions back-end/src/controllers/task.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ const taskService = require("../services/task.service.js");

// get task by id taskSer
async function get(req, res) {
console.log("get")
try {
let task = await taskService.getTask(req.user.houses._id, req.params.id);

if (process.env.NODE_ENV === 'production') {
if (task === null) throw new Error("Task not found");
// Set assignee to the first name of the assignee and only access assignee if its not null
task.assignee = task.assignee?.first_name ?? "No Assignee";
task.assignee = task.assignee?.username ?? "No Assignee";
task.room = task.room?.roomName;
} else {
if (task === undefined) throw new Error("Task not found");
Expand All @@ -27,7 +26,7 @@ async function gets(req, res) {
if (process.env.NODE_ENV === 'production') {
// Set assignee to the first name of the assignee and only access assignee if its not null
tasks.forEach((task) => {
task.assignee = task.assignee?.first_name ?? "No Assignee";
task.assignee = task.assignee?.username ?? "No Assignee";
task.room = task.room?.roomName;
});
}
Expand Down
34 changes: 34 additions & 0 deletions back-end/src/cron/task.update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const CronJob = require('cron').CronJob
const Task = require('../models/task.list.model.js');

const job = new CronJob('0 */30 * * * *', async function () {
// Update task data complete if current date + repeat days has passed

// If due time + repeat is greater than current date set due time to current date
Task.updateMany({
repeat: {
$ne: 0
},
$expr: {
$gt: [{
$add: ['$due_time', {
$multiply: ['$repeat', 24 * 60 * 60 * 1000]
}]
}, Date.now().valueOf()]
},
complete: true
}, {
due_time: Date.now().valueOf(),
complete: false
}).exec().then((result) => {
console.log('Updated Task Data');
console.log(result);
}).catch((err) => {
console.log(err);
});

});

job.start();

module.exports = job;
6 changes: 3 additions & 3 deletions back-end/src/models/room.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ const RoomSchema = new Schema({
});

// When creating new room add it to the house
RoomSchema.post('create', function (next) {
this.model('house').updateOne({
RoomSchema.post('create', async function (next) {
await this.model('house').updateOne({
_id: this.home
}, {
$push: {
rooms: this._id
}
});
}).exec();
next();
});

Expand Down
46 changes: 23 additions & 23 deletions back-end/src/models/task.list.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,110 +49,110 @@ const TaskSchema = new Schema({
});

// Add Cascading remove to delete all references to tasks when a task is deleted
TaskSchema.pre('remove', function (next) {
TaskSchema.pre('remove', async function (next) {
// Remove all the assignment docs that reference the removed task
this.model('user').updateOne({
await this.model('user').updateOne({
_id: this.assignee // Task may not be assigned to anyone
}, {
$pull: {
assigned_tasks: this._id
}
});
}).exec();

this.model('room').updateOne({
await this.model('room').updateOne({
_id: this.room
}, { // Task may not be assigned to any room
$pull: {
tasks: this._id
}
});
}).exec();

this.model('house').updateOne({
await this.model('house').updateOne({
_id: this.house
}, { // Filter by house id
$pull: {
tasks: this._id
}
});
}).exec();

next();
});

// If a task is created not updated, add it to the room, user, and house it is assigned to
TaskSchema.post('create', function (next) {
TaskSchema.post('create', async function (next) {
if (this.assignee != null) {
this.model('user').updateOne({
await this.model('user').updateOne({
_id: this.assignee
}, {
$push: {
assigned_tasks: this._id
}
});
}).exec();
}
if (this.room != null) {
this.model('room').updateOne({
await this.model('room').updateOne({
_id: this.room
}, {
$push: {
tasks: this._id
}
});
}).exec();
}

this.model('house').updateOne({
await this.model('house').updateOne({
_id: this.house
}, {
$push: {
tasks: this._id
}
});
}).exec();

next();
});

// If we update a task, we need to update the room, user, and house it is assigned to
TaskSchema.post('update', function (next) {
TaskSchema.post('update', async function (next) {

// Check if there is a change in assignee
if (this._update.assignee != null && this._update.assignee != this._conditions.assignee) {
// Remove the task from the old assignee
this.model('user').updateOne({
await this.model('user').updateOne({
_id: this._conditions.assignee
}, {
$pull: {
assigned_tasks: this._conditions._id
}
});
}).exec();

// Add the task to the new assignee
this.model('user').updateOne({
await this.model('user').updateOne({
_id: this._update.assignee
}, {
$push: {
assigned_tasks: this._conditions._id
}
});
}).exec();
}

// Check if there is a change in room
if (this._update.room != null && this._update.room != this._conditions.room) {
// Remove the task from the old room
this.model('room').updateOne({
await this.model('room').updateOne({
_id: this._conditions.room
}, {
$pull: {
tasks: this._conditions._id
}
});
}).exec();

// Add the task to the new room
this.model('room').updateOne({
await this.model('room').updateOne({
_id: this._update.room
}, {
$push: {
tasks: this._conditions._id
}
});
}).exec();
}

// NO CHANGE IN HOUSE ALLOWED FOR TASKS
Expand Down
18 changes: 0 additions & 18 deletions back-end/src/services/addmembers.services.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const fs = require('fs');
const path = require('path');
const userModel = require("../models/users.model.js");
const houseModel = require("../models/house.model.js");


async function savingUser(req,res, err) {
Expand All @@ -19,7 +18,6 @@ async function savingUser(req,res, err) {
});

} else {
console.log(loggeduser)
// if user exists, check if password is correct
if (file) {
const {base64, filename} = file;
Expand Down Expand Up @@ -55,22 +53,6 @@ async function savingUser(req,res, err) {
});
}

/*
fs.readFile(require.resolve('../json/household_info.json'), (err, data) => {
if (err) throw err;
console.log('err',err)

const users = JSON.parse(data);
users.push(newUser);

fs.writeFile(require.resolve('../json/household_info.json'), JSON.stringify(users, null, 2), err => {
if (err) throw err;
console.log('err',err)
});
});
}*/


}
module.exports = {
savingUser
Expand Down
24 changes: 1 addition & 23 deletions back-end/src/services/alerts.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// Import json array from task.service.js
let { task_json } = require("./task.service.js");
const userData = require("../json/hardcode.json");
const tasks = require("../models/task.list.model.js");

async function getAlerts(house_id, user_id) {
const tasks = await Task.find({ house: house_id, assignee: user_id }).lean();
Expand All @@ -15,30 +12,11 @@ async function getAlerts(house_id, user_id) {
_id: task._id,
};
});
// const user = userData;
// const filteredAlerts = task_json
// .filter((alert) => {
// return !alert.complete && alert.assignee === user.username;
// })
// .map((alert) => {
// const timestamp = alert.due_time;
// const date = new Date(timestamp).toLocaleDateString("en-US");
// return {
// task: alert.task_name,
// date: date,
// _id: alert._id,
// };
// });
return filteredAlerts;
}

async function logAlertState(alertId, isChecked) {
// console.log(`Alert ID: ${alertId}, Checked: ${isChecked}`);
// const alertIndex = task_json.findIndex((alert) => alert._id === alertId);
// if (alertIndex >= 0) {
// task_json[alertIndex].complete = isChecked;
// }
const task = await Task.findByIdAndUpdate(
await Task.findByIdAndUpdate(
alertId,
{ complete: isChecked },
{ new: true }
Expand Down
3 changes: 0 additions & 3 deletions back-end/src/services/finances.service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
let transaction_json = require("../json/transactions.json");
const Transaction = require("../models/transaction.model.js");
const House = require("../models/house.model.js");

async function getAllTransactions(house_id) {
// return transaction_json;
Expand All @@ -18,7 +16,6 @@ async function addTransaction(transaction, house_id) {
date: transaction.date,
};
newTransaction.house = house_id;
// transaction_json.push(newTransaction);
Transaction.create(newTransaction);
}

Expand Down
1 change: 0 additions & 1 deletion back-end/src/services/home.service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
let rooms_json = require('../json/rooms.json')
const Room = require('../models/room.model.js')
const House = require('../models/house.model.js')
let getAllRooms, addRoom;

if (process.env.NODE_ENV === 'production') {
Expand Down
7 changes: 2 additions & 5 deletions back-end/src/services/task.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ let getTasks, getTask, CreateTask, UpdateTask, RemoveTask;
let task_json = require('../json/tasklist.json')
// load the dataabase models we want to deal with
const Task = require('../models/task.list.model.js');
const User = require('../models/users.model.js');
const Room = require('../models/room.model.js');
const House = require('../models/house.model.js');

//Import the task
if (process.env.NODE_ENV === 'production') {
getTasks = async (house_id) => {
// Find tasks with house id and return it
return Task.find({ house: house_id }).populate('assignee', '-_id first_name').populate('room', '-_id roomName').lean();
return Task.find({ house: house_id }).populate('assignee', '-_id username').populate('room', '-_id roomName').lean();
};

getTask = async (house_id, task_id) => {
// Find task with house id and task id and return it
return Task.findOne({ house: house_id, _id: task_id }).populate('assignee', '-_id first_name').populate('room', '-_id roomName').lean();
return Task.findOne({ house: house_id, _id: task_id }).lean();
};

// make sure task doesnt exist in database then add the task to the Task
Expand Down
2 changes: 1 addition & 1 deletion front-end/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
height: 100vh;
width: var(--max-width);
background: linear-gradient(to top, rgba(223, 223, 223, 1), rgba(255, 255, 255, 1));
overflow: hidden;
overflow-y: auto;
}


Expand Down
1 change: 0 additions & 1 deletion front-end/src/components/AddMemberPic.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const AddMembersPic = (props) => {
setImage(e.target.result);
//append to parent formData
const imageData = {'base64': base64Image, 'filename': selectedFile.name}
console.log(imageData);
props.onImageClick('file', imageData);
}
}
Expand Down
2 changes: 1 addition & 1 deletion front-end/src/components/Profile_info_component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const ProfInfo = (props) => {
})

.then(response => {
console.log(response)
// console.log(response)
})
.catch(error => {
console.log(error);
Expand Down
Loading