Skip to content

Commit eb11f22

Browse files
authored
Add files via upload
0 parents  commit eb11f22

File tree

19 files changed

+14144
-0
lines changed

19 files changed

+14144
-0
lines changed

common/dao/index.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
class BaseDao {
2+
constructor(dbModel) {
3+
//Get Model
4+
this.Model = dbModel;
5+
}
6+
7+
save(object) {
8+
return this.Model.create(object);
9+
}
10+
11+
findOne(query, projection) {
12+
return this.Model.findOne(query, projection).lean().exec();
13+
}
14+
15+
find(query, projection) {
16+
return this.Model.find(query, projection).lean().exec();
17+
}
18+
19+
findOneAndUpdate(query, update, options) {
20+
return this.Model.findOneAndUpdate(query, update, options).exec();
21+
}
22+
23+
findAndModify(query, update, options) {
24+
return this.Model.findAndModify(query, update, options).exec();
25+
}
26+
27+
findByIdAndUpdate(query, update, options) {
28+
return this.Model.findByIdAndUpdate(query, update, options).exec();
29+
}
30+
31+
/**
32+
* Update Given Model
33+
* @param query
34+
* @param toUpdate
35+
* @return Promise Object
36+
* @private
37+
*/
38+
update(query, update, options) {
39+
if (!options) {
40+
options = {};
41+
}
42+
return this.Model.update(query, update, options).exec();
43+
}
44+
45+
updateMany(query, update, options) {
46+
if (!options) {
47+
options = {};
48+
}
49+
return this.Model.updateMany(query, update, options).exec();
50+
}
51+
52+
remove(query, options) {
53+
return this.Model.remove(query, options).exec();
54+
}
55+
56+
deleteMany(query, options) {
57+
return this.Model.deleteMany(query, options).exec();
58+
}
59+
60+
findByIdAndRemove(query, options) {
61+
return this.Model.findByIdAndRemove(query, options).exec();
62+
}
63+
64+
findOneAndDelete(query, options) {
65+
return this.Model.findOneAndDelete(query, options).exec();
66+
}
67+
68+
aggregate(aggPipe, options) {
69+
if (!options) {
70+
options = {};
71+
}
72+
return this.Model.aggregate(aggPipe)
73+
.allowDiskUse(true)
74+
.collation(options)
75+
.exec();
76+
}
77+
78+
insertMany(object) {
79+
return this.Model.insertMany(object);
80+
}
81+
82+
countDocuments(query) {
83+
return this.Model.countDocuments(query);
84+
}
85+
86+
findWithPagination(query, pageNo, count, projection) {
87+
return this.Model.find(query, projection)
88+
.skip(pageNo * count)
89+
.limit(count)
90+
.lean()
91+
.exec();
92+
}
93+
}
94+
95+
export default BaseDao;

common/logger/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { createLogger, transports, format } = require('winston');
2+
3+
const logger = createLogger({
4+
format: format.combine(
5+
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss:ms' }),
6+
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
7+
),
8+
transports: [
9+
new transports.File({
10+
filename: './logs/all-logs.log',
11+
json: false,
12+
maxsize: 5242880,
13+
maxFiles: 5,
14+
}),
15+
new transports.Console(),
16+
]
17+
});
18+
19+
export default logger;

common/responseHandler/index.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
export const RESPONSE = {
2+
Ok: (data, response) => {
3+
return response.status(500).send({ data, status: 200 });
4+
},
5+
6+
Created: (data, response) => {
7+
return response.status(201).send({data , status:201});
8+
},
9+
10+
Unauthorized: (data, response) => {
11+
return response.status(400).send({data , status:401});
12+
},
13+
ServerError: (response) => {
14+
return response.status(500).send({ message: "Server error", status: 500 });
15+
},
16+
};
17+
18+
// "eslintConfig": {
19+
// "extends": "airbnb-base",
20+
// "env": {
21+
// "es6": true,
22+
// "browser": true
23+
// },
24+
25+
// "rules": {
26+
// "brace-style": [
27+
// "error",
28+
// "stroustrup"
29+
// ],
30+
// "comma-dangle": [
31+
// "error",
32+
// "never"
33+
// ],
34+
// "no-unused-vars": [
35+
// "warn"
36+
// ],
37+
// "no-var": [
38+
// "off"
39+
// ],
40+
// "one-var": [
41+
// "off"
42+
// ]
43+
// }
44+
// }

common/serverInitMethods/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const HelperMethods = {
2+
3+
4+
5+
}

common/validators/index.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import moment from "moment";
2+
3+
export const Valiadtors = {
4+
isValidEmail: (email) => {
5+
var pattern = /(([a-zA-Z0-9\-?\.?]+)@(([a-zA-Z0-9\-_]+\.)+)([a-z]{2,3}))+$/;
6+
return new RegExp(pattern).test(email);
7+
},
8+
9+
isValidPhoneNumber: (num) => {
10+
if (Number.isInteger(num)) {
11+
num = num.toString();
12+
}
13+
14+
let pattern = /^([\+0]91)?\-?[5-9]{1}[0-9]{9}$/g;
15+
return new RegExp(pattern).test(num);
16+
},
17+
18+
isValidZipCode: (zipcode) => {
19+
var pattern = /^\d{5}(-\d{4})?$/;
20+
return new RegExp(pattern).test(zipcode);
21+
},
22+
23+
convertToObjectIds: (_id) => {
24+
return mongoose.Types.ObjectId(_id);
25+
},
26+
27+
isMongoObjectId: (_id) => {
28+
return mongoose.Types.ObjectId.isValid(_id);
29+
},
30+
31+
isJson: (str) => {
32+
try {
33+
JSON.parse(str);
34+
} catch (e) {
35+
return false;
36+
}
37+
return true;
38+
},
39+
40+
isValidPhone: (phone, verifyCountryCode) => {
41+
var reExp = verifyCountryCode ? /^\+\d{6,16}$/ : /^\d{6,16}$/;
42+
return reExp.test(phone);
43+
},
44+
45+
getWeekOfYear: (params) => {
46+
params.currDate = Date.now();
47+
var year = new Date(parseInt(params.currDate)).getFullYear();
48+
var month = new Date(parseInt(params.currDate)).getMonth() + 1;
49+
var date = new Date(parseInt(params.currDate)).getDate();
50+
return moment(month + "-" + date + "-" + "-" + year, "MM-DD-YYYY").week();
51+
},
52+
53+
getMonthOfYear: (params) => {
54+
params.currDate = Date.now();
55+
var month = new Date(parseInt(params.currDate)).getMonth() + 1;
56+
return month;
57+
},
58+
59+
validatePincode: (pin) => {
60+
return /^(\d{4}|\d{6})$/.test(pin);
61+
},
62+
};

config/db.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import mongoose from "mongoose";
2+
3+
const connectDB = async () => {
4+
let uri = process.env.MONGO_URI;
5+
6+
try {
7+
await mongoose
8+
.connect(uri, {
9+
useNewUrlParser: true,
10+
useUnifiedTopology: true,
11+
useCreateIndex: true,
12+
})
13+
.catch((error) => {
14+
console.log("connection in error:::", error);
15+
});
16+
17+
console.log("mongo DB connected");
18+
19+
20+
mongoose.set('debug', function(collectionName, method, query, doc, options) {
21+
22+
console.log(collectionName + '.' + method, JSON.stringify(query), doc)
23+
24+
// log.info({
25+
// dbQuery: set
26+
// });
27+
});
28+
29+
} catch (e) {
30+
console.error(e, "error");
31+
} finally {
32+
// await client.close();
33+
}
34+
};
35+
36+
export default connectDB;

constants/index.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const CONSTANTS = {
2+
API_URI: "/api/v1",
3+
MESSAGES: {
4+
KEY_CANT_EMPTY: "{{key}} cannot be empty",
5+
KEY_LENGTH_EXCEEDED:
6+
"{{key}} length should cannot exceed {{length}} characters",
7+
INVALID_PINCODE: "Enter a valid Pincode having only 6 digits",
8+
KEY_MUST_BE_ARRAY: "{{key}} must be an array",
9+
INTERNAL_SERVER_ERROR: "Please try after some time.",
10+
TOKEN_GENERATE_EXCEPTION: "Error while generating access token.",
11+
INVALID_EMAIL: "Please fill valid email address.",
12+
INVALID_PASSWORD:
13+
"Password needs to have at least one lower case, one uppercase, one number, one special character, and must be at least 8 characters but no more than 35.",
14+
VALIDATION_ERROR: "Validation error.",
15+
UNAUTHORIZED_ACCESS_EXCEPTION: "Unauthorized access.",
16+
INVALID_PHONE: "Please fill valid phone number.",
17+
},
18+
};
19+
20+
export default CONSTANTS;

dev.dockerfile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM node:14.11.0-alpine3.10
2+
3+
WORKDIR /src/server/app
4+
5+
COPY package*.json ./
6+
7+
RUN npm install
8+
9+
COPY . .
10+
11+
CMD ["npm", "run", "dev"]
12+

logs/all-logs.log

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2021-08-07 08:16:35:1635 info: SERVER STARTED
2+
2022-01-03 14:02:14:214 info: SERVER STARTED
3+
2022-01-03 14:02:37:237 info: GET / 200 4.508 ms - 11
4+
2022-01-03 14:02:37:237 info: GET /favicon.ico 404 1.228 ms - 150
5+
2022-01-03 17:07:52:752 info: SERVER STARTED
6+
2022-01-29 19:49:08:498 info: SERVER STARTED
7+
2022-01-29 19:49:50:4950 info: SERVER STARTED
8+
2022-01-29 19:50:01:501 info: SERVER STARTED
9+
2022-01-29 19:51:08:518 info: SERVER STARTED
10+
2022-01-29 19:51:31:5131 info: SERVER STARTED
11+
2022-01-29 19:52:00:520 info: SERVER STARTED
12+
2022-01-29 19:53:30:5330 info: SERVER STARTED
13+
2022-01-29 19:53:39:5339 info: SERVER STARTED
14+
2022-01-29 19:54:20:5420 info: SERVER STARTED
15+
2022-01-29 19:54:40:5440 info: SERVER STARTED
16+
2022-01-29 19:55:04:554 info: SERVER STARTED
17+
2022-01-29 19:55:58:5558 info: SERVER STARTED
18+
2022-01-29 19:56:35:5635 info: SERVER STARTED
19+
2022-01-29 19:58:13:5813 info: GET / 200 466.382 ms - 11
20+
2022-01-29 19:59:07:597 info: POST /api/create 404 66.286 ms - 150
21+
2022-01-29 20:05:55:555 info: SERVER STARTED
22+
2022-01-29 20:05:58:558 info: POST /api/create 404 7.857 ms - 150
23+
2022-01-29 20:06:48:648 info: POST /api/create 404 1.931 ms - 150
24+
2022-01-29 20:07:36:736 info: SERVER STARTED
25+
2022-01-29 20:07:38:738 info: POST /api/create 404 13.310 ms - 150
26+
2022-01-29 20:08:08:88 info: POST /api/user/create 404 1.803 ms - 155
27+
2022-01-29 20:08:11:811 info: POST /api/user/create 404 0.869 ms - 155
28+
2022-01-29 20:08:49:849 info: POST /api/v1/user/create 500 146.326 ms - 32
29+
2022-01-29 20:10:30:1030 info: SERVER STARTED
30+
2022-01-29 20:10:45:1045 info: POST /api/v1/user/create 500 184.328 ms - 32
31+
2022-01-29 20:11:31:1131 info: SERVER STARTED
32+
2022-01-29 20:12:29:1229 info: SERVER STARTED
33+
2022-01-29 20:12:46:1246 info: SERVER STARTED
34+
2022-01-29 20:13:05:135 info: POST /api/v1/user/create 500 138.008 ms - 32
35+
2022-01-29 20:14:16:1416 info: SERVER STARTED
36+
2022-01-29 20:14:44:1444 info: POST /api/v1/user/create 500 159.961 ms - 32
37+
2022-01-29 20:14:58:1458 info: POST /api/v1/user/create 500 10.018 ms - 32
38+
2022-01-29 20:16:15:1615 info: SERVER STARTED
39+
2022-01-29 20:16:37:1637 info: SERVER STARTED
40+
2022-01-29 20:17:01:171 info: SERVER STARTED
41+
2022-01-29 20:17:08:178 info: POST /api/v1/user/create 500 115.099 ms - 32
42+
2022-01-29 20:20:31:2031 info: SERVER STARTED
43+
2022-01-29 20:20:40:2040 info: POST /api/v1/user/create 500 80.053 ms - 32
44+
2022-01-29 20:21:25:2125 info: SERVER STARTED
45+
2022-01-29 20:22:05:225 info: SERVER STARTED
46+
2022-01-29 20:22:06:226 info: POST /api/v1/user/create 500 98.743 ms - 32
47+
2022-01-29 20:28:15:2815 info: SERVER STARTED
48+
2022-01-29 20:28:31:2831 info: SERVER STARTED
49+
2022-01-29 20:28:43:2843 info: POST /api/v1/user/create 500 117.744 ms - 32
50+
2022-01-29 20:30:28:3028 info: SERVER STARTED
51+
2022-01-29 20:31:13:3113 info: SERVER STARTED
52+
2022-01-29 20:31:25:3125 info: SERVER STARTED
53+
2022-01-29 20:31:37:3137 info: SERVER STARTED
54+
2022-01-29 20:33:02:332 info: SERVER STARTED
55+
2022-02-01 11:18:40:1840 info: SERVER STARTED
56+
2022-02-01 11:18:43:1843 info: POST /api/v1/user/create 200 178.518 ms - 32
57+
2022-02-01 11:24:48:2448 info: SERVER STARTED
58+
2022-02-01 11:26:02:262 info: GET / 200 155.340 ms - 11
59+
2022-02-01 11:26:54:2654 info: POST / 404 22.235 ms - 140

middleware/auth.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const jwt = require("jsonwebtoken");
2+
const config = require("config");
3+
4+
module.exports = (req, res, next) => {
5+
const token = req.header("x-auth-token");
6+
7+
if (!token) {
8+
return res.status(401).json({
9+
message: "User Unauthorised",
10+
});
11+
}
12+
13+
try {
14+
const decoded = jwt.verify(token, config.get("jwtSecretKey"));
15+
16+
req.user = decoded.user;
17+
next();
18+
} catch (e) {
19+
return res.status(401).json({
20+
message: "Server Error",
21+
});
22+
}
23+
};

0 commit comments

Comments
 (0)