mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-24 06:35:49 +03:00
✨ Added custom count queries for "max" limits
refs https://github.com/TryGhost/Team/issues/597 - When the library is used on a client without a DB connection (e.g. frontend client running in a browser) the library needs to expose a way to override count queries. - The way these can be used is giving a count based on a HTTP request or some other data provider - Example use with max limit like "staff" would be loading the limit servcie if following way: ``` const limitService = new LimitService(); let limits = { staff: { max: 2, currentCountQuery: () => 5 } }; limitService.loadLimits({limits, errors}); await limitService.checkIsOverLimit('staff') ```
This commit is contained in:
parent
326cbf0d34
commit
f4f48712c5
@ -28,7 +28,7 @@ class LimitService {
|
||||
|
||||
if (config[name]) {
|
||||
/** @type LimitConfig */
|
||||
let limitConfig = _.merge({}, limits[name], config[name]);
|
||||
let limitConfig = Object.assign({}, config[name], limits[name]);
|
||||
|
||||
if (_.has(limitConfig, 'max')) {
|
||||
this.limits[name] = new MaxLimit({name: name, config: limitConfig, helpLink, db, errors});
|
||||
@ -97,4 +97,5 @@ module.exports = LimitService;
|
||||
* @prop {Number} [max] - max limit
|
||||
* @prop {Boolean} [disabled] - flag disabling/enabling limit
|
||||
* @prop {String} error - custom error to be displayed when the limit is reached
|
||||
* @prop {Function} [currentCountQuery] - function returning count for the "max" type of limit
|
||||
*/
|
||||
|
@ -109,4 +109,29 @@ describe('Limit Service', function () {
|
||||
limitService.isLimited('customThemes').should.be.true();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Custom limit count query configuration', function () {
|
||||
it('can use a custom implementation of max limit query', async function () {
|
||||
const limitService = new LimitService();
|
||||
|
||||
let limits = {
|
||||
staff: {
|
||||
max: 2,
|
||||
currentCountQuery: () => 5
|
||||
},
|
||||
members: {
|
||||
max: 100,
|
||||
currentCountQuery: () => 100
|
||||
}
|
||||
};
|
||||
|
||||
limitService.loadLimits({limits, errors});
|
||||
|
||||
(await limitService.checkIsOverLimit('staff')).should.be.true();
|
||||
(await limitService.checkWouldGoOverLimit('staff')).should.be.true();
|
||||
|
||||
(await limitService.checkIsOverLimit('members')).should.be.false();
|
||||
(await limitService.checkWouldGoOverLimit('members')).should.be.true();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user