@velaro/cli 1.4.110 → 1.4.111
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/velaro.js +2 -0
- package/lib/commands/site-sale.js +118 -0
- package/lib/site-sale.test.js +25 -0
- package/package.json +1 -1
package/bin/velaro.js
CHANGED
|
@@ -123,6 +123,7 @@ import { emailInboxCommand } from '../lib/commands/email-inbox.js';
|
|
|
123
123
|
import { callbackCommand } from '../lib/commands/callback.js';
|
|
124
124
|
import { caseCommand } from '../lib/commands/case.js';
|
|
125
125
|
import { pickupCommand } from '../lib/commands/pickup.js';
|
|
126
|
+
import { siteSaleCommand } from '../lib/commands/site-sale.js';
|
|
126
127
|
import { visitorMonitorCommand } from '../lib/commands/visitor-monitor.js';
|
|
127
128
|
import { visitorLinkCommand } from '../lib/commands/visitor-link.js';
|
|
128
129
|
import { applePayCommand } from '../lib/commands/apple-pay.js';
|
|
@@ -286,6 +287,7 @@ await yargs(hideBin(process.argv))
|
|
|
286
287
|
.command(callbackCommand)
|
|
287
288
|
.command(caseCommand)
|
|
288
289
|
.command(pickupCommand)
|
|
290
|
+
.command(siteSaleCommand)
|
|
289
291
|
.command(visitorMonitorCommand)
|
|
290
292
|
.command(visitorLinkCommand)
|
|
291
293
|
.command(applePayCommand)
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { messagingGet, messagingPost, messagingPut, messagingDel } from '../api.js';
|
|
2
|
+
import { runCommand } from '../run.js';
|
|
3
|
+
|
|
4
|
+
// ── velaro site-sale ───────────────────────────────────────────────────────────
|
|
5
|
+
// CLI surface for "Current Sales": a time-boxed override Q&A. While a sale is active
|
|
6
|
+
// the bot answers sales/discount/coupon questions deterministically and can issue
|
|
7
|
+
// per-customer coupons; it stops at the end time. siteId is applied server-side from
|
|
8
|
+
// the vmsg-siteId header (see cli/lib/api.js messagingRequest) -- never a CLI arg.
|
|
9
|
+
//
|
|
10
|
+
// Backend contract: velaro-messaging SiteSalesController
|
|
11
|
+
// GET SiteSales
|
|
12
|
+
// POST SiteSales
|
|
13
|
+
// PUT SiteSales/{id}
|
|
14
|
+
// POST SiteSales/{id}/end
|
|
15
|
+
// DELETE SiteSales/{id}
|
|
16
|
+
|
|
17
|
+
const BOLD = '\x1b[1m';
|
|
18
|
+
const DIM = '\x1b[2m';
|
|
19
|
+
const GRN = '\x1b[32m';
|
|
20
|
+
const RST = '\x1b[0m';
|
|
21
|
+
|
|
22
|
+
export function buildSaleBody(argv) {
|
|
23
|
+
return {
|
|
24
|
+
name: argv.name,
|
|
25
|
+
description: argv.description,
|
|
26
|
+
appliesTo: argv.appliesTo,
|
|
27
|
+
appliesToValue: argv.appliesToValue,
|
|
28
|
+
discountType: argv.discountType,
|
|
29
|
+
discountValue: argv.discountValue,
|
|
30
|
+
startsAtUtc: argv.startsAtUtc,
|
|
31
|
+
endsAtUtc: argv.endsAtUtc,
|
|
32
|
+
couponCode: argv.couponCode,
|
|
33
|
+
autoGenerateCoupons: argv.autoGenerateCoupons,
|
|
34
|
+
maxCouponsPerCustomer: argv.maxCouponsPerCustomer,
|
|
35
|
+
couponUsageLimit: argv.couponUsageLimit,
|
|
36
|
+
couponExpiresHours: argv.couponExpiresHours,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function addSaleOptions(y, { creating }) {
|
|
41
|
+
return y
|
|
42
|
+
.option('name', { describe: 'Sale name', type: 'string', demandOption: creating })
|
|
43
|
+
.option('description', { describe: 'Short description the bot can quote', type: 'string' })
|
|
44
|
+
.option('applies-to', { describe: 'Scope: all | category | products', type: 'string', choices: ['all', 'category', 'products'] })
|
|
45
|
+
.option('applies-to-value', { describe: 'Category name or comma-separated products', type: 'string' })
|
|
46
|
+
.option('discount-type', { describe: 'percentage | fixed_amount | free_shipping', type: 'string', choices: ['percentage', 'fixed_amount', 'free_shipping'], demandOption: creating })
|
|
47
|
+
.option('discount-value', { describe: 'Percent or fixed amount', type: 'number' })
|
|
48
|
+
.option('starts-at-utc', { describe: 'ISO 8601 start (UTC)', type: 'string', demandOption: creating })
|
|
49
|
+
.option('ends-at-utc', { describe: 'ISO 8601 end (UTC); the bot stops answering at this time', type: 'string', demandOption: creating })
|
|
50
|
+
.option('coupon-code', { describe: 'Optional shared coupon code', type: 'string' })
|
|
51
|
+
.option('auto-generate-coupons', { describe: 'Issue a unique coupon per customer', type: 'boolean' })
|
|
52
|
+
.option('max-coupons-per-customer',{ describe: 'Max coupons issued to one customer', type: 'number' })
|
|
53
|
+
.option('coupon-usage-limit', { describe: 'Uses allowed per coupon', type: 'number' })
|
|
54
|
+
.option('coupon-expires-hours', { describe: 'Hours before an issued coupon expires', type: 'number' });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function listHandler() {
|
|
58
|
+
const sales = await messagingGet('/SiteSales');
|
|
59
|
+
if (!Array.isArray(sales) || !sales.length) {
|
|
60
|
+
console.log('\n No current sales for this site.\n');
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
console.log(`\n${BOLD}Current Sales${RST}\n`);
|
|
64
|
+
for (const s of sales) {
|
|
65
|
+
console.log(` #${s.id} ${s.name} ${DIM}${s.discountType ?? ''} ${s.discountValue ?? ''} ${s.startsAtUtc ?? '-'} -> ${s.endsAtUtc ?? '-'}${RST}`);
|
|
66
|
+
}
|
|
67
|
+
console.log('');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function createHandler(argv) {
|
|
71
|
+
const sale = await messagingPost('/SiteSales', buildSaleBody(argv));
|
|
72
|
+
console.log(`\n${GRN}✓${RST} Sale created${sale && sale.id != null ? ` (#${sale.id})` : ''}.\n`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function updateHandler(argv) {
|
|
76
|
+
await messagingPut(`/SiteSales/${encodeURIComponent(argv.id)}`, buildSaleBody(argv));
|
|
77
|
+
console.log(`\n${GRN}✓${RST} Sale ${argv.id} updated.\n`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function endHandler(argv) {
|
|
81
|
+
await messagingPost(`/SiteSales/${encodeURIComponent(argv.id)}/end`, {});
|
|
82
|
+
console.log(`\n${GRN}✓${RST} Sale ${argv.id} ended.\n`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function deleteHandler(argv) {
|
|
86
|
+
await messagingDel(`/SiteSales/${encodeURIComponent(argv.id)}`);
|
|
87
|
+
console.log(`\n${GRN}✓${RST} Sale ${argv.id} deleted.\n`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const siteSaleCommand = {
|
|
91
|
+
command: 'site-sale <subcommand>',
|
|
92
|
+
describe: 'Current Sales -- temporary sales or discounts the bot answers about and can issue coupons for',
|
|
93
|
+
builder: (y) => y
|
|
94
|
+
.command({ command: 'list', describe: 'List current sales', handler: runCommand(listHandler) })
|
|
95
|
+
.command({
|
|
96
|
+
command: 'create', describe: 'Create a sale',
|
|
97
|
+
builder: (y2) => addSaleOptions(y2, { creating: true }),
|
|
98
|
+
handler: runCommand(createHandler),
|
|
99
|
+
})
|
|
100
|
+
.command({
|
|
101
|
+
command: 'update <id>', describe: 'Update a sale (omit a flag to leave it unchanged)',
|
|
102
|
+
builder: (y2) => addSaleOptions(y2.positional('id', { describe: 'Sale ID', type: 'number' }), { creating: false }),
|
|
103
|
+
handler: runCommand(updateHandler),
|
|
104
|
+
})
|
|
105
|
+
.command({
|
|
106
|
+
command: 'end <id>', describe: 'End a sale now',
|
|
107
|
+
builder: (y2) => y2.positional('id', { describe: 'Sale ID', type: 'number' }),
|
|
108
|
+
handler: runCommand(endHandler),
|
|
109
|
+
})
|
|
110
|
+
.command({
|
|
111
|
+
command: 'delete <id>', describe: 'Delete a sale',
|
|
112
|
+
builder: (y2) => y2.positional('id', { describe: 'Sale ID', type: 'number' }),
|
|
113
|
+
handler: runCommand(deleteHandler),
|
|
114
|
+
})
|
|
115
|
+
.demandCommand(1, 'Specify a site-sale subcommand: list | create | update | end | delete')
|
|
116
|
+
.strict(),
|
|
117
|
+
handler: () => {},
|
|
118
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import { dirname, join } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { buildSaleBody, siteSaleCommand } from './commands/site-sale.js';
|
|
7
|
+
|
|
8
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
|
|
10
|
+
test('sale body carries only the SiteSales contract fields and never a site id', () => {
|
|
11
|
+
const body = buildSaleBody({ name: 'n', discountType: 'percentage', discountValue: 20, siteId: 5, startsAtUtc: 'a', endsAtUtc: 'b' });
|
|
12
|
+
assert.deepEqual(Object.keys(body).sort(), [
|
|
13
|
+
'appliesTo', 'appliesToValue', 'autoGenerateCoupons', 'couponCode', 'couponExpiresHours', 'couponUsageLimit',
|
|
14
|
+
'description', 'discountType', 'discountValue', 'endsAtUtc', 'maxCouponsPerCustomer', 'name', 'startsAtUtc',
|
|
15
|
+
]);
|
|
16
|
+
assert.equal(body.discountValue, 20);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test('site-sale command registered in velaro.js with all five subcommands', () => {
|
|
20
|
+
assert.equal(siteSaleCommand.command, 'site-sale <subcommand>');
|
|
21
|
+
const src = readFileSync(join(here, '..', 'bin', 'velaro.js'), 'utf8');
|
|
22
|
+
assert.match(src, /\.command\(siteSaleCommand\)/);
|
|
23
|
+
const cmdSrc = readFileSync(join(here, 'commands', 'site-sale.js'), 'utf8');
|
|
24
|
+
for (const c of ["'list'", "'create'", "'update <id>'", "'end <id>'", "'delete <id>'"]) assert.ok(cmdSrc.includes(c), c);
|
|
25
|
+
});
|
package/package.json
CHANGED