feat(admin): support billing deadline units
parent
d2eb589f50
commit
bc5d7ce263
@ -0,0 +1,70 @@
|
||||
(function (root, factory) {
|
||||
'use strict';
|
||||
const api = factory();
|
||||
if (typeof module === 'object' && module.exports) module.exports = api;
|
||||
if (root) root.VenixAdminConfigUnits = api;
|
||||
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
|
||||
'use strict';
|
||||
|
||||
const definitions = Object.freeze({
|
||||
minutes: Object.freeze({ multiplier: 1, max: 10080 }),
|
||||
hours: Object.freeze({ multiplier: 60, max: 168 }),
|
||||
days: Object.freeze({ multiplier: 1440, max: 7 }),
|
||||
});
|
||||
|
||||
function definition(unit) {
|
||||
if (!Object.prototype.hasOwnProperty.call(definitions, unit)) {
|
||||
throw new RangeError('账单支付期限单位无效');
|
||||
}
|
||||
return definitions[unit];
|
||||
}
|
||||
|
||||
function positiveInteger(value) {
|
||||
const raw = String(value == null ? '' : value).trim();
|
||||
if (!/^[1-9][0-9]*$/.test(raw)) {
|
||||
throw new RangeError('账单支付期限请输入正整数');
|
||||
}
|
||||
const amount = Number(raw);
|
||||
if (!Number.isSafeInteger(amount)) {
|
||||
throw new RangeError('账单支付期限超出允许范围');
|
||||
}
|
||||
return amount;
|
||||
}
|
||||
|
||||
function maxValue(unit) {
|
||||
return definition(unit).max;
|
||||
}
|
||||
|
||||
function toMinutes(value, unit) {
|
||||
const amount = positiveInteger(value);
|
||||
const selected = definition(unit);
|
||||
if (amount > selected.max) {
|
||||
throw new RangeError('账单支付期限超出允许范围');
|
||||
}
|
||||
return amount * selected.multiplier;
|
||||
}
|
||||
|
||||
function fromMinutes(value) {
|
||||
const minutes = positiveInteger(value);
|
||||
if (minutes > definitions.minutes.max) {
|
||||
throw new RangeError('账单支付期限超出允许范围');
|
||||
}
|
||||
if (minutes % definitions.days.multiplier === 0) {
|
||||
return Object.freeze({
|
||||
value: minutes / definitions.days.multiplier,
|
||||
unit: 'days',
|
||||
minutes,
|
||||
});
|
||||
}
|
||||
if (minutes % definitions.hours.multiplier === 0) {
|
||||
return Object.freeze({
|
||||
value: minutes / definitions.hours.multiplier,
|
||||
unit: 'hours',
|
||||
minutes,
|
||||
});
|
||||
}
|
||||
return Object.freeze({ value: minutes, unit: 'minutes', minutes });
|
||||
}
|
||||
|
||||
return Object.freeze({ fromMinutes, maxValue, toMinutes });
|
||||
});
|
||||
@ -1,2 +1,2 @@
|
||||
{* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *}
|
||||
{assign var=venix_asset_version value='2026102408' scope='global'}
|
||||
{assign var=venix_asset_version value='2026102409' scope='global'}
|
||||
|
||||
@ -0,0 +1,42 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { createRequire } from 'node:module';
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const units = require('../public/cloud/admin-config-units.js');
|
||||
const template = readFileSync(new URL(
|
||||
'../resources/views/venix/admin/config.tpl', import.meta.url
|
||||
), 'utf8');
|
||||
|
||||
assert.deepEqual(units.fromMinutes(30), {
|
||||
value: 30, unit: 'minutes', minutes: 30,
|
||||
});
|
||||
assert.deepEqual(units.fromMinutes('60'), {
|
||||
value: 1, unit: 'hours', minutes: 60,
|
||||
});
|
||||
assert.deepEqual(units.fromMinutes(1440), {
|
||||
value: 1, unit: 'days', minutes: 1440,
|
||||
});
|
||||
assert.equal(units.toMinutes('45', 'minutes'), 45);
|
||||
assert.equal(units.toMinutes('2', 'hours'), 120);
|
||||
assert.equal(units.toMinutes('7', 'days'), 10080);
|
||||
assert.equal(units.maxValue('minutes'), 10080);
|
||||
assert.equal(units.maxValue('hours'), 168);
|
||||
assert.equal(units.maxValue('days'), 7);
|
||||
|
||||
for (const args of [
|
||||
['0', 'minutes'], ['1.5', 'hours'], ['8', 'days'],
|
||||
['169', 'hours'], ['1', 'weeks'],
|
||||
]) {
|
||||
assert.throws(() => units.toMinutes(...args), RangeError);
|
||||
}
|
||||
for (const value of ['0', '1.5', '10081', 'invalid']) {
|
||||
assert.throws(() => units.fromMinutes(value), RangeError);
|
||||
}
|
||||
|
||||
assert.match(template,
|
||||
/name="cloud_provision_invoice_payment_window_minutes" type="hidden"/);
|
||||
assert.match(template,
|
||||
/id="cloud_provision_invoice_payment_window_unit"[\s\S]*?<option value="minutes">分钟<\/option>[\s\S]*?<option value="hours">小时<\/option>[\s\S]*?<option value="days">天<\/option>/);
|
||||
|
||||
console.log('Administrator payment-window unit conversion checks passed.');
|
||||
Loading…
Reference in New Issue