From dc1c76c2794d6e0b0a421e0bcb85dd92574bc04b Mon Sep 17 00:00:00 2001 From: GTFalcao Date: Wed, 26 Jun 2024 00:59:55 -0300 Subject: [PATCH] Implementing query and pagination for object filter --- .../actions/create-report/create-report.mjs | 20 ++++++++--- components/google_ads/common/queries.mjs | 9 +++-- components/google_ads/google_ads.app.mjs | 34 +++++++++++++------ 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/components/google_ads/actions/create-report/create-report.mjs b/components/google_ads/actions/create-report/create-report.mjs index 699a175c2f8f5..fa2c07a242db2 100644 --- a/components/google_ads/actions/create-report/create-report.mjs +++ b/components/google_ads/actions/create-report/create-report.mjs @@ -49,17 +49,29 @@ export default { label: `Filter by ${label}s`, description: `Select the ${label}s to generate a report for (or leave blank for all ${label}s)`, optional: true, - options: async () => { + useQuery: true, + options: async ({ + query, prevContext: { nextPageToken: pageToken }, + }) => { const { accountId, customerClientId, resource, } = this; - const items = await this.googleAds.listResources({ + const { + results, nextPageToken, + } = await this.googleAds.listResources({ accountId, customerClientId, resource, + query, + pageToken, }); - console.log(items); - return items?.map?.((item) => this.getResourceOption(item, resource)); + const options = results?.map?.((item) => this.getResourceOption(item, resource)); + return { + options, + context: { + nextPageToken, + }, + }; }, }, dateRange: { diff --git a/components/google_ads/common/queries.mjs b/components/google_ads/common/queries.mjs index 2122c913e1d84..d2345abd47bf7 100644 --- a/components/google_ads/common/queries.mjs +++ b/components/google_ads/common/queries.mjs @@ -94,14 +94,19 @@ function listCampaigns({ return `SELECT ${fields.map((s) => `campaign.${s}`).join(", ")} FROM campaign${filter}`; } -function listResources(resource) { +function listResources(resource, query) { const name = resource === "customer" ? "descriptive_name" : "name"; const fieldResource = resource === "ad_group_ad" ? "ad_group_ad.ad" : resource; - return `SELECT ${fieldResource}.id, ${fieldResource}.${name} FROM ${resource}`; + + let result = `SELECT ${fieldResource}.id, ${fieldResource}.${name} FROM ${resource}`; + if (query) { + result += ` WHERE ${fieldResource}.${name} LIKE '%${query}%'`; + } + return result; } export const QUERIES = { diff --git a/components/google_ads/google_ads.app.mjs b/components/google_ads/google_ads.app.mjs index a70794b590c4f..3242829750418 100644 --- a/components/google_ads/google_ads.app.mjs +++ b/components/google_ads/google_ads.app.mjs @@ -128,7 +128,7 @@ export default { }, ...args, }); - return response.results; + return response; }, async listAccessibleCustomers() { const response = await this._makeRequest({ @@ -139,13 +139,15 @@ export default { async listCustomerClients({ query, ...args }) { - return this.search({ + const { results } = await this.search({ query: QUERIES.listCustomerClients(query), ...args, }); + return results; }, async createReport(args) { - return this.search(args); + const { results } = await this.search(args); + return results; }, async createUserList(args) { const response = await this._makeRequest({ @@ -156,52 +158,62 @@ export default { return response; }, async listUserLists(args) { - return this.search({ + const { results } = await this.search({ query: QUERIES.listUserLists(), ...args, }); + return results; }, async listConversionActions(args) { - return this.search({ + const { results } = await this.search({ query: QUERIES.listConversionActions(), ...args, }); + return results; }, async listRemarketingActions(args) { - return this.search({ + const { results } = await this.search({ query: QUERIES.listRemarketingActions(), ...args, }); + return results; }, async listLeadForms(args) { - return this.search({ + const { results } = await this.search({ query: QUERIES.listLeadForms(), ...args, }); + return results; }, async listCampaigns({ query, ...args }) { - return this.search({ + const { results } = await this.search({ query: QUERIES.listCampaigns(query), ...args, }); + return results; }, async listResources({ - resource, ...args + resource, query, pageToken, ...args }) { return this.search({ - query: QUERIES.listResources(resource), + query: QUERIES.listResources(resource, query), + params: { + pageSize: 100, + pageToken, + }, ...args, }); }, async getLeadFormData({ leadFormId, ...args }) { - return this.search({ + const { results } = await this.search({ query: QUERIES.listLeadFormSubmissionData(leadFormId), ...args, }); + return results; }, async createConversionAction(args) { const response = await this._makeRequest({