added base examples and drills
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
const fs = require("fs");
|
||||
|
||||
class TaskService {
|
||||
constructor(filePath) {
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
completeTask(taskName, userName) {
|
||||
const raw = fs.readFileSync(this.filePath, "utf8");
|
||||
const state = JSON.parse(raw);
|
||||
const task = state.tasks.find((item) => item.name === taskName);
|
||||
|
||||
if (!task) {
|
||||
throw new Error("Task not found");
|
||||
}
|
||||
|
||||
if (task.completed) {
|
||||
return {
|
||||
message: "Task already complete",
|
||||
pointsAwarded: 0,
|
||||
badge: null
|
||||
};
|
||||
}
|
||||
|
||||
task.completed = true;
|
||||
task.completedAt = new Date().toISOString();
|
||||
|
||||
let pointsAwarded = 10;
|
||||
|
||||
if (userName && userName.toLowerCase() === "ada") {
|
||||
pointsAwarded = pointsAwarded + 5;
|
||||
}
|
||||
|
||||
if (new Date().getDay() === 5) {
|
||||
pointsAwarded = pointsAwarded * 2;
|
||||
}
|
||||
|
||||
state.points = state.points + pointsAwarded;
|
||||
|
||||
let badge = null;
|
||||
|
||||
if (state.points > 50) {
|
||||
badge = "consistency-star";
|
||||
}
|
||||
|
||||
fs.writeFileSync(this.filePath, JSON.stringify(state, null, 2));
|
||||
|
||||
if (badge) {
|
||||
console.log("Awarded badge:", badge);
|
||||
}
|
||||
|
||||
return {
|
||||
message: "Task completed",
|
||||
pointsAwarded,
|
||||
badge
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
TaskService
|
||||
};
|
||||
Reference in New Issue
Block a user