Commit e73ad6f3 authored by janithGamage's avatar janithGamage

feat: update

Desc : user API's
parent 89ba060e
...@@ -2,7 +2,6 @@ import bcrypt from 'bcryptjs'; ...@@ -2,7 +2,6 @@ import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken'; import jwt from 'jsonwebtoken';
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import nodemailer from "nodemailer"; import nodemailer from "nodemailer";
import { v4 as uuidv4 } from 'uuid';
import User from '../models/user.model.js'; import User from '../models/user.model.js';
export const signIn = async (req, res) => { export const signIn = async (req, res) => {
...@@ -28,89 +27,36 @@ export const signIn = async (req, res) => { ...@@ -28,89 +27,36 @@ export const signIn = async (req, res) => {
export const signUp = async (req, res) => { export const signUp = async (req, res) => {
const { const {
firstName,
lastName,
email, email,
contactNumber,
password, password,
confirmPassword, confirmPassword,
type, type
userFirstName,
userLastName,
userContactNumber,
userAddressLine1,
userAddressLine2,
userAddressLine3,
} = req.body; } = req.body;
try { try {
// if (!type) return res.status(400).json({ code: "02", message: "Type Field Required" }) if (type === null || typeof type == "undefined") return res.status(400).json({ code: "02", message: "Type Field Required" })
if (!email) return res.status(400).json({ code: "02", message: "Email Field Required" }) if (email === null || typeof email == "undefined") return res.status(400).json({ code: "02", message: "Email Field Required" })
if (!userFirstName) return res.status(400).json({ code: "02", message: "User First Name Field Required" }) if (firstName === null || typeof firstName == "undefined") return res.status(400).json({ code: "02", message: "First Name Field Required" })
if (!userLastName) return res.status(400).json({ code: "02", message: "User Last Name Field Required" }) if (lastName === null || typeof lastName == "undefined") return res.status(400).json({ code: "02", message: "Last Name Field Required" })
if (!userContactNumber) return res.status(400).json({ code: "02", message: "User Contact Number Field Required" }) if (contactNumber === null || typeof contactNumber == "undefined") return res.status(400).json({ code: "02", message: "Contact Number Field Required" })
const existingUser = await User.findOne({ email }) const existingUser = await User.findOne({ email: email })
if (existingUser) return res.status(400).json({ code: "02", message: "User already exists" }) if (existingUser) return res.status(400).json({ code: "02", message: "User already exist" })
// if (type === "buyer") { if (password === null || typeof password == "undefined") return res.status(400).json({ code: "02", message: "Password Field Required" })
// if (!password) return res.status(400).json({ code: "02", message: "Password Field Required" }) if (password !== confirmPassword) return res.status(400).json({ code: "02", message: "Password doesn't match" })
// if (password !== confirmPassword) return res.status(400).json({ code: "02", message: "Passwords do not match" }) const hashPassword = await bcrypt.hash(password, 12)
// const hashedPassword = await bcrypt.hash(password, 12)
// const userDetails = new User({
// email,
// password: hashedPassword,
// type,
// userDetails: {
// userQNumber: uuidv4(),
// userEmail: email,
// userName: `${userFirstName} ${userLastName}`,
// userContactNumber,
// userAddress: `${userAddressLine1}, ${userAddressLine2}, ${userAddressLine3}`,
// userType: type,
// }
// })
// const userResult = await userDetails.save()
// const token = jwt.sign({ email: userResult.email, id: userResult._id }, 'test', { expiresIn: "1h" })
// res.status(200).json({ code: "01", result: userResult, token })
// } else if (type === "trader") {
// const userDetails = new User({
// email,
// type,
// userDetails: {
// userQNumber: uuidv4(),
// userEmail: email,
// userName: `${userFirstName} ${userLastName}`,
// userContactNumber,
// userAddress: `${userAddressLine1}, ${userAddressLine2}, ${userAddressLine3}`,
// userType: type,
// },
// states: 2
// })
// const userResult = await userDetails.save()
// const token = jwt.sign({ email: userResult.email, id: userResult._id }, 'test', { expiresIn: "1h" })
// res.status(200).json({ code: "01", result: userResult, token })
// }
if (!password) return res.status(400).json({ code: "02", message: "Password Field Required" })
if (password !== confirmPassword) return res.status(400).json({ code: "02", message: "Passwords do not match" })
const hashedPassword = await bcrypt.hash(password, 12)
const userDetails = new User({ const userDetails = new User({
firstName,
lastName,
email, email,
password: hashedPassword, contactNumber,
type, type,
userDetails: { password: hashPassword,
userQNumber: uuidv4(),
userEmail: email,
userName: `${userFirstName} ${userLastName}`,
userContactNumber,
userAddress: `${userAddressLine1}, ${userAddressLine2}, ${userAddressLine3}`,
}
}) })
const userResult = await userDetails.save() const userResult = await userDetails.save()
...@@ -249,3 +195,4 @@ export const deleteUser = async (req, res) => { ...@@ -249,3 +195,4 @@ export const deleteUser = async (req, res) => {
res.json({ code: "00", "message": error.message }); res.json({ code: "00", "message": error.message });
} }
} }
import mongoose from "mongoose"; import mongoose from "mongoose";
const userSchema = mongoose.Schema({ const userSchema = mongoose.Schema({
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
email: { email: {
type: String, type: String,
required: true, required: true,
unique: true unique: true
}, },
contactNumber: {
type: String,
required: true,
unique: true
},
password: { password: {
type: String type: String,
required: true,
}, },
type: { type: {
type: String, type: String, // administrator/ member
required: true required: true
}, },
userDetails: {
userQNumber: {
type: String,
required: true,
unique: true
},
userEmail: {
type: String,
required: true,
},
userName: {
type: String,
required: true
},
userContactNumber: {
type: String,
required: true
},
userAddress: {
type: String
},
userType: {
type: String,
default: "N/A",
required: true
},
},
states: { states: {
type: String, type: String,
default: "1" default: "1"
}, },
createdAt: { createdAt: {
type: Date type: Date,
default: new Date()
}, },
updatedAt: { updatedAt: {
type: Date type: Date,
default: new Date()
} }
}); });
const User = mongoose.model("Users", userSchema); const User = mongoose.model("Users", userSchema);
export default User; export default User;
...@@ -20,11 +20,11 @@ app.get("/", (req, res) => { ...@@ -20,11 +20,11 @@ app.get("/", (req, res) => {
res.json({ message: "Welcome to Server Node" }); res.json({ message: "Welcome to Server Node" });
}); });
//implement routes //implement routes
app.use("/rest_node/user", userRoutes);
app.use("/rest_node/ssl", translateRoutes); app.use("/rest_node/ssl", translateRoutes);
app.use("/rest_node/user", userRoutes);
const CONNECTION_URL = `mongodb+srv://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@cluster0.dmza8yi.mongodb.net/?retryWrites=true&w=majority`; const CONNECTION_URL = `mongodb+srv://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@researchmanagement-appl.vzhn4.mongodb.net/?retryWrites=true&w=majority`;
const PORT = process.env.PORT || 5000; const PORT = process.env.PORT || 5000;
mongoose mongoose
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment