polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
313 lines • 13.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckinHandler = void 0;
const base_handler_1 = require("./base.handler");
const checkin_service_1 = require("../services/checkin-service");
const errors_1 = require("../utils/errors");
class CheckinHandler extends base_handler_1.BaseHandler {
constructor(authConfig, serviceConfig) {
super();
this.checkinService = new checkin_service_1.CheckinServiceSdk(authConfig, serviceConfig);
}
async startCheckin(options) {
return this.executeWithErrorHandling(async () => {
this.validateStartOptions(options);
const params = {
channelId: options.channelId,
limitTime: options.limitTime,
delayTime: options.delayTime,
message: options.message,
};
if (options.force) {
params.forceCheckInEnabled = 'Y';
}
const result = await this.checkinService.startCheckin(params);
this.displayStartResult(result, options);
}, 'checkin.start');
}
async listCheckins(options) {
return this.executeWithErrorHandling(async () => {
this.validateListOptions(options);
const params = {
channelId: options.channelId,
};
if (options.page !== undefined)
params.page = options.page;
if (options.size !== undefined)
params.pageSize = options.size;
if (options.date !== undefined)
params.date = options.date;
if (options.sessionId !== undefined)
params.sessionId = options.sessionId;
const result = await this.checkinService.listCheckins(params);
this.displayListResult(result, options);
}, 'checkin.list');
}
async getCheckinResult(options) {
return this.executeWithErrorHandling(async () => {
this.validateResultOptions(options);
const result = await this.checkinService.getCheckinResult({
channelId: options.channelId,
checkinId: options.checkinId,
});
this.displayResultResult(result, options);
}, 'checkin.result');
}
async getCheckinBySessionId(options) {
return this.executeWithErrorHandling(async () => {
this.validateSessionResultOptions(options);
const result = await this.checkinService.getCheckinBySessionId({
channelId: options.channelId,
sessionId: options.sessionId,
});
this.displaySessionResult(result, options);
}, 'checkin.session-result');
}
async listSessions(options) {
return this.executeWithErrorHandling(async () => {
this.validateSessionsOptions(options);
const result = await this.checkinService.listSessions({
channelId: options.channelId,
startDate: options.startDate || this.getDefaultStartDate(),
endDate: options.endDate || this.getDefaultEndDate(),
});
this.displaySessionsResult(result, options);
}, 'checkin.sessions');
}
validateStartOptions(options) {
const errors = [];
if (!options.channelId || options.channelId.trim() === '') {
errors.push('channelId is required');
}
if (options.limitTime !== undefined) {
if (typeof options.limitTime !== 'number' || options.limitTime < 0 || options.limitTime > 86400) {
errors.push('limitTime must be between 0 and 86400');
}
}
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(`Checkin start 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.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(`Checkin list options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed');
}
}
validateResultOptions(options) {
const errors = [];
if (!options.channelId || options.channelId.trim() === '') {
errors.push('channelId is required');
}
if (!options.checkinId || options.checkinId.trim() === '') {
errors.push('checkinId 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(`Checkin result options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed');
}
}
validateSessionResultOptions(options) {
const errors = [];
if (!options.channelId || options.channelId.trim() === '') {
errors.push('channelId is required');
}
if (!options.sessionId || options.sessionId.trim() === '') {
errors.push('sessionId 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(`Checkin session result options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed');
}
}
validateSessionsOptions(options) {
const errors = [];
if (!options.channelId || options.channelId.trim() === '') {
errors.push('channelId is required');
}
const dateFormatRegex = /^\d{4}-\d{2}-\d{2}$/;
if (options.startDate && !dateFormatRegex.test(options.startDate)) {
errors.push('Invalid date format. Use yyyy-MM-dd');
}
if (options.endDate && !dateFormatRegex.test(options.endDate)) {
errors.push('Invalid date format. Use yyyy-MM-dd');
}
if (options.startDate && options.endDate) {
const start = new Date(options.startDate);
const end = new Date(options.endDate);
const diffDays = Math.ceil((end.getTime() - start.getTime()) / (1000 * 60 * 60 * 24));
if (diffDays > 30) {
errors.push('Date range cannot exceed 30 days');
}
}
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(`Checkin sessions options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed');
}
}
displayStartResult(result, options) {
const checkinId = result?.data?.checkinId || result?.checkinId;
const nextStep = `Run "checkin sessions -c ${options.channelId}" to find the generated checkin ID.`;
if (options.output === 'json') {
const data = {
channelId: options.channelId,
limitTime: options.limitTime,
delayTime: options.delayTime,
message: options.message,
force: options.force,
result
};
if (checkinId) {
data['checkinId'] = checkinId;
}
else {
data['nextStep'] = nextStep;
}
this.displayData(data, 'json');
}
else {
if (options.delayTime) {
console.log(`Scheduled checkin submitted successfully`);
console.log(`Channel ID: ${options.channelId}`);
console.log(`Scheduled time: ${new Date(options.delayTime).toLocaleString()}`);
}
else {
console.log(`Checkin started successfully`);
console.log(`Channel ID: ${options.channelId}`);
}
if (checkinId) {
console.log(`Checkin ID: ${checkinId}`);
}
else {
console.log(`Next step: ${nextStep}`);
}
}
}
displayListResult(result, options) {
const data = result?.data ?? result;
const contents = data?.contents || [];
const count = data?.count || data?.totalItems || contents.length;
if (contents.length === 0) {
this.displayInfo(`No checkins found for channel ${options.channelId}`);
return;
}
if (options.output === 'json') {
this.displayData({
channelId: options.channelId,
count,
data: contents
}, 'json');
}
else {
const tableData = contents.map((item) => ({
'Checkin ID': item.checkinid || item.checkinId || '-',
'User ID': item.userid || item.viewerId || '-',
'Nickname': item.nickname || item.nick || '-',
'Time': item.timeFormat || (item.time ? new Date(item.time).toLocaleString() : '-'),
}));
console.log(`Found ${count} checkin records`);
this.displayAsTable(tableData);
}
}
displayResultResult(result, options) {
const data = result?.data ?? result ?? [];
if (!data || (Array.isArray(data) && data.length === 0)) {
this.displayInfo(`No checkin result found for checkin ID ${options.checkinId}`);
return;
}
if (options.output === 'json') {
this.displayData({
channelId: options.channelId,
checkinId: options.checkinId,
data
}, 'json');
}
else {
const tableData = data.map((item) => ({
'User ID': item.userid || item.viewerId || '-',
'Nickname': item.nickname || item.nick || '-',
'Checked': item.checked === 'Y' ? 'Yes' : 'No',
'Time': item.timeFormat || (item.time ? new Date(item.time).toLocaleString() : '-'),
}));
const checkedCount = data.filter((item) => item.checked === 'Y').length;
const uncheckedCount = data.filter((item) => item.checked === 'N').length;
console.log(`Checkin result for ${options.checkinId}`);
console.log(`Checked: ${checkedCount}, Unchecked: ${uncheckedCount}`);
this.displayAsTable(tableData);
}
}
displaySessionsResult(result, options) {
const data = result?.data ?? result ?? [];
if (!data || (Array.isArray(data) && data.length === 0)) {
this.displayInfo(`No checkin sessions found for the specified date range`);
return;
}
if (options.output === 'json') {
this.displayData({
channelId: options.channelId,
startDate: options.startDate,
endDate: options.endDate,
data
}, 'json');
}
else {
const tableData = data.map((item) => ({
'Checkin ID': item.checkinid || item.checkinId || '-',
'Time': item.timeFormat || (item.time ? new Date(item.time).toLocaleString() : '-'),
}));
console.log(`Found ${data.length} checkin sessions`);
this.displayAsTable(tableData);
}
}
displaySessionResult(result, options) {
const data = result?.data ?? result;
if (options.output === 'json') {
this.displayData({
channelId: options.channelId,
sessionId: options.sessionId,
data,
}, 'json');
return;
}
if (!data || (Array.isArray(data) && data.length === 0)) {
this.displayInfo(`No checkin result found for session ${options.sessionId}`);
return;
}
this.displayData(data, 'table');
}
getDefaultStartDate() {
const date = new Date();
date.setDate(date.getDate() - 7);
return date.toISOString().split('T')[0] ?? '';
}
getDefaultEndDate() {
return new Date().toISOString().split('T')[0] ?? '';
}
}
exports.CheckinHandler = CheckinHandler;
//# sourceMappingURL=checkin.handler.js.map