Skip to content

Commit

Permalink
some polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
habasefa committed Jun 1, 2024
1 parent 8c2df40 commit 7883fc4
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 43 deletions.
2 changes: 2 additions & 0 deletions lib/chalkLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ const success = (mssg) => console.log(chalk.inverse.green("%s"), mssg);
const flag = (mssg) => console.log(chalk.inverse.blue("%s"), mssg);
const warning = (mssg) => console.log(chalk.inverse.orange("%s"), mssg);
const error = (mssg) => console.log(chalk.inverse.red("%s"), mssg);
const info = (mssg) => console.log(chalk.inverse.yellow("%s"), mssg);

module.exports = {
error,
flag,
success,
warning,
info,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "temaribet-api",
"name": "tts-api",
"version": "1.0.0",
"description": "Temaribet api built with nodejs express mongodb",
"main": "index.js",
Expand Down
1 change: 1 addition & 0 deletions routes/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const prisma = new PrismaClient();
router.post("/register", async (req, res, next) => {
await adminRegister(req, res, next);
});

// Login Admins
router.post("/login", async (req, res, next) => {
await adminLogin(req, res, next);
Expand Down
1 change: 1 addition & 0 deletions routes/parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ router.get('/basedOnMonth',check_auth,async(req,res,next)=>{
next(error)
}
})

router.get("/", check_auth, async (req, res, next) => {
console.log("hi")

Expand Down
87 changes: 50 additions & 37 deletions routes/tutor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ router.post("/", check_auth, async (req, res, next) => {
...req.body,
},
});

res.status(201).json({
success: true,
message: "Tutor Registered.",
Expand Down Expand Up @@ -48,7 +49,6 @@ router.get("/pending", check_auth, async (req, res, next) => {
}
});
router.get("/", check_auth, async (req, res, next) => {
console.log("hi");
try {
const users = await prisma.tutor.findMany({
where: {
Expand Down Expand Up @@ -90,8 +90,9 @@ router.get("/location/:location", check_auth, async (req, res, next) => {
try {
const users = await prisma.tutor.findMany({
where: {
location:{
contains: String(location).toLowerCase()}
location: {
contains: String(location).toLowerCase(),
},
},
include: {
students: true,
Expand Down Expand Up @@ -234,7 +235,7 @@ router.post("/followUp", check_auth, async (req, res, next) => {
res.status(201).json({
success: true,
message: "Tutor follow Up Registered.",
followUp: tutorFollowUp ,
followUp: tutorFollowUp,
});
} catch (error) {
console.log(error);
Expand All @@ -251,7 +252,7 @@ router.get("/followUp:id", check_auth, async (req, res, next) => {
id: id,
},
include: {
tutor:true
tutor: true,
},
});
console.log(user);
Expand All @@ -274,12 +275,16 @@ router.get("/followUp:year", check_auth, async (req, res, next) => {
year: Number(year),
},
include: {
tutor:true
tutor: true,
},
});
console.log(user);
if (user) {
res.json({ success: true, message: "List of Pending Tutors", user: user });
res.json({
success: true,
message: "List of Pending Tutors",
user: user,
});
} else {
res.json({ success: false, message: `tutor followUp was found` });
}
Expand All @@ -302,7 +307,7 @@ router.post(
console.log(req.body);
const result = await cloudinary.uploader.upload(req.file.path);
// Create new user

console.log(result.url);
const data = JSON.parse(req.body.data);
console.log(data);
Expand All @@ -313,7 +318,7 @@ router.post(
month: data?.month,
},
});

// If the image already exists, return success: true and duplication: true
if (existingImage) {
return res.json({ success: true, duplication: true });
Expand All @@ -330,7 +335,12 @@ router.post(
},
});
console.log(image);
res.json({ success: true, duplication: false, message: "Image Created", image });
res.json({
success: true,
duplication: false,
message: "Image Created",
image,
});
} catch (err) {
console.log(err);
next(err);
Expand All @@ -352,12 +362,13 @@ router.get("/fetchImage/:year", check_auth, async (req, res, next) => {
});
console.log(timeSheets);
if (timeSheets) {
const formattedTimesheet = timeSheets.map(user => {
const timestamp = parseInt(user.id.toString().substring(0, 8), 16) * 1000; // Extract the creation time from the `ObjectId`
const formattedTimesheet = timeSheets.map((user) => {
const timestamp =
parseInt(user.id.toString().substring(0, 8), 16) * 1000; // Extract the creation time from the `ObjectId`
const createdAt = new Date(timestamp); // Convert the timestamp to a readable date and time string
return {
...user,
createdAt
createdAt,
};
});
res.json({
Expand All @@ -382,16 +393,13 @@ router.get("/image/rejected/:id", check_auth, async (req, res, next) => {
where: {
tutorId: val,
statusOfAcceptance: {
in: [Status.SUCCESS, Status.REJECTED]
}
in: [Status.SUCCESS, Status.REJECTED],
},
},
orderBy: [
{ year: 'desc'},
{month: 'desc'}
],
orderBy: [{ year: "desc" }, { month: "desc" }],
include: {
tutor: true,
parent:true,
parent: true,
},
});
console.log(timeSheets);
Expand All @@ -405,7 +413,7 @@ router.get("/image/rejected/:id", check_auth, async (req, res, next) => {
res.json({ success: false, message: `image not found` });
}
} catch (error) {
console.log(error)
console.log(error);
next(error);
}
});
Expand All @@ -423,8 +431,7 @@ router.patch("/image/:id", async (req, res, next) => {
parent: true,
},
});



console.log(updatedUser, "image");

res.json({ success: true, message: `Updated image ${id}`, updatedUser });
Expand All @@ -447,7 +454,7 @@ router.patch("/imagestatus/:id", async (req, res, next) => {
parent: true,
},
});

const phone = updatedUser.parent.phone1;
const TOKEN = process.env.MESSAGE_TOKEN;
const IDENTIFIER_ID = process.env.IDENTIFIER_ID;
Expand All @@ -474,7 +481,7 @@ router.patch("/imagestatus/:id", async (req, res, next) => {
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error(error));

console.log(updatedUser, "image");

res.json({ success: true, message: `Updated image ${id}`, updatedUser });
Expand All @@ -486,39 +493,45 @@ router.patch("/imagestatus/:id", async (req, res, next) => {

router.delete("/image/:id", async (req, res, next) => {
const { id } = req.params;
console.log(id,'find')
console.log(id, "find");
try {
const deletedTimesheet = await prisma.image.delete({
where: {
id: id,
},
});
res.json({ success: true, message: `Deleted timesheet ${id}`, deletedTimesheet });
res.json({
success: true,
message: `Deleted timesheet ${id}`,
deletedTimesheet,
});
} catch (error) {
console.log(error)
console.log(error);
next(error);

}
});

router.get("/image/:id", async (req, res, next) => {
const { id } = req.params;
console.log(id,'find')
console.log(id, "find");
try {
const fetchedTimesheet = await prisma.image.findUnique({
where: {
id: id,
},
include:{
parent:true,
tutor:true,
}
include: {
parent: true,
tutor: true,
},
});
res.json({
success: true,
message: `fetched timesheet ${id}`,
fetchedTimesheet,
});
res.json({ success: true, message: `fetched timesheet ${id}`, fetchedTimesheet });
} catch (error) {
console.log(error)
console.log(error);
next(error);

}
});

Expand Down
26 changes: 21 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require("dotenv").config();

const http = require('https');
const http = require("https");
const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
Expand Down Expand Up @@ -54,12 +54,28 @@ app.use((error, req, res, next) => {
});
});

setInterval(()=>{
console.log("every 10 min")
http.get('https://temaribet-api.onrender.com');
// keep server alive
const minInterval = 5 * 60 * 1000; // 5 minutes in milliseconds
const maxInterval = 15 * 60 * 1000; // 15 minutes in milliseconds

function getRandomInterval() {
return Math.floor(
Math.random() * (maxInterval - minInterval + 1) + minInterval
);
}

function keepServerAlive() {
let count = 0;
console.log("Calling server to keep it alive...");
http.get("https://temaribet-api.onrender.com");
count++;

const interval = getRandomInterval();
setTimeout(keepServerAlive, interval);
}

},10 * 60 * 1000)
// listen to server
app.listen(port, () => {
log.success(`Server is running on port ${port}.`);
keepServerAlive();
});

0 comments on commit 7883fc4

Please sign in to comment.