From e1893e5b91ffe595a35f1a246aa773d6c4c152db Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 11 Feb 2025 13:03:15 +0530 Subject: [PATCH] create new zone account --- src/controllers/departmentController.js | 42 ++++++++++++++++++++++++- src/models/Department.js | 28 ++++++++++++++++- src/routes/departmentRoute.js | 28 +++++++++++++++++ 3 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index df2fe414..e8daf6e0 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, Branch} = require('../models/Department') +const {Department, Desgination, City, Deparments, Branch, Zone} = require('../models/Department') // const generateDepartmentId = async (prefix) => { // const result = await Counter.findOneAndUpdate( // { _id: 'department_id' }, @@ -175,6 +175,46 @@ const generateDepartmentId = async (city, departmentName) => { reply.status(500).send({ message: err.message }); } }; + + exports.addZone = async (request, reply) => { + try { + const { + officeName, + location, + city, + zone, + createdBy, + updatedBy, + } = 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 zoneId = `AWZN${b_id}`; + + // Check for existing department + const existingStore = await Zone.findOne({ zoneId }); + if (existingStore) { + return reply.status(400).send({ message: 'Branch is already registered' }); + } + + const zones = new Zone({ + zoneId, + officeName, + location, + city, + zone, + createdBy, + updatedBy, + }); + + await zones.save(); + + reply.send({ zones, message: 'Account Created Successfully' }); + } catch (err) { + reply.status(500).send({ message: err.message }); + } + }; // exports.getSinledepartmentData = async (req, reply) => { // try { // const { departmentId } = req.params; diff --git a/src/models/Department.js b/src/models/Department.js index da36e9c9..3365e206 100644 --- a/src/models/Department.js +++ b/src/models/Department.js @@ -72,6 +72,31 @@ const citySchema = new mongoose.Schema( }, { versionKey: false } ); + + const zoneSchema = new mongoose.Schema( + { + zoneId:{type:String}, + officeName: { type: String }, + zone: { type: String , default: "ALL"}, + city: { type: String }, + location: [{ type : 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( { @@ -162,8 +187,9 @@ const citySchema = new mongoose.Schema( const City = mongoose.model('City', citySchema); const Deparments = mongoose.model('Deparments', departmentsSchema); const Branch = mongoose.model('Branch', branchSchema); + const Zone = mongoose.model('Zone', zoneSchema); const TeamMemberProfilePicture = mongoose.model('TeamMemberProfilePicture', teamMemberProfilePictureSchema); const CompanyProfilePicture = mongoose.model('CompanyProfilePicture', companyProfilePictureSchema); - module.exports = { City,Deparments,Branch,TeamMemberProfilePicture,CompanyProfilePicture}; + module.exports = { City,Deparments,Branch,TeamMemberProfilePicture,CompanyProfilePicture,Zone}; diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index 4fec9e1c..cf4f6bc8 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -517,6 +517,34 @@ module.exports = function (fastify, opts, next) { }, handler: departmentController.addBranch, }); + + fastify.route({ + method: "POST", + url: "/api/zoneSignup", + schema: { + tags: ["Department"], + description: "This is for creating a new Zone account", + summary: "This is for creating a new Zone account", + body: { + type: "object", + properties: { + city: { type: "string" }, + officeName: { type: "string" }, + location: { + type: "array", + items: { type: "string" }, + }, + zone: { type: "string" }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + handler: departmentController.addZone, + }); fastify.delete("/api/deletebranch/:branchId", { schema: {