diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js new file mode 100644 index 00000000..08a9ef45 --- /dev/null +++ b/src/controllers/departmentController.js @@ -0,0 +1,74 @@ +const boom = require("boom"); +const bcrypt = require('bcrypt'); +const jwt = require('jsonwebtoken'); +const customJwtAuth = require("../customAuthJwt"); +const fastify = require("fastify")({ + logger: true, + genReqId(req) { + return uuidv4(); + }, +}); +const { Counter} = require('../models/User') + +const {Department} = require('../models/Department') + +const generateDepartmentId = async () => { + const result = await Counter.findOneAndUpdate( + { _id: 'department_id' }, + { $inc: { seq: 1 } }, + { upsert: true, new: true } + ); + return result.seq; + }; + +exports.addDepartment = async (request, reply) => { + try { + const d_id = await generateDepartmentId(); + const departmentId = `AWDP${d_id}`; + + const { + phone, + city, + state, + password, + country, + zone, + address1, + address2, + pincode, + departmentName, + createdBy, + updatedBy, + } = request.body; + + + const existingStore = await Department.findOne({ phone }); + if (existingStore) { + return reply.status(400).send({ message: 'Phone is already registered' }); + } + + const hashedPassword = await bcrypt.hash(password, 10); + + const department = new Department({ + departmentId: departmentId, + city, + phone, + address1, + address2, + services: { password: { bcrypt: hashedPassword } }, + state, + zone, + country, + pincode, + departmentName, + createdBy, + updatedBy, + }); + + await department.save(); + + reply.send({department, message: 'Account Created Successfully' }); + } catch (err) { + reply.status(500).send({ message: err.message }); + } + }; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 38ab3ab9..19dfed5c 100644 --- a/src/index.js +++ b/src/index.js @@ -588,6 +588,8 @@ fastify.register(require("./routes/supplierOrdersRoutes")); fastify.register(require("./routes/friendRequestRoute")); fastify.register(require("./routes/adminRoute")); fastify.register(require("./routes/storeRoute")); +fastify.register(require("./routes/departmentRoute.js")); + // Testing route allows for retrieving a user by phone so one can see what is the phone verification code sent for a given user's phone // Also allows deletion of a user with a given phone number diff --git a/src/models/Department.js b/src/models/Department.js new file mode 100644 index 00000000..b44fddcd --- /dev/null +++ b/src/models/Department.js @@ -0,0 +1,42 @@ +const mongoose = require("mongoose"); +const Schema = mongoose.Schema; +const ObjectId = Schema.Types.ObjectId; + + + +const departmentSchema = new mongoose.Schema( + { + departmentId:{type:String}, + departmentName: { type: String }, + phone: { type: String, unique: true, trim: true }, + address1: String, + address2: String, + pincode: { type: String }, + zone: { type: String }, + city: { 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 Department = mongoose.model('Department', departmentSchema); + + + module.exports = { Department}; diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js new file mode 100644 index 00000000..af3f8953 --- /dev/null +++ b/src/routes/departmentRoute.js @@ -0,0 +1,45 @@ +const departmentController = require('../controllers/departmentController') + +module.exports = function (fastify, opts, next) { + + + fastify.route({ + method: "POST", + url: "/api/departmentSignup", + schema: { + tags: ["Department"], + description: "This is for creating a new Department Account", + summary: "This is for creating a new Department Account", + body: { + type: "object", + //required: ["phone", "username", "password", "role"], // Add role to required fields + properties: { + phone: { type: "string" }, + password: { type: "string" }, + city: { type: "string" }, + state: { type: "string" }, + country: { type: "string" }, + address1: { type: "string" }, + address2: { type: "string" }, + zone: { type: "string" }, + pincode: { type: "string" }, + departmentName: { type: "string" }, + + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + handler: departmentController.addDepartment, + }); + + + + + + + next(); + }; \ No newline at end of file