secajs/src/services/offerService.js

78 lines
2.5 KiB
JavaScript
Raw Normal View History

2025-02-26 00:48:16 +01:00
const uuid = require('uuid');
const OfferCreated = require('../models/OfferCreated');
const OfferDetailsSet = require('../models/OfferDetailsSet');
2025-02-25 17:00:29 +01:00
async function createOffer(publicKey, offerDetails) {
2025-02-26 00:48:16 +01:00
const offerCreated = await OfferCreated.create({
uuid: uuid.v7(),
public_key: publicKey,
created_at: new Date().toISOString(),
});
2025-02-26 23:56:06 +01:00
await OfferDetailsSet.create({
2025-02-26 00:48:16 +01:00
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(),
});
2025-02-25 17:00:29 +01:00
}
2025-02-28 23:58:51 +01:00
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 };