2021-10-22 15:22:33 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
const errors = require('@tryghost/errors');
|
2021-10-05 14:19:23 +03:00
|
|
|
const offersService = require('../../services/offers');
|
|
|
|
|
2021-10-22 15:22:33 +03:00
|
|
|
const messages = {
|
|
|
|
offerNotFound: 'Offer not found.'
|
|
|
|
};
|
|
|
|
|
2021-10-05 14:19:23 +03:00
|
|
|
module.exports = {
|
|
|
|
docName: 'offers',
|
|
|
|
|
|
|
|
browse: {
|
2021-10-12 19:37:56 +03:00
|
|
|
options: [
|
|
|
|
'filter'
|
|
|
|
],
|
2021-10-05 14:19:23 +03:00
|
|
|
permissions: true,
|
|
|
|
async query(frame) {
|
2021-10-12 19:37:56 +03:00
|
|
|
const offers = await offersService.api.listOffers(frame.options);
|
2021-10-05 14:19:23 +03:00
|
|
|
frame.response = {
|
|
|
|
offers
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
read: {
|
|
|
|
data: ['id'],
|
|
|
|
permissions: true,
|
|
|
|
async query(frame) {
|
|
|
|
const offer = await offersService.api.getOffer(frame.data);
|
2021-10-22 15:22:33 +03:00
|
|
|
if (!offer) {
|
|
|
|
throw new errors.NotFoundError({
|
|
|
|
message: tpl(messages.offerNotFound)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-05 14:19:23 +03:00
|
|
|
frame.response = {
|
|
|
|
offers: [offer]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
edit: {
|
|
|
|
options: ['id'],
|
|
|
|
permissions: true,
|
2021-10-12 15:24:43 +03:00
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
2021-10-05 14:19:23 +03:00
|
|
|
async query(frame) {
|
|
|
|
const offer = await offersService.api.updateOffer({
|
|
|
|
...frame.data.offers[0],
|
|
|
|
id: frame.options.id
|
|
|
|
});
|
2021-10-22 15:45:37 +03:00
|
|
|
|
|
|
|
if (!offer) {
|
|
|
|
throw new errors.NotFoundError({
|
|
|
|
message: tpl(messages.offerNotFound)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-05 14:19:23 +03:00
|
|
|
frame.response = {
|
|
|
|
offers: [offer]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add: {
|
|
|
|
permissions: true,
|
2021-10-12 15:24:43 +03:00
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
2021-10-05 14:19:23 +03:00
|
|
|
async query(frame) {
|
|
|
|
const offer = await offersService.api.createOffer(frame.data.offers[0]);
|
|
|
|
frame.response = {
|
|
|
|
offers: [offer]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|