Skip to content

Commit

Permalink
Ft submit seller request 187904713 (#87)
Browse files Browse the repository at this point in the history
* initial commit

* Delivers-[ft-submit-seller-request-#187904713]

* Delivers[ft-admin-get-seller-requests-#187904754]
  • Loading branch information
hbapte authored Jul 9, 2024
1 parent 8b3e71d commit 7a79e7c
Show file tree
Hide file tree
Showing 17 changed files with 568 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ DOCKER_DATABASE_PASSWORD=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=

PASSWORD_EXPIRATION_DAYS=
PASSWORD_EXPIRATION_DAYS=
ADMIN_EMAIL=
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Our e-commerce web application server, developed by Team Ninjas, facilitates smo
- User mark All notifications Endpoint
- User mark notification Endpoint
- Buyer review product Endpoint
- Submit a seller request

## TABLE OF API ENDPOINTS SPECIFICATION AND DESCRIPTION

Expand Down Expand Up @@ -117,8 +118,8 @@ Our e-commerce web application server, developed by Team Ninjas, facilitates smo
| 42 | GET | /api/user/user-get-notification/:id | 200 OK | private | user get notification |
| 43 | PUT | /api/user/user-mark-all-notifications | 200 OK | private | user mark all notifications |
| 44 | PUT | /api/user/user-mark-notification/:id | 200 OK | private | user mark notification |
| 45 | POST | /api/shop/buyer-review-product/:id | 200 OK | private | Buyer Create review

| 45 | POST | /api/shop/buyer-review-product/:id | 200 OK | private | Buyer Create review |
| 46 | POST | /api/user/user-submit-seller-request | 200 OK | private | Submit a seller request |

## INSTALLATION

Expand Down
44 changes: 44 additions & 0 deletions src/databases/migrations/20240704115208-create-sellerRequests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-disable comma-dangle */
import { QueryInterface, DataTypes } from "sequelize";

export = {
up: async (queryInterface: QueryInterface) => {
await queryInterface.createTable("sellerRequests", {
id: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
},
userId: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: "users",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
requestStatus: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: "Pending",
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
});
},

down: async (queryInterface: QueryInterface) => {
await queryInterface.dropTable("sellerRequests");
},
};
4 changes: 3 additions & 1 deletion src/databases/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import wishLists from "./wishLists";
import Notifications from "./notifications";
import ProductReviews from "./productReviews";
import wishListProducts from "./wishListProducts";
import SellerRequest from "./sellerRequests";

const db = {
CartProducts,
Expand All @@ -23,7 +24,8 @@ const db = {
wishLists,
Notifications,
ProductReviews,
wishListProducts
wishListProducts,
SellerRequest
};

Object.values(db).forEach(model => {
Expand Down
68 changes: 68 additions & 0 deletions src/databases/models/sellerRequests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable quotes */
/* eslint-disable comma-dangle */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable require-jsdoc */
import { Model, DataTypes, Optional } from "sequelize";
import sequelizeConnection from "../config/db.config";
import Users from "./users";

export interface SellerRequestAttributes {
id: string;
userId: string;
requestStatus: string;
createdAt?: Date;
updatedAt?: Date;
}

export interface SellerRequestCreationAttributes extends Optional<SellerRequestAttributes, "id"> {}

class SellerRequest extends Model<SellerRequestAttributes, SellerRequestCreationAttributes> implements SellerRequestAttributes {
declare id: string;
declare userId: string;
declare requestStatus: string;
declare createdAt?: Date;
declare updatedAt?: Date;

static associate() {
SellerRequest.belongsTo(Users, { foreignKey: "userId", as: "user" });
}
}

SellerRequest.init(
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
userId: {
type: DataTypes.UUID,
allowNull: false,
},
requestStatus: {
type: DataTypes.STRING(128),
allowNull: false,
defaultValue: "Pending",
},
createdAt: {
field: "createdAt",
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
updatedAt: {
field: "updatedAt",
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
},
{
sequelize: sequelizeConnection,
tableName: "sellerRequests",
timestamps: true,
modelName: "SellerRequest",
}
);

export default SellerRequest;
2 changes: 2 additions & 0 deletions src/databases/models/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { hashPassword } from "../../helpers";
import Sessions from "./sessions";
import Shops from "./shops";
import Notifications from "./notifications";
import SellerRequest from "./sellerRequests";
export interface usersAttributes {
id: string;
firstName?: string;
Expand Down Expand Up @@ -56,6 +57,7 @@ class Users extends Model<usersAttributes, UsersCreationAttributes> implements u
Users.hasOne(Sessions, { foreignKey: "userId", as: "sessions" });
Users.hasOne(Shops, { foreignKey: "userId", as: "shops" });
Users.hasMany(Notifications, { foreignKey: "userId", as: "notifications" });
Users.hasMany(SellerRequest, { foreignKey: "userId", as: "sellerRequests" });
}
}

Expand Down
28 changes: 26 additions & 2 deletions src/databases/seeders/20240520202759-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
userEightId,
userNineId,
userTenId,
userElevenId
userElevenId,
userTwelveId
} from "../../types/uuid";

const userOne = {
Expand Down Expand Up @@ -243,6 +244,28 @@ const userEleven = {
is2FAEnabled: false,
};

const userTwelve = {
id: userTwelveId,
createdAt: new Date(),
updatedAt: new Date(),
passwordUpdatedAt: new Date(),
firstName: "New",
lastName: "Admin",
email: "[email protected]",
password: hashPassword("AdminPassword@123"),
phone: 25089767999,
profilePicture: "",
gender: "male",
birthDate: "1-1-1980",
language: "english",
currency: "USD",
role: "admin",
status: "enabled",
isVerified: true,
is2FAEnabled: false,
};


export const up = (queryInterface: QueryInterface) =>
queryInterface.bulkInsert("users", [
userOne,
Expand All @@ -255,7 +278,8 @@ export const up = (queryInterface: QueryInterface) =>
userEight,
userNine,
userTen,
userEleven
userEleven,
userTwelve
]);

export const down = async (queryInterface: QueryInterface) => {
Expand Down
48 changes: 47 additions & 1 deletion src/middlewares/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const currentDate = new Date();

import cartRepositories from "../modules/cart/repositories/cartRepositories";
import db from "../databases/models";
import userRepositories from "../modules/user/repository/userRepositories";

const validation =
(schema: Joi.ObjectSchema | Joi.ArraySchema) =>
Expand Down Expand Up @@ -821,7 +822,6 @@ const isNotificationsExist = async (req: Request, res: Response, next: NextFunct
const isProductOrdered = async (req: ExtendRequest,res: Response,next: NextFunction) => {
try {
const cart = await cartRepositories.getCartsByProductId(req.params.id, req.user.id);
console.log("Order info: ",cart);
if (!cart) {
return res
.status(httpStatus.NOT_FOUND)
Expand All @@ -846,6 +846,50 @@ const isProductOrdered = async (req: ExtendRequest,res: Response,next: NextFunct
}
};

const isUserProfileComplete = async (req: Request, res: Response, next: NextFunction) => {
try {
const userId = req.user.id;
const user = await userRepositories.findUserById(userId);

const requiredFields = ["firstName", "lastName", "email", "phone", "gender", "birthDate", "language", "currency"];
const isProfileComplete = requiredFields.every(field => user[field]);

if (!isProfileComplete) {
return res.status(httpStatus.BAD_REQUEST).json({
status: httpStatus.BAD_REQUEST,
message: "User profile is incomplete. Please fill out all required fields.",
});
}

next();
} catch (error) {
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({
status: httpStatus.INTERNAL_SERVER_ERROR,
error: error.message,
});
}
};

const isSellerRequestExist = async (req: Request, res: Response, next: NextFunction) => {
try {
const userId = req.user.id;
const existingRequest = await userRepositories.findSellerRequestByUserId(userId);

if (existingRequest) {
return res.status(httpStatus.BAD_REQUEST).json({
status: httpStatus.BAD_REQUEST,
message: "Seller request already submitted",
});
}

next();
} catch (error) {
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({
status: httpStatus.INTERNAL_SERVER_ERROR,
error: error.message,
});
}
};

export {
validation,
Expand Down Expand Up @@ -877,4 +921,6 @@ export {
isWishListProductExist,
isProductExistIntoWishList,
isProductOrdered,
isUserProfileComplete,
isSellerRequestExist
};
Loading

0 comments on commit 7a79e7c

Please sign in to comment.