diff --git a/src/controllers/supplierController.js b/src/controllers/supplierController.js index 00926c5d..7cf9437e 100644 --- a/src/controllers/supplierController.js +++ b/src/controllers/supplierController.js @@ -87,7 +87,7 @@ exports.loginSupplier = async (req) => { //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. + // If fields are sent via form encoding , capture the fields and assign them to the supplier Object. checkFormEncoding = isSupplierFormUrlEncoded(req); if (checkFormEncoding.isSupplierFormUrlEncoded) { @@ -147,4 +147,63 @@ exports.loginSupplier = async (req) => { } catch (err) { throw boom.boomify(err); } - }; \ No newline at end of file + }; + + exports.editCuurentSupplierInfo = async (req, reply) => { + try { + const { supplierId } = req.params; + const supplierInfo = await Supplier.findOne({ supplierId: supplierId.toString() }); + const updateData = req.body; + + if (updateData.firstName) supplierInfo.profile.firstName = updateData.firstName; + if (updateData.lastName) supplierInfo.profile.lastName = updateData.lastName; + if (updateData.suppliername) supplierInfo.suppliername = updateData.suppliername; + if (updateData.phone) supplierInfo.profile.contactNumber = updateData.phone; + if (updateData.office_address) supplierInfo.profile.office_address = updateData.office_address; + if (updateData.alternativeContactNumber) supplierInfo.profile.alternativeContactNumber = updateData.alternativeContactNumber; + if (updateData.city) supplierInfo.profile.city = updateData.city; + if (updateData.state) supplierInfo.profile.state = updateData.state; + if (updateData.country) supplierInfo.profile.country = updateData.country; + if (updateData.zip) supplierInfo.profile.zip = updateData.zip; + if (updateData.phone) supplierInfo.phone = updateData.phone; + if (updateData.emails) supplierInfo.emails = updateData.emails; + console.log(supplierInfo.emails[0].email) + if (updateData.role) supplierInfo.profile.role = updateData.role; + + if (updateData.phone) { + const phoneNumber = updateData.phone //libphonenumberjs.parsePhoneNumber(updateData.phone); + if (phoneNumber) { + // access returned collection + if (!phoneNumber) { //if (!phoneNumber.isValid()) { + error = { + armintatankdata: { + error: true, + code: 10002, + message: + "10002 - Phone # " + + updateData.phone + + " is not a valid phone number", + }, + }; + req.body.regError = error; + reply.status(406).send(error); + } + } + } + + if (supplierInfo.phone == updateData.phone) { + console.log("IF++++++++++++++="); + supplierInfo.phone = updateData.phone; + supplierInfo.phoneVerified = true; + } else { + console.log("Ilse++++++++++++++="); + supplierInfo.phone = updateData.phone; + supplierInfo.phoneVerified = false; + } + const supplier = await supplierInfo.save(); + return supplier; + } catch (err) { + throw boom.boomify(err); + } + }; + \ No newline at end of file diff --git a/src/routes/supplierRoute.js b/src/routes/supplierRoute.js index 7cc3cf04..9e363a8c 100644 --- a/src/routes/supplierRoute.js +++ b/src/routes/supplierRoute.js @@ -189,6 +189,57 @@ module.exports = function (fastify, opts, next) { }); + fastify.route({ + method: "PUT", + url: "/api/update/currentSupplier/:supplierId", + schema: { + tags: ["Supplier-Data"], + summary: "This is for update current supplier", + description: "This is for update current supplier", + params: { + type: "object", + properties: { + supplierId: { + type: "string", + description: "supplierId", + }, + }, + }, + body: { + type: "object", + properties: { + phone: { type: "string" }, + firstName: { type: "string" }, + lastName: { type: "string" }, + suppliername: { type: "string" }, + emails: { + type: "array", + maxItems: 2, + items: { + type: "object", + properties: { + email: { type: "string", default: null }, + }, + }, + }, + office_address: { type: "string" }, + alternativeContactNumber: { type: "string" }, + city: { type: "string" }, + state: { type: "string" }, + country: { type: "string" }, + zip: { type: "string" }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: [fastify.auth([fastify.authenticate])], + handler: supplierController.editCuurentSupplierInfo, + }); + next(); }