UNPKG

@memberjunction/actions-bizapps-social

Version:

Social Media Actions for MemberJunction - Twitter, LinkedIn, Facebook, Instagram, TikTok, YouTube, HootSuite, Buffer

209 lines 8.13 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HootSuiteGetScheduledPostsAction = void 0; const global_1 = require("@memberjunction/global"); const hootsuite_base_action_1 = require("../hootsuite-base.action"); const core_1 = require("@memberjunction/core"); const actions_1 = require("@memberjunction/actions"); /** * Action to retrieve scheduled posts from HootSuite */ let HootSuiteGetScheduledPostsAction = class HootSuiteGetScheduledPostsAction extends hootsuite_base_action_1.HootSuiteBaseAction { /** * Get scheduled posts from HootSuite */ async InternalRunAction(params) { const { Params, ContextUser } = params; try { // Initialize OAuth const companyIntegrationId = this.getParamValue(Params, 'CompanyIntegrationID'); if (!await this.initializeOAuth(companyIntegrationId)) { throw new Error('Failed to initialize OAuth connection'); } // Extract parameters const profileId = this.getParamValue(Params, 'ProfileID'); const startDate = this.getParamValue(Params, 'StartDate'); const endDate = this.getParamValue(Params, 'EndDate'); const limit = this.getParamValue(Params, 'Limit') || 100; const includeAnalytics = this.getParamValue(Params, 'IncludeAnalytics') || false; // Build query parameters const queryParams = { state: 'SCHEDULED', limit: Math.min(limit, 100), maxResults: limit }; if (profileId) { queryParams.socialProfileIds = profileId; } if (startDate) { queryParams.scheduledAfter = this.formatHootSuiteDate(startDate); } if (endDate) { queryParams.scheduledBefore = this.formatHootSuiteDate(endDate); } // Get scheduled posts const posts = await this.makePaginatedRequest('/messages', queryParams); // Convert to common format const normalizedPosts = await Promise.all(posts.map(async (post) => { const normalized = this.normalizePost(post); // Optionally include analytics if (includeAnalytics && post.state === 'PUBLISHED') { try { const analytics = await this.getPostAnalytics(post.id); normalized.analytics = this.normalizeAnalytics(analytics); } catch (error) { (0, core_1.LogStatus)(`Failed to get analytics for post ${post.id}: ${error instanceof Error ? error.message : 'Unknown error'}`); } } return normalized; })); // Create summary const summary = { totalPosts: normalizedPosts.length, byProfile: this.groupByProfile(posts), byDate: this.groupByDate(normalizedPosts), dateRange: { start: startDate || 'Not specified', end: endDate || 'Not specified' } }; // Update output parameters const outputParams = [...Params]; const postsParam = outputParams.find(p => p.Name === 'ScheduledPosts'); if (postsParam) postsParam.Value = normalizedPosts; const summaryParam = outputParams.find(p => p.Name === 'Summary'); if (summaryParam) summaryParam.Value = summary; return { Success: true, ResultCode: 'SUCCESS', Message: `Retrieved ${normalizedPosts.length} scheduled posts`, Params: outputParams }; } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; return { Success: false, ResultCode: 'ERROR', Message: `Failed to get scheduled posts: ${errorMessage}`, Params }; } } /** * Convert HootSuite post to common format */ normalizePost(hootsuitePost) { return { id: hootsuitePost.id, platform: 'HootSuite', profileId: hootsuitePost.socialProfileIds.join(','), // Multiple profiles possible content: hootsuitePost.text, mediaUrls: hootsuitePost.mediaIds || [], publishedAt: this.parseHootSuiteDate(hootsuitePost.createdTime), scheduledFor: hootsuitePost.scheduledTime ? this.parseHootSuiteDate(hootsuitePost.scheduledTime) : undefined, platformSpecificData: { state: hootsuitePost.state, tags: hootsuitePost.tags, location: hootsuitePost.location, socialProfileIds: hootsuitePost.socialProfileIds } }; } /** * Get analytics for a specific post */ async getPostAnalytics(postId) { try { const response = await this.axiosInstance.get(`/analytics/posts/${postId}`); return response.data; } catch (error) { // Analytics might not be available for all posts return null; } } /** * Group posts by profile */ groupByProfile(posts) { const groups = {}; posts.forEach(post => { post.socialProfileIds.forEach(profileId => { groups[profileId] = (groups[profileId] || 0) + 1; }); }); return groups; } /** * Group posts by scheduled date */ groupByDate(posts) { const groups = {}; posts.forEach(post => { if (post.scheduledFor) { const dateKey = post.scheduledFor.toISOString().split('T')[0]; groups[dateKey] = (groups[dateKey] || 0) + 1; } }); return groups; } /** * Define the parameters this action expects */ get Params() { return [ ...this.commonSocialParams, { Name: 'StartDate', Type: 'Input', Value: null }, { Name: 'EndDate', Type: 'Input', Value: null }, { Name: 'Limit', Type: 'Input', Value: null }, { Name: 'IncludeAnalytics', Type: 'Input', Value: null }, { Name: 'ScheduledPosts', Type: 'Output', Value: null }, { Name: 'Summary', Type: 'Output', Value: null } ]; } /** * Get action description */ get Description() { return 'Retrieves scheduled posts from HootSuite with optional date filtering and analytics'; } }; exports.HootSuiteGetScheduledPostsAction = HootSuiteGetScheduledPostsAction; exports.HootSuiteGetScheduledPostsAction = HootSuiteGetScheduledPostsAction = __decorate([ (0, global_1.RegisterClass)(actions_1.BaseAction, 'HootSuiteGetScheduledPostsAction') ], HootSuiteGetScheduledPostsAction); //# sourceMappingURL=get-scheduled-posts.action.js.map