/** * @file Manages Salesforce Analytics API * @author Shinichi Tomita */ import { registerModule } from '../jsforce'; import Connection from '../connection'; import { Schema } from '../types'; import { ReportMetadata, ReportExecuteResult, ReportRetrieveResult, ReportDescribeResult, ReportInfo, ReportInstanceInfo, DashboardMetadata, DashboardResult, DashboardStatusResult, DashboardRefreshResult, DashboardInfo, } from './analytics/types'; import { QueryExplainResult } from '../query'; /*----------------------------------------------------------------------------------*/ export type { ReportMetadata, ReportExecuteResult, ReportRetrieveResult, ReportDescribeResult, ReportInfo, ReportInstanceInfo, DashboardMetadata, DashboardResult, DashboardStatusResult, DashboardRefreshResult, DashboardInfo, }; export type ReportExecuteOptions = { details?: boolean; metadata?: { reportMetadata: Partial; }; }; /*----------------------------------------------------------------------------------*/ /** * Report object class in Analytics API */ export class ReportInstance { _report: Report; _conn: Connection; id: string; /** * */ constructor(report: Report, id: string) { this._report = report; this._conn = report._conn; this.id = id; } /** * Retrieve report result asynchronously executed */ retrieve(): Promise { const url = [ this._conn._baseUrl(), 'analytics', 'reports', this._report.id, 'instances', this.id, ].join('/'); return this._conn.request(url); } } /*----------------------------------------------------------------------------------*/ /** * Report object class in Analytics API */ export class Report { _conn: Connection; id: string; /** * */ constructor(conn: Connection, id: string) { this._conn = conn; this.id = id; } /** * Describe report metadata */ describe(): Promise { const url = [ this._conn._baseUrl(), 'analytics', 'reports', this.id, 'describe', ].join('/'); return this._conn.request(url); } /** * Destroy a report */ destroy(): Promise { const url = [this._conn._baseUrl(), 'analytics', 'reports', this.id].join( '/', ); return this._conn.request({ method: 'DELETE', url }); } /** * Synonym of Analytics~Report#destroy() */ delete = this.destroy; /** * Synonym of Analytics~Report#destroy() */ del = this.destroy; /** * Clones a given report */ clone(name: string): Promise { const url = [this._conn._baseUrl(), 'analytics', 'reports'].join('/') + '?cloneId=' + this.id; const config = { reportMetadata: { name } }; return this._conn.request({ method: 'POST', url, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(config), }); } /** * Explain plan for executing report */ explain(): Promise { const url = '/query/?explain=' + this.id; return this._conn.request(url); } /** * Run report synchronously */ execute(options: ReportExecuteOptions = {}): Promise { const url = [this._conn._baseUrl(), 'analytics', 'reports', this.id].join('/') + '?includeDetails=' + (options.details ? 'true' : 'false'); return this._conn.request({ url, ...(options.metadata ? { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(options.metadata), } : { method: 'GET' }), }); } /** * Synonym of Analytics~Report#execute() */ run = this.execute; /** * Synonym of Analytics~Report#execute() */ exec = this.execute; /** * Run report asynchronously */ executeAsync( options: ReportExecuteOptions = {}, ): Promise { const url = [ this._conn._baseUrl(), 'analytics', 'reports', this.id, 'instances', ].join('/') + (options.details ? '?includeDetails=true' : ''); return this._conn.request({ method: 'POST', url, ...(options.metadata ? { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(options.metadata), } : { body: '' }), }); } /** * Get report instance for specified instance ID */ instance(id: string) { return new ReportInstance(this, id); } /** * List report instances which had been executed asynchronously */ instances(): Promise { const url = [ this._conn._baseUrl(), 'analytics', 'reports', this.id, 'instances', ].join('/'); return this._conn.request(url); } } /*----------------------------------------------------------------------------------*/ /** * Dashboard object class in the Analytics API */ export class Dashboard { _conn: Connection; id: string; /** * */ constructor(conn: Connection, id: string) { this._conn = conn; this.id = id; } /** * Describe dashboard metadata * * @method Analytics~Dashboard#describe * @param {Callback.} [callback] - Callback function * @returns {Promise.} */ describe(): Promise { const url = [ this._conn._baseUrl(), 'analytics', 'dashboards', this.id, 'describe', ].join('/'); return this._conn.request(url); } /** * Get details about dashboard components */ components(componentIds?: string | string[]): Promise { const url = [ this._conn._baseUrl(), 'analytics', 'dashboards', this.id, ].join('/'); const config = { componentIds: Array.isArray(componentIds) ? componentIds : typeof componentIds === 'string' ? [componentIds] : undefined, }; return this._conn.request({ method: 'POST', url, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(config), }); } /** * Get dashboard status */ status(): Promise { const url = [ this._conn._baseUrl(), 'analytics', 'dashboards', this.id, 'status', ].join('/'); return this._conn.request(url); } /** * Refresh a dashboard */ refresh(): Promise { const url = [ this._conn._baseUrl(), 'analytics', 'dashboards', this.id, ].join('/'); return this._conn.request({ method: 'PUT', url, body: '', }); } /** * Clone a dashboard */ clone( config: { name: string; folderId?: string } | string, folderId?: string, ): Promise { const url = [this._conn._baseUrl(), 'analytics', 'dashboards'].join('/') + '?cloneId=' + this.id; if (typeof config === 'string') { config = { name: config, folderId }; } return this._conn.request({ method: 'POST', url, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(config), }); } /** * Destroy a dashboard */ destroy(): Promise { const url = [ this._conn._baseUrl(), 'analytics', 'dashboards', this.id, ].join('/'); return this._conn.request({ method: 'DELETE', url }); } /** * Synonym of Analytics~Dashboard#destroy() */ delete = this.destroy; /** * Synonym of Analytics~Dashboard#destroy() */ del = this.destroy; } /*----------------------------------------------------------------------------------*/ /** * API class for Analytics API */ export class Analytics { _conn: Connection; /** * */ constructor(conn: Connection) { this._conn = conn; } /** * Get report object of Analytics API */ report(id: string) { return new Report(this._conn, id); } /** * Get recent report list */ reports() { const url = [this._conn._baseUrl(), 'analytics', 'reports'].join('/'); return this._conn.request(url); } /** * Get dashboard object of Analytics API */ dashboard(id: string) { return new Dashboard(this._conn, id); } /** * Get recent dashboard list */ dashboards() { const url = [this._conn._baseUrl(), 'analytics', 'dashboards'].join('/'); return this._conn.request(url); } } /*--------------------------------------------*/ /* * Register hook in connection instantiation for dynamically adding this API module features */ registerModule('analytics', (conn) => new Analytics(conn)); export default Analytics;