mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-24 06:35:49 +03:00
5674036902
refs https://github.com/TryGhost/Team/issues/1083 This is the initial scaffolding for setting up Offers in Ghost
87 lines
2.3 KiB
JavaScript
87 lines
2.3 KiB
JavaScript
const Offer = require('./Offer');
|
|
const OfferMapper = require('./OfferMapper');
|
|
const UniqueChecker = require('./UniqueChecker');
|
|
|
|
class OffersAPI {
|
|
/**
|
|
* @param {import('./OfferRepository')} repository
|
|
*/
|
|
constructor(repository) {
|
|
this.repository = repository;
|
|
}
|
|
|
|
/**
|
|
* @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]
|
|
* @param {string} [data.title]
|
|
* @param {string} [data.description]
|
|
* @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) {
|
|
offer.updateName(data.name, uniqueChecker);
|
|
}
|
|
|
|
if (data.code) {
|
|
offer.updateCode(data.code, uniqueChecker);
|
|
}
|
|
|
|
if (data.title) {
|
|
offer.displayTitle = data.title;
|
|
}
|
|
|
|
if (data.description) {
|
|
offer.displayDescription = data.description;
|
|
}
|
|
|
|
await this.repository.save(offer, options);
|
|
|
|
transaction.commit();
|
|
|
|
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;
|