100 lines
2.0 KiB
JavaScript
100 lines
2.0 KiB
JavaScript
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 "bcrypt";
|
|
|
|
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,
|
|
isFirstTime: true,
|
|
isActive: true,
|
|
},
|
|
],
|
|
{ session },
|
|
);
|
|
|
|
console.log("EMPLOYEE", employee);
|
|
|
|
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 },
|
|
);
|
|
await session.commitTransaction();
|
|
session.endSession();
|
|
res.status(200).json({ result: access });
|
|
} catch (error) {
|
|
console.log("createEmployee Error", error);
|
|
await session.abortTransaction();
|
|
session.endSession();
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
};
|