department account creation

master
Bhaskar 10 months ago
parent ecaec81a08
commit 6df643e8dd

@ -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 });
}
};

@ -588,6 +588,8 @@ fastify.register(require("./routes/supplierOrdersRoutes"));
fastify.register(require("./routes/friendRequestRoute")); fastify.register(require("./routes/friendRequestRoute"));
fastify.register(require("./routes/adminRoute")); fastify.register(require("./routes/adminRoute"));
fastify.register(require("./routes/storeRoute")); 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 // 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 // Also allows deletion of a user with a given phone number

@ -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};

@ -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();
};
Loading…
Cancel
Save