Commit e73ad6f3 authored by janithGamage's avatar janithGamage

feat: update

Desc : user API's
parent 89ba060e
......@@ -2,7 +2,6 @@ import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken';
import mongoose from 'mongoose';
import nodemailer from "nodemailer";
import { v4 as uuidv4 } from 'uuid';
import User from '../models/user.model.js';
export const signIn = async (req, res) => {
......@@ -28,89 +27,36 @@ export const signIn = async (req, res) => {
export const signUp = async (req, res) => {
const {
firstName,
lastName,
email,
contactNumber,
password,
confirmPassword,
type,
userFirstName,
userLastName,
userContactNumber,
userAddressLine1,
userAddressLine2,
userAddressLine3,
type
} = req.body;
try {
// if (!type) 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 (!userFirstName) return res.status(400).json({ code: "02", message: "User First Name Field Required" })
if (!userLastName) return res.status(400).json({ code: "02", message: "User Last Name Field Required" })
if (!userContactNumber) return res.status(400).json({ code: "02", message: "User Contact Number Field Required" })
const existingUser = await User.findOne({ email })
if (existingUser) return res.status(400).json({ code: "02", message: "User already exists" })
// if (type === "buyer") {
// 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({
// 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)
if (type === null || typeof type == "undefined") return res.status(400).json({ code: "02", message: "Type Field Required" })
if (email === null || typeof email == "undefined") return res.status(400).json({ code: "02", message: "Email Field Required" })
if (firstName === null || typeof firstName == "undefined") return res.status(400).json({ code: "02", message: "First Name Field Required" })
if (lastName === null || typeof lastName == "undefined") return res.status(400).json({ code: "02", message: "Last Name 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: email })
if (existingUser) return res.status(400).json({ code: "02", message: "User already exist" })
if (password === null || typeof password == "undefined") 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" })
const hashPassword = await bcrypt.hash(password, 12)
const userDetails = new User({
firstName,
lastName,
email,
password: hashedPassword,
contactNumber,
type,
userDetails: {
userQNumber: uuidv4(),
userEmail: email,
userName: `${userFirstName} ${userLastName}`,
userContactNumber,
userAddress: `${userAddressLine1}, ${userAddressLine2}, ${userAddressLine3}`,
}
password: hashPassword,
})
const userResult = await userDetails.save()
......@@ -249,3 +195,4 @@ export const deleteUser = async (req, res) => {
res.json({ code: "00", "message": error.message });
}
}
import mongoose from "mongoose";
const userSchema = mongoose.Schema({
email: {
firstName: {
type: String,
required: true,
unique: true
},
password: {
type: String
},
type: {
lastName: {
type: String,
required: true
required: true,
},
userDetails: {
userQNumber: {
email: {
type: String,
required: true,
unique: true
},
userEmail: {
contactNumber: {
type: String,
required: true,
unique: true
},
userName: {
type: String,
required: true
},
userContactNumber: {
password: {
type: String,
required: true
},
userAddress: {
type: String
required: true,
},
userType: {
type: String,
default: "N/A",
type: {
type: String, // administrator/ member
required: true
},
},
states: {
type: String,
default: "1"
},
createdAt: {
type: Date
type: Date,
default: new Date()
},
updatedAt: {
type: Date
type: Date,
default: new Date()
}
});
const User = mongoose.model("Users", userSchema);
export default User;
......@@ -21,10 +21,10 @@ app.get("/", (req, res) => {
});
//implement routes
app.use("/rest_node/user", userRoutes);
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;
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