Skip to content

Commit

Permalink
Merge branch 'demo-bn-19-07' into ft-chat
Browse files Browse the repository at this point in the history
  • Loading branch information
sevelinCa authored Jul 20, 2024
2 parents da839b9 + f85074f commit 516da73
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 31 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions src/controllers/orderController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response } from "express";
import Order from "../database/models/order";
import { findVendorByUserId } from "../services/orderStatus";
import Product from "../database/models/product";

const allowedStatuses = ["pending", "delivered", "cancelled"];

Expand Down Expand Up @@ -61,6 +62,42 @@ export const modifyOrderStatus = async (req: Request, res: Response) => {
};



export const getAllOrders = async (req:Request, res: Response) => {
try{
const userId = (req as any).token.id;
const vendorId = req.params.vendorId;

let orders: any;

if(vendorId){
const allOrders: any = await Order.findAll({
include: [{ model: Product, as: 'products' }]
});

if(allOrders.length === 0){
return res.status(404).json({ message: "No orders found" });
}

orders = allOrders.filter(order =>
order.products.some(product => product.vendorId === vendorId)
)


} else {
orders = await Order.findAll({ where: { userId }})

}


return res.status(200).json(orders);
} catch(error: any){
console.error(error);
return res.status(500).json({ error: error.message})
}
}


export const getAllOrder = async (req: Request, res: Response) => {
try {
const response = await Order.findAll();
Expand All @@ -86,4 +123,30 @@ export const getOrder = async(req: Request, res: Response) => {
}
}

export const getSellerOrder = async (req: Request, res: Response) => {
try {
const vendorId = req.params.vendorId;
const orders: any = await Order.findAll();
if (!orders) {
return res.status(404).json({ message: "No order found" });
}

const products: any[] = [];
for (const order of orders) {
for (const data of order.products) {
const single_product = await Product.findOne({
where: { productId: data.productId },
});
if (single_product?.vendorId === vendorId) {
products.push(order);
}
}
}

res.status(200).send(products);
} catch (error) {
console.log(error);
res.status(500).json({ message: "Internal error server" });
}
};

30 changes: 23 additions & 7 deletions src/controllers/product.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { Request, Response } from "express";

import { saveProduct, searchProducts, getAllProducts, getProductById, fetchSimilarProducts } from "../services/productService";
import {
saveProduct,
searchProducts,
getAllProducts,
} from "../services/productService";
import Product from "../database/models/product";
import { checkVendorModifyPermission, checkVendorPermission } from "../services/PermisionService";
import { PRODUCT_ADDED, PRODUCT_REMOVED, PRODUCT_UPDATED, productLifecycleEmitter } from "../helpers/events";
import CartItem from "../database/models/cartitem";
import {
checkVendorModifyPermission,
checkVendorPermission,
} from "../services/PermisionService";
import {
PRODUCT_ADDED,
PRODUCT_REMOVED,
PRODUCT_UPDATED,
productLifecycleEmitter,
} from "../helpers/events";
import { Op } from "sequelize";
import { ParsedQs } from "qs";
import Vendor from "../database/models/vendor";
import { request } from "http";
import CartItem from "../database/models/cartitem";
import { getProductById } from "../services/productService";
import { fetchSimilarProducts } from "../services/productService";


export const createProduct = async (req: Request, res: Response) => {
try {
Expand Down Expand Up @@ -57,7 +73,7 @@ export const createProduct = async (req: Request, res: Response) => {
if (!save) {
return res.status(500).json({ error: "Failed to save data" });
}

productLifecycleEmitter.emit(PRODUCT_ADDED, data);

return res.status(201).json({ message: "Product Created", data: save });
Expand Down Expand Up @@ -93,7 +109,7 @@ export const similarProducts = async (req: Request, res: Response) => {

return res.status(404).json({ error: "Product not found" });
}
console.log("hhhhhggggggggghhhhh",product)

const category = product.category;
const similarProducts = await fetchSimilarProducts(productId, category);

Expand Down
3 changes: 2 additions & 1 deletion src/controllers/roles.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export const approveVendor = async (req: Request, res: Response) => {
// Reject Vendor's Request
export const rejectVendor = async (req: Request, res: Response) => {
const { userId } = req.params;
const { message } = req.body;
try {
const result = await rejectVendorRequest(userId);
const result = await rejectVendorRequest(userId, message);
res.status(result.status).json({ message: result.message });
} catch (error) {
return res.status(500).json({ message: "Internal Server Error", error });
Expand Down
35 changes: 35 additions & 0 deletions src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,41 @@ export const deleteUser = async (req: Request, res: Response) => {
}
};


export const findUser = async (req: Request, res: Response) => {
const userId = req.params.id;
try {
const user = await User.findOne({where: {userId: userId}});
res.status(200).json(user);
} catch (error: any) {
if (error.message === "user not found") {
res.status(404).json({ error: "User not found" });
} else {
res.status(500).json({ error: "Internal server error" });
}
}
};

export const allUsers = async (req: Request, res: Response) => {
try {
const users = await User.findAll()
res.status(200).json(users);
} catch (error: any) {
res.status(500).json({ error: error.message });
}
}


export const allVendors = async (req: Request, res: Response) => {
try {
const sellers = await User.findAll({where: {role: 'vendor'}})
res.status(200).json(sellers);
} catch (error: any) {
res.status(500).json({ error: error.message });
}
}


export const editUser = async (req: Request, res: Response) => {
const { name, email, profile } = req.body;
const userId = req.params.id;
Expand Down
19 changes: 19 additions & 0 deletions src/controllers/vendor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,22 @@ export const vendorOrder = async (req: Request, res: Response) => {
res.status(500).json({ error: "Internal Server Error" });
}
};

export const allRequests = async (req: Request, res: Response) => {
try {
const sellers = await Vendor.findAll({where: {status: 'pending'}})
res.status(200).json(sellers);
} catch (error: any) {
res.status(500).json({ error: error.message });
}
}

export const allStores = async (req: Request, res: Response) => {
try {
const stores = await Vendor.findAll({ where: { status: 'approved' } })
res.status(200).json(stores)

} catch (error:any) {
res.status(500).json({error: error.message})
}
}
1 change: 0 additions & 1 deletion src/database/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ const config = {
};

module.exports = config;

1 change: 1 addition & 0 deletions src/database/config/db.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const connectSequelize: Sequelize = new Sequelize(getURL(), {
logging: false,
});


export default connectSequelize;


1 change: 0 additions & 1 deletion src/database/seeders/20240522075149-seed-orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ module.exports = {
productName: product.name,
status: "pending",
quantity: 3,

},
]),
createdAt: new Date(),
Expand Down
27 changes: 19 additions & 8 deletions src/helpers/generateToken.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import jwt from 'jsonwebtoken';
import User from '../database/models/user';
import dotenv from 'dotenv';
import Vendor from '../database/models/vendor';

dotenv.config();

const generateToken = async (userData: User) => {
return jwt.sign({
const vendor = await Vendor.findOne({
where: { userId: userData?.userId },
});
const vendorId = vendor ? vendor.vendorId : null;

return jwt.sign(
{
role: userData.role,
email: userData.email,
id: userData.userId,
password:userData.password
}, "crafters1234", {
expiresIn: '1d'
});
}

export { generateToken };
vendor: vendorId,
password: userData.password,
},
"crafters1234",
{
expiresIn: "1d",
}
);
};

export { generateToken };


5 changes: 1 addition & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ app.use(cors({
credentials: true
}
));

app.use(cookieParser());
app.use((req, res, next) => {
if (req.originalUrl === '/webhook') {
Expand Down Expand Up @@ -103,16 +104,12 @@ app.use("/admin", adminRoute);
app.use("/", cartroute);
app.use("/", wishlistroute);
app.use("/", TwoFaRoute);

app.use("/", chatRouter);

app.use('/', messageRoutes);
app.use("/", messageRoutes);

app.use("/", analyticRoute);



cron.schedule("0 0 * * *", () => {
checkExpiredProducts();
});
Expand Down
5 changes: 5 additions & 0 deletions src/routes/order.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import {
getAllOrder,
modifyOrderStatus,
getOrder,
getAllOrders,
getSellerOrder
} from "../controllers/orderController";
import { VerifyAccessToken } from "../middleware/verfiyToken";
import {
getOrderStatus,
updateOrderStatus,
} from "../controllers/orderStatus.controller";

const router = express.Router();

router.put(
Expand All @@ -26,5 +29,7 @@ router.put(

router.get("/order/getAllOrder", getAllOrder);
router.get("/order/getOrder/:orderId", getOrder);
router.get("/orders", VerifyAccessToken, getAllOrders);
router.get("/order/getSellerOrder/:vendorId", getSellerOrder);

export default router;
5 changes: 3 additions & 2 deletions src/routes/product.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
createProduct,
readAllProducts,
viewProducts,

similarProducts,
getPopularProduct
getPopularProduct

} from "../controllers/product.controller";
import { VerifyAccessToken } from "../middleware/verfiyToken";

const router = express.Router();

router.post("/create/product/:id", VerifyAccessToken, createProduct);
Expand All @@ -25,6 +25,7 @@ router.get("/products/search", searchProduct);
router.get("/products/vendor/:id", VerifyAccessToken, viewProducts);
router.put("/updateProduct/:id", VerifyAccessToken, updateProduct);
router.delete("/deleteProduct/:id", VerifyAccessToken, deleteProduct);
router.get("/popular-product", getPopularProduct)


export default router;
6 changes: 6 additions & 0 deletions src/routes/user.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { twoFAController } from "./../middleware/2fa.middleware";
import express,{Request,Response} from "express";
import {
Welcome,
allUsers,
allVendors,
deleteUser,
editUser,
findUser,
login,
register,
updatePassword,
Expand All @@ -20,6 +23,9 @@ const route = express.Router();

route.get("/", Welcome);

route.get("/finduser/:id", findUser)
route.get("/allusers", allUsers)
route.get("/allvendors", allVendors)
route.post("/register", register);
route.patch("/updateuser/:id", editUser);
route.patch("/updatepassword/:id", updatePassword);
Expand Down
6 changes: 4 additions & 2 deletions src/routes/vendor.route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import express from "express"
const route = express.Router()
import { deletingVendor, editVendor, registerVendor, selectVendorInfo, vendorOrder } from "../controllers/vendor.controller"

import { allRequests, allStores, deletingVendor, editVendor, registerVendor,selectVendorInfo, vendorOrder } from "../controllers/vendor.controller"
import { viewProducts } from "../controllers/product.controller"
import { VerifyAccessToken } from "../middleware/verfiyToken"
import { selectFeedback, selectReview, view_vendor_feedback } from "../controllers/review.controller"


route.get('/allstores', allStores)
route.get('/allrequests', allRequests)
route.post('/requestVendor', registerVendor)
route.delete('/deleteVendor/:id',VerifyAccessToken,deletingVendor)
route.get('/vendorProduct/:id', viewProducts)
Expand Down
Loading

0 comments on commit 516da73

Please sign in to comment.