offer service

This commit is contained in:
counterweight 2025-03-07 16:06:44 +01:00
parent ec725f8e4e
commit bf0775ba31
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
2 changed files with 24 additions and 19 deletions

View file

@ -45,7 +45,10 @@ class ServicesProvider {
const ProfileServiceProvider = require('../services/profileService'); const ProfileServiceProvider = require('../services/profileService');
const profileService = new ProfileServiceProvider({ models }).provide(); const profileService = new ProfileServiceProvider({ models }).provide();
const OfferServiceProvider = require('../services/offerService'); const OfferServiceProvider = require('../services/offerService');
const offerService = new OfferServiceProvider().provide(); const offerService = new OfferServiceProvider({
models,
errors,
}).provide();
return { return {
invitesService, invitesService,

View file

@ -1,19 +1,19 @@
const uuid = require('uuid'); const uuid = require('uuid');
const models = require('../models');
const errors = require('../errors');
class OfferServiceProvider { class OfferServiceProvider {
constructor({ models, errors }) {
this.models = models;
this.errors = errors;
}
provide() { provide() {
async function createOffer(publicKey, offerDetails) { const createOffer = async (publicKey, offerDetails) => {
const offerCreated = await models.OfferCreated.create({ const offerCreated = await this.models.OfferCreated.create({
uuid: uuid.v7(), uuid: uuid.v7(),
public_key: publicKey, public_key: publicKey,
created_at: new Date().toISOString(), created_at: new Date().toISOString(),
}); });
await models.OfferDetailsSet.create({ await this.models.OfferDetailsSet.create({
uuid: uuid.v7(), uuid: uuid.v7(),
offer_uuid: offerCreated.uuid, offer_uuid: offerCreated.uuid,
wants: offerDetails.wants, wants: offerDetails.wants,
@ -30,29 +30,31 @@ class OfferServiceProvider {
are_big_notes_accepted: offerDetails.are_big_notes_accepted, are_big_notes_accepted: offerDetails.are_big_notes_accepted,
created_at: new Date().toISOString(), created_at: new Date().toISOString(),
}); });
} };
async function deleteOffer(offerUuid) { const deleteOffer = async (offerUuid) => {
const offerExists = Boolean( const offerExists = Boolean(
await models.OfferCreated.findOne({ where: { uuid: offerUuid } }) await this.models.OfferCreated.findOne({ where: { uuid: offerUuid } })
); );
const offerHasBeenDeleted = Boolean( const offerHasBeenDeleted = Boolean(
await models.OfferDeleted.findOne({ where: { offer_uuid: offerUuid } }) await this.models.OfferDeleted.findOne({
where: { offer_uuid: offerUuid },
})
); );
if (!offerExists || offerHasBeenDeleted) { if (!offerExists || offerHasBeenDeleted) {
throw new errors.NotFoundError(`Could not find the offer.`); throw new this.errors.NotFoundError(`Could not find the offer.`);
} }
return models.OfferDeleted.create({ return this.models.OfferDeleted.create({
uuid: uuid.v7(), uuid: uuid.v7(),
offer_uuid: offerUuid, offer_uuid: offerUuid,
created_at: new Date().toISOString(), created_at: new Date().toISOString(),
}); });
} };
async function getOffersByPublicKey(publicKey) { const getOffersByPublicKey = async (publicKey) => {
const offers = await models.OfferCreated.findAll({ const offers = await this.models.OfferCreated.findAll({
where: { where: {
public_key: publicKey, public_key: publicKey,
}, },
@ -67,7 +69,7 @@ class OfferServiceProvider {
const offersToReturn = []; const offersToReturn = [];
if (offers) { if (offers) {
for (const someOffer of offers) { for (const someOffer of offers) {
const offerDetails = await models.OfferDetailsSet.findOne({ const offerDetails = await this.models.OfferDetailsSet.findOne({
where: { where: {
offer_uuid: someOffer.uuid, offer_uuid: someOffer.uuid,
}, },
@ -96,7 +98,7 @@ class OfferServiceProvider {
} }
return offersToReturn; return offersToReturn;
} };
return { createOffer, getOffersByPublicKey, deleteOffer }; return { createOffer, getOffersByPublicKey, deleteOffer };
} }