diff --git a/schemas/User&AdSchema.js b/schemas/User&AdSchema.js index d0bb962e20e9d249c090d2270914662e4b2bc478..a653fd1f64aa988ffb9d0d07298c5232308ddc3f 100644 --- a/schemas/User&AdSchema.js +++ b/schemas/User&AdSchema.js @@ -73,7 +73,6 @@ const typeDefs = ` price: Float! availabilityDate: String! photos: [String] - userName: String! } type User { @@ -106,9 +105,65 @@ const resolvers = { }, Mutation: { addAd: (_, { input, token }) => addAd(input, token), - updateAd: (_, { announcementId, input, token }) => updateAd(announcementId, input, token), - deleteAd: (_, { announcementId, token }) => deleteAd(announcementId, token), - askQuestion: (_, { announcementId, question, token }) => askQuestion(announcementId, question, token), + updateAd: async (_, { announcementId, input, token }) => { + try { + const result = await updateAd(announcementId, input, token); + + if (result.status === 200) { + return result.updatedAd; + } else { + throw new Error(result.message); + } + } catch (error) { + throw new Error('An error occurred while updating the ad.'); + } + }, + deleteAd: async (_, { announcementId, token }) => { + try { + const result = await deleteAd(announcementId, token); + + if (result.status === 200) { + return { + success: true, + message: result.message, + }; + } else { + return { + success: false, + message: result.message, + }; + } + } catch (error) { + return { + success: false, + message: 'An error occurred while deleting the ad.', + error: error.message, + }; + } + }, + askQuestion: async (_, { announcementId, question, token }) => { + try { + const result = await askQuestion(announcementId, question, token); + + if (result.status === 200) { + return { + success: true, + message: result.message, + }; + } else { + return { + success: false, + message: result.message, + }; + } + } catch (error) { + return { + success: false, + message: 'An error occurred while asking the question.', + error: error.message, + }; + } + }, answerQuestion: (_, { announcementId, questionId, answer, token }) => answerQuestion(announcementId, questionId, answer, token), createUser: async (_, { input }) => { diff --git a/service/AdService.js b/service/AdService.js index 18c98b7928a7fb1ea92dd431a20e1d4c51d18040..39d11dccafb13365ae95d595feb3b61f8d85ea34 100644 --- a/service/AdService.js +++ b/service/AdService.js @@ -8,6 +8,7 @@ const secretKey = 'secretKey'; // Service for adding an ad exports.addAd = async function (body, token) { try { + console.log(body); const { title, propertyType, publicationStatus, propertyStatus, description, price, availabilityDate} = body; const photos = []; /* @@ -30,7 +31,7 @@ exports.addAd = async function (body, token) { description, price, availabilityDate, - photos, + //photos, userName: user.username }); @@ -45,47 +46,38 @@ exports.addAd = async function (body, token) { // Méthode pour mettre à jour une annonce -exports.updateAd = function (announcementId, reqBody, token) { - return new Promise(async (resolve, reject) => { +exports.updateAd = async function (announcementId, reqBody, token) { try { const user = await extractUserFromToken(token, secretKey); - const { title, propertyType, publicationStatus, propertyStatus, description, price, availabilityDate } = reqBody; - /* - let photos = []; - const oldPhotos = reqBody.oldPhotos || []; - - reqFiles.forEach(file => { - photos.push(file.filename); - }); + const { title, propertyType, publicationStatus, description, price, availabilityDate } = reqBody; + + const updatedAnnouncement = await Ad.findByIdAndUpdate( + announcementId, + { + title, + propertyType, + publicationStatus, + description, + price, + availabilityDate, + }, + { new: true } + ); + + if (!updatedAnnouncement) { + return { status: 404, message: 'Annonce non trouvée.' }; + } - photos = photos.concat(oldPhotos); - */ - const updatedAnnouncement = await Ad.findByIdAndUpdate( - announcementId, - { - title, - propertyType, - publicationStatus, - propertyStatus, - description, - price, - availabilityDate, - //photos, - } - ); - if (!updatedAnnouncement) { - return resolve({status: 404, message: 'annonce non trouvée'}); - } - if (user.username !== updatedAnnouncement.userName) { - return resolve({status: 401, message: 'Utilisateur non autorisé'}); - } + if (user.username !== updatedAnnouncement.userName) { + return { status: 401, message: 'Utilisateur non autorisé.' }; + } - resolve({ status: 200, message: 'Annonce Modifié avec succès' }); + return { status: 200, message: 'Annonce modifiée avec succès.', updatedAd: updatedAnnouncement }; } catch (error) { - reject(error); + throw error; } - }); -} +}; + exports.deleteAd = function (announcementId, token) { return new Promise(async (resolve, reject) => { @@ -182,6 +174,7 @@ exports.askQuestion = async function(announcementId, question, token) { question }); await announcement.save(); + console.log(announcement, 'hhhhhhhhh') resolve({ status: 200, message: 'Question posée avec succès.' }); } catch (error) { throw error;