Skip to content

Commit

Permalink
Merge pull request #81 from fdhhhdjd/backend-backend-#78
Browse files Browse the repository at this point in the history
 #78 Backend backend
  • Loading branch information
fdhhhdjd authored Oct 15, 2022
2 parents 78bbd28 + 9395170 commit 67d2a54
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
10 changes: 10 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ const session = require("express-session");
let RedisStore = require("connect-redis")(session);
const bodyParser = require("body-parser");
const compression = require("compression");
const cron = require("node-cron");

//! Import
const REDIS = require("./src/v1/db/redis_db");
const Mongo_DB = require("./src/v1/db/mongo_db");
const CONSTANTS = require("./src/v1/configs/constants");
const STORAGE = require("./src/v1/utils/storage");
const Cron_Users = require("./src/v1/user_api/cron_users/index");
//! Connect
Mongo_DB();

Expand Down Expand Up @@ -63,6 +66,13 @@ app.use(
},
})
);

//***** Run Cron ******/
cron.schedule("*/5 * * * *", function () {
console.log("Run Check Uncheck Users");
Cron_Users.Delete_User_Un_Check_Expired();
});

//* ------------------------- Upload General ------------------------- //
const upload_routes = require("./src/v1/upload_cloudinary/routes/upload.routes");
app.use("/api", upload_routes);
Expand Down
2 changes: 2 additions & 0 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if (process.env.NODE_ENV !== "PRODUCTION") {
require("dotenv").config({ path: "../.env" });
}
const numCpu = os.cpus().length;

app.get("/api/", (req, res) => {
const health_check = {
uptime: process.uptime(),
Expand All @@ -16,6 +17,7 @@ app.get("/api/", (req, res) => {
};
return res.send(health_check);
});

if (cluster.isMaster) {
for (let i = 0; i < numCpu; i++) {
cluster.fork();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const uploadStorage = async (file_upload) => {
return data;
};
const destroyStorage = async (public_id) => {
console.log(public_id);
let results = null;
await cloudinary.v2.uploader.destroy(public_id, async (err, result) => {
if (err) throw (results = err);
Expand Down
25 changes: 25 additions & 0 deletions backend/src/v1/user_api/cron_users/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const Users = require("../../models/userModel");
const UserVerifications = require("../../models/userVerificationModel");
const CONSTANTS = require("../../configs/constants");
module.exports = {
/**
* @author Tiến Tài
* Delete Uncheck
*/
async Delete_User_Un_Check_Expired() {
const data = await UserVerifications.find({
expiresAt: { $lt: Date.now() },
}).select("userId");
const users = await Users.find({
verified: CONSTANTS.DELETED_DISABLE,
}).select("_id");
for (var i = 0; i < data.length; i++) {
for (var j = 0; j < users.length; j++) {
if (data[i].userId == users[j].id) {
await Users.deleteOne({ _id: users[j].id });
await UserVerifications.deleteOne({ userId: data[i].userId });
}
}
}
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const Users = require("../../../models/userModel");
const UserVerifications = require("../../../models/userVerificationModel");
const CONSTANTS = require("../../../configs/constants");
const HELPER = require("../../../utils/helper");
const { RedisPub } = require("../../../utils/limited_redis");
const {
destroyStorage,
} = require("../../../upload_cloudinary/services/uploadStorage.service");

// ** Delete Verification
const deleteVerification = async (userId) => {
Expand Down Expand Up @@ -36,6 +40,7 @@ const UpdateProfile = async ({
date_of_birth,
user_id,
}) => {
await deleteImageAutoCloud(user_id);
await Users.findOneAndUpdate(
{ _id: user_id },
{
Expand All @@ -47,6 +52,7 @@ const UpdateProfile = async ({
}
);
let userId = user_id;

if (userId) {
await updateProfileId(userId);
}
Expand Down Expand Up @@ -76,13 +82,17 @@ const NewAcceptToken = (user) => {
};
//** Update Password */
const UpdatePassword = async ({ user_id, password }) => {
console.log(user_id, password);
return Users.findByIdAndUpdate(
{ _id: user_id },
{ password: password },
{ new: true }
);
};
//** Delete Image Cloud auto */
const deleteImageAutoCloud = async (user_id) => {
const user = await Users.findById(user_id);
return await destroyStorage(user?.image?.public_id);
};
module.exports = {
//* CreateUser
createUser,
Expand Down
Binary file modified dump.rdb
Binary file not shown.

0 comments on commit 67d2a54

Please sign in to comment.