diff --git a/src/controllers/admincontroller.js b/src/controllers/admincontroller.js new file mode 100644 index 00000000..1ce9557f --- /dev/null +++ b/src/controllers/admincontroller.js @@ -0,0 +1,73 @@ +const Admin = require('../models/admin') +const boom = require("boom"); +const jwt = require('jsonwebtoken') +const bcrypt = require('bcrypt') + +const fastify = require("fastify"); + +exports.adminSignUp = async (request, reply) => { + + try { + const { email, password } = request.body + + // Check if an admin with the same email address already exists + const existingAdmin = await Admin.findOne({ email }) + + if (existingAdmin) { + return reply.status(400).send({ message: 'Email already registered' }) + } + + // Hash the password using bcrypt + const hashedPassword = await bcrypt.hash(password, 10) + + // Create a new admin object with the hashed password + const admin = new Admin({ email, password: hashedPassword }) + + // Save the new admin to the database + await admin.save() + + // Generate a JWT token for the new admin + // const token = jwt.sign({ email: admin.email }, 'secret') + + // // Return the token to the client + // return { token } + reply.send({message : "Admin Account Created Sucessfully"}) + } catch (err) { + reply.status(500).send({ message: err.message }) + } + } + + + + + exports.adminLogin = async (request, reply) => { + try { + const { email, password } = request.body + + // Check if an admin with the email address exists + const admin = await Admin.findOne({ email }) + + if (!admin) { + return reply.status(401).send({ message: 'Invalid email or password' }) + } + + // Compare the password entered by the user with the hashed password stored in the database + const isPasswordValid = await bcrypt.compare(password, admin.password) + + if (!isPasswordValid) { + return reply.status(401).send({ message: 'Invalid email or password' }) + } + + // Generate a JWT token for the authenticated admin + const token = jwt.sign({ email: admin.email }, 'secret') + + // Return the token to the client + return { token } + } catch (err) { + reply.status(500).send({ message: err.message }) + } + } + + + + \ No newline at end of file diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index d9bb1cbf..b91a8670 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -12,6 +12,7 @@ const fastify = require("fastify")({ return uuidv4(); }, }); +const moment = require('moment'); const fastifyEnv = require("fastify-env"); @@ -1017,8 +1018,10 @@ exports.getPendingSuppliers = async (req, reply) => { (request) => request.supplierId ); console.log(supplierIdsToInclude, "SUPLIERINCLUDE"); - const timestamps = friendRequests.map((request) => request.timestamp); - console.log(timestamps, "timestamps"); + const timestamps = friendRequests.map(request => + moment(request.timestamp).format("DD-MM-YYYY hh:mm:ss") + ); + console.log(timestamps, "timestamps"); await Supplier.find({ supplierId: { $in: supplierIdsToInclude } }) .limit(limit) .skip(startindex) @@ -1133,8 +1136,9 @@ exports.getPendingCustomers = async (req, reply) => { const supplierIdsToInclude = friendRequests.map( (request) => request.customerId ); - const timestamps = friendRequests.map((request) => request.timestamp); - + const timestamps = friendRequests.map(request => + moment(request.timestamp).format("DD-MM-YYYY hh:mm:ss") + ); await User.find({ customerId: { $in: supplierIdsToInclude } }) .limit(limit) .skip(startindex) diff --git a/src/index.js b/src/index.js index 2e328572..0af4d460 100644 --- a/src/index.js +++ b/src/index.js @@ -342,6 +342,7 @@ fastify.register(require("./routes/tankersRoute.js")); fastify.register(require("./routes/supplierRoute")); fastify.register(require("./routes/supplierOrdersRoutes")); fastify.register(require("./routes/friendRequestRoute")); +fastify.register(require("./routes/adminRoute")); // 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/admin.js b/src/models/admin.js new file mode 100644 index 00000000..29de8268 --- /dev/null +++ b/src/models/admin.js @@ -0,0 +1,18 @@ +const mongoose = require('mongoose') + +const adminSchema = new mongoose.Schema({ + email: { + type: String, + required: true, + unique: true, + lowercase: true + }, + password: { + type: String, + required: true + } +}) + +const Admin = mongoose.model('Admin', adminSchema) + +module.exports = Admin diff --git a/src/models/supplier.js b/src/models/supplier.js index da313600..39afa54d 100644 --- a/src/models/supplier.js +++ b/src/models/supplier.js @@ -2,6 +2,7 @@ const mongoose = require("mongoose"); const Schema = mongoose.Schema; const ObjectId = Schema.Types.ObjectId; +const moment = require('moment'); const code = Math.floor(100000 + Math.random() * 900000); @@ -102,7 +103,9 @@ const supplierSchema = new mongoose.Schema( customerId: { type: String, default: null }, supplierId: { type: String, default: null }, status: { type: String, default: "pending" }, - timestamp: { type: Date, default: Date.now } + // timestamp: { type: Date, default: Date.now } + timestamp: { type: String, default: moment().format('DD-MM-YY hh:mm:ss') }, + }); diff --git a/src/routes/adminRoute.js b/src/routes/adminRoute.js new file mode 100644 index 00000000..9d17c75b --- /dev/null +++ b/src/routes/adminRoute.js @@ -0,0 +1,51 @@ +const fastify = require("fastify"); +const adminController = require('../controllers/admincontroller') + + +module.exports = function (fastify, opts, next) { +fastify.route({ + method: "POST", + url: "/api/adminSignup", + schema: { + tags: ["Admin"], + description: "This is for cretae New Admin Account", + summary: "This is for cretae New Admin Account", + body: { + type: "object", + properties: { + email: { type: "string" }, + password: { type: "string" }, + + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + + handler: adminController.adminSignUp, + + }); + + fastify.post("/api/adminLogin", { + schema: { + description: "This is for Login Admin", + tags: ["Admin"], + summary: "This is for Login Admin", + body: { + type: "object", + required: ["email", "password"], + properties: { + email: { type: "string" }, + password: { type: "string" }, + }, + }, + }, + handler: adminController.adminLogin, +}); + + +next(); +};