|
|
@ -10,7 +10,7 @@ const fastify = require("fastify")({
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const { Counter} = require('../models/User')
|
|
|
|
const { Counter} = require('../models/User')
|
|
|
|
|
|
|
|
|
|
|
|
const {Department} = require('../models/Department')
|
|
|
|
const {Department, Desgination} = require('../models/Department')
|
|
|
|
|
|
|
|
|
|
|
|
const generateDepartmentId = async () => {
|
|
|
|
const generateDepartmentId = async () => {
|
|
|
|
const result = await Counter.findOneAndUpdate(
|
|
|
|
const result = await Counter.findOneAndUpdate(
|
|
|
@ -20,6 +20,14 @@ const generateDepartmentId = async () => {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
return result.seq;
|
|
|
|
return result.seq;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const generateDesginationId = async () => {
|
|
|
|
|
|
|
|
const result = await Counter.findOneAndUpdate(
|
|
|
|
|
|
|
|
{ _id: 'desgination_id' },
|
|
|
|
|
|
|
|
{ $inc: { seq: 1 } },
|
|
|
|
|
|
|
|
{ upsert: true, new: true }
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
return result.seq;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.addDepartment = async (request, reply) => {
|
|
|
|
exports.addDepartment = async (request, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -179,3 +187,164 @@ exports.addDepartment = async (request, reply) => {
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.addDesgination = async (request, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const d_id = await generateDepartmentId();
|
|
|
|
|
|
|
|
const desginationId = `AWDES${d_id}`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
|
|
|
phone,
|
|
|
|
|
|
|
|
city,
|
|
|
|
|
|
|
|
state,
|
|
|
|
|
|
|
|
password,
|
|
|
|
|
|
|
|
country,
|
|
|
|
|
|
|
|
zone,
|
|
|
|
|
|
|
|
address1,
|
|
|
|
|
|
|
|
address2,
|
|
|
|
|
|
|
|
pincode,
|
|
|
|
|
|
|
|
desginationName,
|
|
|
|
|
|
|
|
createdBy,
|
|
|
|
|
|
|
|
updatedBy,
|
|
|
|
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const existingStore = await Desgination.findOne({ phone });
|
|
|
|
|
|
|
|
if (existingStore) {
|
|
|
|
|
|
|
|
return reply.status(400).send({ message: 'Phone is already registered' });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const hashedPassword = await bcrypt.hash(password, 10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const department = new Desgination({
|
|
|
|
|
|
|
|
desginationId: desginationId,
|
|
|
|
|
|
|
|
city,
|
|
|
|
|
|
|
|
phone,
|
|
|
|
|
|
|
|
address1,
|
|
|
|
|
|
|
|
address2,
|
|
|
|
|
|
|
|
services: { password: { bcrypt: hashedPassword } },
|
|
|
|
|
|
|
|
state,
|
|
|
|
|
|
|
|
zone,
|
|
|
|
|
|
|
|
country,
|
|
|
|
|
|
|
|
pincode,
|
|
|
|
|
|
|
|
desginationName,
|
|
|
|
|
|
|
|
createdBy,
|
|
|
|
|
|
|
|
updatedBy,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await department.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({department, message: 'Account Created Successfully' });
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getSinledesginationData = async (req, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const { desginationId } = req.params;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const department = await Desgination.findOne({ desginationId: desginationId });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!department) {
|
|
|
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
|
|
message: 'Department not found.'
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.code(200).send({
|
|
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
|
|
message: 'Designation data retrieved successfully.',
|
|
|
|
|
|
|
|
data: department
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
console.error('Error fetching department data:', error);
|
|
|
|
|
|
|
|
reply.code(500).send({
|
|
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
|
|
message: 'Failed to retrieve department data.',
|
|
|
|
|
|
|
|
error: error.message,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getalldesgination = async (req, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
await Desgination.find()
|
|
|
|
|
|
|
|
.exec()
|
|
|
|
|
|
|
|
.then((docs) => {
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, data: docs, count: docs.length });
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
|
|
|
console.log(err);
|
|
|
|
|
|
|
|
reply.send({ error: err });
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
throw boom.boomify(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.deletedesginationInfo = async (req, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const desginationId = req.params.desginationId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const department = await Desgination.findOneAndDelete({ desginationId:desginationId });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, message: 'Delete Sucessfully', department});
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
throw boom.boomify(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.editdesgination = async (request, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const { desginationId } = request.params;
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
phone,
|
|
|
|
|
|
|
|
city,
|
|
|
|
|
|
|
|
state,
|
|
|
|
|
|
|
|
country,
|
|
|
|
|
|
|
|
zone,
|
|
|
|
|
|
|
|
address1,
|
|
|
|
|
|
|
|
address2,
|
|
|
|
|
|
|
|
pincode,
|
|
|
|
|
|
|
|
desginationName
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const existing = await Desgination.findOne({ desginationId });
|
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
|
|
|
|
return reply.status(404).send({ message: 'Designation not found' });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const phoneExists = await Desgination.findOne({ phone, desginationId: { $ne: desginationId } });
|
|
|
|
|
|
|
|
if (phoneExists) {
|
|
|
|
|
|
|
|
return reply.status(400).send({ message: 'Phone is already registered to another user' });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
existing.phone = phone || existing.phone;
|
|
|
|
|
|
|
|
existing.city = city || existing.city;
|
|
|
|
|
|
|
|
existing.state = state || existing.state;
|
|
|
|
|
|
|
|
existing.country = country || existing.country;
|
|
|
|
|
|
|
|
existing.zone = zone || existing.zone;
|
|
|
|
|
|
|
|
existing.desginationName = desginationName || existing.desginationName;
|
|
|
|
|
|
|
|
existing.pincode = pincode || existing.pincode;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
existing.address1 = address1 || existing.address1;
|
|
|
|
|
|
|
|
existing.address2 = address2 || existing.address2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await existing.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ message: 'Designation user updated successfully' });
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|