@@ -2,7 +2,7 @@ import { generateToken } from "../configs/utils.js";
22import User from "../models/user.js" ;
33import bcrypt from "bcryptjs" ;
44import randomstring from "randomstring" ;
5- import * as brevo from ' @getbrevo/brevo' ;
5+ import * as brevo from " @getbrevo/brevo" ;
66import { v2 as cloudinary } from "cloudinary" ;
77import { extractPublicId } from "cloudinary-build-url" ;
88import dotenv , { config } from "dotenv" ;
@@ -103,29 +103,36 @@ export const updateProfilePic = async (req, res) => {
103103 try {
104104 const userId = req . user . _id ;
105105 const { profilePic } = req . body ;
106+
106107 if ( ! profilePic ) {
107108 return res
108109 . status ( 400 )
109110 . json ( { message : "Please provide a profile picture" } ) ;
110111 }
112+
111113 const existingUser = await User . findById ( { _id : userId } ) ;
114+
112115 if ( existingUser ?. profilePic ) {
113116 const publicId = extractPublicId ( existingUser . profilePic ) ;
114117 // console.log(publicId);
115118 const result = await cloudinary . uploader . destroy ( publicId ) ;
116119 }
120+
117121 let uploadImage = await cloudinary . uploader . upload ( profilePic , {
118122 folder : "chatzy/profile_pictures" ,
119123 } ) ;
124+
120125 const user = await User . findByIdAndUpdate (
121126 userId ,
122127 { profilePic : uploadImage . secure_url } ,
123128 { new : true }
124129 ) ;
130+
125131 await user . save ( ) ;
126132 res
127133 . status ( 200 )
128134 . json ( { user, message : "Profile Picture Updated Successfully!" } ) ;
135+
129136 } catch ( e ) {
130137 console . log ( e . message ) ;
131138 res . status ( 500 ) . json ( {
@@ -139,19 +146,24 @@ export const deleteProfilePic = async (req, res) => {
139146 if ( ! req . user . profilePic ) {
140147 return res . status ( 400 ) . json ( { message : "No profile picture to delete" } ) ;
141148 }
149+
142150 const publicId = extractPublicId ( req . user . profilePic ) ;
143151 // console.log(publicId);
152+
144153 const result = await cloudinary . uploader . destroy ( publicId ) ;
145154 const userId = req . user . _id ;
155+
146156 const user = await User . findByIdAndUpdate (
147157 userId ,
148158 { profilePic : "" } ,
149159 { new : true }
150160 ) ;
161+
151162 await user . save ( ) ;
152163 res
153164 . status ( 200 )
154165 . json ( { user, message : "Profile Picture Deleted Successfully!" } ) ;
166+
155167 } catch ( e ) {
156168 console . log ( e . message ) ;
157169 res . status ( 500 ) . json ( {
@@ -188,19 +200,19 @@ async function sendOtp(email, otp) {
188200 const sendSmtpEmail = new brevo . SendSmtpEmail ( ) ;
189201 sendSmtpEmail . subject = "OTP Verification" ;
190202 sendSmtpEmail . to = [ { email : email } ] ;
191- sendSmtpEmail . sender = {
192- name : "OTP Authentication" ,
193- email : process . env . MAIL_USER
203+ sendSmtpEmail . sender = {
204+ name : "OTP Authentication" ,
205+ email : process . env . MAIL_USER ,
194206 } ;
195207 sendSmtpEmail . textContent = `Your OTP for verification is ${ otp } . DO NOT share it with anyone.` ;
196208
197209 const result = await apiInstance . sendTransacEmail ( sendSmtpEmail ) ;
198- console . log ( ' OTP sent successfully via Brevo:' , result ?. body ?. messageId ) ;
210+ console . log ( " OTP sent successfully via Brevo:" , result ?. body ?. messageId ) ;
199211 return true ;
200212 } catch ( error ) {
201- console . error ( ' Brevo OTP sending failed:' , error . message ) ;
213+ console . error ( " Brevo OTP sending failed:" , error . message ) ;
202214 if ( error . response ) {
203- console . error ( ' Brevo error details:' , error . response . body ) ;
215+ console . error ( " Brevo error details:" , error . response . body ) ;
204216 }
205217 return false ;
206218 }
@@ -212,13 +224,13 @@ export const getOtp = async (req, res) => {
212224 console . log ( "Getting OTP for email:" , email ) ;
213225 console . log ( "BREVO_API_KEY set:" , ! ! process . env . BREVO_API_KEY ) ;
214226 console . log ( "MAIL_USER set:" , ! ! process . env . MAIL_USER ) ;
215-
227+
216228 const otp = generateOtp ( ) ;
217229 otpCache [ email ] = await bcrypt . hash ( otp , 10 ) ;
218230
219231 const result = await sendOtp ( email , otp ) ;
220232 console . log ( "Send OTP result:" , result ) ;
221-
233+
222234 if ( result ) {
223235 res . cookie ( "otpCache" , otpCache , {
224236 maxAge : 300000 ,
@@ -241,14 +253,14 @@ export const getOtp = async (req, res) => {
241253export const verifyOtp = async ( req , res ) => {
242254 const { formData, givenOTP } = req . body ;
243255 const actualOTPCache = req . cookies . otpCache ;
244-
256+
245257 if ( ! actualOTPCache ) {
246258 return res . status ( 400 ) . json ( { message : "OTP expired." } ) ;
247259 }
248260 if ( ! actualOTPCache . hasOwnProperty ( formData . email ) ) {
249261 return res . status ( 400 ) . json ( { message : "Email not found, try again" } ) ;
250262 }
251-
263+
252264 const decodedOtp = await bcrypt . compare (
253265 givenOTP . trim ( ) ,
254266 actualOTPCache [ formData . email ]
@@ -263,4 +275,4 @@ export const verifyOtp = async (req, res) => {
263275 } else {
264276 return res . status ( 400 ) . json ( { message : "Invalid OTP" } ) ;
265277 }
266- } ;
278+ } ;
0 commit comments