Files
Dwinzo-Backend-V0.0/src/api-server/controller/user-Controller.ts

104 lines
4.1 KiB
TypeScript

import { Request, Response } from "express";
import { Server } from 'http';
import userModel from "../../shared/model/user-Model.ts";
import { isSharedArrayBuffer } from "util/types";
import {hashGenerate,hashValidator} from "../../shared/security/Hasing.ts"
// import {hashGenerate} from '../security/Hasing'
let serverAlive = true;
export class user {
static async signup(req: Request, res: Response) {
try {
let role;
const { userName, email, password,organization,profilePicture } = req.body;
const caseChange = email.toLowerCase();
const emailcheck = await userModel(organization).findOne({ email: caseChange });
if (emailcheck!==null) {
res.json({
message:"User already exists"
});
} else {
const hashpassword=await hashGenerate(password)
const userCount = await userModel(organization).countDocuments({});
role = userCount === 0 ? "Admin" : "User";
const isShare = "true";
const newuser = await userModel(organization).create({
userName: userName,
email: caseChange,
isShare:isShare,
password: hashpassword,
role:role,
profilePicture:profilePicture
});
newuser.save();
res.status(200).json({
message:"New User created"
});
}
} catch (error:any) {
res.status(500).send(error);
}
}
static async login(req: Request, res: Response) {
try {
let role;
const { email, password,organization } = req.body;
// console.log(' req.body: ', req.body);
const existingMail = await userModel(organization).findOne({
email:email
});
if (existingMail === null || !existingMail) {
res.status(401).json({ message: "User Not Found!!! Kindly signup..." });
} else {
const hashedpassword= existingMail.password
const checkpassword = await hashValidator(
password,
hashedpassword
)
// console.log('checkpassword: ', checkpassword);
if (checkpassword) {
// const tokenValidation=await tokenGenerator(existingMail.email)
res.status(200).send({
message: "login successfull",
email: existingMail.email,
name: existingMail.userName,
userId: existingMail._id,
isShare:existingMail.isShare,
// token:tokenValidation
});
} else {
res.status(404).json({message:"email & password is invalid...Check the credentials"})
}
}
} catch (error:any) {
res.status(500).send(error);
}
}
// static async checkserverHealth(server:Server,organization: string){
// try {
// if (server.listening) {
// console.log('Server is running');
// serverAlive = true;
// // Update all users to online status
// } else {
// // await userModel(organization).updateMany({}, { activeStatus: "offline" }); // Replace `activeStatus` with your actual field
// throw new Error('Server is not running');
// }
// } catch (error:any) {
// console.error('Server health check failed:', error.message);
// serverAlive = false;
// // Update all users to offline status
// // await userModel(organization).updateMany({}, { activeStatus: "offline" });
// }
// }
}
// export const startHealthCheck = (server: Server, organization: string) => {
// setInterval(() => user.checkserverHealth(server, organization), 5000);
// };