@newpeak/barista-cli 0.1.3 → 0.1.4

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.
Files changed (52) hide show
  1. package/bin/barista.js +0 -0
  2. package/dist/commands/liberica/clients/create.d.ts +3 -0
  3. package/dist/commands/liberica/clients/create.d.ts.map +1 -0
  4. package/dist/commands/liberica/clients/create.js +125 -0
  5. package/dist/commands/liberica/clients/create.js.map +1 -0
  6. package/dist/commands/liberica/clients/delete.d.ts +3 -0
  7. package/dist/commands/liberica/clients/delete.d.ts.map +1 -0
  8. package/dist/commands/liberica/clients/delete.js +125 -0
  9. package/dist/commands/liberica/clients/delete.js.map +1 -0
  10. package/dist/commands/liberica/clients/get.d.ts +3 -0
  11. package/dist/commands/liberica/clients/get.d.ts.map +1 -0
  12. package/dist/commands/liberica/clients/get.js +77 -0
  13. package/dist/commands/liberica/clients/get.js.map +1 -0
  14. package/dist/commands/liberica/clients/index.d.ts +3 -0
  15. package/dist/commands/liberica/clients/index.d.ts.map +1 -0
  16. package/dist/commands/liberica/clients/index.js +17 -0
  17. package/dist/commands/liberica/clients/index.js.map +1 -0
  18. package/dist/commands/liberica/clients/list.d.ts +3 -0
  19. package/dist/commands/liberica/clients/list.d.ts.map +1 -0
  20. package/dist/commands/liberica/clients/list.js +92 -0
  21. package/dist/commands/liberica/clients/list.js.map +1 -0
  22. package/dist/commands/liberica/clients/update.d.ts +3 -0
  23. package/dist/commands/liberica/clients/update.d.ts.map +1 -0
  24. package/dist/commands/liberica/clients/update.js +106 -0
  25. package/dist/commands/liberica/clients/update.js.map +1 -0
  26. package/dist/commands/liberica/index.d.ts.map +1 -1
  27. package/dist/commands/liberica/index.js +3 -0
  28. package/dist/commands/liberica/index.js.map +1 -1
  29. package/dist/core/api/client.d.ts +6 -0
  30. package/dist/core/api/client.d.ts.map +1 -1
  31. package/dist/core/api/client.js +125 -0
  32. package/dist/core/api/client.js.map +1 -1
  33. package/dist/index.js +1 -1
  34. package/dist/types/client.d.ts +100 -0
  35. package/dist/types/client.d.ts.map +1 -0
  36. package/dist/types/client.js +2 -0
  37. package/dist/types/client.js.map +1 -0
  38. package/dist/types/index.d.ts +2 -0
  39. package/dist/types/index.d.ts.map +1 -1
  40. package/docs/commands/REFERENCE.md +21 -15
  41. package/package.json +1 -1
  42. package/src/commands/liberica/clients/create.ts +122 -0
  43. package/src/commands/liberica/clients/delete.ts +125 -0
  44. package/src/commands/liberica/clients/get.ts +88 -0
  45. package/src/commands/liberica/clients/index.ts +19 -0
  46. package/src/commands/liberica/clients/list.ts +123 -0
  47. package/src/commands/liberica/clients/update.ts +103 -0
  48. package/src/commands/liberica/index.ts +3 -0
  49. package/src/core/api/client.ts +152 -0
  50. package/src/index.ts +1 -1
  51. package/src/types/client.ts +115 -0
  52. package/src/types/index.ts +3 -0
@@ -14,8 +14,8 @@
14
14
  | **Arabica** | access | 1 | 📋 待开发 | P0 |
15
15
  | **Liberica** | auth | 2 | ✅ 已实现 | P0 |
16
16
  | **Liberica** | context | 3 | ✅ 已实现 | P0 |
17
- | **Liberica** | users | 3 | 📋 待开发 | P1 |
18
- | **Liberica** | employees | 3 | 📋 待开发 | P1 |
17
+ | **Liberica** | users | 3 | 已实现 | P1 |
18
+ | **Liberica** | employees | 3 | 已实现 | P1 |
19
19
  | **Liberica** | clients | 4 | 📋 待开发 | P0 |
20
20
  | **Liberica** | suppliers | 4 | 📋 待开发 | P0 |
21
21
  | **Liberica** | materials | 5 | 📋 待开发 | P0 |
@@ -104,21 +104,21 @@
104
104
  | 切换环境 | `barista context use-env [env]` | 切换操作的环境 | [docs](./liberica/context/index.md) |
105
105
  | 切换租户 | `barista context use-tenant <tenant>` | 切换操作的企业 | [docs](./liberica/context/index.md) |
106
106
 
107
- ### 3. users(用户管理)- 📋 待开发
107
+ ### 3. users(用户管理)- 已实现
108
108
 
109
109
  | 功能 | 命令 | HTTP | 说明 | 设计文档 |
110
110
  |------|------|------|------|----------|
111
- | 创建新用户 | `barista liberica users create` | POST | 创建系统用户账号 | 待创建 |
112
- | 获取用户列表 | `barista liberica users list` | GET | 查看所有用户 | 待创建 |
113
- | 获取用户详情 | `barista liberica users get <id>` | GET | 查看用户信息 | 待创建 |
111
+ | 创建新用户 | `barista liberica users create` | POST | 创建系统用户账号 | [docs](./liberica/users/create.md) |
112
+ | 获取用户列表 | `barista liberica users list` | GET | 查看所有用户 | [docs](./liberica/users/list.md) |
113
+ | 获取用户详情 | `barista liberica users get <id>` | GET | 查看用户信息 | [docs](./liberica/users/get.md) |
114
114
 
115
- ### 4. employees(职员管理)- 📋 待开发
115
+ ### 4. employees(职员管理)- 已实现
116
116
 
117
117
  | 功能 | 命令 | HTTP | 说明 | 设计文档 |
118
118
  |------|------|------|------|----------|
119
- | 创建新职员 | `barista liberica employees create` | POST | 添加企业员工 | 待创建 |
120
- | 获取职员列表 | `barista liberica employees list` | GET | 查看所有职员 | 待创建 |
121
- | 获取职员详情 | `barista liberica employees get <id>` | GET | 查看职员信息 | 待创建 |
119
+ | 创建新职员 | `barista liberica employees create` | POST | 添加企业员工 | [docs](./liberica/employees/create.md) |
120
+ | 获取职员列表 | `barista liberica employees list` | GET | 查看所有职员 | [docs](./liberica/employees/list.md) |
121
+ | 获取职员详情 | `barista liberica employees get <id>` | GET | 查看职员信息 | [docs](./liberica/employees/get.md) |
122
122
 
123
123
  ### 5. clients(客户管理)- 📋 待开发
124
124
 
@@ -236,10 +236,16 @@ docs/commands/
236
236
  │ └── index.md
237
237
  ├── context/ # ✅ 已实现
238
238
  │ └── index.md
239
- ├── users/ # 📋 待开发
240
- └── index.md # 待创建
241
- ├── employees/ # 📋 待开发
242
- │ └── index.md # 待创建
239
+ ├── users/ # 已实现
240
+ ├── create.md
241
+ ├── list.md
242
+ │ └── get.md
243
+ ├── employees/ # ✅ 已实现
244
+ │ ├── create.md
245
+ │ ├── list.md
246
+ │ ├── get.md
247
+ │ ├── enable.md
248
+ │ └── disable.md
243
249
  ├── clients/ # 📋 待开发
244
250
  │ └── index.md # 待创建
245
251
  ├── suppliers/ # 📋 待开发
@@ -294,4 +300,4 @@ Arabica(会员) Liberica(租户员工)
294
300
 
295
301
  ---
296
302
 
297
- **最后更新**: 2025-04-12
303
+ **最后更新**: 2026-04-14
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@newpeak/barista-cli",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "AI Tools CLI for Liberica and Arabica services",
5
5
  "type": "module",
6
6
  "bin": {
@@ -0,0 +1,122 @@
1
+ import { Command } from 'commander';
2
+ import chalk from 'chalk';
3
+ import { configManager } from '../../../core/config/manager.js';
4
+ import { apiClient } from '../../../core/api/client.js';
5
+ import { CreateClientRequest, StatusFlag } from '../../../types/client.js';
6
+
7
+ export function createClientCreateCommand(): Command {
8
+ const cmd = new Command('create');
9
+ cmd.description('Create a new client (auto-generates client code if not provided)');
10
+
11
+ cmd
12
+ .option('--client-code <code>', 'Client code (auto-generated if not provided)')
13
+ .option('--client-name <name>', 'Client name')
14
+ .option('--follower <name>', 'Follower/salesperson')
15
+ .option('--office-address <address>', 'Office address')
16
+ .option('--office-telephone <phone>', 'Office telephone')
17
+ .option('--status <status>', 'Status (1=enable, 2=disable)')
18
+ .option('--remark <remark>', 'Remark')
19
+ .option('--dry-run', 'Preview the operation without executing')
20
+ .option('--json', 'Output as JSON')
21
+ .action(async (options: Record<string, unknown>) => {
22
+ const context = configManager.getCurrentContext();
23
+ const environment = context.environment;
24
+ const tenant = context.tenant;
25
+ const jsonOutput = options.json === true;
26
+
27
+ let clientCode = options.clientCode as string | undefined;
28
+ const clientName = options.clientName as string | undefined;
29
+ const follower = options.follower as string | undefined;
30
+ const officeAddress = options.officeAddress as string | undefined;
31
+ const officeTelephone = options.officeTelephone as string | undefined;
32
+ const status = options.status as string | undefined;
33
+ const remark = options.remark as string | undefined;
34
+
35
+ if (!clientCode) {
36
+ const codeResponse = await apiClient.getCodeByType(environment, tenant, 'TenantClientCode');
37
+ if (codeResponse.success && codeResponse.data) {
38
+ clientCode = codeResponse.data;
39
+ } else {
40
+ const errMsg = codeResponse.error?.message || 'Failed to generate client code';
41
+ if (jsonOutput) {
42
+ console.log(JSON.stringify({ success: false, error: { code: 'CODE_GENERATION_ERROR', message: errMsg } }));
43
+ } else {
44
+ console.error(chalk.red(`\n✗ Failed to generate client code: ${errMsg}\n`));
45
+ }
46
+ process.exit(1);
47
+ }
48
+ }
49
+
50
+ const data: CreateClientRequest = {
51
+ clientCode,
52
+ clientName,
53
+ follower,
54
+ officeAddress,
55
+ officeTelephone,
56
+ statusFlag: status ? parseInt(status) as StatusFlag : undefined,
57
+ remark,
58
+ };
59
+
60
+ Object.keys(data).forEach(key => {
61
+ if (data[key as keyof CreateClientRequest] === undefined) {
62
+ delete data[key as keyof CreateClientRequest];
63
+ }
64
+ });
65
+
66
+ if (options.dryRun) {
67
+ if (jsonOutput) {
68
+ console.log(JSON.stringify({ success: true, dryRun: true, data }));
69
+ } else {
70
+ console.log(chalk.cyan('\n🔍 Dry-Run Mode: No actual API call will be made\n'));
71
+ console.log(chalk.gray(' This operation will create the following client:'));
72
+ console.log(` ${chalk.gray('Client Code:')} ${clientCode}`);
73
+ if (clientName) console.log(` ${chalk.gray('Client Name:')} ${clientName}`);
74
+ if (follower) console.log(` ${chalk.gray('Follower:')} ${follower}`);
75
+ if (officeAddress) console.log(` ${chalk.gray('Office:')} ${officeAddress}`);
76
+ console.log();
77
+ }
78
+ return;
79
+ }
80
+
81
+ try {
82
+ const response = await apiClient.createClient(environment, tenant, data);
83
+
84
+ if (response.success) {
85
+ if (jsonOutput) {
86
+ console.log(JSON.stringify({ success: true, data: response.data }));
87
+ } else {
88
+ console.log(chalk.green('\n✓ Client created successfully\n'));
89
+ if (response.data && typeof response.data === 'object' && 'clientId' in response.data) {
90
+ console.log(` ${chalk.gray('Client ID:')} ${chalk.green(String((response.data as { clientId: string }).clientId))}`);
91
+ }
92
+ if (clientCode) console.log(` ${chalk.gray('Client Code:')} ${chalk.green(clientCode)}`);
93
+ if (clientName) console.log(` ${chalk.gray('Client Name:')} ${clientName}`);
94
+ console.log();
95
+ }
96
+ } else {
97
+ const errorMsg = response.error?.message || 'Failed to create client';
98
+ const errorCode = response.error?.code || 'CREATE_CLIENT_ERROR';
99
+ if (jsonOutput) {
100
+ console.log(JSON.stringify({ success: false, error: { code: errorCode, message: errorMsg } }));
101
+ } else {
102
+ console.error(chalk.red(`\n✗ Failed to create client: ${errorMsg}`));
103
+ if (response.error?.code) {
104
+ console.error(chalk.gray(` Error code: ${response.error.code}`));
105
+ }
106
+ console.error();
107
+ }
108
+ process.exit(1);
109
+ }
110
+ } catch (error) {
111
+ const errMsg = error instanceof Error ? error.message : 'Unknown error';
112
+ if (jsonOutput) {
113
+ console.log(JSON.stringify({ success: false, error: { code: 'ERROR', message: errMsg } }));
114
+ } else {
115
+ console.error(chalk.red(`\n✗ Error: ${errMsg}\n`));
116
+ }
117
+ process.exit(1);
118
+ }
119
+ });
120
+
121
+ return cmd;
122
+ }
@@ -0,0 +1,125 @@
1
+ import { Command } from 'commander';
2
+ import chalk from 'chalk';
3
+ import { configManager } from '../../../core/config/manager.js';
4
+ import { apiClient } from '../../../core/api/client.js';
5
+ import { Environment } from '../../../types/index.js';
6
+ import { MasterClientResponse } from '../../../types/client.js';
7
+
8
+ export function createClientDeleteCommand(): Command {
9
+ const deleteCommand = new Command('delete');
10
+ deleteCommand
11
+ .description('Delete a client')
12
+ .argument('<id>', 'Client ID')
13
+ .option('--force', 'Skip confirmation prompt')
14
+ .option('--dry-run', 'Preview the operation without executing')
15
+ .option('--json', 'Output as JSON')
16
+ .action(async (id: string, options: Record<string, unknown>) => {
17
+ const context = configManager.getCurrentContext();
18
+ const environment = context.environment as Environment;
19
+ const tenant = context.tenant;
20
+ const jsonOutput = options.json === true;
21
+
22
+ const clientId = id;
23
+
24
+ if (!clientId) {
25
+ if (jsonOutput) {
26
+ console.log(JSON.stringify({ success: false, error: { code: 'VALIDATION_ERROR', message: 'Client ID is required' } }));
27
+ } else {
28
+ console.error(chalk.red('\n✗ Client ID is required\n'));
29
+ }
30
+ process.exit(1);
31
+ }
32
+
33
+ if (!jsonOutput) {
34
+ console.log(chalk.bold(`\n🗑️ Delete Client (${environment})\n`));
35
+ console.log(chalk.gray(` Client ID: ${clientId}`));
36
+ }
37
+
38
+ if (options.dryRun) {
39
+ if (jsonOutput) {
40
+ console.log(JSON.stringify({ success: true, dryRun: true, clientId }));
41
+ } else {
42
+ console.log(chalk.cyan('\n🔍 Dry-Run Mode: No actual API call will be made\n'));
43
+ console.log(chalk.gray(' This operation will delete the client.'));
44
+ console.log();
45
+ }
46
+ return;
47
+ }
48
+
49
+ let client: MasterClientResponse | undefined;
50
+ try {
51
+ const clientResponse = await apiClient.getClient(environment, tenant, clientId);
52
+ if (!clientResponse.success || !clientResponse.data) {
53
+ const errMsg = `Client not found: ${clientId}`;
54
+ if (jsonOutput) {
55
+ console.log(JSON.stringify({ success: false, error: { code: 'NOT_FOUND', message: errMsg } }));
56
+ } else {
57
+ console.error(chalk.red(`\n✗ ${errMsg}\n`));
58
+ }
59
+ process.exit(1);
60
+ }
61
+ client = clientResponse.data as MasterClientResponse;
62
+ } catch (error) {
63
+ const errMsg = error instanceof Error ? error.message : 'Unknown error';
64
+ if (jsonOutput) {
65
+ console.log(JSON.stringify({ success: false, error: { code: 'FETCH_ERROR', message: `Failed to fetch client: ${errMsg}` } }));
66
+ } else {
67
+ console.error(chalk.red(`\n✗ Failed to fetch client: ${errMsg}\n`));
68
+ }
69
+ process.exit(1);
70
+ }
71
+
72
+ if (!options.force) {
73
+ if (!jsonOutput) {
74
+ console.log(chalk.gray(` Client Name: ${client.clientName}`));
75
+ console.log(chalk.gray(` Client Code: ${client.clientCode}`));
76
+ console.log(chalk.yellow(`\n⚠️ This action cannot be undone.`));
77
+ console.log(chalk.gray(' Use --force to skip this confirmation.\n'));
78
+ } else {
79
+ console.log(JSON.stringify({
80
+ success: false,
81
+ error: {
82
+ code: 'CONFIRMATION_REQUIRED',
83
+ message: `Delete client "${client.clientName}"? Use --force to confirm.`
84
+ }
85
+ }));
86
+ }
87
+ process.exit(1);
88
+ }
89
+
90
+ try {
91
+ const response = await apiClient.deleteClient(environment, tenant, clientId);
92
+
93
+ if (response.success) {
94
+ if (jsonOutput) {
95
+ console.log(JSON.stringify({ success: true, clientId, name: client.clientName }));
96
+ } else {
97
+ console.log(chalk.green(`\n✓ Client "${client.clientName}" (ID: ${clientId}) deleted\n`));
98
+ }
99
+ } else {
100
+ const errorMsg = response.error?.message || 'Unknown error';
101
+ const errorCode = response.error?.code || 'DELETE_ERROR';
102
+ if (jsonOutput) {
103
+ console.log(JSON.stringify({ success: false, error: { code: errorCode, message: errorMsg } }));
104
+ } else {
105
+ console.error(chalk.red(`\n✗ Failed to delete client: ${errorMsg}`));
106
+ if (response.error?.code) {
107
+ console.error(chalk.gray(` Error code: ${response.error.code}`));
108
+ }
109
+ console.error();
110
+ }
111
+ process.exit(1);
112
+ }
113
+ } catch (error) {
114
+ const errMsg = error instanceof Error ? error.message : 'Unknown error';
115
+ if (jsonOutput) {
116
+ console.log(JSON.stringify({ success: false, error: { code: 'ERROR', message: errMsg } }));
117
+ } else {
118
+ console.error(chalk.red(`\n✗ Error: ${errMsg}\n`));
119
+ }
120
+ process.exit(1);
121
+ }
122
+ });
123
+
124
+ return deleteCommand;
125
+ }
@@ -0,0 +1,88 @@
1
+ import { Command } from 'commander';
2
+ import chalk from 'chalk';
3
+ import { configManager } from '../../../core/config/manager.js';
4
+ import { apiClient } from '../../../core/api/client.js';
5
+ import { Environment } from '../../../types/index.js';
6
+ import { MasterClientResponse } from '../../../types/client.js';
7
+
8
+ export function createClientGetCommand(): Command {
9
+ const getCommand = new Command('get');
10
+ getCommand.description('Get client details by ID');
11
+ getCommand.arguments('<id>');
12
+ getCommand.option('--json', 'Output as JSON');
13
+
14
+ getCommand.action(async (id: string, options: Record<string, unknown>) => {
15
+ const context = configManager.getCurrentContext();
16
+ const environment = (options.env as Environment) || context.environment;
17
+ const tenant = (options.tenant as string) || context.tenant;
18
+
19
+ if (!id) {
20
+ console.error(chalk.red('\n✗ Client ID is required\n'));
21
+ process.exit(1);
22
+ }
23
+
24
+ const clientId = id;
25
+ if (!clientId || clientId.trim() === '') {
26
+ console.error(chalk.red('\n✗ Invalid client ID.\n'));
27
+ process.exit(1);
28
+ }
29
+
30
+ try {
31
+ const response = await apiClient.getClient(environment, tenant, clientId);
32
+
33
+ if (!response.success) {
34
+ const errorCode = response.error?.code || 'UNKNOWN_ERROR';
35
+ const errorMessage = response.error?.message || 'Unknown error';
36
+
37
+ if (errorCode === '01001150001' || errorMessage.includes('不存在')) {
38
+ console.error(chalk.red(`\n✗ Client not found: ${clientId}\n`));
39
+ } else {
40
+ console.error(chalk.red(`\n✗ Error: ${errorMessage} (${errorCode})\n`));
41
+ }
42
+ process.exit(1);
43
+ }
44
+
45
+ const client = response.data as MasterClientResponse | null;
46
+
47
+ if (!client) {
48
+ console.error(chalk.red('\n✗ Client not found\n'));
49
+ process.exit(1);
50
+ }
51
+
52
+ if (options.json) {
53
+ console.log(JSON.stringify({
54
+ success: true,
55
+ data: client,
56
+ }, null, 2));
57
+ return;
58
+ }
59
+
60
+ console.log(chalk.bold('\n🏢 Client Details\n'));
61
+ console.log(` ${chalk.gray('Client ID:')} ${client.clientId}`);
62
+ console.log(` ${chalk.gray('Client Code:')} ${client.clientCode}`);
63
+ console.log(` ${chalk.gray('Client Name:')} ${client.clientName}`);
64
+ if (client.follower) {
65
+ console.log(` ${chalk.gray('Follower:')} ${client.follower}`);
66
+ }
67
+ if (client.officeAddress) {
68
+ console.log(` ${chalk.gray('Office:')} ${client.officeAddress}`);
69
+ }
70
+ if (client.officeTelephone) {
71
+ console.log(` ${chalk.gray('Telephone:')} ${client.officeTelephone}`);
72
+ }
73
+ if (client.statusFlag !== undefined) {
74
+ const statusText = client.statusFlag === 1 ? chalk.green('Enable') : chalk.red('Disable');
75
+ console.log(` ${chalk.gray('Status:')} ${statusText}`);
76
+ }
77
+ if (client.remark) {
78
+ console.log(` ${chalk.gray('Remark:')} ${client.remark}`);
79
+ }
80
+ console.log();
81
+ } catch (error) {
82
+ console.error(chalk.red(`\n✗ Unexpected error: ${error instanceof Error ? error.message : 'Unknown error'}\n`));
83
+ process.exit(1);
84
+ }
85
+ });
86
+
87
+ return getCommand;
88
+ }
@@ -0,0 +1,19 @@
1
+ import { Command } from 'commander';
2
+ import { createClientListCommand } from './list.js';
3
+ import { createClientGetCommand } from './get.js';
4
+ import { createClientCreateCommand } from './create.js';
5
+ import { createClientUpdateCommand } from './update.js';
6
+ import { createClientDeleteCommand } from './delete.js';
7
+
8
+ export function createClientsCommand(): Command {
9
+ const clientsCommand = new Command('clients');
10
+ clientsCommand.description('Manage clients');
11
+
12
+ clientsCommand.addCommand(createClientListCommand());
13
+ clientsCommand.addCommand(createClientGetCommand());
14
+ clientsCommand.addCommand(createClientCreateCommand());
15
+ clientsCommand.addCommand(createClientUpdateCommand());
16
+ clientsCommand.addCommand(createClientDeleteCommand());
17
+
18
+ return clientsCommand;
19
+ }
@@ -0,0 +1,123 @@
1
+ import { Command } from 'commander';
2
+ import chalk from 'chalk';
3
+ import Table from 'cli-table3';
4
+ import { configManager } from '../../../core/config/manager.js';
5
+ import { apiClient } from '../../../core/api/client.js';
6
+ import { ClientListResponse, StatusFlag } from '../../../types/client.js';
7
+ import { Environment } from '../../../types/index.js';
8
+
9
+ export function createClientListCommand(): Command {
10
+ const listCommand = new Command('list');
11
+ listCommand.description('List clients with pagination');
12
+
13
+ listCommand
14
+ .option('-p, --page <number>', 'Page number', '1')
15
+ .option('-s, --size <number>', 'Page size', '20')
16
+ .option('--status <number>', 'Status filter (1=enable, 2=disable)')
17
+ .option('--json', 'Output as JSON')
18
+ .action(async (options) => {
19
+ const context = configManager.getCurrentContext();
20
+ const environment = (options.env as Environment) || context.environment;
21
+ const tenant = options.tenant || context.tenant;
22
+
23
+ const page = parseInt(options.page as string, 10) - 1; // API uses 0-based pagination
24
+ const size = parseInt(options.size as string, 10);
25
+
26
+ const params: {
27
+ pageNo: number;
28
+ pageSize: number;
29
+ status?: StatusFlag;
30
+ } = { pageNo: page, pageSize: size };
31
+
32
+ if (options.status !== undefined) {
33
+ const statusNum = parseInt(options.status as string, 10);
34
+ if (statusNum === 1 || statusNum === 2) {
35
+ params.status = statusNum as StatusFlag;
36
+ }
37
+ }
38
+
39
+ const response = await apiClient.listClients(environment, tenant, params);
40
+
41
+ if (!response.success) {
42
+ console.error(chalk.red(`\n✗ Failed to list clients: ${response.error?.message || 'Unknown error'}`));
43
+ if (response.error?.code) {
44
+ console.error(chalk.gray(` Error code: ${response.error.code}`));
45
+ }
46
+ console.error();
47
+ process.exit(1);
48
+ }
49
+
50
+ const data = response.data as ClientListResponse | null;
51
+
52
+ if (!data) {
53
+ if (options.json) {
54
+ console.log(JSON.stringify({ success: true, data: { items: [], pagination: { pageNo: 0, pageSize: 20, totalRows: 0 } } }, null, 2));
55
+ } else {
56
+ console.log(chalk.bold('\n📋 Client List\n'));
57
+ console.log(chalk.gray(' No clients found\n'));
58
+ }
59
+ return;
60
+ }
61
+
62
+ if (options.json) {
63
+ console.log(
64
+ JSON.stringify(
65
+ {
66
+ success: true,
67
+ data: {
68
+ items: data.rows || [],
69
+ pagination: {
70
+ pageNo: data.pageNo,
71
+ pageSize: data.pageSize,
72
+ totalRows: data.totalRows,
73
+ },
74
+ },
75
+ },
76
+ null,
77
+ 2
78
+ )
79
+ );
80
+ return;
81
+ }
82
+
83
+ const records = data.rows || [];
84
+
85
+ if (records.length === 0) {
86
+ console.log(chalk.bold('\n📋 Client List\n'));
87
+ console.log(chalk.gray(' No clients found\n'));
88
+ return;
89
+ }
90
+
91
+ console.log(chalk.bold('\n📋 Client List\n'));
92
+
93
+ const table = new Table({
94
+ head: [chalk.bold('CLIENT CODE'), chalk.bold('CLIENT NAME'), chalk.bold('FOLLOWER'), chalk.bold('STATUS')],
95
+ colWidths: [15, 25, 20, 10],
96
+ style: {
97
+ head: [],
98
+ border: [],
99
+ },
100
+ });
101
+
102
+ for (const client of records) {
103
+ const statusText = client.statusFlag === 1 ? chalk.green('Enable') : chalk.red('Disable');
104
+ table.push([
105
+ client.clientCode,
106
+ client.clientName || '-',
107
+ client.follower || '-',
108
+ statusText,
109
+ ]);
110
+ }
111
+
112
+ console.log(table.toString());
113
+ console.log();
114
+ console.log(
115
+ chalk.gray(
116
+ ` Total: ${data.totalRows} | Page: ${data.pageNo + 1} / ${Math.ceil(data.totalRows / data.pageSize) || 1} | Size: ${data.pageSize}`
117
+ )
118
+ );
119
+ console.log();
120
+ });
121
+
122
+ return listCommand;
123
+ }
@@ -0,0 +1,103 @@
1
+ import { Command } from 'commander';
2
+ import chalk from 'chalk';
3
+ import { configManager } from '../../../core/config/manager.js';
4
+ import { apiClient } from '../../../core/api/client.js';
5
+ import { Environment } from '../../../types/index.js';
6
+ import { UpdateClientRequest, StatusFlag } from '../../../types/client.js';
7
+
8
+ export function createClientUpdateCommand(): Command {
9
+ const cmd = new Command('update');
10
+ cmd.description('Update a client');
11
+ cmd.arguments('<clientId>');
12
+
13
+ cmd
14
+ .option('--client-name <name>', 'Client name')
15
+ .option('--follower <name>', 'Follower/salesperson')
16
+ .option('--office-address <address>', 'Office address')
17
+ .option('--office-telephone <phone>', 'Office telephone')
18
+ .option('--status <status>', 'Status (1=enable, 2=disable)')
19
+ .option('--remark <remark>', 'Remark')
20
+ .option('--json', 'Output as JSON')
21
+ .action(async (clientId: string, options: Record<string, unknown>) => {
22
+ const context = configManager.getCurrentContext();
23
+ const environment = context.environment as Environment;
24
+ const tenant = context.tenant;
25
+ const jsonOutput = options.json === true;
26
+
27
+ if (!clientId) {
28
+ if (jsonOutput) {
29
+ console.log(JSON.stringify({ success: false, error: { code: 'VALIDATION_ERROR', message: 'Client ID is required' } }));
30
+ } else {
31
+ console.error(chalk.red('\n✗ Client ID is required\n'));
32
+ }
33
+ process.exit(1);
34
+ }
35
+
36
+ const fields: Partial<UpdateClientRequest> = {
37
+ clientId,
38
+ };
39
+
40
+ if (options.clientName) fields.clientName = options.clientName as string;
41
+ if (options.follower) fields.follower = options.follower as string;
42
+ if (options.officeAddress) fields.officeAddress = options.officeAddress as string;
43
+ if (options.officeTelephone) fields.officeTelephone = options.officeTelephone as string;
44
+ if (options.status) fields.statusFlag = parseInt(options.status as string) as StatusFlag;
45
+ if (options.remark) fields.remark = options.remark as string;
46
+
47
+ const updateFields = Object.keys(fields).filter(k => k !== 'clientId' && fields[k as keyof UpdateClientRequest] !== undefined);
48
+ if (updateFields.length === 0) {
49
+ if (jsonOutput) {
50
+ console.log(JSON.stringify({ success: false, error: { code: 'VALIDATION_ERROR', message: 'No fields to update. Provide at least one of: --client-name, --follower, --office-address, --office-telephone, --status, --remark' } }));
51
+ } else {
52
+ console.error(chalk.red('\n✗ No fields to update. Provide at least one of: --client-name, --follower, --office-address, --office-telephone, --status, --remark\n'));
53
+ }
54
+ process.exit(1);
55
+ }
56
+
57
+ const updateData: UpdateClientRequest = fields as UpdateClientRequest;
58
+
59
+ try {
60
+ const response = await apiClient.updateClient(environment, tenant, updateData);
61
+
62
+ if (response.success) {
63
+ if (jsonOutput) {
64
+ console.log(JSON.stringify({ success: true, data: response.data }));
65
+ } else {
66
+ console.log(chalk.green('\n✓ Client updated successfully\n'));
67
+ }
68
+ } else {
69
+ const errorMsg = response.error?.message || 'Unknown error';
70
+ const errorCode = response.error?.code || 'UPDATE_CLIENT_ERROR';
71
+
72
+ if (errorCode === '01001150001' || errorMsg.includes('不存在')) {
73
+ if (jsonOutput) {
74
+ console.log(JSON.stringify({ success: false, error: { code: 'NOT_FOUND', message: `Client not found: ${clientId}` } }));
75
+ } else {
76
+ console.error(chalk.red(`\n✗ Client not found: ${clientId}\n`));
77
+ }
78
+ } else {
79
+ if (jsonOutput) {
80
+ console.log(JSON.stringify({ success: false, error: { code: errorCode, message: errorMsg } }));
81
+ } else {
82
+ console.error(chalk.red(`\n✗ Failed to update client: ${errorMsg}`));
83
+ if (response.error?.code) {
84
+ console.error(chalk.gray(` Error code: ${response.error.code}`));
85
+ }
86
+ console.error();
87
+ }
88
+ }
89
+ process.exit(1);
90
+ }
91
+ } catch (error) {
92
+ const errMsg = error instanceof Error ? error.message : 'Unknown error';
93
+ if (jsonOutput) {
94
+ console.log(JSON.stringify({ success: false, error: { code: 'ERROR', message: errMsg } }));
95
+ } else {
96
+ console.error(chalk.red(`\n✗ Error: ${errMsg}\n`));
97
+ }
98
+ process.exit(1);
99
+ }
100
+ });
101
+
102
+ return cmd;
103
+ }