0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

Ensured that Offers can be filtered on status

refs https://github.com/TryGhost/Team/issues/1131

This adds a mapping between the status property used in the domain & API
and the active column used in the database. As we only have the usecase
of filtering by `status` right now, we have not added support for all
the other columns. Instead of these potentially erroring where the
column name does not match the property name in the domain/api - we've
added a transformer which will ignore all filters for properties other
than `status`. This follows postels law, in that we can be liberal with
the filters we accept, but conservative in the ones we implement.
This commit is contained in:
Fabien O'Carroll 2021-10-12 18:28:06 +02:00
parent cf8fcc6f35
commit 96e87d6798

View file

@ -1,8 +1,36 @@
const {flowRight} = require('lodash');
const {mapKeyValues, mapQuery} = require('@nexes/mongo-utils');
const DomainEvents = require('@tryghost/domain-events');
const OfferCodeChangeEvent = require('../domain/events/OfferCodeChange');
const Offer = require('../domain/models/Offer');
const OfferStatus = require('../domain/models/OfferStatus');
const statusTransformer = mapKeyValues({
key: {
from: 'status',
to: 'active'
},
values: [{
from: 'active',
to: true
}, {
from: 'archived',
to: false
}]
});
const rejectNonStatusTransformer = input => mapQuery(input, function (value, key) {
if (key !== 'status') {
return;
}
return {
[key]: value
};
});
const mongoTransformer = flowRight(statusTransformer, rejectNonStatusTransformer);
/**
* @typedef {object} BaseOptions
* @prop {import('knex').Transaction} transacting
@ -110,6 +138,7 @@ class OfferRepository {
async getAll(options) {
const models = await this.OfferModel.findAll({
...options,
mongoTransformer,
withRelated: ['product']
});
return Promise.all(models.toJSON().map(toDomain).map(Offer.create));