From e5ba590b119283364c2c64814efa198c7cabe07e Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 25 Mar 2025 11:23:42 +0530 Subject: [PATCH] states and cites get apis --- src/controllers/departmentController.js | 32 ++++- src/index.js | 154 +++++++++++++++++++++++- src/models/Department.js | 8 +- src/routes/departmentRoute.js | 27 +++++ 4 files changed, 218 insertions(+), 3 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index f8b7eb0f..c059a2bd 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -2,6 +2,8 @@ const boom = require("boom"); const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); const customJwtAuth = require("../customAuthJwt"); +const mongoose = require("mongoose"); + const fastify = require("fastify")({ logger: true, genReqId(req) { @@ -10,7 +12,7 @@ const fastify = require("fastify")({ }); const { Counter} = require('../models/User') -const {Department, Desgination, City, Deparments, Branch, Zone} = require('../models/Department') +const {Department, Desgination, City, Deparments, Branch, Zone,IndianLocations} = require('../models/Department') // const generateDepartmentId = async (prefix) => { // const result = await Counter.findOneAndUpdate( // { _id: 'department_id' }, @@ -1186,3 +1188,31 @@ const getDepartmentsByName = async (officeName, city, departmentName) => { } }; + + + exports.getAllStates = async (req, reply) => { + try { + const states = await IndianLocations.find({}, "state").sort({ state: 1 }); // Sorting A to Z + reply.send(states); + } catch (err) { + reply.status(500).send({ error: "Failed to fetch states" }); + } + }; + + exports.getStaeBasedCites = async (request, reply) => { + try { + const { stateName } = request.params; + const state = await IndianLocations.findOne({ state: stateName }, "majorCities"); + + if (!state) { + return reply.status(404).send({ error: "State not found" }); + } + + reply.send(state.majorCities); + } catch (err) { + reply.status(500).send({ error: "Failed to fetch cities" }); + } + }; + + + \ No newline at end of file diff --git a/src/index.js b/src/index.js index 98c770f7..0ef7affe 100644 --- a/src/index.js +++ b/src/index.js @@ -601,7 +601,7 @@ const {Storage} = require('@google-cloud/storage'); const { Supplier, profilePictureSupplier } = require("./models/supplier"); const multer = require('fastify-multer'); const { ProfilePictureInstall, Install } = require("./models/store.js"); -const { TeamMemberProfilePicture, CompanyProfilePicture, Deparments } = require("./models/Department.js"); +const { TeamMemberProfilePicture, CompanyProfilePicture, Deparments, IndianLocations } = require("./models/Department.js"); fastify.register(require('fastify-formbody')); // fastify.register(multer.contentParser); // const multipart = require('fastify-multipart'); @@ -1354,7 +1354,159 @@ fastify.post("/api/teamMemberLogin", { }, }); + fastify.post("/add-states", async (request, reply) => { + try { + const statesData = [ + { + state: "Andhra Pradesh", + majorCities: [ + "Adoni", "Amaravati", "Anantapur", "Chandragiri", "Chittoor", + "Guntur", "Kadapa", "Kakinada", "Kurnool", "Machilipatnam", + "Rajahmundry", "Tirupati", "Vijayawada", "Visakhapatnam", "Vizianagaram" + ], + }, + { + state: "Arunachal Pradesh", + majorCities: ["Itanagar", "Naharlagun", "Tawang", "Pasighat", "Ziro"], + }, + { + state: "Assam", + majorCities: ["Guwahati", "Dibrugarh", "Silchar", "Jorhat", "Tezpur"], + }, + { + state: "Bihar", + majorCities: ["Patna", "Gaya", "Bhagalpur", "Muzaffarpur", "Purnia"], + }, + { + state: "Chhattisgarh", + majorCities: ["Raipur", "Bhilai", "Bilaspur", "Durg", "Korba"], + }, + { + state: "Goa", + majorCities: ["Panaji", "Margao", "Vasco da Gama", "Mapusa"], + }, + { + state: "Gujarat", + majorCities: ["Ahmedabad", "Surat", "Vadodara", "Rajkot", "Bhavnagar"], + }, + { + state: "Haryana", + majorCities: ["Chandigarh", "Faridabad", "Gurugram", "Hisar", "Panipat"], + }, + { + state: "Himachal Pradesh", + majorCities: ["Shimla", "Manali", "Dharamshala", "Mandi", "Solan"], + }, + { + state: "Jharkhand", + majorCities: ["Ranchi", "Jamshedpur", "Dhanbad", "Bokaro", "Hazaribagh"], + }, + { + state: "Karnataka", + majorCities: ["Bengaluru", "Mysuru", "Hubballi", "Mangaluru", "Belagavi"], + }, + { + state: "Kerala", + majorCities: ["Thiruvananthapuram", "Kochi", "Kozhikode", "Thrissur", "Alappuzha"], + }, + { + state: "Madhya Pradesh", + majorCities: ["Bhopal", "Indore", "Gwalior", "Jabalpur", "Ujjain"], + }, + { + state: "Maharashtra", + majorCities: ["Mumbai", "Pune", "Nagpur", "Nashik", "Aurangabad"], + }, + { + state: "Manipur", + majorCities: ["Imphal", "Bishnupur", "Thoubal", "Churachandpur"], + }, + { + state: "Meghalaya", + majorCities: ["Shillong", "Tura", "Jowai", "Nongstoin"], + }, + { + state: "Mizoram", + majorCities: ["Aizawl", "Lunglei", "Champhai", "Serchhip"], + }, + { + state: "Nagaland", + majorCities: ["Kohima", "Dimapur", "Mokokchung", "Tuensang"], + }, + { + state: "Odisha", + majorCities: ["Bhubaneswar", "Cuttack", "Rourkela", "Berhampur", "Sambalpur"], + }, + { + state: "Punjab", + majorCities: ["Chandigarh", "Ludhiana", "Amritsar", "Jalandhar", "Patiala"], + }, + { + state: "Rajasthan", + majorCities: ["Jaipur", "Jodhpur", "Udaipur", "Kota", "Ajmer"], + }, + { + state: "Sikkim", + majorCities: ["Gangtok", "Namchi", "Mangan", "Gyalshing"], + }, + { + state: "Tamil Nadu", + majorCities: ["Chennai", "Coimbatore", "Madurai", "Tiruchirappalli", "Salem"], + }, + { + state: "Telangana", + majorCities: ["Hyderabad", "Warangal", "Nizamabad", "Karimnagar", "Khammam"], + }, + { + state: "Tripura", + majorCities: ["Agartala", "Udaipur", "Dharmanagar", "Kailashahar"], + }, + { + state: "Uttar Pradesh", + majorCities: ["Lucknow", "Kanpur", "Varanasi", "Agra", "Meerut"], + }, + { + state: "Uttarakhand", + majorCities: ["Dehradun", "Haridwar", "Nainital", "Rishikesh"], + }, + { + state: "West Bengal", + majorCities: ["Kolkata", "Howrah", "Durgapur", "Siliguri", "Asansol"], + }, + { + state: "Andaman and Nicobar Islands", + majorCities: ["Port Blair"], + }, + { + state: "Chandigarh", + majorCities: ["Chandigarh"], + }, + { + state: "Dadra and Nagar Haveli and Daman and Diu", + majorCities: ["Daman", "Diu", "Silvassa"], + }, + { + state: "Lakshadweep", + majorCities: ["Kavaratti"], + }, + { + state: "Delhi", + majorCities: ["New Delhi"], + }, + { + state: "Puducherry", + majorCities: ["Puducherry", "Karaikal", "Mahe", "Yanam"], + }, + ]; + await IndianLocations.deleteMany(); + await IndianLocations.insertMany(statesData); + + reply.send({ message: "✅ All states added successfully!" }); + } catch (error) { + reply.status(500).send({ error: "❌ Error inserting states: " + error }); + } + }); // Run the server! const start = async () => { diff --git a/src/models/Department.js b/src/models/Department.js index 0bb9ee9c..a701ab83 100644 --- a/src/models/Department.js +++ b/src/models/Department.js @@ -201,6 +201,12 @@ const citySchema = new mongoose.Schema( } }); + const stateSchema = new mongoose.Schema({ + state: { type: String, required: true, unique: true }, + majorCities: { type: [String], required: true } + }); + + const IndianLocations = mongoose.model("IndianLocations", stateSchema); const City = mongoose.model('City', citySchema); const Deparments = mongoose.model('Deparments', departmentsSchema); @@ -210,4 +216,4 @@ const citySchema = new mongoose.Schema( const CompanyProfilePicture = mongoose.model('CompanyProfilePicture', companyProfilePictureSchema); - module.exports = { City,Deparments,Branch,TeamMemberProfilePicture,CompanyProfilePicture,Zone}; + module.exports = { City,Deparments,Branch,TeamMemberProfilePicture,CompanyProfilePicture,Zone,IndianLocations}; diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index 49082a0a..20402265 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -647,6 +647,33 @@ module.exports = function (fastify, opts, next) { }, handler: departmentController.getDepartmentsByCity, }); + + fastify.route({ + method: "GET", + url: "/api/states", + schema: { + tags: ["Department"], + description: "Get the States", + summary: "Get the states", + }, + handler:departmentController.getAllStates + }); + fastify.route({ + method: "GET", + url: "/api/states/:stateName/cities", + schema: { + tags: ["Department"], + description: "Get the States by cities", + summary: "Get the states by cites", + params: { + type: "object", + properties: { + stateName: { type: "string" }, + }, + }, + }, + handler:departmentController.getStaeBasedCites + }); next(); }; \ No newline at end of file