UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

166 lines 7.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AccountApiHandler = void 0; const base_handler_1 = require("./base.handler"); const errors_1 = require("../utils/errors"); const confirmation_1 = require("../utils/confirmation"); const account_service_1 = require("../services/account-service"); class AccountApiHandler extends base_handler_1.BaseHandler { constructor(authConfig, serviceConfig) { super(); this.accountService = new account_service_1.AccountServiceSdk(authConfig, serviceConfig); } async listChannels(options) { return this.show(await this.accountService.channels(this.compact({ categoryId: options.categoryId, keyword: options.keyword, labelId: options.labelId, })), options); } async listPlayback(options) { return this.show(await this.accountService.userPlaybackList(this.compact({ page: options.page, pageSize: options.pageSize, startDate: options.startDate, endDate: options.endDate, keyword: options.keyword, })), options); } async listChannelBasic(options) { return this.show(await this.accountService.userChannelBasicList(this.compact({ categoryIds: options.categoryIds, page: options.page, pageSize: options.pageSize, keyword: options.keyword, })), options); } async listSimpleChannels(options) { return this.show(await this.accountService.getSimpleChannelList(this.compact({ page: options.page, pageSize: options.pageSize, categoryId: options.categoryId, watchStatus: options.watchStatus, keyword: options.keyword, })), options); } async listChannelDetails(options) { return this.show(await this.accountService.channelDetailList(this.compact({ page: options.page, pageSize: options.pageSize, categoryId: options.categoryId, watchStatus: options.watchStatus, keyword: options.keyword, })), options); } async getUserDurations(options) { return this.show(await this.accountService.getUserDurations(), options); } async getMicDuration(options) { return this.show(await this.accountService.micDuration(), options); } async listIncome(options) { this.requireFields(options, ['userId', 'startDate', 'endDate']); return this.show(await this.accountService.getIncomeDetail(this.compact({ userId: options.userId, startDate: options.startDate, endDate: options.endDate, channelId: options.channelId, page: options.page, pageSize: options.pageSize, })), options); } async listCategories(options) { return this.show(await this.accountService.getCategoryList(), options); } async createCategory(options) { this.requireFields(options, ['name']); await this.confirmWrite(options.force, `Create live category "${options.name}"?`); const result = await this.accountService.createCategory(options.name); return this.show(result, options); } async deleteCategory(options) { this.requirePositiveNumber(options.categoryId, 'categoryId'); await this.confirmWrite(options.force, `Delete live category ${options.categoryId}?`); const result = await this.accountService.deleteCategory(options.categoryId); return this.show(result, options); } async updateCategoryName(options) { this.requirePositiveNumber(options.categoryId, 'categoryId'); this.requireFields(options, ['name']); await this.confirmWrite(options.force, `Rename live category ${options.categoryId}?`); const result = await this.accountService.updateCategoryName(options.categoryId, options.name); return this.show(result, options); } async updateCategoryRank(options) { this.requirePositiveNumber(options.categoryId, 'categoryId'); this.requirePositiveNumber(options.afterCategoryId, 'afterCategoryId'); await this.confirmWrite(options.force, `Move live category ${options.categoryId} to after category ${options.afterCategoryId}?`); const result = await this.accountService.updateCategoryRank(options.categoryId, options.afterCategoryId); return this.show(result, options); } async listReceiveChannels(options) { this.requireFields(options, ['channelId']); return this.show(await this.accountService.receiveList(this.compact({ channelId: options.channelId, keyword: options.keyword, page: options.page, pageSize: options.pageSize, })), options); } async setSsoToken(options) { this.requireFields(options, ['token']); await this.confirmWrite(options.force, options.childEmail ? 'Set child account SSO token?' : 'Set account SSO token?'); const result = options.childEmail ? await this.accountService.setUserChildrenLoginToken({ childEmail: options.childEmail, token: options.token, }) : await this.accountService.setUserLoginToken({ token: options.token }); return this.show(result, options); } async setCallback(options) { this.requireFields(options, ['type', 'userId']); await this.confirmWrite(options.force, `Set ${options.type} callback for user ${options.userId}?`); const params = this.compact({ userId: options.userId, url: options.url }); const result = options.type === 'stream' ? await this.accountService.setStreamCallback(params) : options.type === 'record' ? await this.accountService.setRecordCallback(params) : await this.accountService.setPlaybackCallback(params); return this.show(result, options); } show(data, options) { this.displayData(data, (options.output || 'table')); } requireFields(options, fields) { const values = options; const missing = fields.filter((field) => { const value = values[field]; return value === undefined || value === null || value === ''; }); if (missing.length > 0) { throw this.validationError(`Missing required option(s): ${missing.join(', ')}`, 'options', options); } } requirePositiveNumber(value, field) { if (!Number.isFinite(value) || Number(value) <= 0) { throw this.validationError(`${field} must be a positive number`, field, value); } } compact(params) { return Object.fromEntries(Object.entries(params).filter(([, value]) => value !== undefined)); } validationError(message, field, value) { return new errors_1.PolyVValidationError(message, field, value, 'validation_failed'); } async confirmWrite(force, message) { if (force) return; const confirmed = await (0, confirmation_1.confirmDeletion)(message); if (!confirmed) { throw new Error('Operation cancelled.'); } } } exports.AccountApiHandler = AccountApiHandler; //# sourceMappingURL=account-api.handler.js.map