Fixed missing options when creating or editing an Offer

- without this, the model doesn't have the context on who was
  adding/editing it
- this resulted in being unable to store actions for Offers because the
  `actor` is unknown
- this is the pattern we use elsewhere in the code so I've copied it
  into here
This commit is contained in:
Daniel Lockyer 2022-08-24 13:13:58 +02:00 committed by Daniel Lockyer
parent ba5899d147
commit 046fd2bd82
2 changed files with 11 additions and 9 deletions

View File

@ -49,7 +49,7 @@ module.exports = {
const offer = await offersService.api.updateOffer({
...frame.data.offers[0],
id: frame.options.id
});
}, frame.options);
if (!offer) {
throw new errors.NotFoundError({
@ -69,7 +69,7 @@ module.exports = {
cacheInvalidate: true
},
async query(frame) {
const offer = await offersService.api.createOffer(frame.data.offers[0]);
const offer = await offersService.api.createOffer(frame.data.offers[0], frame.options);
return {
data: [offer]
};

View File

@ -37,17 +37,18 @@ class OffersAPI {
/**
* @param {any} data
* @param {Object} [options]
*
* @returns {Promise<OfferMapper.OfferDTO>}
*/
async createOffer(data) {
async createOffer(data, options = {}) {
return this.repository.createTransaction(async (transaction) => {
const options = {transacting: transaction};
const saveOptions = {...options, transacting: transaction};
const uniqueChecker = new UniqueChecker(this.repository, transaction);
const offer = await Offer.create(data, uniqueChecker);
await this.repository.save(offer, options);
await this.repository.save(offer, saveOptions);
return OfferMapper.toDTO(offer);
});
@ -61,15 +62,16 @@ class OffersAPI {
* @param {string} [data.display_description]
* @param {string} [data.code]
* @param {string} [data.status]
* @param {Object} [options]
*
* @returns {Promise<OfferMapper.OfferDTO>}
*/
async updateOffer(data) {
async updateOffer(data, options = {}) {
return await this.repository.createTransaction(async (transaction) => {
const options = {transacting: transaction};
const updateOptions = {...options, transacting: transaction};
const uniqueChecker = new UniqueChecker(this.repository, transaction);
const offer = await this.repository.getById(data.id, options);
const offer = await this.repository.getById(data.id, updateOptions);
if (!offer) {
return null;
@ -100,7 +102,7 @@ class OffersAPI {
offer.status = status;
}
await this.repository.save(offer, options);
await this.repository.save(offer, updateOptions);
return OfferMapper.toDTO(offer);
});