polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
241 lines • 9.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WhitelistHandler = void 0;
const base_handler_1 = require("./base.handler");
const whitelist_service_1 = require("../services/whitelist-service");
const errors_1 = require("../utils/errors");
class WhitelistHandler extends base_handler_1.BaseHandler {
constructor(authConfig, serviceConfig) {
super();
this.whitelistService = new whitelist_service_1.WhitelistServiceSdk(authConfig, serviceConfig);
}
async listWhitelist(options) {
return this.executeWithErrorHandling(async () => {
this.validateListOptions(options);
const params = {
rank: options.rank,
};
if (options.channelId) {
params.channelId = options.channelId;
}
if (options.page !== undefined) {
params.page = options.page;
}
if (options.pageSize !== undefined) {
params.pageSize = options.pageSize;
}
if (options.keyword) {
params.keyword = options.keyword;
}
const result = await this.whitelistService.getWhiteList(params);
this.displayListResult(result, options);
}, 'whitelist.list');
}
async addWhitelist(options) {
return this.executeWithErrorHandling(async () => {
this.validateAddOptions(options);
const params = {
rank: options.rank,
code: options.code,
};
if (options.channelId) {
params.channelId = options.channelId;
}
if (options.name) {
params.name = options.name;
}
await this.whitelistService.addWhiteList(params);
this.displayAddResult(options);
}, 'whitelist.add');
}
async updateWhitelist(options) {
return this.executeWithErrorHandling(async () => {
this.validateUpdateOptions(options);
const params = {
rank: options.rank,
oldCode: options.oldCode,
code: options.code,
};
if (options.channelId) {
params.channelId = options.channelId;
}
if (options.name !== undefined) {
params.name = options.name;
}
await this.whitelistService.updateWhiteList(params);
this.displayUpdateResult(options);
}, 'whitelist.update');
}
async removeWhitelist(options) {
return this.executeWithErrorHandling(async () => {
this.validateRemoveOptions(options);
const params = {
rank: options.rank,
isClear: options.clear ? 'Y' : 'N',
};
if (!options.clear && options.codes) {
params.codes = options.codes;
}
if (options.channelId) {
params.channelId = options.channelId;
}
await this.whitelistService.deleteWhiteList(params);
this.displayRemoveResult(options);
}, 'whitelist.remove');
}
validateListOptions(options) {
const errors = [];
if (options.rank !== 1 && options.rank !== 2) {
errors.push('rank 必须是 1 (主要条件) 或 2 (次要条件)');
}
if (options.output && !['table', 'json'].includes(options.output)) {
errors.push('输出格式必须是 "table" 或 "json"');
}
if (errors.length > 0) {
throw new errors_1.PolyVValidationError(errors.join(', '), 'options', options, 'validation_failed');
}
}
validateAddOptions(options) {
const errors = [];
if (options.rank !== 1 && options.rank !== 2) {
errors.push('rank 必须是 1 (主要条件) 或 2 (次要条件)');
}
if (!options.code || options.code.trim() === '') {
errors.push('code (会员码) 是必需的');
}
else if (options.code.length > 50) {
errors.push('code 不能超过50个字符');
}
if (options.name && options.name.length > 50) {
errors.push('name 不能超过50个字符');
}
if (options.output && !['table', 'json'].includes(options.output)) {
errors.push('输出格式必须是 "table" 或 "json"');
}
if (errors.length > 0) {
throw new errors_1.PolyVValidationError(errors.join(', '), 'options', options, 'validation_failed');
}
}
validateUpdateOptions(options) {
const errors = [];
if (options.rank !== 1 && options.rank !== 2) {
errors.push('rank 必须是 1 (主要条件) 或 2 (次要条件)');
}
if (!options.oldCode || options.oldCode.trim() === '') {
errors.push('old-code (原会员码) 是必需的');
}
if (!options.code || options.code.trim() === '') {
errors.push('code (新会员码) 是必需的');
}
else if (options.code.length > 50) {
errors.push('code 不能超过50个字符');
}
if (options.name && options.name.length > 50) {
errors.push('name 不能超过50个字符');
}
if (options.output && !['table', 'json'].includes(options.output)) {
errors.push('输出格式必须是 "table" 或 "json"');
}
if (errors.length > 0) {
throw new errors_1.PolyVValidationError(errors.join(', '), 'options', options, 'validation_failed');
}
}
validateRemoveOptions(options) {
const errors = [];
if (options.rank !== 1 && options.rank !== 2) {
errors.push('rank 必须是 1 (主要条件) 或 2 (次要条件)');
}
if (!options.clear && (!options.codes || options.codes.trim() === '')) {
errors.push('必须指定 --codes 或使用 --clear 清空所有');
}
if (options.output && !['table', 'json'].includes(options.output)) {
errors.push('输出格式必须是 "table" 或 "json"');
}
if (errors.length > 0) {
throw new errors_1.PolyVValidationError(errors.join(', '), 'options', options, 'validation_failed');
}
}
displayListResult(result, options) {
const output = options.output || 'table';
if (output === 'json') {
this.displayData(result, 'json');
}
else {
const scope = options.channelId ? `channel ${options.channelId}` : 'global settings';
console.log(`找到 ${result.contents?.length || 0} 条白名单记录 (${scope})`);
console.log(`页码: ${result.pageNumber || 1} / ${result.totalPages || 1}`);
if (result.contents && result.contents.length > 0) {
const tableData = result.contents.map((item) => ({
'会员码': item.phone || '-',
'昵称': item.name || '-',
}));
this.displayAsTable(tableData);
}
else {
this.displayInfo('没有找到白名单记录');
}
}
}
displayAddResult(options) {
const output = options.output || 'table';
if (output === 'json') {
this.displayData({
success: true,
message: '白名单添加成功',
code: options.code,
channelId: options.channelId || 'global',
}, 'json');
}
else {
const target = options.channelId
? `channel ${options.channelId}`
: 'global settings';
this.displaySuccess(`Successfully added whitelist item for ${target}`);
}
}
displayUpdateResult(options) {
const output = options.output || 'table';
if (output === 'json') {
this.displayData({
success: true,
message: '白名单更新成功',
oldCode: options.oldCode,
newCode: options.code,
channelId: options.channelId || 'global',
}, 'json');
}
else {
const target = options.channelId
? `channel ${options.channelId}`
: 'global settings';
this.displaySuccess(`Successfully updated whitelist item for ${target}`);
}
}
displayRemoveResult(options) {
const output = options.output || 'table';
if (output === 'json') {
this.displayData({
success: true,
message: options.clear ? '白名单已清空' : '白名单删除成功',
codes: options.codes,
clear: options.clear || false,
channelId: options.channelId || 'global',
}, 'json');
}
else {
const target = options.channelId
? `channel ${options.channelId}`
: 'global settings';
if (options.clear) {
this.displaySuccess(`Successfully cleared all whitelist items for ${target}`);
}
else {
const codeCount = options.codes ? options.codes.split(',').length : 0;
const itemText = codeCount === 1 ? 'item' : 'items';
this.displaySuccess(`Successfully removed ${codeCount} whitelist ${itemText} for ${target}`);
}
}
}
}
exports.WhitelistHandler = WhitelistHandler;
//# sourceMappingURL=whitelist.handler.js.map