Initial Version

This commit is contained in:
Ruslan845
2026-03-10 03:45:00 +09:00
commit 2c4fc7f933
128 changed files with 7617 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
class APIFeatures {
constructor(query, queryStr) {
this.query = query;
this.queryStr = queryStr;
}
search() {
const keyword = this.queryStr.keyword
? {
name: {
$regex: this.queryStr.keyword,
$options: "i",
},
}
: {};
this.query = this.query.find({ ...keyword });
return this;
}
filter() {
const queryCopy = { ...this.queryStr };
// Removing fields from the query
const removeFields = ["keyword", "limit", "page"];
removeFields.forEach((el) => delete queryCopy[el]);
// Advance filter for price, ratings etc
let queryStr = JSON.stringify(queryCopy);
queryStr = queryStr.replace(
/\b(gt|gte|lt|lte)\b/g,
(match) => `$${match}`
);
this.query = this.query.find(JSON.parse(queryStr));
return this;
}
pagination(resPerPage) {
const currentPage = Number(this.queryStr.page) || 1;
const skip = resPerPage * (currentPage - 1);
this.query = this.query.limit(resPerPage).skip(skip);
return this;
}
}
module.exports = APIFeatures;

View File

@@ -0,0 +1,10 @@
class ErrorHandler extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode
Error.captureStackTrace(this, this.constructor);
}
}
module.exports = ErrorHandler;

21
server/utils/jwtToken.js Normal file
View File

@@ -0,0 +1,21 @@
// Create and send token and save in the cookie.
const sendToken = (user, statusCode, res) => {
// Create Jwt token
const token = user.getJwtToken();
// Options for cookie
const options = {
expires: new Date(
Date.now() + process.env.COOKIE_EXPIRES_TIME * 24 * 60 * 60 * 1000
),
httpOnly: true,
};
res.status(statusCode).cookie("token", token, options).json({
success: true,
token,
user,
});
};
module.exports = sendToken;

View File

@@ -0,0 +1,51 @@
class SearchFeatures {
constructor(query, queryString) {
this.query = query
this.queryString = queryString
}
search() {
const keyword = this.queryString.keyword ? {
name: {
$regex: this.queryString.keyword,
$options: "i",
}
} : {};
// console.log(keyword);
this.query = this.query.find({ ...keyword });
return this;
}
filter() {
const queryCopy = { ...this.queryString }
// fields to remove for category
const removeFields = ["keyword", "page", "limit"];
// console.log(queryCopy);
removeFields.forEach(key => delete queryCopy[key]);
// console.log(queryCopy);
// price filter
let queryString = JSON.stringify(queryCopy);
queryString = queryString.replace(/\b(gt|gte|lt|lte)\b/g, key => `$${key}`);
// console.log(JSON.parse(queryString));
this.query = this.query.find(JSON.parse(queryString));
return this;
}
pagination(resultPerPage) {
const currentPage = Number(this.queryString.page) || 1;
const skipProducts = resultPerPage * (currentPage - 1);
this.query = this.query.limit(resultPerPage).skip(skipProducts);
return this;
}
};
module.exports = SearchFeatures;

36
server/utils/sendEmail.js Normal file
View File

@@ -0,0 +1,36 @@
// const nodeMailer = require('nodemailer');
const sendEmail = async (options) => {
// const transporter = nodeMailer.createTransport({
// host: process.env.SMTP_HOST,
// port: process.env.SMTP_PORT,
// service: process.env.SMTP_SERVICE,
// auth: {
// user: process.env.SMTP_MAIL,
// pass: process.env.SMTP_PASSWORD,
// },
// });
// const mailOptions = {
// from: process.env.SMTP_MAIL,
// to: options.email,
// subject: options.subject,
// html: options.message,
// };
// await transporter.sendMail(mailOptions);
// const msg = {
// to: options.email,
// from: process.env.SENDGRID_MAIL,
// templateId: options.templateId,
// dynamic_template_data: options.data,
// }
// sgMail.send(msg).then(() => {
// console.log('Email Sent')
// }).catch((error) => {
// console.error(error)
// });
};
module.exports = sendEmail;

18
server/utils/sendToken.js Normal file
View File

@@ -0,0 +1,18 @@
const sendToken = (user, statusCode, res) => {
const token = user.getJWTToken();
const options = {
expires: new Date(
Date.now() + process.env.COOKIE_EXPIRE * 24 * 60 * 60 * 1000
),
httpOnly: true
}
res.status(statusCode).cookie('token', token, options).json({
success: true,
user,
token,
});
}
module.exports = sendToken;