mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-28 05:14:12 +03:00
ceb2b7e5ea
refs https://linear.app/tryghost/issue/CORE-49/fix-errors-in-utils-repo-limit-service - As I've touched these files did a little refactor and changed where the error messages are stored to keep it up with our lates coding standard - having "messages" hash defined in the module storing all messages that have pottential for i18y in the future.
38 lines
1.3 KiB
JavaScript
38 lines
1.3 KiB
JavaScript
const {DateTime} = require('luxon');
|
|
const {IncorrectUsageError} = require('@tryghost/errors');
|
|
|
|
const messages = {
|
|
invalidInterval: 'Invalid interval specified. Only "month" value is accepted.'
|
|
};
|
|
|
|
const SUPPORTED_INTERVALS = ['month'];
|
|
/**
|
|
* Calculates the start of the last period (billing, cycle, etc.) based on the start date
|
|
* and the interval at which the cycle renews.
|
|
*
|
|
* @param {String} startDate - date in ISO 8601 format (https://en.wikipedia.org/wiki/ISO_8601)
|
|
* @param {('month')} interval - currently only supports 'month' value, in the future might support 'year', etc.
|
|
*
|
|
* @returns {String} - date in ISO 8601 format (https://en.wikipedia.org/wiki/ISO_8601) of the last period start
|
|
*/
|
|
const lastPeriodStart = (startDate, interval) => {
|
|
if (interval === 'month') {
|
|
const startDateISO = DateTime.fromISO(startDate, {zone: 'UTC'});
|
|
const now = DateTime.now().setZone('UTC');
|
|
const fullPeriodsPast = Math.floor(now.diff(startDateISO, 'months').months);
|
|
|
|
const lastPeriodStartDate = startDateISO.plus({months: fullPeriodsPast});
|
|
|
|
return lastPeriodStartDate.toISO();
|
|
}
|
|
|
|
throw new IncorrectUsageError({
|
|
message: messages.invalidInterval
|
|
});
|
|
};
|
|
|
|
module.exports = {
|
|
lastPeriodStart,
|
|
SUPPORTED_INTERVALS
|
|
};
|