From e317398a462184e4e6e3c554c2e6db0a71d14871 Mon Sep 17 00:00:00 2001 From: mugaboshafidanny Date: Wed, 22 May 2024 00:46:49 +0200 Subject: [PATCH] created user registtation and vendor --- src/controllers/user.controller.ts | 9 +++++++++ src/controllers/vendor.controller.ts | 16 ++++++++++++++++ src/database/models/user.ts | 1 + src/index.ts | 2 ++ src/routes/vendor.route.ts | 7 +++++++ src/services/userService.ts | 9 +++++---- src/services/vendorServices.ts | 16 ++++++++++++++++ 7 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 src/controllers/vendor.controller.ts create mode 100644 src/routes/vendor.route.ts create mode 100644 src/services/vendorServices.ts diff --git a/src/controllers/user.controller.ts b/src/controllers/user.controller.ts index 5b15a34..a14ad4d 100644 --- a/src/controllers/user.controller.ts +++ b/src/controllers/user.controller.ts @@ -15,6 +15,15 @@ export const Welcome = async (req: Request, res: Response) => { }; export const register = async (req: Request, res: Response) => { + + const {name, email, password} = req.body + if(!name || !email || !password) { + return res.status(400).json({message: 'Please fill all fields'}) + } + const duplicate: any = await User.findOne({where: {email: email}}) + if(duplicate){ + res.status(409).json({Message: 'Email already exists'}) + } try { const senddata = await saveUser(req.body); res.status(201).json({ message: "User created", user: senddata }); diff --git a/src/controllers/vendor.controller.ts b/src/controllers/vendor.controller.ts new file mode 100644 index 0000000..1b99ab0 --- /dev/null +++ b/src/controllers/vendor.controller.ts @@ -0,0 +1,16 @@ +import { Request, Response } from "express"; +import Vendor from "../database/models/vendor"; +import { saveVendor } from "../services/vendorServices"; + +export const registerVendor = async (req: Request, res: Response) => { + const {userId, storeName, address, TIN, bankAccount, paymentDetails} = req.body + if(!storeName || !address || !TIN || !bankAccount || !paymentDetails){ + return res.status(400).json({message: "Please fill all the fields"}) + } + try { + const result = await saveVendor(req.body) + return res.status(200).json({message: "Vendor registered successfully", result}) + } catch (error: any) { + return res.status(500).json({message: error.message}) + } +} \ No newline at end of file diff --git a/src/database/models/user.ts b/src/database/models/user.ts index 304d9bd..fd4c346 100644 --- a/src/database/models/user.ts +++ b/src/database/models/user.ts @@ -32,6 +32,7 @@ import connectSequelize from "../config/db.config"; userId: {type:DataTypes.UUID,primaryKey: true,defaultValue: DataTypes.UUIDV4}, name: {type:DataTypes.STRING,allowNull: false}, email: {type:DataTypes.STRING,allowNull: false}, + password: {type:DataTypes.STRING,allowNull: false}, status: {type:DataTypes.STRING,defaultValue: 'active'}, wishlistId: {type:DataTypes.STRING}, cartId: {type:DataTypes.STRING}, diff --git a/src/index.ts b/src/index.ts index 4f5f9cb..9ec0937 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ const PORT = process.env.PORT; import userRoute from "./routes/user.route"; +import vendorRoute from "./routes/vendor.route"; import swaggerRoute from "./config/SwaggerConfig"; @@ -15,6 +16,7 @@ const app = express(); app.use(express.static("public")); app.use(express.json()) app.use("/", userRoute); +app.use("/", vendorRoute); app.use("/api-docs", swaggerRoute); diff --git a/src/routes/vendor.route.ts b/src/routes/vendor.route.ts new file mode 100644 index 0000000..4caf9b6 --- /dev/null +++ b/src/routes/vendor.route.ts @@ -0,0 +1,7 @@ +import express from "express" +const route = express.Router() +import { registerVendor } from "../controllers/vendor.controller" + +route.post('/registerVendor', registerVendor) + +export default route \ No newline at end of file diff --git a/src/services/userService.ts b/src/services/userService.ts index 57ec023..c0f8c37 100644 --- a/src/services/userService.ts +++ b/src/services/userService.ts @@ -1,11 +1,12 @@ import User from "../database/models/user"; - +import bcrypt from 'bcrypt' export const saveUser = async (data: any) => { - const { username, email, password } = data; + const { name, email, password } = data; + const hashedPwd = bcrypt.hashSync(password, 10) const insertUser = await User.create({ - username: username, + name: name, email: email, - password: password, + password: hashedPwd, }); return insertUser; }; diff --git a/src/services/vendorServices.ts b/src/services/vendorServices.ts new file mode 100644 index 0000000..c65aadf --- /dev/null +++ b/src/services/vendorServices.ts @@ -0,0 +1,16 @@ +import Vendor from "../database/models/vendor"; + +export const saveVendor = async (data: any) => { + const {userId, storeName, address, TIN, bankAccount, paymentDetails} = data + + const insertVendor = await Vendor.create({ + userId: userId, + storeName: storeName, + address: address, + TIN: TIN, + bankAccount: bankAccount, + paymentDetails + }) + + return insertVendor; +} \ No newline at end of file