@optima-chat/dev-skills 0.7.36 → 0.7.37
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/.claude/skills/discount-codes/SKILL.md +61 -0
- package/README.md +2 -0
- package/bin/helpers/discount/create.ts +79 -0
- package/bin/helpers/discount/disable.ts +44 -0
- package/bin/helpers/discount/generate.ts +84 -0
- package/bin/helpers/discount/list.ts +46 -0
- package/bin/helpers/discount.ts +41 -0
- package/dist/bin/helpers/discount/create.js +101 -0
- package/dist/bin/helpers/discount/disable.js +48 -0
- package/dist/bin/helpers/discount/generate.js +135 -0
- package/dist/bin/helpers/discount/list.js +59 -0
- package/dist/bin/helpers/discount.js +47 -0
- package/dist/bin/helpers/entitlement.js +0 -0
- package/dist/bin/helpers/generate-test-token.js +0 -0
- package/dist/bin/helpers/grant-balance.js +0 -0
- package/dist/bin/helpers/grant-subscription.js +0 -0
- package/dist/bin/helpers/product.js +0 -0
- package/dist/bin/helpers/query-db.js +0 -0
- package/dist/bin/helpers/show-env.js +0 -0
- package/docs/2026-05-27-discount-codes-plan-B-dev-skills-cli.md +517 -0
- package/package.json +2 -1
- package/dist/bin/helpers/grant-credits.js +0 -71
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: "discount-codes"
|
|
3
|
+
description: "当用户请求创建优惠码、发优惠码、生成折扣码、promo code、discount code、批量生成优惠码、停用优惠码、查看优惠码时,使用此技能。支持 Stage、Prod 两个环境。"
|
|
4
|
+
allowed-tools: ["Bash"]
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# 优惠码管理
|
|
8
|
+
|
|
9
|
+
为 billing 结账(技能包 / 会员)创建和管理百分比优惠码。
|
|
10
|
+
|
|
11
|
+
## 执行方式:使用 CLI 工具
|
|
12
|
+
|
|
13
|
+
无论用户用 `/discount-codes` 还是直接请求,都使用 `optima-discount` CLI(thin HTTP client,调 billing admin 端点):
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
optima-discount <subcommand> [options]
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 子命令
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# 建共享码:LAUNCH20 = 8 折,限 scout,6/30 截止,最多核销 100 次
|
|
23
|
+
optima-discount create --code LAUNCH20 --percent 20 --products scout --ends 2026-06-30 --max 100 --env prod
|
|
24
|
+
|
|
25
|
+
# 批量唯一码(每码用一次)—— 码写入本地文件,不打屏
|
|
26
|
+
optima-discount generate --count 100 --percent 50 --campaign partner-q3 --products scout --env prod
|
|
27
|
+
# → ./discount-codes-partner-q3-<ts>.txt
|
|
28
|
+
|
|
29
|
+
# 查看(按 campaign / code 过滤)
|
|
30
|
+
optima-discount list --campaign partner-q3 --env prod
|
|
31
|
+
|
|
32
|
+
# 停用
|
|
33
|
+
optima-discount disable --code LAUNCH20 --env prod
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## 参数
|
|
37
|
+
|
|
38
|
+
| 参数 | 说明 |
|
|
39
|
+
|------|------|
|
|
40
|
+
| `--code` | 优惠码(存为大写) |
|
|
41
|
+
| `--percent` | 折扣百分比 1-100 |
|
|
42
|
+
| `--products` | 逗号分隔的 productKey;省略 = 所有商品 |
|
|
43
|
+
| `--starts` / `--ends` | 有效期(`YYYY-MM-DD` 或 ISO datetime) |
|
|
44
|
+
| `--max` | 总核销上限(省略 = 不限;`1` = 一次性) |
|
|
45
|
+
| `--campaign` | 分组标签(generate 时也是码前缀) |
|
|
46
|
+
| `--count` | generate 生成数量 1-1000 |
|
|
47
|
+
| `--limit` | list 返回上限(默认 500,最大 1000) |
|
|
48
|
+
| `--env` | `stage` / `prod`(默认 `stage`) |
|
|
49
|
+
| `--yes` | 跳过 prod 二次确认 |
|
|
50
|
+
|
|
51
|
+
## 安全提醒
|
|
52
|
+
|
|
53
|
+
1. **Stage 优先**:默认 `stage`。
|
|
54
|
+
2. **Prod 谨慎**:`create` / `generate` / `disable` 在 prod 会要求输入 "yes" 确认(`--yes` 跳过)。
|
|
55
|
+
3. **唯一码写文件**:`generate` 的码写入当前目录文件(`mode 0600`),不打屏——避免复制时被终端 padding 破坏。
|
|
56
|
+
4. 依赖 billing 已部署对应环境(admin 端点存在)。越界值(如 `--percent 0`)由 billing 服务端校验返 400。
|
|
57
|
+
|
|
58
|
+
## 相关
|
|
59
|
+
|
|
60
|
+
- `optima-product` — 管理付费商品(优惠码作用于其结账)
|
|
61
|
+
- `optima-query-db` — 查 `discount_codes` / `discount_redemptions` 表核对
|
package/README.md
CHANGED
|
@@ -42,6 +42,7 @@ Optima Dev Skills 让 Claude Code 能够直接在 **CI、Stage、Prod** 三个
|
|
|
42
42
|
- **generate-test-token** - 生成测试 Access Token 用于 API 测试
|
|
43
43
|
- **use-commerce-cli** - 使用 Commerce CLI 管理电商店铺
|
|
44
44
|
- **read-code** - 阅读 Optima-Chat 组织下任意仓库的代码
|
|
45
|
+
- **discount-codes** - 创建/生成/查看/停用 billing 优惠码(Stage/Prod)
|
|
45
46
|
|
|
46
47
|
## 👤 用户故事
|
|
47
48
|
|
|
@@ -103,6 +104,7 @@ Claude:
|
|
|
103
104
|
| `optima-query-db` | 数据库查询工具 | `optima-query-db user-auth "SELECT COUNT(*) FROM users" prod` |
|
|
104
105
|
| `optima-show-env` | 查看服务环境变量 | `optima-show-env commerce-backend stage --filter DATABASE` |
|
|
105
106
|
| `optima-generate-test-token` | 生成测试 token | `optima-generate-test-token --business-name "测试店铺"` |
|
|
107
|
+
| `optima-discount` | 优惠码管理 | `optima-discount create --code LAUNCH20 --percent 20 --env stage` |
|
|
106
108
|
|
|
107
109
|
**特点**:
|
|
108
110
|
- ✅ 支持 CI、Stage、Prod 三个环境(query-db)
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { callBilling, validateEnv } from '../billing-http';
|
|
2
|
+
import { confirmIfProd } from '../confirm-prompt';
|
|
3
|
+
|
|
4
|
+
interface CreateArgs {
|
|
5
|
+
code: string;
|
|
6
|
+
percentOff: number;
|
|
7
|
+
productKeys?: string[];
|
|
8
|
+
startsAt?: string;
|
|
9
|
+
endsAt?: string;
|
|
10
|
+
maxRedemptions?: number;
|
|
11
|
+
campaign?: string;
|
|
12
|
+
env: string;
|
|
13
|
+
yes: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Normalize a date/datetime arg to a full ISO datetime string (billing requires z.iso.datetime). */
|
|
17
|
+
function toIso(v: string): string {
|
|
18
|
+
const d = new Date(v);
|
|
19
|
+
if (isNaN(d.getTime())) throw new Error(`Invalid date: ${v} (use YYYY-MM-DD or ISO datetime)`);
|
|
20
|
+
return d.toISOString();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function parseArgs(argv: string[]): CreateArgs {
|
|
24
|
+
if (argv.length === 0 || argv[0] === '-h' || argv[0] === '--help') {
|
|
25
|
+
console.log(`Usage: optima-discount create --code <CODE> --percent <1-100> [options]
|
|
26
|
+
|
|
27
|
+
Required:
|
|
28
|
+
--code <CODE> Promo code (stored uppercased)
|
|
29
|
+
--percent <1-100> Percentage off
|
|
30
|
+
|
|
31
|
+
Optional:
|
|
32
|
+
--products <a,b,...> Limit to these productKeys (default: all)
|
|
33
|
+
--starts <date> Valid-from (YYYY-MM-DD or ISO; default: immediately)
|
|
34
|
+
--ends <date> Valid-until
|
|
35
|
+
--max <N> Max total redemptions (default: unlimited; 1 = single-use)
|
|
36
|
+
--campaign <label> Grouping label
|
|
37
|
+
--env stage|prod (default: stage)
|
|
38
|
+
--yes Skip prod confirmation`);
|
|
39
|
+
process.exit(0);
|
|
40
|
+
}
|
|
41
|
+
const out: Partial<CreateArgs> = { env: 'stage', yes: false };
|
|
42
|
+
for (let i = 0; i < argv.length; i++) {
|
|
43
|
+
const a = argv[i];
|
|
44
|
+
const next = argv[i + 1];
|
|
45
|
+
switch (a) {
|
|
46
|
+
case '--code': out.code = next; i++; break;
|
|
47
|
+
case '--percent': out.percentOff = parseInt(next, 10); i++; break;
|
|
48
|
+
case '--products': if (!next) throw new Error('--products requires a value'); out.productKeys = next.split(',').map((s) => s.trim()).filter(Boolean); i++; break;
|
|
49
|
+
case '--starts': out.startsAt = toIso(next); i++; break;
|
|
50
|
+
case '--ends': out.endsAt = toIso(next); i++; break;
|
|
51
|
+
case '--max': out.maxRedemptions = parseInt(next, 10); i++; break;
|
|
52
|
+
case '--campaign': out.campaign = next; i++; break;
|
|
53
|
+
case '--env': out.env = next; i++; break;
|
|
54
|
+
case '--yes': out.yes = true; break;
|
|
55
|
+
default: throw new Error(`Unknown arg: ${a}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (!out.code) throw new Error('--code required');
|
|
59
|
+
if (out.percentOff === undefined || isNaN(out.percentOff)) throw new Error('--percent required (1-100)');
|
|
60
|
+
return out as CreateArgs;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function runCreate(argv: string[]): Promise<void> {
|
|
64
|
+
const args = parseArgs(argv);
|
|
65
|
+
validateEnv(args.env);
|
|
66
|
+
await confirmIfProd(args.env, `Create discount code ${args.code.toUpperCase()} (${args.percentOff}% off)`, args.yes);
|
|
67
|
+
|
|
68
|
+
const body: Record<string, unknown> = { code: args.code, percentOff: args.percentOff };
|
|
69
|
+
if (args.productKeys) body.productKeys = args.productKeys;
|
|
70
|
+
if (args.startsAt) body.startsAt = args.startsAt;
|
|
71
|
+
if (args.endsAt) body.endsAt = args.endsAt;
|
|
72
|
+
if (args.maxRedemptions !== undefined) body.maxRedemptions = args.maxRedemptions;
|
|
73
|
+
if (args.campaign) body.campaign = args.campaign;
|
|
74
|
+
|
|
75
|
+
console.log(`\n🎟️ Creating discount code on ${args.env.toUpperCase()}...`);
|
|
76
|
+
const res = await callBilling(args.env, 'POST', '/api/billing/admin/discount-codes', body);
|
|
77
|
+
console.log(`✓ Created (HTTP ${res.status}):`);
|
|
78
|
+
console.log(JSON.stringify(res.body, null, 2));
|
|
79
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { callBilling, validateEnv } from '../billing-http';
|
|
2
|
+
import { confirmIfProd } from '../confirm-prompt';
|
|
3
|
+
|
|
4
|
+
interface DisableArgs {
|
|
5
|
+
code: string;
|
|
6
|
+
env: string;
|
|
7
|
+
yes: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function parseArgs(argv: string[]): DisableArgs {
|
|
11
|
+
if (argv.length === 0 || argv[0] === '-h' || argv[0] === '--help') {
|
|
12
|
+
console.log(`Usage: optima-discount disable --code <CODE> [options]
|
|
13
|
+
|
|
14
|
+
Required:
|
|
15
|
+
--code <CODE>
|
|
16
|
+
|
|
17
|
+
Optional:
|
|
18
|
+
--env stage|prod (default: stage)
|
|
19
|
+
--yes Skip prod confirmation`);
|
|
20
|
+
process.exit(0);
|
|
21
|
+
}
|
|
22
|
+
const out: Partial<DisableArgs> = { env: 'stage', yes: false };
|
|
23
|
+
for (let i = 0; i < argv.length; i++) {
|
|
24
|
+
const a = argv[i];
|
|
25
|
+
const next = argv[i + 1];
|
|
26
|
+
switch (a) {
|
|
27
|
+
case '--code': out.code = next; i++; break;
|
|
28
|
+
case '--env': out.env = next; i++; break;
|
|
29
|
+
case '--yes': out.yes = true; break;
|
|
30
|
+
default: throw new Error(`Unknown arg: ${a}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (!out.code) throw new Error('--code required');
|
|
34
|
+
return out as DisableArgs;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function runDisable(argv: string[]): Promise<void> {
|
|
38
|
+
const args = parseArgs(argv);
|
|
39
|
+
validateEnv(args.env);
|
|
40
|
+
await confirmIfProd(args.env, `Disable discount code ${args.code.toUpperCase()}`, args.yes);
|
|
41
|
+
const res = await callBilling(args.env, 'PATCH', `/api/billing/admin/discount-codes/${encodeURIComponent(args.code)}`, { status: 'DISABLED' });
|
|
42
|
+
console.log(`✓ Disabled (HTTP ${res.status}):`);
|
|
43
|
+
console.log(JSON.stringify(res.body, null, 2));
|
|
44
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import { callBilling, validateEnv } from '../billing-http';
|
|
3
|
+
import { confirmIfProd } from '../confirm-prompt';
|
|
4
|
+
|
|
5
|
+
interface GenArgs {
|
|
6
|
+
count: number;
|
|
7
|
+
percentOff: number;
|
|
8
|
+
campaign: string;
|
|
9
|
+
productKeys?: string[];
|
|
10
|
+
startsAt?: string;
|
|
11
|
+
endsAt?: string;
|
|
12
|
+
env: string;
|
|
13
|
+
yes: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function toIso(v: string): string {
|
|
17
|
+
const d = new Date(v);
|
|
18
|
+
if (isNaN(d.getTime())) throw new Error(`Invalid date: ${v} (use YYYY-MM-DD or ISO datetime)`);
|
|
19
|
+
return d.toISOString();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function parseArgs(argv: string[]): GenArgs {
|
|
23
|
+
if (argv.length === 0 || argv[0] === '-h' || argv[0] === '--help') {
|
|
24
|
+
console.log(`Usage: optima-discount generate --count <N> --percent <1-100> --campaign <label> [options]
|
|
25
|
+
|
|
26
|
+
Generates N unique single-use codes (maxRedemptions=1). Codes are written to a
|
|
27
|
+
local file (NOT printed) to keep them copy-paste clean.
|
|
28
|
+
|
|
29
|
+
Required:
|
|
30
|
+
--count <N> How many codes (1-1000)
|
|
31
|
+
--percent <1-100> Percentage off
|
|
32
|
+
--campaign <label> Grouping label (also the code prefix)
|
|
33
|
+
|
|
34
|
+
Optional:
|
|
35
|
+
--products <a,b,...> Limit to these productKeys
|
|
36
|
+
--starts <date> Valid-from (YYYY-MM-DD or ISO)
|
|
37
|
+
--ends <date> Valid-until
|
|
38
|
+
--env stage|prod (default: stage)
|
|
39
|
+
--yes Skip prod confirmation`);
|
|
40
|
+
process.exit(0);
|
|
41
|
+
}
|
|
42
|
+
const out: Partial<GenArgs> = { env: 'stage', yes: false };
|
|
43
|
+
for (let i = 0; i < argv.length; i++) {
|
|
44
|
+
const a = argv[i];
|
|
45
|
+
const next = argv[i + 1];
|
|
46
|
+
switch (a) {
|
|
47
|
+
case '--count': out.count = parseInt(next, 10); i++; break;
|
|
48
|
+
case '--percent': out.percentOff = parseInt(next, 10); i++; break;
|
|
49
|
+
case '--campaign': out.campaign = next; i++; break;
|
|
50
|
+
case '--products': if (!next) throw new Error('--products requires a value'); out.productKeys = next.split(',').map((s) => s.trim()).filter(Boolean); i++; break;
|
|
51
|
+
case '--starts': out.startsAt = toIso(next); i++; break;
|
|
52
|
+
case '--ends': out.endsAt = toIso(next); i++; break;
|
|
53
|
+
case '--env': out.env = next; i++; break;
|
|
54
|
+
case '--yes': out.yes = true; break;
|
|
55
|
+
default: throw new Error(`Unknown arg: ${a}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (out.count === undefined || isNaN(out.count)) throw new Error('--count required (1-1000)');
|
|
59
|
+
if (out.percentOff === undefined || isNaN(out.percentOff)) throw new Error('--percent required (1-100)');
|
|
60
|
+
if (!out.campaign) throw new Error('--campaign required');
|
|
61
|
+
return out as GenArgs;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function runGenerate(argv: string[]): Promise<void> {
|
|
65
|
+
const args = parseArgs(argv);
|
|
66
|
+
validateEnv(args.env);
|
|
67
|
+
await confirmIfProd(args.env, `Generate ${args.count} unique discount codes (${args.percentOff}% off, campaign ${args.campaign})`, args.yes);
|
|
68
|
+
|
|
69
|
+
const body: Record<string, unknown> = { count: args.count, percentOff: args.percentOff, campaign: args.campaign };
|
|
70
|
+
if (args.productKeys) body.productKeys = args.productKeys;
|
|
71
|
+
if (args.startsAt) body.startsAt = args.startsAt;
|
|
72
|
+
if (args.endsAt) body.endsAt = args.endsAt;
|
|
73
|
+
|
|
74
|
+
console.log(`\n🎟️ Generating ${args.count} codes on ${args.env.toUpperCase()}...`);
|
|
75
|
+
const res = await callBilling<{ codes: string[] }>(args.env, 'POST', '/api/billing/admin/discount-codes/batch', body);
|
|
76
|
+
const codes = res.body.codes ?? [];
|
|
77
|
+
|
|
78
|
+
const safeCampaign = args.campaign.replace(/[^A-Za-z0-9_-]/g, '');
|
|
79
|
+
const file = `./discount-codes-${safeCampaign}-${Date.now()}.txt`;
|
|
80
|
+
// mode 0o600: single-use codes are sensitive-ish; written to the operator's CWD.
|
|
81
|
+
fs.writeFileSync(file, codes.join('\n') + '\n', { encoding: 'utf-8', mode: 0o600 });
|
|
82
|
+
console.log(`✓ Generated ${codes.length} codes (HTTP ${res.status}). Written to: ${file}`);
|
|
83
|
+
console.log(` (codes are in the file, not printed, to keep them copy-paste clean)`);
|
|
84
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { callBilling, validateEnv } from '../billing-http';
|
|
2
|
+
|
|
3
|
+
interface ListArgs {
|
|
4
|
+
campaign?: string;
|
|
5
|
+
code?: string;
|
|
6
|
+
limit?: number;
|
|
7
|
+
env: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function parseArgs(argv: string[]): ListArgs {
|
|
11
|
+
if (argv[0] === '-h' || argv[0] === '--help') {
|
|
12
|
+
console.log(`Usage: optima-discount list [options]
|
|
13
|
+
|
|
14
|
+
Optional:
|
|
15
|
+
--campaign <label> Filter by campaign
|
|
16
|
+
--code <CODE> Filter by exact code
|
|
17
|
+
--limit <N> Max rows (default 500, max 1000)
|
|
18
|
+
--env stage|prod (default: stage)`);
|
|
19
|
+
process.exit(0);
|
|
20
|
+
}
|
|
21
|
+
const out: Partial<ListArgs> = { env: 'stage' };
|
|
22
|
+
for (let i = 0; i < argv.length; i++) {
|
|
23
|
+
const a = argv[i];
|
|
24
|
+
const next = argv[i + 1];
|
|
25
|
+
switch (a) {
|
|
26
|
+
case '--campaign': out.campaign = next; i++; break;
|
|
27
|
+
case '--code': out.code = next; i++; break;
|
|
28
|
+
case '--limit': { const n = parseInt(next, 10); if (isNaN(n)) throw new Error('--limit requires a number'); out.limit = n; i++; break; }
|
|
29
|
+
case '--env': out.env = next; i++; break;
|
|
30
|
+
default: throw new Error(`Unknown arg: ${a}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return out as ListArgs;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function runList(argv: string[]): Promise<void> {
|
|
37
|
+
const args = parseArgs(argv);
|
|
38
|
+
validateEnv(args.env);
|
|
39
|
+
const qs = new URLSearchParams();
|
|
40
|
+
if (args.campaign) qs.set('campaign', args.campaign);
|
|
41
|
+
if (args.code) qs.set('code', args.code);
|
|
42
|
+
if (args.limit !== undefined) qs.set('limit', String(args.limit));
|
|
43
|
+
const suffix = qs.toString() ? `?${qs.toString()}` : '';
|
|
44
|
+
const res = await callBilling(args.env, 'GET', `/api/billing/admin/discount-codes${suffix}`);
|
|
45
|
+
console.log(JSON.stringify(res.body, null, 2));
|
|
46
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { runCreate } from './discount/create';
|
|
4
|
+
import { runGenerate } from './discount/generate';
|
|
5
|
+
import { runList } from './discount/list';
|
|
6
|
+
import { runDisable } from './discount/disable';
|
|
7
|
+
|
|
8
|
+
function printHelp() {
|
|
9
|
+
console.log(`Usage: optima-discount <subcommand> [options]
|
|
10
|
+
|
|
11
|
+
Subcommands:
|
|
12
|
+
create Create one discount code (shared or single-use)
|
|
13
|
+
generate Generate N unique single-use codes (written to a file)
|
|
14
|
+
list List discount codes (filter by campaign/code)
|
|
15
|
+
disable Disable a discount code
|
|
16
|
+
|
|
17
|
+
Run 'optima-discount <subcommand> --help' for subcommand-specific options.`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function main() {
|
|
21
|
+
const [, , subcommand, ...rest] = process.argv;
|
|
22
|
+
if (!subcommand || subcommand === '-h' || subcommand === '--help') {
|
|
23
|
+
printHelp();
|
|
24
|
+
process.exit(0);
|
|
25
|
+
}
|
|
26
|
+
switch (subcommand) {
|
|
27
|
+
case 'create': await runCreate(rest); break;
|
|
28
|
+
case 'generate': await runGenerate(rest); break;
|
|
29
|
+
case 'list': await runList(rest); break;
|
|
30
|
+
case 'disable': await runDisable(rest); break;
|
|
31
|
+
default:
|
|
32
|
+
console.error(`Unknown subcommand: ${subcommand}`);
|
|
33
|
+
printHelp();
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
main().catch((err) => {
|
|
39
|
+
console.error(err.message);
|
|
40
|
+
process.exit(1);
|
|
41
|
+
});
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.runCreate = runCreate;
|
|
4
|
+
const billing_http_1 = require("../billing-http");
|
|
5
|
+
const confirm_prompt_1 = require("../confirm-prompt");
|
|
6
|
+
/** Normalize a date/datetime arg to a full ISO datetime string (billing requires z.iso.datetime). */
|
|
7
|
+
function toIso(v) {
|
|
8
|
+
const d = new Date(v);
|
|
9
|
+
if (isNaN(d.getTime()))
|
|
10
|
+
throw new Error(`Invalid date: ${v} (use YYYY-MM-DD or ISO datetime)`);
|
|
11
|
+
return d.toISOString();
|
|
12
|
+
}
|
|
13
|
+
function parseArgs(argv) {
|
|
14
|
+
if (argv.length === 0 || argv[0] === '-h' || argv[0] === '--help') {
|
|
15
|
+
console.log(`Usage: optima-discount create --code <CODE> --percent <1-100> [options]
|
|
16
|
+
|
|
17
|
+
Required:
|
|
18
|
+
--code <CODE> Promo code (stored uppercased)
|
|
19
|
+
--percent <1-100> Percentage off
|
|
20
|
+
|
|
21
|
+
Optional:
|
|
22
|
+
--products <a,b,...> Limit to these productKeys (default: all)
|
|
23
|
+
--starts <date> Valid-from (YYYY-MM-DD or ISO; default: immediately)
|
|
24
|
+
--ends <date> Valid-until
|
|
25
|
+
--max <N> Max total redemptions (default: unlimited; 1 = single-use)
|
|
26
|
+
--campaign <label> Grouping label
|
|
27
|
+
--env stage|prod (default: stage)
|
|
28
|
+
--yes Skip prod confirmation`);
|
|
29
|
+
process.exit(0);
|
|
30
|
+
}
|
|
31
|
+
const out = { env: 'stage', yes: false };
|
|
32
|
+
for (let i = 0; i < argv.length; i++) {
|
|
33
|
+
const a = argv[i];
|
|
34
|
+
const next = argv[i + 1];
|
|
35
|
+
switch (a) {
|
|
36
|
+
case '--code':
|
|
37
|
+
out.code = next;
|
|
38
|
+
i++;
|
|
39
|
+
break;
|
|
40
|
+
case '--percent':
|
|
41
|
+
out.percentOff = parseInt(next, 10);
|
|
42
|
+
i++;
|
|
43
|
+
break;
|
|
44
|
+
case '--products':
|
|
45
|
+
if (!next)
|
|
46
|
+
throw new Error('--products requires a value');
|
|
47
|
+
out.productKeys = next.split(',').map((s) => s.trim()).filter(Boolean);
|
|
48
|
+
i++;
|
|
49
|
+
break;
|
|
50
|
+
case '--starts':
|
|
51
|
+
out.startsAt = toIso(next);
|
|
52
|
+
i++;
|
|
53
|
+
break;
|
|
54
|
+
case '--ends':
|
|
55
|
+
out.endsAt = toIso(next);
|
|
56
|
+
i++;
|
|
57
|
+
break;
|
|
58
|
+
case '--max':
|
|
59
|
+
out.maxRedemptions = parseInt(next, 10);
|
|
60
|
+
i++;
|
|
61
|
+
break;
|
|
62
|
+
case '--campaign':
|
|
63
|
+
out.campaign = next;
|
|
64
|
+
i++;
|
|
65
|
+
break;
|
|
66
|
+
case '--env':
|
|
67
|
+
out.env = next;
|
|
68
|
+
i++;
|
|
69
|
+
break;
|
|
70
|
+
case '--yes':
|
|
71
|
+
out.yes = true;
|
|
72
|
+
break;
|
|
73
|
+
default: throw new Error(`Unknown arg: ${a}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (!out.code)
|
|
77
|
+
throw new Error('--code required');
|
|
78
|
+
if (out.percentOff === undefined || isNaN(out.percentOff))
|
|
79
|
+
throw new Error('--percent required (1-100)');
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
async function runCreate(argv) {
|
|
83
|
+
const args = parseArgs(argv);
|
|
84
|
+
(0, billing_http_1.validateEnv)(args.env);
|
|
85
|
+
await (0, confirm_prompt_1.confirmIfProd)(args.env, `Create discount code ${args.code.toUpperCase()} (${args.percentOff}% off)`, args.yes);
|
|
86
|
+
const body = { code: args.code, percentOff: args.percentOff };
|
|
87
|
+
if (args.productKeys)
|
|
88
|
+
body.productKeys = args.productKeys;
|
|
89
|
+
if (args.startsAt)
|
|
90
|
+
body.startsAt = args.startsAt;
|
|
91
|
+
if (args.endsAt)
|
|
92
|
+
body.endsAt = args.endsAt;
|
|
93
|
+
if (args.maxRedemptions !== undefined)
|
|
94
|
+
body.maxRedemptions = args.maxRedemptions;
|
|
95
|
+
if (args.campaign)
|
|
96
|
+
body.campaign = args.campaign;
|
|
97
|
+
console.log(`\n🎟️ Creating discount code on ${args.env.toUpperCase()}...`);
|
|
98
|
+
const res = await (0, billing_http_1.callBilling)(args.env, 'POST', '/api/billing/admin/discount-codes', body);
|
|
99
|
+
console.log(`✓ Created (HTTP ${res.status}):`);
|
|
100
|
+
console.log(JSON.stringify(res.body, null, 2));
|
|
101
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.runDisable = runDisable;
|
|
4
|
+
const billing_http_1 = require("../billing-http");
|
|
5
|
+
const confirm_prompt_1 = require("../confirm-prompt");
|
|
6
|
+
function parseArgs(argv) {
|
|
7
|
+
if (argv.length === 0 || argv[0] === '-h' || argv[0] === '--help') {
|
|
8
|
+
console.log(`Usage: optima-discount disable --code <CODE> [options]
|
|
9
|
+
|
|
10
|
+
Required:
|
|
11
|
+
--code <CODE>
|
|
12
|
+
|
|
13
|
+
Optional:
|
|
14
|
+
--env stage|prod (default: stage)
|
|
15
|
+
--yes Skip prod confirmation`);
|
|
16
|
+
process.exit(0);
|
|
17
|
+
}
|
|
18
|
+
const out = { env: 'stage', yes: false };
|
|
19
|
+
for (let i = 0; i < argv.length; i++) {
|
|
20
|
+
const a = argv[i];
|
|
21
|
+
const next = argv[i + 1];
|
|
22
|
+
switch (a) {
|
|
23
|
+
case '--code':
|
|
24
|
+
out.code = next;
|
|
25
|
+
i++;
|
|
26
|
+
break;
|
|
27
|
+
case '--env':
|
|
28
|
+
out.env = next;
|
|
29
|
+
i++;
|
|
30
|
+
break;
|
|
31
|
+
case '--yes':
|
|
32
|
+
out.yes = true;
|
|
33
|
+
break;
|
|
34
|
+
default: throw new Error(`Unknown arg: ${a}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (!out.code)
|
|
38
|
+
throw new Error('--code required');
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
async function runDisable(argv) {
|
|
42
|
+
const args = parseArgs(argv);
|
|
43
|
+
(0, billing_http_1.validateEnv)(args.env);
|
|
44
|
+
await (0, confirm_prompt_1.confirmIfProd)(args.env, `Disable discount code ${args.code.toUpperCase()}`, args.yes);
|
|
45
|
+
const res = await (0, billing_http_1.callBilling)(args.env, 'PATCH', `/api/billing/admin/discount-codes/${encodeURIComponent(args.code)}`, { status: 'DISABLED' });
|
|
46
|
+
console.log(`✓ Disabled (HTTP ${res.status}):`);
|
|
47
|
+
console.log(JSON.stringify(res.body, null, 2));
|
|
48
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.runGenerate = runGenerate;
|
|
37
|
+
const fs = __importStar(require("fs"));
|
|
38
|
+
const billing_http_1 = require("../billing-http");
|
|
39
|
+
const confirm_prompt_1 = require("../confirm-prompt");
|
|
40
|
+
function toIso(v) {
|
|
41
|
+
const d = new Date(v);
|
|
42
|
+
if (isNaN(d.getTime()))
|
|
43
|
+
throw new Error(`Invalid date: ${v} (use YYYY-MM-DD or ISO datetime)`);
|
|
44
|
+
return d.toISOString();
|
|
45
|
+
}
|
|
46
|
+
function parseArgs(argv) {
|
|
47
|
+
if (argv.length === 0 || argv[0] === '-h' || argv[0] === '--help') {
|
|
48
|
+
console.log(`Usage: optima-discount generate --count <N> --percent <1-100> --campaign <label> [options]
|
|
49
|
+
|
|
50
|
+
Generates N unique single-use codes (maxRedemptions=1). Codes are written to a
|
|
51
|
+
local file (NOT printed) to keep them copy-paste clean.
|
|
52
|
+
|
|
53
|
+
Required:
|
|
54
|
+
--count <N> How many codes (1-1000)
|
|
55
|
+
--percent <1-100> Percentage off
|
|
56
|
+
--campaign <label> Grouping label (also the code prefix)
|
|
57
|
+
|
|
58
|
+
Optional:
|
|
59
|
+
--products <a,b,...> Limit to these productKeys
|
|
60
|
+
--starts <date> Valid-from (YYYY-MM-DD or ISO)
|
|
61
|
+
--ends <date> Valid-until
|
|
62
|
+
--env stage|prod (default: stage)
|
|
63
|
+
--yes Skip prod confirmation`);
|
|
64
|
+
process.exit(0);
|
|
65
|
+
}
|
|
66
|
+
const out = { env: 'stage', yes: false };
|
|
67
|
+
for (let i = 0; i < argv.length; i++) {
|
|
68
|
+
const a = argv[i];
|
|
69
|
+
const next = argv[i + 1];
|
|
70
|
+
switch (a) {
|
|
71
|
+
case '--count':
|
|
72
|
+
out.count = parseInt(next, 10);
|
|
73
|
+
i++;
|
|
74
|
+
break;
|
|
75
|
+
case '--percent':
|
|
76
|
+
out.percentOff = parseInt(next, 10);
|
|
77
|
+
i++;
|
|
78
|
+
break;
|
|
79
|
+
case '--campaign':
|
|
80
|
+
out.campaign = next;
|
|
81
|
+
i++;
|
|
82
|
+
break;
|
|
83
|
+
case '--products':
|
|
84
|
+
if (!next)
|
|
85
|
+
throw new Error('--products requires a value');
|
|
86
|
+
out.productKeys = next.split(',').map((s) => s.trim()).filter(Boolean);
|
|
87
|
+
i++;
|
|
88
|
+
break;
|
|
89
|
+
case '--starts':
|
|
90
|
+
out.startsAt = toIso(next);
|
|
91
|
+
i++;
|
|
92
|
+
break;
|
|
93
|
+
case '--ends':
|
|
94
|
+
out.endsAt = toIso(next);
|
|
95
|
+
i++;
|
|
96
|
+
break;
|
|
97
|
+
case '--env':
|
|
98
|
+
out.env = next;
|
|
99
|
+
i++;
|
|
100
|
+
break;
|
|
101
|
+
case '--yes':
|
|
102
|
+
out.yes = true;
|
|
103
|
+
break;
|
|
104
|
+
default: throw new Error(`Unknown arg: ${a}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (out.count === undefined || isNaN(out.count))
|
|
108
|
+
throw new Error('--count required (1-1000)');
|
|
109
|
+
if (out.percentOff === undefined || isNaN(out.percentOff))
|
|
110
|
+
throw new Error('--percent required (1-100)');
|
|
111
|
+
if (!out.campaign)
|
|
112
|
+
throw new Error('--campaign required');
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
async function runGenerate(argv) {
|
|
116
|
+
const args = parseArgs(argv);
|
|
117
|
+
(0, billing_http_1.validateEnv)(args.env);
|
|
118
|
+
await (0, confirm_prompt_1.confirmIfProd)(args.env, `Generate ${args.count} unique discount codes (${args.percentOff}% off, campaign ${args.campaign})`, args.yes);
|
|
119
|
+
const body = { count: args.count, percentOff: args.percentOff, campaign: args.campaign };
|
|
120
|
+
if (args.productKeys)
|
|
121
|
+
body.productKeys = args.productKeys;
|
|
122
|
+
if (args.startsAt)
|
|
123
|
+
body.startsAt = args.startsAt;
|
|
124
|
+
if (args.endsAt)
|
|
125
|
+
body.endsAt = args.endsAt;
|
|
126
|
+
console.log(`\n🎟️ Generating ${args.count} codes on ${args.env.toUpperCase()}...`);
|
|
127
|
+
const res = await (0, billing_http_1.callBilling)(args.env, 'POST', '/api/billing/admin/discount-codes/batch', body);
|
|
128
|
+
const codes = res.body.codes ?? [];
|
|
129
|
+
const safeCampaign = args.campaign.replace(/[^A-Za-z0-9_-]/g, '');
|
|
130
|
+
const file = `./discount-codes-${safeCampaign}-${Date.now()}.txt`;
|
|
131
|
+
// mode 0o600: single-use codes are sensitive-ish; written to the operator's CWD.
|
|
132
|
+
fs.writeFileSync(file, codes.join('\n') + '\n', { encoding: 'utf-8', mode: 0o600 });
|
|
133
|
+
console.log(`✓ Generated ${codes.length} codes (HTTP ${res.status}). Written to: ${file}`);
|
|
134
|
+
console.log(` (codes are in the file, not printed, to keep them copy-paste clean)`);
|
|
135
|
+
}
|