UNPKG

prism-ad-campaigns

Version:
67 lines (53 loc) 1.75 kB
const RootModel = require('structure-root-model') const PrismFacebookMarketingApi = require('prism-facebook-marketing-api') const {ApplicationModel} = require('structure-applications') const {AdAccountModel} = require('prism-ad-accounts') /** * FacebookApiModel Class * * @public * @class AdSetModel */ class FacebookApiModel extends RootModel { /** * Get or create an api instance. * * Orginally this cached the instance so you didn't keep remaking new * instances. However, it appears there is a bug with the API where the * account ID can get overridden by the ID returned in the latest API call's * response. To try and fix this, lets just make a new instance for every * call * * @private * @param {Object} pkg */ async getApi(adAccountId) { // if (this.api) { // return this.api // } const applicationModel = new ApplicationModel({ logger: this.logger, organizationId: this.organizationId }) const app = await applicationModel.getById(this.applicationId) const adAccountModel = new AdAccountModel({ logger: this.logger, organizationId: this.organizationId, applicationId: this.applicationId }) const adAccount = await adAccountModel.getById(adAccountId) const api = new PrismFacebookMarketingApi({ logger: this.logger, accessToken: app.facebook.accessToken, adAccountId: adAccount.facebookAdAccountId, customConversionId: adAccount.facebookCustomConversionId, appId: app.facebook.appId, appSecret: app.facebook.appSecret, pageId: app.facebook.pageId, pixelId: app.facebook.pixelId, }) // this.api = api return api } } module.exports = FacebookApiModel