2021-10-08 13:27:17 +03:00
|
|
|
const Offer = require('../domain/models/Offer');
|
|
|
|
const OfferName = require('../domain/models/OfferName');
|
|
|
|
const OfferCode = require('../domain/models/OfferCode');
|
|
|
|
const OfferTitle = require('../domain/models/OfferTitle');
|
|
|
|
const OfferDescription = require('../domain/models/OfferDescription');
|
2021-10-12 18:11:52 +03:00
|
|
|
const OfferStatus = require('../domain/models/OfferStatus');
|
2021-10-05 12:15:57 +03:00
|
|
|
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);
|
|
|
|
|
2021-10-22 15:13:04 +03:00
|
|
|
if (!offer) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-10-05 17:25:56 +03:00
|
|
|
return OfferMapper.toDTO(offer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:15:57 +03:00
|
|
|
/**
|
2021-10-12 18:11:52 +03:00
|
|
|
* @param {any} data
|
2022-08-24 14:13:58 +03:00
|
|
|
* @param {Object} [options]
|
2021-10-05 12:15:57 +03:00
|
|
|
*
|
|
|
|
* @returns {Promise<OfferMapper.OfferDTO>}
|
|
|
|
*/
|
2022-08-24 14:13:58 +03:00
|
|
|
async createOffer(data, options = {}) {
|
2021-10-05 12:15:57 +03:00
|
|
|
return this.repository.createTransaction(async (transaction) => {
|
2022-08-24 14:13:58 +03:00
|
|
|
const saveOptions = {...options, transacting: transaction};
|
2021-10-05 12:15:57 +03:00
|
|
|
const uniqueChecker = new UniqueChecker(this.repository, transaction);
|
|
|
|
|
|
|
|
const offer = await Offer.create(data, uniqueChecker);
|
|
|
|
|
2022-08-24 14:13:58 +03:00
|
|
|
await this.repository.save(offer, saveOptions);
|
2021-10-05 12:15:57 +03:00
|
|
|
|
|
|
|
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]
|
2021-10-12 18:11:52 +03:00
|
|
|
* @param {string} [data.status]
|
2022-08-24 14:13:58 +03:00
|
|
|
* @param {Object} [options]
|
2021-10-05 12:15:57 +03:00
|
|
|
*
|
|
|
|
* @returns {Promise<OfferMapper.OfferDTO>}
|
|
|
|
*/
|
2022-08-24 14:13:58 +03:00
|
|
|
async updateOffer(data, options = {}) {
|
2021-10-05 12:15:57 +03:00
|
|
|
return await this.repository.createTransaction(async (transaction) => {
|
2022-08-24 14:13:58 +03:00
|
|
|
const updateOptions = {...options, transacting: transaction};
|
2021-10-05 12:15:57 +03:00
|
|
|
const uniqueChecker = new UniqueChecker(this.repository, transaction);
|
|
|
|
|
2022-08-24 14:13:58 +03:00
|
|
|
const offer = await this.repository.getById(data.id, updateOptions);
|
2021-10-05 12:15:57 +03:00
|
|
|
|
2021-10-22 15:43:52 +03:00
|
|
|
if (!offer) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-10-19 12:00:18 +03:00
|
|
|
if (Reflect.has(data, 'name')) {
|
2021-10-07 17:42:56 +03:00
|
|
|
const name = OfferName.create(data.name);
|
|
|
|
await offer.updateName(name, uniqueChecker);
|
2021-10-05 12:15:57 +03:00
|
|
|
}
|
|
|
|
|
2021-10-19 12:00:18 +03:00
|
|
|
if (Reflect.has(data, 'code')) {
|
2021-10-07 17:42:56 +03:00
|
|
|
const code = OfferCode.create(data.code);
|
|
|
|
await offer.updateCode(code, uniqueChecker);
|
2021-10-05 12:15:57 +03:00
|
|
|
}
|
|
|
|
|
2021-10-19 12:00:18 +03:00
|
|
|
if (Reflect.has(data, 'display_title')) {
|
2021-10-07 17:42:56 +03:00
|
|
|
const title = OfferTitle.create(data.display_title);
|
|
|
|
offer.displayTitle = title;
|
2021-10-05 12:15:57 +03:00
|
|
|
}
|
|
|
|
|
2021-10-19 12:00:18 +03:00
|
|
|
if (Reflect.has(data, 'display_description')) {
|
2021-10-07 17:42:56 +03:00
|
|
|
const description = OfferDescription.create(data.display_description);
|
|
|
|
offer.displayDescription = description;
|
2021-10-05 12:15:57 +03:00
|
|
|
}
|
|
|
|
|
2021-10-19 12:00:18 +03:00
|
|
|
if (Reflect.has(data, 'status')) {
|
2021-10-12 18:11:52 +03:00
|
|
|
const status = OfferStatus.create(data.status);
|
|
|
|
offer.status = status;
|
|
|
|
}
|
|
|
|
|
2022-08-24 14:13:58 +03:00
|
|
|
await this.repository.save(offer, updateOptions);
|
2021-10-05 12:15:57 +03:00
|
|
|
|
|
|
|
return OfferMapper.toDTO(offer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-12 18:53:08 +03:00
|
|
|
* @param {object} options
|
|
|
|
* @param {string} options.filter
|
2021-10-05 12:15:57 +03:00
|
|
|
* @returns {Promise<OfferMapper.OfferDTO[]>}
|
|
|
|
*/
|
2021-10-12 18:53:08 +03:00
|
|
|
async listOffers(options) {
|
2021-10-05 12:15:57 +03:00
|
|
|
return await this.repository.createTransaction(async (transaction) => {
|
2021-10-12 18:53:08 +03:00
|
|
|
const opts = {transacting: transaction, filter: options.filter};
|
2021-10-05 12:15:57 +03:00
|
|
|
|
2021-10-12 18:53:08 +03:00
|
|
|
const offers = await this.repository.getAll(opts);
|
2021-10-05 12:15:57 +03:00
|
|
|
|
|
|
|
return offers.map(OfferMapper.toDTO);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = OffersAPI;
|