You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
4.6 KiB
149 lines
4.6 KiB
3 years ago
|
const fastify = require("fastify")({
|
||
|
logger: true,
|
||
|
});
|
||
|
const boom = require("boom");
|
||
|
const customJwtAuth = require("../customAuthJwt");
|
||
|
|
||
|
const bcrypt = require("bcrypt");
|
||
|
const saltRounds = 10;
|
||
|
|
||
|
|
||
|
|
||
|
//Get the data models
|
||
|
const { Supplier ,ProfilePicture, generateSupplierId} = require('../models/supplier');
|
||
|
|
||
|
|
||
|
async function bcryptPassword(password) {
|
||
|
encryptedPwd = bcrypt.hash(password, saltRounds);
|
||
|
return encryptedPwd;
|
||
|
}
|
||
|
|
||
|
async function bcryptComparePassword(pwd, encpassword) {
|
||
|
isSame = bcrypt.compare(pwd, encpassword);
|
||
|
return isSame;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
//Supplier Login Controller
|
||
|
|
||
|
exports.loginSupplier = async (req) => {
|
||
|
try {
|
||
|
const phone = req.body.phone;
|
||
|
const password = req.body.password;
|
||
|
|
||
|
const supplier = await Supplier.findOne({ phone: phone });
|
||
|
// compare supplier password with what is supplied
|
||
|
if (supplier) {
|
||
|
isSame = await bcryptComparePassword(
|
||
|
password,
|
||
|
supplier.services.password.bcrypt
|
||
|
);
|
||
|
// if password supplied matches return object
|
||
|
if (isSame) {
|
||
|
return { same: true, supplier: supplier };
|
||
|
} else {
|
||
|
return { same: false };
|
||
|
}
|
||
|
} else {
|
||
|
return { same: false };
|
||
|
}
|
||
|
} catch (err) {
|
||
|
throw boom.boomify(err);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
|
||
|
exports.addSupplier = async (req, reply) => {
|
||
|
try {
|
||
|
// await resetCounter();//to set customer id back to 0
|
||
|
var s_id = await generateSupplierId()
|
||
|
var building= ((req.body.suppliername).slice(0, 3)).toUpperCase();
|
||
|
var supplier_id = `AWS${building}${s_id}`
|
||
|
// console.log("This is the reply in the handler after the validations", reply);
|
||
|
s_data = {
|
||
|
supplierId: supplier_id,
|
||
|
suppliername: req.body.suppliername,
|
||
|
emails: req.body.emails,
|
||
|
password: req.body.password,
|
||
|
phone: req.body.phone,
|
||
|
profile: {
|
||
|
firstName: req.body.firstName,
|
||
|
lastName: req.body.lastName,
|
||
|
contactNumber: req.body.phone,
|
||
|
alternativeContactNumber: req.body.alternativeContactNumber,
|
||
|
country: req.body.country,
|
||
|
state: req.body.state,
|
||
|
city: req.body.city,
|
||
|
office_adress: req.body.office_adress,
|
||
|
zip: req.body.zip,
|
||
|
},
|
||
|
};
|
||
|
|
||
|
var supplier = new Supplier(s_data);
|
||
|
|
||
|
//password is not at the top level in the collection.
|
||
|
supplierpass = req.body.password;
|
||
|
|
||
|
// If fields are sent via form encoding , capture the fields and assign them to the user Object.
|
||
|
|
||
|
checkFormEncoding = isSupplierFormUrlEncoded(req);
|
||
|
if (checkFormEncoding.isSupplierFormUrlEncoded) {
|
||
|
suppliertobeInserted = checkFormEncoding.supplier;
|
||
|
console.log("thsi true url string");
|
||
|
supplier.suppliername = suppliertobeInserted.suppliername;
|
||
|
supplier.firstName = suppliertobeInserted.firstName;
|
||
|
supplier.lastName = suppliertobeInserted.lastName;
|
||
|
supplier.phone = suppliertobeInserted.phone;
|
||
|
supplier.emails = suppliertobeInserted.emails;
|
||
|
supplier.passsword = suppliertobeInserted.password;
|
||
|
supplier.supplierId = suppliertobeInserted.supplier_id;
|
||
|
supplier.office_adress = suppliertobeInserted.office_adress;
|
||
|
supplier.alternativeContactNumber = suppliertobeInserted.alternativeContactNumber;
|
||
|
|
||
|
}
|
||
|
|
||
|
console.log("---------checkurl ecnoded string-----------------------");
|
||
|
|
||
|
// Store hash in your password DB.
|
||
|
hash = await bcryptPassword(supplierpass);
|
||
|
|
||
|
if (hash) {
|
||
|
supplier.services.password.bcrypt = hash;
|
||
|
if (req.body.role) {
|
||
|
supplier.profile.role = req.body.role;
|
||
|
console.log("******************************************************");
|
||
|
console.log(supplier);
|
||
|
} else {
|
||
|
role = ["supplier"];
|
||
|
supplier.profile.role = role;
|
||
|
}
|
||
|
|
||
|
insertedSupplier = await supplier.save();
|
||
|
console.log(insertedSupplier);
|
||
|
if (insertedSupplier) {
|
||
|
// Prepare supplier object and wrap it inside the armintatankdata
|
||
|
var retSupplier = {
|
||
|
armintatankdata: {
|
||
|
suppliername: insertedSupplier.suppliername,
|
||
|
phone: insertedSupplier.phone,
|
||
|
supplierId: insertedSupplier.supplierId,
|
||
|
office_adress: insertedSupplier.office_adress,
|
||
|
emails: [
|
||
|
{
|
||
|
email: insertedSupplier.emails[0].email,
|
||
|
},
|
||
|
],
|
||
|
profile: insertedSupplier.profile,
|
||
|
},
|
||
|
status_code: 200,
|
||
|
};
|
||
|
|
||
|
return retSupplier;
|
||
|
}
|
||
|
}
|
||
|
} catch (err) {
|
||
|
throw boom.boomify(err);
|
||
|
}
|
||
|
};
|