You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.7 KiB

const boom = require("boom");
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const customJwtAuth = require("../customAuthJwt");
const fastify = require("fastify")({
logger: true,
genReqId(req) {
return uuidv4();
},
});
const { Counter} = require('../models/User')
const {Department} = require('../models/Department')
const generateDepartmentId = async () => {
const result = await Counter.findOneAndUpdate(
{ _id: 'department_id' },
{ $inc: { seq: 1 } },
{ upsert: true, new: true }
);
return result.seq;
};
exports.addDepartment = async (request, reply) => {
try {
const d_id = await generateDepartmentId();
const departmentId = `AWDP${d_id}`;
const {
phone,
city,
state,
password,
country,
zone,
address1,
address2,
pincode,
departmentName,
createdBy,
updatedBy,
} = request.body;
const existingStore = await Department.findOne({ phone });
if (existingStore) {
return reply.status(400).send({ message: 'Phone is already registered' });
}
const hashedPassword = await bcrypt.hash(password, 10);
const department = new Department({
departmentId: departmentId,
city,
phone,
address1,
address2,
services: { password: { bcrypt: hashedPassword } },
state,
zone,
country,
pincode,
departmentName,
createdBy,
updatedBy,
});
await department.save();
reply.send({department, message: 'Account Created Successfully' });
} catch (err) {
reply.status(500).send({ message: err.message });
}
};