import { User, Access, Profile, Team, Branch, Partner, Vendor, Venue, } from "../models/index.js"; import dayjs from "dayjs"; import mongoose from "mongoose"; import { coordinatesToGeoJson } from "../helpers/geoJson.helper.js"; import bcrypt from "bcryptjs"; export const createEmployee = async (req, res) => { const session = await mongoose.startSession(); session.startTransaction(); try { const { user } = res.locals; const { name, mobile, id, password, address, city, state, pincode, email, designation, department, branch, role, } = req.body; const hash = await bcrypt.hash(password, 10); const userExists = await User.exists({ id }); if (userExists) { throw { code: 400, message: "ID already assigned for Another Employee" }; } const employee = await User.create( [ { name, id, hash, createdOn: dayjs().toISOString(), createdBy: user.id, }, ], { session }, ); const profile = await Profile.create( [ { user: employee?._id, mobile, address, city, email, state, pincode, isActive: true, }, ], { session }, ); const access = await Access.create( [ { user: employee?._id, designation, department, branch, role, createdBy: user.id, createdOn: dayjs().toISOString(), }, ], { session }, ); res.status(200).json({ result: access }); } catch (error) { res.status(500).json({ error: error.message }); } };