From 5c1fe0fe8680bae4057fe273c2f52178ea30ffb1 Mon Sep 17 00:00:00 2001 From: bhaskar Date: Tue, 30 May 2023 13:33:37 +0530 Subject: [PATCH] profile picture --- node_modules/.package-lock.json | 2 +- src/handlers/supplierHandler.js | 2 +- src/index.js | 2 ++ src/models/supplier.js | 62 +++++++++++++++++++++----------- src/routes/supplierRoute.js | 63 +++++++++++++++++++++++++++++++-- 5 files changed, 105 insertions(+), 26 deletions(-) diff --git a/node_modules/.package-lock.json b/node_modules/.package-lock.json index d7e36179..6d3474ff 100644 --- a/node_modules/.package-lock.json +++ b/node_modules/.package-lock.json @@ -1,7 +1,7 @@ { "name": "armintatankapi", "version": "1.0.0", - "lockfileVersion": 3, + "lockfileVersion": 2, "requires": true, "packages": { "node_modules/@adminjs/design-system": { diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index d72146fc..21196937 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -1257,7 +1257,7 @@ exports.uploadProfilePicture = async (req, res) => { } const supplierId = req.params.supplierId; - const picture = req.file.filename; // Assuming the file field in the request is named 'picture' + const picture = req.file.filename; let profilePicture = await ProfilePictureSupplier.findOne({ supplierId }); diff --git a/src/index.js b/src/index.js index aad38643..00e52e91 100644 --- a/src/index.js +++ b/src/index.js @@ -325,7 +325,9 @@ fastify.get('/testtemp', (req, reply) => { reply.view('layouts/main', {}); }); +const multipart = require('fastify-multipart'); +fastify.register(multipart); //fastify-auth plugin is required so we can define routes in seperate files and verify jwt supplied in preHandlers for each request. //const multer = require("fastify-multer"); fastify.register(require("fastify-auth")); diff --git a/src/models/supplier.js b/src/models/supplier.js index 7bd84f04..ca7af497 100644 --- a/src/models/supplier.js +++ b/src/models/supplier.js @@ -132,34 +132,54 @@ const supplierSchema = new mongoose.Schema( fcmId: { type: String, default: null }, }); + // const profilePictureSupplierSchema = new Schema({ + // // supplierId: { + // // type: String,unique: true, + // // required: true + // // }, + // // // picture: { + // // // type: String, + // // // required: true + // // // } + // // picture: { + // // type: String, + // // required: true, + // // validate: { + // // validator: function (value) { + // // const supportedFormats = ['jpg', 'jpeg', 'png']; + // // const fileExtension = value.split('.').pop().toLowerCase(); + // // return supportedFormats.includes(fileExtension); + // // }, + // // message: 'Picture must be a JPEG, PNG or JPG image' + // // } + // // } + // supplierId: { + // type: String, + // unique: true, + // required: true + // }, + // picture: { + // type: Buffer, + // required: true, + // validate: { + // validator: function (value) { + // const supportedFormats = ['jpg', 'jpeg', 'png']; + // const fileExtension = value.split('.').pop().toLowerCase(); + // return supportedFormats.includes(fileExtension); + // }, + // message: 'Picture must be a JPEG, PNG, or JPG image' + // } + // } + // }); + const profilePictureSupplierSchema = new Schema({ - // supplierId: { - // type: String,unique: true, - // required: true - // }, - // // picture: { - // // type: String, - // // required: true - // // } - // picture: { - // type: String, - // required: true, - // validate: { - // validator: function (value) { - // const supportedFormats = ['jpg', 'jpeg', 'png']; - // const fileExtension = value.split('.').pop().toLowerCase(); - // return supportedFormats.includes(fileExtension); - // }, - // message: 'Picture must be a JPEG, PNG or JPG image' - // } - // } supplierId: { type: String, unique: true, required: true }, picture: { - type: Buffer, // Store the file as binary data + type: String, // Change the type to String required: true, validate: { validator: function (value) { diff --git a/src/routes/supplierRoute.js b/src/routes/supplierRoute.js index 250ce4fb..fb55711a 100644 --- a/src/routes/supplierRoute.js +++ b/src/routes/supplierRoute.js @@ -577,6 +577,59 @@ module.exports = function (fastify, opts, next) { // }); + // fastify.route({ + // method: 'POST', + // url: '/api/users/profile-picture-supplier', + // schema: { + // tags: ['Supplier'], + // description: 'Upload a profile picture for a supplier', + // summary: 'Upload a profile picture for a supplier', + // params: { + // type: 'object', + // properties: { + // supplierId: { + // type: 'string', + // description: 'Supplier ID', + // }, + // }, + // }, + // consumes: ['multipart/form-data'], + // body: { + // type: 'object', + // properties: { + // picture: { + // type: 'string', + // description: 'Profile picture file', + // }, + // }, + // required: ['picture'], + // }, + // response: { + // 200: { + // description: 'Profile picture uploaded successfully', + // type: 'object', + // properties: { + // message: { type: 'string' }, + // }, + // }, + // 400: { + // description: 'Failed to upload profile picture', + // type: 'object', + // properties: { + // error: { type: 'string' }, + // }, + // }, + // 500: { + // description: 'Internal server error', + // type: 'object', + // properties: { + // error: { type: 'string' }, + // }, + // }, + // }, + // }, + // handler: validationHandler.uploadProfilePicture, + // }); fastify.route({ method: 'POST', url: '/api/users/profile-picture-supplier/:supplierId', @@ -598,8 +651,13 @@ module.exports = function (fastify, opts, next) { type: 'object', properties: { picture: { - type: 'string', - description: 'Profile picture file', + type: 'object', + properties: { + data: { type: 'string' }, // Remove the "format" property + encoding: { type: 'string' }, + filename: { type: 'string' }, + }, + required: ['data', 'encoding', 'filename'], // Add required properties }, }, required: ['picture'], @@ -630,7 +688,6 @@ module.exports = function (fastify, opts, next) { }, handler: validationHandler.uploadProfilePicture, }); - const multer = require('multer');