diff --git a/src/routes/apiRoutes.js b/src/routes/apiRoutes.js index 547cdb3..7ff2616 100644 --- a/src/routes/apiRoutes.js +++ b/src/routes/apiRoutes.js @@ -234,4 +234,31 @@ router.post( } ); +router.get( + '/publickey-offers', + rejectIfNotAuthorizedMiddleware, + attachPublicKeyMiddleware, + async (req, res) => { + console.log('elo'); + const publicKey = req.cookies.publicKey; + + const offers = await offerService.getOffersByPublicKey(publicKey); + + if (!offers) { + return res.status(404).json({ + success: true, + message: 'No offers posted by this public key.', + }); + } + + if (offers) { + return res.status(200).json({ + success: true, + message: 'Offers found', + data: offers, + }); + } + } +); + module.exports = router; diff --git a/src/services/offerService.js b/src/services/offerService.js index 56ef780..4defc25 100644 --- a/src/services/offerService.js +++ b/src/services/offerService.js @@ -27,4 +27,51 @@ async function createOffer(publicKey, offerDetails) { created_at: new Date().toISOString(), }); } -module.exports = { createOffer }; + +async function getOffersByPublicKey(publicKey) { + const offers = await OfferCreated.findAll({ + where: { + public_key: publicKey, + }, + }); + + console.log(offers); + + if (!offers) { + return []; + } + + const offersToReturn = []; + if (offers) { + for (const someOffer of offers) { + const offerDetails = await OfferDetailsSet.findOne({ + where: { + offer_uuid: someOffer.uuid, + }, + order: [['created_at', 'DESC']], + }); + + console.log(offerDetails); + + offersToReturn.push({ + uuid: someOffer.uuid, + public_key: someOffer.public_key, + wants: offerDetails.wants, + premium: offerDetails.premium, + trade_amount_eur: offerDetails.trade_amount_eur, + location_details: offerDetails.location_details, + time_availability_details: offerDetails.time_availability_details, + show_offer_to_trusted: offerDetails.show_offer_to_trusted, + show_offer_to_trusted_trusted: + offerDetails.show_offer_to_trusted_trusted, + show_offer_to_all_members: offerDetails.show_offer_to_all_members, + is_onchain_accepted: offerDetails.is_onchain_accepted, + is_lightning_accepted: offerDetails.is_lightning_accepted, + are_big_notes_accepted: offerDetails.are_big_notes_accepted, + }); + } + } + + return offersToReturn; +} +module.exports = { createOffer, getOffersByPublicKey };