diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 69d9486b..b33c1a98 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -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 { diff --git a/src/models/Department.js b/src/models/Department.js index eec39c34..e4a7f162 100644 --- a/src/models/Department.js +++ b/src/models/Department.js @@ -39,6 +39,40 @@ const citySchema = new mongoose.Schema( ); + const branchSchema = new mongoose.Schema( + { + branchId:{type:String}, + phone: { type: String, unique: true, trim: true }, + land_line_number: { type: String, unique: true, trim: true }, + office_address1: String, + officeName: { type: String }, + email: { type: String }, + address2: String, + pincode: { type: String }, + zone: { type: String }, + city: { type: String }, + location: [{ type : String}], + state: String, + country: String, + services: { password: { bcrypt: String } }, + createdAt: { + type: Date, + default: function () { + return Date.now(); + }, + }, + createdBy: ObjectId, + updatedAt: { + type: Date, + default: function () { + return Date.now(); + }, + }, + updatedBy: ObjectId, + }, + { versionKey: false } + ); + const departmentsSchema = new mongoose.Schema( { departmentId:{type:String}, @@ -82,6 +116,7 @@ const citySchema = new mongoose.Schema( const City = mongoose.model('City', citySchema); const Deparments = mongoose.model('Deparments', departmentsSchema); + const Branch = mongoose.model('Branch', branchSchema); - module.exports = { City,Deparments}; + module.exports = { City,Deparments,Branch}; diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index 891fe819..d6141424 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -136,7 +136,7 @@ module.exports = function (fastify, opts, next) { body: { type: "object", properties: { - // phone: { type: "string" }, + phone: { type: "string" }, city: { type: "string" }, state: { type: "string" }, country: { type: "string" }, @@ -144,7 +144,7 @@ module.exports = function (fastify, opts, next) { address2: { type: "string" }, zone: { type: "string" }, pincode: { type: "string" }, - departmentName: { type: "string" }, + officeName: { type: "string" }, email: { type: "string" }, }, @@ -452,5 +452,101 @@ module.exports = function (fastify, opts, next) { handler: departmentController.getAllDepartmentNames, }); + + fastify.route({ + method: "POST", + url: "/api/branchSignup", + schema: { + tags: ["Department"], + description: "This is for creating a new Branch account", + summary: "This is for creating a new Branch account", + body: { + type: "object", + properties: { + phone: { type: "string" }, + land_line_number: { type: "string" }, + city: { type: "string" }, + officeName: { type: "string" }, + location: { + type: "array", + items: { type: "string" }, + }, + state: { type: "string" }, + email: { type: "string" }, + country: { type: "string" }, + office_address1: { type: "string" }, + address2: { type: "string" }, + zone: { type: "string" }, + pincode: { type: "string" }, + //departmentName: { type: "string" }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + handler: departmentController.addBranch, + }); + + fastify.delete("/api/deletebranch/:branchId", { + schema: { + description: "Delete a city by branchId", + tags: ["Department"], + summary: "Delete a user by branch", + params: { + type: "object", + properties: { + branchId: { type: "string" }, + }, + required: ["branchId"], + }, + response: { + 200: { + type: "object", + properties: { + success: { type: "boolean" }, + message: { type: "string" }, + } + } + } + }, + handler: departmentController.deleteBranchInfo, + }); + + fastify.put('/api/editbranch/:branchId', { + schema: { + description: "Edit Branch details by branch", + tags: ["Department"], + summary: "Edit Branch details.", + params: { + type: "object", + properties: { + branchId: { type: "string" }, + }, + required: ["branchId"], + }, + body: { + type: "object", + properties: { + phone: { type: "string" }, + land_line_number: { type: "string" }, + city: { type: "string" }, + state: { type: "string" }, + country: { type: "string" }, + address1: { type: "string" }, + address2: { type: "string" }, + zone: { type: "string" }, + pincode: { type: "string" }, + officeName: { type: "string" }, + email: { type: "string" }, + + }, + } + }, + handler: departmentController.editBranch, + }); + next(); }; \ No newline at end of file