|
|
|
@ -10,7 +10,7 @@ const fastify = require("fastify")({
|
|
|
|
|
});
|
|
|
|
|
const { Counter} = require('../models/User')
|
|
|
|
|
|
|
|
|
|
const {Department, Desgination, City, Deparments} = require('../models/Department')
|
|
|
|
|
const {Department, Desgination, City, Deparments, Branch} = require('../models/Department')
|
|
|
|
|
// const generateDepartmentId = async (prefix) => {
|
|
|
|
|
// const result = await Counter.findOneAndUpdate(
|
|
|
|
|
// { _id: 'department_id' },
|
|
|
|
@ -27,6 +27,15 @@ const generateCityId = async () => {
|
|
|
|
|
{ upsert: true, new: true }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return result.seq;
|
|
|
|
|
};
|
|
|
|
|
const generateBranchId = async () => {
|
|
|
|
|
var result = await Counter.findOneAndUpdate(
|
|
|
|
|
{ _id: 'customer_id' },
|
|
|
|
|
{ $inc: { seq: 1 } },
|
|
|
|
|
{ upsert: true, new: true }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return result.seq;
|
|
|
|
|
};
|
|
|
|
|
// const generateDesginationId = async (prefix) => {
|
|
|
|
@ -106,6 +115,64 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.addBranch = async (request, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const {
|
|
|
|
|
phone,
|
|
|
|
|
land_line_number,
|
|
|
|
|
officeName,
|
|
|
|
|
location,
|
|
|
|
|
city,
|
|
|
|
|
state,
|
|
|
|
|
country,
|
|
|
|
|
zone,
|
|
|
|
|
office_address1,
|
|
|
|
|
address2,
|
|
|
|
|
pincode,
|
|
|
|
|
createdBy,
|
|
|
|
|
updatedBy,
|
|
|
|
|
email
|
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
|
// Generate departmentId based on departmentName
|
|
|
|
|
// const prefix = departmentName.substring(0, 2).toUpperCase(); // Extract first two letters and convert to uppercase
|
|
|
|
|
const b_id = await generateBranchId();
|
|
|
|
|
const branchId = `AWBR${b_id}`;
|
|
|
|
|
|
|
|
|
|
// Check for existing department
|
|
|
|
|
const existingStore = await Branch.findOne({ branchId });
|
|
|
|
|
if (existingStore) {
|
|
|
|
|
return reply.status(400).send({ message: 'Branch is already registered' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create new department
|
|
|
|
|
const branch = new Branch({
|
|
|
|
|
branchId,
|
|
|
|
|
phone,
|
|
|
|
|
land_line_number,
|
|
|
|
|
officeName,
|
|
|
|
|
location,
|
|
|
|
|
city,
|
|
|
|
|
office_address1,
|
|
|
|
|
address2,
|
|
|
|
|
state,
|
|
|
|
|
zone,
|
|
|
|
|
country,
|
|
|
|
|
pincode,
|
|
|
|
|
email,
|
|
|
|
|
// departmentName,
|
|
|
|
|
createdBy,
|
|
|
|
|
updatedBy,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await branch.save();
|
|
|
|
|
|
|
|
|
|
reply.send({ branch, message: 'Account Created Successfully' });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// exports.getSinledepartmentData = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { departmentId } = req.params;
|
|
|
|
@ -190,13 +257,23 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.deleteBranchInfo = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const branchId = req.params.branchId;
|
|
|
|
|
|
|
|
|
|
const branch = await Branch.findOneAndDelete({ branchId:branchId });
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, message: 'Delete Sucessfully', branch});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw boom.boomify(err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
exports.editcity = async (request, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { cityId } = request.params;
|
|
|
|
|
const {
|
|
|
|
|
|
|
|
|
|
// phone,
|
|
|
|
|
phone,
|
|
|
|
|
city,
|
|
|
|
|
state,
|
|
|
|
|
country,
|
|
|
|
@ -204,8 +281,8 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
|
address1,
|
|
|
|
|
address2,
|
|
|
|
|
pincode,
|
|
|
|
|
email
|
|
|
|
|
// departmentName
|
|
|
|
|
email,
|
|
|
|
|
officeName
|
|
|
|
|
|
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
@ -221,12 +298,12 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// existing.phone = phone || existing.phone;
|
|
|
|
|
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.departmentName = departmentName || existing.departmentName;
|
|
|
|
|
existing.officeName = officeName || existing.officeName;
|
|
|
|
|
existing.pincode = pincode || existing.pincode;
|
|
|
|
|
|
|
|
|
|
existing.address1 = address1 || existing.address1;
|
|
|
|
@ -243,6 +320,61 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.editBranch = async (request, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { branchId } = request.params;
|
|
|
|
|
const {
|
|
|
|
|
|
|
|
|
|
phone,
|
|
|
|
|
land_line_number,
|
|
|
|
|
officeName,
|
|
|
|
|
city,
|
|
|
|
|
state,
|
|
|
|
|
country,
|
|
|
|
|
zone,
|
|
|
|
|
address1,
|
|
|
|
|
address2,
|
|
|
|
|
pincode,
|
|
|
|
|
email
|
|
|
|
|
// departmentName
|
|
|
|
|
|
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const existing = await Branch.findOne({ branchId });
|
|
|
|
|
if (!existing) {
|
|
|
|
|
return reply.status(404).send({ message: 'Branch not found' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// const phoneExists = await Department.findOne({ phone, departmentId: { $ne: departmentId } });
|
|
|
|
|
// if (phoneExists) {
|
|
|
|
|
// return reply.status(400).send({ message: 'Phone is already registered to another user' });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
existing.phone = phone || existing.phone;
|
|
|
|
|
existing.land_line_number = land_line_number || existing.land_line_number;
|
|
|
|
|
existing.city = city || existing.city;
|
|
|
|
|
existing.state = state || existing.state;
|
|
|
|
|
existing.country = country || existing.country;
|
|
|
|
|
existing.zone = zone || existing.zone;
|
|
|
|
|
existing.officeName = officeName || existing.officeName;
|
|
|
|
|
existing.pincode = pincode || existing.pincode;
|
|
|
|
|
|
|
|
|
|
existing.address1 = address1 || existing.address1;
|
|
|
|
|
existing.address2 = address2 || existing.address2;
|
|
|
|
|
existing.email = email || existing.email;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await existing.save();
|
|
|
|
|
|
|
|
|
|
reply.send({ message: 'Branch user updated successfully' });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.addDesgination = async (request, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|