diff --git a/src/services/index.js b/src/services/index.js index 06fe6e3..027cb45 100644 --- a/src/services/index.js +++ b/src/services/index.js @@ -2,6 +2,14 @@ class ServicesProvider { constructor() {} provide() { + const invitesService = require('../services/invitesService'); + const nostrService = require('../services/nostrService'); + const loginService = require('../services/loginService'); + const sessionService = require('../services/sessionService'); + const profileService = require('../services/profileService'); + const OfferServiceProvider = require('../services/offerService'); + const offerService = new OfferServiceProvider().provide(); + return { invitesService, nostrService, @@ -13,11 +21,4 @@ class ServicesProvider { } } -const invitesService = require('../services/invitesService'); -const nostrService = require('../services/nostrService'); -const loginService = require('../services/loginService'); -const sessionService = require('../services/sessionService'); -const profileService = require('../services/profileService'); -const offerService = require('../services/offerService'); - module.exports = ServicesProvider; diff --git a/src/services/offerService.js b/src/services/offerService.js index ab1f19e..25f7b6d 100644 --- a/src/services/offerService.js +++ b/src/services/offerService.js @@ -4,76 +4,18 @@ const models = require('../models'); const errors = require('../errors'); -async function createOffer(publicKey, offerDetails) { - const offerCreated = await models.OfferCreated.create({ - uuid: uuid.v7(), - public_key: publicKey, - created_at: new Date().toISOString(), - }); - - await models.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 deleteOffer(offerUuid) { - const offerExists = Boolean( - await models.OfferCreated.findOne({ where: { uuid: offerUuid } }) - ); - const offerHasBeenDeleted = Boolean( - await models.OfferDeleted.findOne({ where: { offer_uuid: offerUuid } }) - ); - - if (!offerExists || offerHasBeenDeleted) { - throw new errors.NotFoundError(`Could not find the offer.`); - } - - return models.OfferDeleted.create({ - uuid: uuid.v7(), - offer_uuid: offerUuid, - created_at: new Date().toISOString(), - }); -} - -async function getOffersByPublicKey(publicKey) { - const offers = await models.OfferCreated.findAll({ - where: { - public_key: publicKey, - }, - }); - - console.log(offers); - - if (!offers) { - return []; - } - - const offersToReturn = []; - if (offers) { - for (const someOffer of offers) { - const offerDetails = await models.OfferDetailsSet.findOne({ - where: { - offer_uuid: someOffer.uuid, - }, - order: [['created_at', 'DESC']], +class OfferServiceProvider { + provide() { + async function createOffer(publicKey, offerDetails) { + const offerCreated = await models.OfferCreated.create({ + uuid: uuid.v7(), + public_key: publicKey, + created_at: new Date().toISOString(), }); - offersToReturn.push({ - uuid: someOffer.uuid, - public_key: someOffer.public_key, + await models.OfferDetailsSet.create({ + uuid: uuid.v7(), + offer_uuid: offerCreated.uuid, wants: offerDetails.wants, premium: offerDetails.premium, trade_amount_eur: offerDetails.trade_amount_eur, @@ -86,12 +28,78 @@ async function getOffersByPublicKey(publicKey) { 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: someOffer.created_at, - last_updated_at: offerDetails.created_at, + created_at: new Date().toISOString(), }); } - } - return offersToReturn; + async function deleteOffer(offerUuid) { + const offerExists = Boolean( + await models.OfferCreated.findOne({ where: { uuid: offerUuid } }) + ); + const offerHasBeenDeleted = Boolean( + await models.OfferDeleted.findOne({ where: { offer_uuid: offerUuid } }) + ); + + if (!offerExists || offerHasBeenDeleted) { + throw new errors.NotFoundError(`Could not find the offer.`); + } + + return models.OfferDeleted.create({ + uuid: uuid.v7(), + offer_uuid: offerUuid, + created_at: new Date().toISOString(), + }); + } + + async function getOffersByPublicKey(publicKey) { + const offers = await models.OfferCreated.findAll({ + where: { + public_key: publicKey, + }, + }); + + console.log(offers); + + if (!offers) { + return []; + } + + const offersToReturn = []; + if (offers) { + for (const someOffer of offers) { + const offerDetails = await models.OfferDetailsSet.findOne({ + where: { + offer_uuid: someOffer.uuid, + }, + order: [['created_at', 'DESC']], + }); + + 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, + created_at: someOffer.created_at, + last_updated_at: offerDetails.created_at, + }); + } + } + + return offersToReturn; + } + + return { createOffer, getOffersByPublicKey, deleteOffer }; + } } -module.exports = { createOffer, getOffersByPublicKey, deleteOffer }; + +module.exports = OfferServiceProvider;