diff --git a/connectors/src/connectors/zendesk/temporal/activities.ts b/connectors/src/connectors/zendesk/temporal/activities.ts index 07afe6c274c4..029f9e6d650e 100644 --- a/connectors/src/connectors/zendesk/temporal/activities.ts +++ b/connectors/src/connectors/zendesk/temporal/activities.ts @@ -136,12 +136,25 @@ export async function syncZendeskBrandActivity({ } /** - * Retrieves the IDs of every brand stored in db that has read permissions on their Help Center. + * Retrieves the IDs of every brand in db that has read permissions on their Help Center or in one of their Categories. + * This activity will be used to retrieve the brands that need to be incrementally synced. + * + * Note: in this approach; if a single category has read permissions and not its Help Center, + * diffs for the whole Help Center are fetched since there is no endpoint that returns the diff for the Category. */ export async function getZendeskHelpCenterReadAllowedBrandIdsActivity( connectorId: ModelId ): Promise { - return ZendeskBrandResource.fetchHelpCenterReadAllowedBrandIds(connectorId); + // fetching the brands that have a Help Center selected as a whole + const brandsWithHelpCenter = + await ZendeskBrandResource.fetchHelpCenterReadAllowedBrandIds(connectorId); + // fetching the brands that have at least one Category selected + const brandWithCategories = + await ZendeskCategoryResource.fetchBrandIdsOfReadOnlyCategories( + connectorId + ); + // removing duplicates + return [...new Set([...brandsWithHelpCenter, ...brandWithCategories])]; } /** diff --git a/connectors/src/resources/zendesk_resources.ts b/connectors/src/resources/zendesk_resources.ts index 038c9d743c2f..4329e737a1fe 100644 --- a/connectors/src/resources/zendesk_resources.ts +++ b/connectors/src/resources/zendesk_resources.ts @@ -6,7 +6,7 @@ import type { ModelStatic, Transaction, } from "sequelize"; -import { Op } from "sequelize"; +import { col, fn, Op } from "sequelize"; import { getArticleInternalId, @@ -498,6 +498,16 @@ export class ZendeskCategoryResource extends BaseResource { return categories.map((category) => category.get().categoryId); } + static async fetchBrandIdsOfReadOnlyCategories( + connectorId: number + ): Promise { + const categories = await ZendeskCategory.findAll({ + where: { connectorId, permission: "read" }, + attributes: [[fn("DISTINCT", col("brandId")), "brandId"]], + }); + return categories.map((category) => category.get().brandId); + } + static async fetchByBrandIdReadOnly({ connectorId, brandId,