2021-10-05 12:15:57 +03:00
|
|
|
const Offer = require('./Offer');
|
|
|
|
const OfferMapper = require('./OfferMapper');
|
|
|
|
const UniqueChecker = require('./UniqueChecker');
|
|
|
|
|
|
|
|
class OffersAPI {
|
|
|
|
/**
|
|
|
|
* @param {import('./OfferRepository')} repository
|
|
|
|
*/
|
|
|
|
constructor(repository) {
|
|
|
|
this.repository = repository;
|
|
|
|
}
|
|
|
|
|
2021-10-05 17:25:56 +03:00
|
|
|
/**
|
|
|
|
* @param {object} data
|
|
|
|
* @param {string} data.id
|
|
|
|
*
|
|
|
|
* @returns {Promise<OfferMapper.OfferDTO>}
|
|
|
|
*/
|
|
|
|
async getOffer(data) {
|
|
|
|
return this.repository.createTransaction(async (transaction) => {
|
|
|
|
const options = {transacting: transaction};
|
|
|
|
|
|
|
|
const offer = await this.repository.getById(data.id, options);
|
|
|
|
|
|
|
|
return OfferMapper.toDTO(offer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:15:57 +03:00
|
|
|
/**
|
|
|
|
* @param {object} data
|
|
|
|
*
|
|
|
|
* @returns {Promise<OfferMapper.OfferDTO>}
|
|
|
|
*/
|
|
|
|
async createOffer(data) {
|
|
|
|
return this.repository.createTransaction(async (transaction) => {
|
|
|
|
const options = {transacting: transaction};
|
|
|
|
const uniqueChecker = new UniqueChecker(this.repository, transaction);
|
|
|
|
|
|
|
|
const offer = await Offer.create(data, uniqueChecker);
|
|
|
|
|
|
|
|
await this.repository.save(offer, options);
|
|
|
|
|
|
|
|
return OfferMapper.toDTO(offer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} data
|
|
|
|
* @param {string} data.id
|
|
|
|
* @param {string} [data.name]
|
2021-10-06 13:07:03 +03:00
|
|
|
* @param {string} [data.display_title]
|
|
|
|
* @param {string} [data.display_description]
|
2021-10-05 12:15:57 +03:00
|
|
|
* @param {string} [data.code]
|
|
|
|
*
|
|
|
|
* @returns {Promise<OfferMapper.OfferDTO>}
|
|
|
|
*/
|
|
|
|
async updateOffer(data) {
|
|
|
|
return await this.repository.createTransaction(async (transaction) => {
|
|
|
|
const options = {transacting: transaction};
|
|
|
|
const uniqueChecker = new UniqueChecker(this.repository, transaction);
|
|
|
|
|
|
|
|
const offer = await this.repository.getById(data.id, options);
|
|
|
|
|
|
|
|
if (data.name) {
|
2021-10-06 13:07:03 +03:00
|
|
|
await offer.updateName(data.name, uniqueChecker);
|
2021-10-05 12:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data.code) {
|
2021-10-06 13:07:03 +03:00
|
|
|
await offer.updateCode(data.code, uniqueChecker);
|
2021-10-05 12:15:57 +03:00
|
|
|
}
|
|
|
|
|
2021-10-06 13:07:03 +03:00
|
|
|
if (data.display_title) {
|
|
|
|
offer.displayTitle = data.display_title;
|
2021-10-05 12:15:57 +03:00
|
|
|
}
|
|
|
|
|
2021-10-06 13:07:03 +03:00
|
|
|
if (data.display_description) {
|
|
|
|
offer.displayDescription = data.display_description;
|
2021-10-05 12:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
await this.repository.save(offer, options);
|
|
|
|
|
|
|
|
return OfferMapper.toDTO(offer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Promise<OfferMapper.OfferDTO[]>}
|
|
|
|
*/
|
|
|
|
async listOffers() {
|
|
|
|
return await this.repository.createTransaction(async (transaction) => {
|
|
|
|
const options = {transacting: transaction};
|
|
|
|
|
|
|
|
const offers = await this.repository.getAll(options);
|
|
|
|
|
|
|
|
return offers.map(OfferMapper.toDTO);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = OffersAPI;
|