offer service makes transaction for offer creation

This commit is contained in:
counterweight 2025-03-22 17:02:56 +01:00
parent 3b995dfc70
commit d19c057937
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
2 changed files with 37 additions and 23 deletions

View file

@ -44,6 +44,7 @@ class ServicesProvider {
const offerService = new OfferServiceProvider({ const offerService = new OfferServiceProvider({
models: this.models, models: this.models,
errors: this.errors, errors: this.errors,
sequelize: this.sequelize,
}).provide(); }).provide();
return { return {

View file

@ -1,35 +1,48 @@
const uuid = require('uuid'); const uuid = require('uuid');
class OfferServiceProvider { class OfferServiceProvider {
constructor({ models, errors }) { constructor({ models, errors, sequelize }) {
this.models = models; this.models = models;
this.errors = errors; this.errors = errors;
this.sequelize = sequelize;
} }
provide() { provide() {
const createOffer = async (publicKey, offerDetails) => { const createOffer = async (publicKey, offerDetails) => {
const offerCreated = await this.models.OfferCreated.create({ const createOfferTransaction = await this.sequelize.transaction();
uuid: uuid.v7(), try {
public_key: publicKey, const offerCreated = await this.models.OfferCreated.create(
created_at: new Date().toISOString(), {
}); uuid: uuid.v7(),
public_key: publicKey,
created_at: new Date().toISOString(),
},
{ transaction: createOfferTransaction }
);
await this.models.OfferDetailsSet.create({ await this.models.OfferDetailsSet.create(
uuid: uuid.v7(), {
offer_uuid: offerCreated.uuid, uuid: uuid.v7(),
wants: offerDetails.wants, offer_uuid: offerCreated.uuid,
premium: offerDetails.premium, wants: offerDetails.wants,
trade_amount_eur: offerDetails.trade_amount_eur, premium: offerDetails.premium,
location_details: offerDetails.location_details, trade_amount_eur: offerDetails.trade_amount_eur,
time_availability_details: offerDetails.time_availability_details, location_details: offerDetails.location_details,
show_offer_to_trusted: offerDetails.show_offer_to_trusted, time_availability_details: offerDetails.time_availability_details,
show_offer_to_trusted_trusted: show_offer_to_trusted: offerDetails.show_offer_to_trusted,
offerDetails.show_offer_to_trusted_trusted, show_offer_to_trusted_trusted:
show_offer_to_all_members: offerDetails.show_offer_to_all_members, offerDetails.show_offer_to_trusted_trusted,
is_onchain_accepted: offerDetails.is_onchain_accepted, show_offer_to_all_members: offerDetails.show_offer_to_all_members,
is_lightning_accepted: offerDetails.is_lightning_accepted, is_onchain_accepted: offerDetails.is_onchain_accepted,
are_big_notes_accepted: offerDetails.are_big_notes_accepted, is_lightning_accepted: offerDetails.is_lightning_accepted,
created_at: new Date().toISOString(), 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) => { const deleteOffer = async (offerUuid) => {