const uuid = require('uuid'); const OfferCreated = require('../models/OfferCreated'); const OfferDetailsSet = require('../models/OfferDetailsSet'); async function createOffer(publicKey, offerDetails) { const offerCreated = await OfferCreated.create({ uuid: uuid.v7(), public_key: publicKey, created_at: new Date().toISOString(), }); await OfferDetailsSet.create({ uuid: uuid.v7(), offer_uuid: offerCreated.uuid, 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, created_at: new Date().toISOString(), }); } 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 };