Skip to content

Commit

Permalink
created user registtation and vendor
Browse files Browse the repository at this point in the history
  • Loading branch information
mugabodannyshafi committed May 21, 2024
1 parent 4f25b10 commit e317398
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
16 changes: 16 additions & 0 deletions src/controllers/vendor.controller.ts
Original file line number Diff line number Diff line change
@@ -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})
}
}
1 change: 1 addition & 0 deletions src/database/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";


Expand All @@ -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);


Expand Down
7 changes: 7 additions & 0 deletions src/routes/vendor.route.ts
Original file line number Diff line number Diff line change
@@ -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
9 changes: 5 additions & 4 deletions src/services/userService.ts
Original file line number Diff line number Diff line change
@@ -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;
};
16 changes: 16 additions & 0 deletions src/services/vendorServices.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit e317398

Please sign in to comment.