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({
models: this.models,
errors: this.errors,
sequelize: this.sequelize,
}).provide();
return {

View file

@ -1,19 +1,26 @@
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({
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({
await this.models.OfferDetailsSet.create(
{
uuid: uuid.v7(),
offer_uuid: offerCreated.uuid,
wants: offerDetails.wants,
@ -29,7 +36,13 @@ class OfferServiceProvider {
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) => {