polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
402 lines • 14.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LotteryServiceSdk = void 0;
const polyv_live_api_sdk_1 = require("polyv-live-api-sdk");
const errors_1 = require("../utils/errors");
class LotteryServiceSdk {
constructor(authConfig, _serviceConfig) {
this.client = new polyv_live_api_sdk_1.PolyVClient(authConfig);
this.v4Channel = this.client.v4Channel;
this.liveInteraction = this.client.liveInteraction;
}
async createLotteryActivity(params) {
try {
const result = await this.v4Channel.lotteryActivityCreate(this.buildLotteryActivityCreate(params));
return result;
}
catch (error) {
throw this.wrapError(error, 'createLotteryActivity');
}
}
async listLotteryActivities(params) {
try {
const result = await this.v4Channel.lotteryActivityList(params);
return result;
}
catch (error) {
throw this.wrapError(error, 'listLotteryActivities');
}
}
async getLotteryActivity(params) {
try {
const result = await this.v4Channel.lotteryActivityGet(params);
return result;
}
catch (error) {
throw this.wrapError(error, 'getLotteryActivity');
}
}
async updateLotteryActivity(params) {
try {
const currentResponse = await this.v4Channel.lotteryActivityGet({
channelId: params.channelId,
id: params.id,
});
const current = this.unwrapData(currentResponse) ?? {};
const result = await this.v4Channel.lotteryActivityUpdate(this.buildLotteryActivityUpdate(params, current));
return result ?? this.successResponse();
}
catch (error) {
throw this.wrapError(error, 'updateLotteryActivity');
}
}
async deleteLotteryActivity(params) {
try {
const result = await this.v4Channel.lotteryActivityDelete(params);
return result ?? this.successResponse();
}
catch (error) {
throw this.wrapError(error, 'deleteLotteryActivity');
}
}
async getWinnerDetail(params) {
try {
if (params.viewerId) {
const result = await this.v4Channel.queryWinnerViewer({
channelId: params.channelId,
lotteryId: params.lotteryId,
viewerId: params.viewerId,
pageNumber: params.page,
pageSize: params.limit,
});
return result;
}
const result = await this.liveInteraction.getWinnerDetail({
channelId: params.channelId,
lotteryId: params.lotteryId,
page: params.page,
limit: params.limit,
});
return result;
}
catch (error) {
throw this.wrapError(error, 'getWinnerDetail');
}
}
async listLottery(params) {
try {
const query = this.withDefaultLotteryTimeRange(params);
const result = await this.v4Channel.listLotteryActivityRecords({
channelId: query.channelId,
sessionId: query.sessionId,
startTimeBegin: query.startTime,
startTimeEnd: query.endTime,
pageNumber: query.page,
pageSize: query.limit,
});
return result;
}
catch (error) {
throw this.wrapError(error, 'listLottery');
}
}
async listLegacyLottery(params) {
try {
const result = await this.liveInteraction.listLottery({
channelId: params.channelId,
startTime: params.startTime,
endTime: params.endTime,
sessionId: params.sessionId,
page: params.page,
limit: params.limit,
});
return result;
}
catch (error) {
throw this.wrapError(error, 'listLegacyLottery');
}
}
async listChannelsLottery(params) {
try {
const result = await this.liveInteraction.listChannelsLottery({
channelIds: params.channelIds,
startTime: params.startTime,
endTime: params.endTime,
sessionId: params.sessionId,
page: params.page,
limit: params.limit,
});
return result;
}
catch (error) {
throw this.wrapError(error, 'listChannelsLottery');
}
}
async downloadWinnerDetail(params) {
try {
const result = await this.liveInteraction.downloadWinnerDetail({
channelId: params.channelId,
lotteryId: params.lotteryId,
});
return result;
}
catch (error) {
throw this.wrapError(error, 'downloadWinnerDetail');
}
}
async addReceiveInfoV4(params) {
try {
const result = await this.liveInteraction.addReceiveInfoV4({
channelId: params.channelId,
lotteryId: params.lotteryId,
winnerCode: params.winnerCode,
viewerId: params.viewerId,
receiveInfo: params.receiveInfo,
});
return result;
}
catch (error) {
throw this.wrapError(error, 'addReceiveInfoV4');
}
}
async createConditionWaitLottery(params) {
try {
const result = await this.v4Channel.createConditionWaitLottery(params);
return result ?? this.successResponse();
}
catch (error) {
throw this.wrapError(error, 'createConditionWaitLottery');
}
}
async listLotteryViewerGroups(params) {
try {
return await this.v4Channel.listLotteryViewerGroups(params);
}
catch (error) {
throw this.wrapError(error, 'listLotteryViewerGroups');
}
}
async createLotteryViewerGroup(params) {
try {
return await this.v4Channel.createLotteryViewerGroup(params);
}
catch (error) {
throw this.wrapError(error, 'createLotteryViewerGroup');
}
}
async updateLotteryViewerGroup(params) {
try {
return await this.v4Channel.updateLotteryViewerGroup(params);
}
catch (error) {
throw this.wrapError(error, 'updateLotteryViewerGroup');
}
}
async deleteLotteryViewerGroup(params) {
try {
const result = await this.v4Channel.deleteLotteryViewerGroup(params);
return result ?? this.successResponse();
}
catch (error) {
throw this.wrapError(error, 'deleteLotteryViewerGroup');
}
}
async listLotteryGroupViewers(params) {
try {
return await this.v4Channel.listLotteryGroupViewers(params);
}
catch (error) {
throw this.wrapError(error, 'listLotteryGroupViewers');
}
}
async createLotteryGroupViewers(params) {
try {
return await this.v4Channel.createLotteryGroupViewers(params);
}
catch (error) {
throw this.wrapError(error, 'createLotteryGroupViewers');
}
}
async createLotteryGroupViewerNames(params) {
try {
return await this.v4Channel.createLotteryGroupViewerNames(params);
}
catch (error) {
throw this.wrapError(error, 'createLotteryGroupViewerNames');
}
}
async deleteLotteryGroupViewers(params) {
try {
const result = await this.v4Channel.deleteLotteryGroupViewers(params);
return result ?? this.successResponse();
}
catch (error) {
throw this.wrapError(error, 'deleteLotteryGroupViewers');
}
}
async listLotteryBlacklistViewers(params) {
try {
return await this.v4Channel.listLotteryBlacklistViewers(params);
}
catch (error) {
throw this.wrapError(error, 'listLotteryBlacklistViewers');
}
}
async createLotteryBlacklistViewers(params) {
try {
return await this.v4Channel.createLotteryBlacklistViewers(params);
}
catch (error) {
throw this.wrapError(error, 'createLotteryBlacklistViewers');
}
}
async deleteLotteryBlacklistViewers(params) {
try {
const result = await this.v4Channel.deleteLotteryBlacklistViewers(params);
return result ?? this.successResponse();
}
catch (error) {
throw this.wrapError(error, 'deleteLotteryBlacklistViewers');
}
}
async listLuckyBagWinners(params) {
try {
return await this.v4Channel.listLuckyBagWinners(params);
}
catch (error) {
throw this.wrapError(error, 'listLuckyBagWinners');
}
}
buildLotteryActivityUpdate(params, current) {
const updateParams = {
channelId: params.channelId,
id: params.id,
activityName: params.activityName ?? current['activityName'],
lotteryCondition: current['lotteryCondition'],
amount: params.amount ?? current['amount'],
prizeName: params.prizeName ?? current['prizeName'],
};
[
'hiddenWinnerAmount',
'lotteryRange',
'customGroupIds',
'customGroupLotteryType',
'customGroupLotteryAmount',
'hiddenAttendeeNumber',
'repeatWinEnabled',
'receiveEnabled',
'receiveInfo',
'thumbnail',
'externalListLink',
'externalInviteNumLink',
'comment',
'realPrice',
'price',
'prizeInfo',
'questionGroupId',
'perAnswerDuration',
'lotteryOnlineEnabled',
'answerType',
'showWinnerCode',
'showWinners',
].forEach((key) => {
if (current[key] !== undefined && current[key] !== null) {
updateParams[key] = current[key];
}
});
const lotteryCondition = updateParams['lotteryCondition'];
if (lotteryCondition !== 'none') {
this.copyDefined(current, updateParams, ['activityDuration', 'activityDurationType']);
if (current['acceptType']) {
this.copyDefined(current, updateParams, ['acceptType', 'formInfo', 'prizeUrl', 'qrCode', 'qrCodeTips']);
}
}
if (lotteryCondition === 'invite') {
this.copyDefined(current, updateParams, ['inviteType']);
const inviteNum = this.numberOrUndefined(current['inviteNum']);
if (inviteNum !== undefined && inviteNum >= 1) {
updateParams['inviteNum'] = inviteNum;
}
}
if (lotteryCondition === 'duration') {
const duration = this.numberOrUndefined(current['duration']);
if (duration !== undefined && duration >= 2) {
updateParams['duration'] = duration;
}
}
return updateParams;
}
buildLotteryActivityCreate(params) {
const createParams = { ...params };
if (params.lotteryCondition !== 'none' && params.duration !== undefined) {
createParams['activityDuration'] = String(Math.max(1, Math.ceil(params.duration / 60)));
createParams['activityDurationType'] = 'minute';
}
if (params.lotteryCondition === 'invite' && createParams['inviteType'] === undefined) {
createParams['inviteType'] = 'poster';
}
if (params.lotteryCondition !== 'none' && createParams['acceptType'] === undefined) {
createParams['acceptType'] = 'form';
createParams['formInfo'] = [
{
type: 'userName',
field: 'Name',
tips: 'Please enter name',
required: true,
},
];
}
return createParams;
}
copyDefined(source, target, keys) {
keys.forEach((key) => {
if (source[key] !== undefined && source[key] !== null) {
target[key] = source[key];
}
});
}
numberOrUndefined(value) {
if (typeof value === 'number' && Number.isFinite(value)) {
return value;
}
if (typeof value === 'string' && value.trim() !== '') {
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : undefined;
}
return undefined;
}
withDefaultLotteryTimeRange(params) {
if (params.startTime !== undefined && params.endTime !== undefined) {
return params;
}
const endTime = params.endTime ?? Date.now();
const startTime = params.startTime ?? endTime - 7 * 24 * 60 * 60 * 1000;
return {
...params,
startTime,
endTime,
};
}
unwrapData(value) {
if (value && typeof value === 'object' && 'data' in value) {
return value.data;
}
return value;
}
successResponse() {
return {
code: 200,
status: 'success',
data: {},
};
}
wrapError(error, operation) {
if (error instanceof errors_1.PolyVError) {
return error;
}
const message = error instanceof Error ? error.message : String(error);
return new errors_1.PolyVError(`${operation} failed: ${message}`, 'LOTTERY_API_ERROR', 1, { originalError: error });
}
}
exports.LotteryServiceSdk = LotteryServiceSdk;
//# sourceMappingURL=lottery-service.js.map