Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:atlp-rwanda/e-commerce-ninjas-bn…
Browse files Browse the repository at this point in the history
… into fixes
  • Loading branch information
Aime-Patrick committed Aug 25, 2024
2 parents aa86fd2 + ac63cf4 commit 0e92648
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 110 deletions.
32 changes: 20 additions & 12 deletions src/databases/migrations/20240704115209-create-termsAndCondition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ export default {
up: async (queryInterface: QueryInterface) => {
await queryInterface.createTable("termsAndConditions", {

id: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true,
defaultValue: DataTypes.UUIDV4
},
content: {
id: {
type: DataTypes.UUID,
allowNull: false,
type: DataTypes.STRING
},
type: {
primaryKey: true,
defaultValue: DataTypes.UUIDV4
},
content: {
allowNull: true,
type: DataTypes.TEXT
},
type: {
type: DataTypes.STRING,
allowNull: true
},
allowNull: false
},
pdfUrl: {
type: DataTypes.STRING,
allowNull: true,
unique: true,
validate: {
isUrl: true
}
},
createdAt: {
allowNull: false,
type: DataTypes.DATE,
Expand Down
16 changes: 13 additions & 3 deletions src/databases/models/termsAndCodition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ export interface ITermsAndConditions {
id: string;
content:string;
type: string;
pdfUrl: string;
}
class TermsAndConditions extends Model<ITermsAndConditions> implements ITermsAndConditions {
declare id: string;
declare content: string;
declare type: string;
declare pdfUrl: string;

static associate() {

Expand All @@ -29,12 +31,20 @@ TermsAndConditions.init(
defaultValue: DataTypes.UUIDV4
},
content: {
allowNull: false,
type: DataTypes.STRING,
allowNull: true,
type: DataTypes.TEXT,
},
type: {
type: DataTypes.STRING,
allowNull: true
allowNull: false
},
pdfUrl:{
type: DataTypes.STRING,
allowNull: true,
unique: true,
validate:{
isUrl: true
}
}
},
{
Expand Down
8 changes: 4 additions & 4 deletions src/databases/seeders/20240520202759-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const userFour = {
passwordUpdatedAt: new Date(),
firstName: "F Seller",
lastName: "L Seller",
email: "seller@gmail.com",
email: "aimegetz@gmail.com",
password: hashPassword("Password@123"),
phone: 25089767099,
profilePicture: "https://res.cloudinary.com/djrmfg6k9/image/upload/v1720294521/cce1ffu7uw3j2vg9s2vl.jpg",
Expand All @@ -109,7 +109,7 @@ const userFive = {
passwordUpdatedAt: new Date(),
firstName: "dj5090",
lastName: "dj2090",
email: "dj@gmail.com",
email: "jadowacu@gmail.com",
password: hashPassword("Password@123"),
phone: 25089767899,
profilePicture: "https://res.cloudinary.com/djrmfg6k9/image/upload/v1720294521/cce1ffu7uw3j2vg9s2vl.jpg",
Expand All @@ -130,7 +130,7 @@ const userSix = {
passwordUpdatedAt: new Date(),
firstName: "F Seller3",
lastName: "L Seller3",
email: "seller3@gmail.com",
email: "ndahimana154@gmail.com",
password: hashPassword("Password@123"),
phone: 25089767899,
profilePicture: "https://res.cloudinary.com/djrmfg6k9/image/upload/v1720294521/cce1ffu7uw3j2vg9s2vl.jpg",
Expand All @@ -151,7 +151,7 @@ const userSeven = {
passwordUpdatedAt: new Date(),
firstName: "F Seller4",
lastName: "L Seller4",
email: "seller4@gmail.com",
email: "ijbapte@gmail.com",
password: hashPassword("Password@123"),
phone: 25089767899,
profilePicture: "https://res.cloudinary.com/djrmfg6k9/image/upload/v1720294521/cce1ffu7uw3j2vg9s2vl.jpg",
Expand Down
30 changes: 16 additions & 14 deletions src/databases/seeders/20240601224834-shops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,60 @@ import { shopFiveId, shopFourId, shopOneId, shopSixId, shopThreeId, shopTwoId, u

const shopOne = {
id: shopOneId,
name: "Paccy Shop 250",
name: "GadgetHub 250",
userId: userFourId,
description: "Selling",
description: "Your one-stop shop for the latest gadgets and electronics.",
createdAt: new Date(),
updatedAt: new Date()
}

const shopTwo = {
id: shopTwoId,
name: "Paccy Shop 509",
name: "UrbanStyle Boutique",
userId: userSevenId,
description: "Selling",
description: "Bringing you the trendiest fashion and accessories in town.",
createdAt: new Date(),
updatedAt: new Date()
}

const shopThree = {
id: shopThreeId,
name: "Shoes Shop 509",
name: "SoleMates",
userId: userFourTeenId,
description: "Selling",
description: "Premium footwear for every step of your journey.",
createdAt: new Date(),
updatedAt: new Date()
}

const shopFour = {
id: shopFourId,
name: "electronic Shop 509",
name: "TechNest",
userId: userSixId,
description: "Selling",
description: "Explore a world of cutting-edge electronics and accessories.",
createdAt: new Date(),
updatedAt: new Date()
}

const shopFive = {
id: shopFiveId,
name: "Shop 509",
name: "HomeEssentials",
userId: userFiveId,
description: "Selling",
description: "Everything you need to make your house a home.",
createdAt: new Date(),
updatedAt: new Date()
}

const shopSix = {
id: shopSixId,
name: "electronics Shop 509",
name: "ElectroMart",
userId: userFiveTeenId,
description: "Selling",
description: "Your trusted source for all things electronic.",
createdAt: new Date(),
updatedAt: new Date()
}


export const up = async (queryInterface: QueryInterface) => {
await queryInterface.bulkInsert("shops", [shopOne, shopTwo,shopThree,shopFour, shopFive, shopSix]);
await queryInterface.bulkInsert("shops", [shopOne, shopTwo, shopThree, shopFour, shopFive, shopSix]);
};

export const down = async (queryInterface: QueryInterface) => {
Expand Down
21 changes: 4 additions & 17 deletions src/middlewares/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { decodeToken } from "../helpers";
import Session from "../databases/models/sessions";
import { Socket } from "socket.io"
import { ExtendedError } from "socket.io/dist/namespace"
import cookie from "cookie";


interface ExtendedRequest extends Request {
Expand All @@ -19,7 +18,7 @@ interface ExtendedRequest extends Request {
export const userAuthorization = function (roles: string[]) {
return async (req: ExtendedRequest, res: Response, next: NextFunction) => {
try {
const token = req.cookies.token;
const token = req.headers["authorization"]?.split(" ")[1];

if (!token) {
return res
Expand Down Expand Up @@ -71,19 +70,7 @@ export const userAuthorization = function (roles: string[]) {

export const socketAuthMiddleware = async (socket: Socket, next: NextFunction) => {
try {
// Extract the cookie from the socket handshake headers
const cookies = socket.handshake.headers.cookie;

if (!cookies) {
const err = new Error("Authentication error") as ExtendedError;
err.data = { message: "No cookies found" };
return next(err);
}

// Parse the cookies and extract the token
const parsedCookies = cookie.parse(cookies);
const token = parsedCookies.token; // Adjust 'token' to match the cookie name where the token is stored

const token = socket.handshake.auth.token;
if (!token) {
const err = new Error("Authentication error") as ExtendedError;
err.data = { message: "No token provided" };
Expand Down Expand Up @@ -120,7 +107,7 @@ export const socketAuthMiddleware = async (socket: Socket, next: NextFunction) =
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
role: user.role,
role:user.role,
profilePicture: user.profilePicture,
};

Expand All @@ -130,4 +117,4 @@ export const socketAuthMiddleware = async (socket: Socket, next: NextFunction) =
err.data = { message: "Internal server error" };
return next(err);
}
};
};
12 changes: 0 additions & 12 deletions src/modules/auth/controller/authControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,6 @@ const loginUser = async (req: any, res: Response) => {
otp: null
};
await authRepositories.createSession(session);
res.cookie('token', token, {
httpOnly: process.env.NODE_ENV === 'production' ? true : false,
secure: process.env.NODE_ENV === 'production' ? true : false,
sameSite: process.env.NODE_ENV === 'production' ? 'strict' : 'lax',
maxAge: 3600000
});
res
.status(httpStatus.OK)
.json({ message: "Logged in successfully", data: { token } });
Expand All @@ -175,12 +169,6 @@ const logoutUser = async (req: any, res: Response) => {
"token",
req.session.token
);
res.cookie('token', "", {
httpOnly: process.env.NODE_ENV === 'production' ? true : false,
secure: process.env.NODE_ENV === 'production' ? true : false,
sameSite: process.env.NODE_ENV === 'production' ? 'strict' : 'lax',
expires: new Date(0)
});
res.status(httpStatus.OK).json({ status: httpStatus.OK, message: "Successfully logged out" });
} catch (err) {
return res
Expand Down
1 change: 0 additions & 1 deletion src/modules/auth/repository/authRepositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Op } from "sequelize";
import db from "../../../databases/models";

const createUser = async (body: any) => {
console.log("body" + JSON.stringify(body))
return await db.Users.create(body);
};

Expand Down
5 changes: 2 additions & 3 deletions src/modules/auth/test/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,11 +882,10 @@ describe("updateUser2FA", () => {
router()
.post("/api/auth/login")
.send({
email: "seller4@gmail.com",
email: "ijbapte@gmail.com",
password: "Password@123"
})
.end((error, response) => {
console.log(response)
token = response.body.data.token;
done(error);
});
Expand Down Expand Up @@ -1029,7 +1028,7 @@ describe("verifyUserCredentials Middleware", () => {
router()
.post("/api/auth/login")
.send({
email: "seller4@gmail.com",
email: "ijbapte@gmail.com",
password: "Password@123"
})
.end((error, response) => {
Expand Down
28 changes: 27 additions & 1 deletion src/modules/product/controller/productController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,31 @@ const sellerGetOrdersHistory = async(req: ExtendRequest, res:Response)=>{
})
}

const getProductsByShopId = async (req: ExtendRequest, res: Response) => {
try {
const products = await productRepositories.getProductsByShopId(req.params.id);

if (products.length === 0) {
return res.status(httpStatus.OK).json({
status: httpStatus.OK,
message: "No products found for this shop",
data: { products: [] },
});
}

res.status(httpStatus.OK).json({
status: httpStatus.OK,
message: "Products fetched successfully",
data: { products },
});
} catch (error) {
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({
status: httpStatus.INTERNAL_SERVER_ERROR,
message: error.message,
});
}
};

export {
sellerCreateProduct,
sellerCreateShop,
Expand All @@ -460,5 +485,6 @@ export {
buyerViewWishListProducts,
buyerDeleteWishListProduct,
adminGetShops,
sellerGetOrdersHistory
sellerGetOrdersHistory,
getProductsByShopId
};
7 changes: 6 additions & 1 deletion src/modules/product/repositories/productRepositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ const sellerGetOrdersHistory = async (shopId: string) => {
return db.Orders.findAll({ where: { shopId } });
};

const getProductsByShopId = async (shopId: string) => {
return await db.Products.findAll({ where: { shopId } });
};

export default {
createProduct,
updateProduct,
Expand Down Expand Up @@ -280,7 +284,8 @@ export default {
removeWishList,
userCreateReview,
findSingleProductById,
sellerGetOrdersHistory
sellerGetOrdersHistory,
getProductsByShopId
};


8 changes: 4 additions & 4 deletions src/modules/product/test/product.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ describe("Product and Shops API Tests", () => {
before((done) => {
router()
.post("/api/auth/login")
.send({ email: "dj@gmail.com", password: "Password@123" })
.send({ email: "jadowacu@gmail.com", password: "Password@123" })
.end((err, res) => {
console.log(res);
token = res.body.data.token;
done(err);
});
Expand Down Expand Up @@ -399,7 +400,7 @@ describe("Seller test cases", () => {
before((done) => {
router()
.post("/api/auth/login")
.send({ email: "seller@gmail.com", password: "Password@123" })
.send({ email: "aimegetz@gmail.com", password: "Password@123" })
.end((err, res) => {
token = res.body.data.token;
done(err);
Expand Down Expand Up @@ -465,7 +466,6 @@ describe("Seller test cases", () => {
// // description: "A new Shops description",
// // })
// // .end((err, res) => {
// // console.log(res)
// // expect(res).to.have.status(httpStatus.INTERNAL_SERVER_ERROR);
// // expect(res.body).to.have.property("message");
// // done(err);
Expand Down Expand Up @@ -601,7 +601,7 @@ describe("Product Controller", () => {
before((done) => {
router()
.post("/api/auth/login")
.send({ email: "seller3@gmail.com", password: "Password@123" })
.send({ email: "ndahimana154@gmail.com", password: "Password@123" })
.end((err, res) => {
token = res.body.data.token;
done(err);
Expand Down
Loading

0 comments on commit 0e92648

Please sign in to comment.