-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
71 lines (62 loc) · 1.97 KB
/
app.js
File metadata and controls
71 lines (62 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import express from "express";
import { config } from "dotenv";
import { errorMiddleware } from "./middlewares/error.js";
import cookieParser from "cookie-parser";
import cors from "cors";
import cron from "node-cron";
import { User } from "./models/user.js";
config({
path: "./data/config.env",
});
export const app = express();
// Using Middlewares
app.use(express.json());
app.use(cookieParser());
app.use(
cors({
credentials: true,
methods: ["GET", "POST", "PUT", "DELETE"],
origin: "*",
// origin: [process.env.FRONTEND_URI_1, process.env.FRONTEND_URI_2],
})
);
app.get("/", (req, res, next) => {
res.send("Working");
});
// Importing Routers here
import user from "./routes/user.js";
import product from "./routes/product.js";
import order from "./routes/order.js";
import sub from "./routes/sub.js";
app.use("/api/v1/user", user);
app.use("/api/v1/product", product);
app.use("/api/v1/order", order);
app.use("/api/v1/sub", sub);
// Using Error Middleware
app.use(errorMiddleware);
// */5 * * * * *..5secs, 0 0 * * *..every 12am
// cron.schedule("*/5 * * * *", async () => {
// const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
// await NotificationModel.deleteMany({status:"read",createdAt: {$lt: thirtyDaysAgo}});
// console.log('Deleted read notifications');
// try {
// Find all users in the database
// const users = await User.find();
// // Loop through each user and perform an action (replace this with your action)
// // Subtract 1 from the user's active days
// for (const user of users) {
// if (user.activeSubType > 0) {
// user.activeSubType -= 1;
// }
// // If active days are 0, set subscription to false
// if (user.activeSubType === 0) {
// user.isActiveSub = false;
// }
// // Save the updated user to the database
// await user.save();
// }
// console.log("Cron job completed.");
// } catch (error) {
// console.error("Error:", error);
// }
// });