polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
282 lines • 12.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DonateHandler = void 0;
const base_handler_1 = require("./base.handler");
const donate_service_1 = require("../services/donate-service");
const errors_1 = require("../utils/errors");
const api_command_1 = require("../utils/api-command");
class DonateHandler extends base_handler_1.BaseHandler {
constructor(authConfig, serviceConfig) {
super();
this.donateService = new donate_service_1.DonateServiceSdk(authConfig, serviceConfig);
}
async getConfig(options) {
return this.executeWithErrorHandling(async () => {
this.validateConfigGetOptions(options);
const result = await this.donateService.getDonateConfig({
channelId: options.channelId,
});
this.displayConfigGetResult(result, options);
}, 'donate.config.get');
}
async updateConfig(options) {
return this.executeWithErrorHandling(async () => {
this.validateConfigUpdateOptions(options);
await (0, api_command_1.confirmWrite)(options.force, `Update donate configuration for channel ${options.channelId}?`);
const params = {
channelId: options.channelId,
};
if (options.cashEnabled !== undefined) {
params.donateEnabled = options.cashEnabled;
}
if (options.giftEnabled !== undefined) {
params.donateGiftEnabled = options.giftEnabled;
}
if (options.amounts !== undefined) {
if (typeof options.amounts === 'string') {
params.donateAmounts = options.amounts.split(',').map(n => parseFloat(n.trim()));
}
else {
params.donateAmounts = options.amounts;
}
}
await this.donateService.updateDonateConfig(params);
this.displayConfigUpdateResult(options);
}, 'donate.config.update');
}
async listRecords(options) {
return this.executeWithErrorHandling(async () => {
this.validateListOptions(options);
const params = {
channelId: options.channelId,
start: options.start,
end: options.end,
pageNumber: options.page ?? 1,
pageSize: options.size ?? 10,
};
const result = await this.donateService.listRewardGift(params);
this.displayListResult(result, options);
}, 'donate.list');
}
async listLikes(options) {
return this.executeWithErrorHandling(async () => {
this.validateLikeListOptions(options);
const params = {
channelId: options.channelId,
pageNumber: options.page ?? 1,
pageSize: options.size ?? 10,
...(options.start !== undefined ? { start: options.start } : {}),
...(options.end !== undefined ? { end: options.end } : {}),
};
const result = await this.donateService.listRewardLikes(params);
this.displayGenericResult(result, options.output);
}, 'donate.likes');
}
validateConfigGetOptions(options) {
const errors = [];
if (!options.channelId || options.channelId.trim() === '') {
errors.push('channelId is required');
}
if (options.output && !['table', 'json'].includes(options.output)) {
errors.push('output must be either "table" or "json"');
}
if (errors.length > 0) {
throw new errors_1.PolyVValidationError(`Donate config get options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed');
}
}
validateConfigUpdateOptions(options) {
const errors = [];
if (!options.channelId || options.channelId.trim() === '') {
errors.push('channelId is required');
}
if (options.cashEnabled !== undefined && !['Y', 'N'].includes(options.cashEnabled)) {
errors.push('cashEnabled must be either "Y" or "N"');
}
if (options.giftEnabled !== undefined && !['Y', 'N'].includes(options.giftEnabled)) {
errors.push('giftEnabled must be either "Y" or "N"');
}
if (options.cashEnabled !== undefined && options.amounts === undefined) {
errors.push('cashEnabled requires amounts');
}
if (options.output && !['table', 'json'].includes(options.output)) {
errors.push('output must be either "table" or "json"');
}
if (errors.length > 0) {
throw new errors_1.PolyVValidationError(`Donate config update options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed');
}
}
validateListOptions(options) {
const errors = [];
if (!options.channelId || options.channelId.trim() === '') {
errors.push('channelId is required');
}
if (options.start === undefined || options.start === null) {
errors.push('start is required');
}
if (options.end === undefined || options.end === null) {
errors.push('end is required');
}
if (options.start !== undefined && options.end !== undefined && options.start > options.end) {
errors.push('start must be before end');
}
if (options.page !== undefined) {
if (typeof options.page !== 'number' || !Number.isInteger(options.page) || options.page < 1) {
errors.push('page must be a positive integer');
}
}
if (options.size !== undefined) {
if (typeof options.size !== 'number' || !Number.isInteger(options.size) || options.size < 1) {
errors.push('size must be a positive integer');
}
}
if (options.output && !['table', 'json'].includes(options.output)) {
errors.push('output must be either "table" or "json"');
}
if (errors.length > 0) {
throw new errors_1.PolyVValidationError(`Donate list options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed');
}
}
validateLikeListOptions(options) {
const errors = [];
if (!options.channelId || options.channelId.trim() === '') {
errors.push('channelId is required');
}
if (options.start !== undefined && options.end !== undefined && options.start > options.end) {
errors.push('start must be before end');
}
if (options.page !== undefined) {
if (typeof options.page !== 'number' || !Number.isInteger(options.page) || options.page < 1) {
errors.push('page must be a positive integer');
}
}
if (options.size !== undefined) {
if (typeof options.size !== 'number' || !Number.isInteger(options.size) || options.size < 1) {
errors.push('size must be a positive integer');
}
}
if (options.output && !['table', 'json'].includes(options.output)) {
errors.push('output must be either "table" or "json"');
}
if (errors.length > 0) {
throw new errors_1.PolyVValidationError(`Donate likes options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed');
}
}
displayConfigGetResult(result, options) {
const data = result?.data ?? result;
if (!data || (typeof data === 'object' && Object.keys(data).length === 0)) {
this.displayInfo(`No donate configuration found for channel ${options.channelId}`);
return;
}
if (options.output === 'json') {
this.displayData({
channelId: options.channelId,
...data
}, 'json');
}
else {
const cashDonate = data.cashDonate ?? {};
const giftDonate = data.giftDonate ?? {};
const cashAmounts = cashDonate.cashs ?? data.cashes;
const cashMin = cashDonate.cashMin ?? data.cashMin;
const giftEnabled = data.donateGiftEnabled ?? data.donateGoodEnabled;
const cashPayGifts = giftDonate.cashPays ?? data.goods ?? [];
const pointPayGifts = giftDonate.pointPays ?? [];
console.log(`Donate Configuration for Channel: ${options.channelId}`);
console.log('');
console.log(`Global Setting Enabled: ${data.globalSettingEnabled || 'N'}`);
console.log(`Cash Donate Enabled: ${data.donateCashEnabled || 'N'}`);
console.log(`Gift Donate Enabled: ${giftEnabled || 'N'}`);
console.log(`Point Donate Enabled: ${data.donatePointEnabled || 'N'}`);
console.log(`Donate Tips: ${data.donateTips || '-'}`);
console.log(`Cash Min: ${cashMin ?? '-'}`);
console.log(`Cash Amounts: ${cashAmounts?.join(', ') || '-'}`);
if (cashPayGifts.length > 0) {
console.log(`Cash Pay Gifts:`);
cashPayGifts.forEach((gift) => {
const name = gift.name ?? gift.goodName ?? '-';
const price = gift.price ?? gift.goodPrice ?? '-';
const enabled = gift.enabled ?? gift.goodEnabled;
console.log(` - ${name} (${price}) - ${enabled === 'Y' ? 'Enabled' : 'Disabled'}`);
});
}
if (pointPayGifts.length > 0) {
console.log(`Point Pay Gifts:`);
pointPayGifts.forEach((gift) => {
const name = gift.name ?? '-';
const price = gift.price ?? '-';
console.log(` - ${name} (${price}) - ${gift.enabled === 'Y' ? 'Enabled' : 'Disabled'}`);
});
}
}
}
displayConfigUpdateResult(options) {
if (options.output === 'json') {
this.displayData({
channelId: options.channelId,
cashEnabled: options.cashEnabled,
giftEnabled: options.giftEnabled,
amounts: options.amounts,
}, 'json');
}
else {
console.log(`Donate config updated successfully`);
console.log(`Channel ID: ${options.channelId}`);
if (options.cashEnabled !== undefined) {
console.log(`Cash Enabled: ${options.cashEnabled}`);
}
if (options.giftEnabled !== undefined) {
console.log(`Gift Enabled: ${options.giftEnabled}`);
}
if (options.amounts !== undefined) {
const amountsStr = typeof options.amounts === 'string' ? options.amounts : options.amounts.join(', ');
console.log(`Amounts: ${amountsStr}`);
}
}
}
displayListResult(result, options) {
const data = result?.data;
const contents = data?.contents || [];
const totalItems = data?.totalItems || 0;
if (totalItems === 0) {
this.displayInfo(`No donate records found for channel ${options.channelId}`);
return;
}
if (options.output === 'json') {
this.displayData({
channelId: options.channelId,
pageNumber: data.pageNumber,
pageSize: data.pageSize,
totalPages: data.totalPages,
totalItems: data.totalItems,
contents: contents
}, 'json');
}
else {
const tableData = contents.map((item) => ({
'User ID': this.truncate(item.userId || '-', 15),
'Nickname': this.truncate(item.nickName || '-', 20),
'Type': item.type === '1' ? 'Props/Points' : 'Cash',
'Amount': item.amount || '-',
'Name': this.truncate(item.name || '-', 20),
'Session ID': this.truncate(item.sessionId || '-', 12),
'Time': item.timestamp ? new Date(item.timestamp).toLocaleString() : '-',
}));
console.log(`Found ${totalItems} donate records`);
console.log(`Page: ${data.pageNumber}, Size: ${data.pageSize}`);
console.log(`Total pages: ${data.totalPages}`);
if (tableData.length > 0) {
this.displayAsTable(tableData);
}
}
}
displayGenericResult(result, output) {
this.displayData(result ?? { success: true }, output === 'json' ? 'json' : 'table');
}
truncate(str, maxLength) {
if (!str)
return '-';
return str.length > maxLength ? str.substring(0, maxLength) + '...' : str;
}
}
exports.DonateHandler = DonateHandler;
//# sourceMappingURL=donate.handler.js.map