UNPKG

@memberjunction/actions-bizapps-social

Version:

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

188 lines 7.03 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.BufferGetSentPostsAction = void 0; const global_1 = require("@memberjunction/global"); const buffer_base_action_1 = require("../buffer-base.action"); const actions_1 = require("@memberjunction/actions"); /** * Action to get sent (published) posts from Buffer */ let BufferGetSentPostsAction = class BufferGetSentPostsAction extends buffer_base_action_1.BufferBaseAction { /** * Get sent posts from Buffer */ async InternalRunAction(params) { const { Params, ContextUser } = params; try { // Get parameters const companyIntegrationId = this.getParamValue(Params, 'CompanyIntegrationID'); const profileId = this.getParamValue(Params, 'ProfileID'); const page = this.getParamValue(Params, 'Page') || 1; const count = this.getParamValue(Params, 'Count') || 10; const since = this.getParamValue(Params, 'Since'); const useUTC = this.getParamValue(Params, 'UseUTC') !== false; // Validate required parameters if (!companyIntegrationId) { throw new Error('CompanyIntegrationID is required'); } if (!profileId) { throw new Error('ProfileID is required'); } // Initialize OAuth if (!await this.initializeOAuth(companyIntegrationId)) { return { Success: false, ResultCode: 'INVALID_TOKEN', Message: 'Failed to initialize Buffer OAuth connection', Params }; } // Get sent posts const result = await this.getUpdates(profileId, 'sent', { page, count, since: since ? new Date(since) : undefined, utc: useUTC }); // Format posts const posts = result.updates || []; const formattedPosts = posts.map((update) => this.normalizePost(update)); // Create summary const summary = { totalPosts: formattedPosts.length, page: page, hasMore: posts.length === count, profileId: profileId, dateRange: { earliest: formattedPosts.length > 0 ? formattedPosts[formattedPosts.length - 1].publishedAt : null, latest: formattedPosts.length > 0 ? formattedPosts[0].publishedAt : null }, postsByDay: this.groupPostsByDay(formattedPosts), totalEngagements: this.calculateTotalEngagements(formattedPosts), topPerformingPost: this.findTopPerformingPost(formattedPosts) }; // Update output parameters const outputParams = [...Params]; const postsParam = outputParams.find(p => p.Name === 'Posts'); if (postsParam) postsParam.Value = formattedPosts; const summaryParam = outputParams.find(p => p.Name === 'Summary'); if (summaryParam) summaryParam.Value = summary; return { Success: true, ResultCode: 'SUCCESS', Message: `Retrieved ${formattedPosts.length} sent posts from Buffer`, Params: outputParams }; } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; const resultCode = this.mapBufferError(error); return { Success: false, ResultCode: resultCode, Message: `Failed to get sent posts: ${errorMessage}`, Params }; } } /** * Group posts by published day */ groupPostsByDay(posts) { return posts.reduce((acc, post) => { if (post.publishedAt) { const day = post.publishedAt.toISOString().split('T')[0]; acc[day] = (acc[day] || 0) + 1; } return acc; }, {}); } /** * Calculate total engagements across all posts */ calculateTotalEngagements(posts) { return posts.reduce((total, post) => { if (post.analytics) { return total + post.analytics.engagements; } return total; }, 0); } /** * Find the top performing post by engagements */ findTopPerformingPost(posts) { if (posts.length === 0) return null; return posts.reduce((top, post) => { if (!top || (post.analytics?.engagements || 0) > (top.analytics?.engagements || 0)) { return post; } return top; }, posts[0]); } /** * Define the parameters this action expects */ get Params() { return [ ...this.commonSocialParams, { Name: 'ProfileID', Type: 'Input', Value: null }, { Name: 'Page', Type: 'Input', Value: 1 }, { Name: 'Count', Type: 'Input', Value: 10 }, { Name: 'Since', Type: 'Input', Value: null }, { Name: 'UseUTC', Type: 'Input', Value: true }, { Name: 'Posts', Type: 'Output', Value: null }, { Name: 'Summary', Type: 'Output', Value: null } ]; } /** * Metadata about this action */ get Description() { return 'Retrieves sent (published) posts from Buffer for a specific social media profile with analytics data'; } }; exports.BufferGetSentPostsAction = BufferGetSentPostsAction; exports.BufferGetSentPostsAction = BufferGetSentPostsAction = __decorate([ (0, global_1.RegisterClass)(actions_1.BaseAction, 'BufferGetSentPostsAction') ], BufferGetSentPostsAction); //# sourceMappingURL=get-sent-posts.action.js.map