diff --git a/src/services/index.js b/src/services/index.js index 5b9fee5..1ec7ea2 100644 --- a/src/services/index.js +++ b/src/services/index.js @@ -44,6 +44,7 @@ class ServicesProvider { const offerService = new OfferServiceProvider({ models: this.models, errors: this.errors, + sequelize: this.sequelize, }).provide(); return { diff --git a/src/services/offerService.js b/src/services/offerService.js index 09c4079..bd65fd8 100644 --- a/src/services/offerService.js +++ b/src/services/offerService.js @@ -1,35 +1,48 @@ const uuid = require('uuid'); class OfferServiceProvider { - constructor({ models, errors }) { + constructor({ models, errors, sequelize }) { this.models = models; this.errors = errors; + this.sequelize = sequelize; } provide() { const createOffer = async (publicKey, offerDetails) => { - const offerCreated = await this.models.OfferCreated.create({ - uuid: uuid.v7(), - public_key: publicKey, - created_at: new Date().toISOString(), - }); + const createOfferTransaction = await this.sequelize.transaction(); + try { + const offerCreated = await this.models.OfferCreated.create( + { + uuid: uuid.v7(), + public_key: publicKey, + created_at: new Date().toISOString(), + }, + { transaction: createOfferTransaction } + ); - await this.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(), - }); + await this.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(), + }, + { transaction: createOfferTransaction } + ); + await createOfferTransaction.commit(); + } catch (error) { + await createOfferTransaction.rollback(); + } }; const deleteOffer = async (offerUuid) => {