offer service
This commit is contained in:
parent
ec725f8e4e
commit
bf0775ba31
2 changed files with 24 additions and 19 deletions
|
|
@ -45,7 +45,10 @@ class ServicesProvider {
|
|||
const ProfileServiceProvider = require('../services/profileService');
|
||||
const profileService = new ProfileServiceProvider({ models }).provide();
|
||||
const OfferServiceProvider = require('../services/offerService');
|
||||
const offerService = new OfferServiceProvider().provide();
|
||||
const offerService = new OfferServiceProvider({
|
||||
models,
|
||||
errors,
|
||||
}).provide();
|
||||
|
||||
return {
|
||||
invitesService,
|
||||
|
|
|
|||
|
|
@ -1,19 +1,19 @@
|
|||
const uuid = require('uuid');
|
||||
|
||||
const models = require('../models');
|
||||
|
||||
const errors = require('../errors');
|
||||
|
||||
class OfferServiceProvider {
|
||||
constructor({ models, errors }) {
|
||||
this.models = models;
|
||||
this.errors = errors;
|
||||
}
|
||||
provide() {
|
||||
async function createOffer(publicKey, offerDetails) {
|
||||
const offerCreated = await models.OfferCreated.create({
|
||||
const createOffer = async (publicKey, offerDetails) => {
|
||||
const offerCreated = await this.models.OfferCreated.create({
|
||||
uuid: uuid.v7(),
|
||||
public_key: publicKey,
|
||||
created_at: new Date().toISOString(),
|
||||
});
|
||||
|
||||
await models.OfferDetailsSet.create({
|
||||
await this.models.OfferDetailsSet.create({
|
||||
uuid: uuid.v7(),
|
||||
offer_uuid: offerCreated.uuid,
|
||||
wants: offerDetails.wants,
|
||||
|
|
@ -30,29 +30,31 @@ class OfferServiceProvider {
|
|||
are_big_notes_accepted: offerDetails.are_big_notes_accepted,
|
||||
created_at: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
async function deleteOffer(offerUuid) {
|
||||
const deleteOffer = async (offerUuid) => {
|
||||
const offerExists = Boolean(
|
||||
await models.OfferCreated.findOne({ where: { uuid: offerUuid } })
|
||||
await this.models.OfferCreated.findOne({ where: { uuid: offerUuid } })
|
||||
);
|
||||
const offerHasBeenDeleted = Boolean(
|
||||
await models.OfferDeleted.findOne({ where: { offer_uuid: offerUuid } })
|
||||
await this.models.OfferDeleted.findOne({
|
||||
where: { offer_uuid: offerUuid },
|
||||
})
|
||||
);
|
||||
|
||||
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(),
|
||||
offer_uuid: offerUuid,
|
||||
created_at: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
async function getOffersByPublicKey(publicKey) {
|
||||
const offers = await models.OfferCreated.findAll({
|
||||
const getOffersByPublicKey = async (publicKey) => {
|
||||
const offers = await this.models.OfferCreated.findAll({
|
||||
where: {
|
||||
public_key: publicKey,
|
||||
},
|
||||
|
|
@ -67,7 +69,7 @@ class OfferServiceProvider {
|
|||
const offersToReturn = [];
|
||||
if (offers) {
|
||||
for (const someOffer of offers) {
|
||||
const offerDetails = await models.OfferDetailsSet.findOne({
|
||||
const offerDetails = await this.models.OfferDetailsSet.findOne({
|
||||
where: {
|
||||
offer_uuid: someOffer.uuid,
|
||||
},
|
||||
|
|
@ -96,7 +98,7 @@ class OfferServiceProvider {
|
|||
}
|
||||
|
||||
return offersToReturn;
|
||||
}
|
||||
};
|
||||
|
||||
return { createOffer, getOffersByPublicKey, deleteOffer };
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue