Skip to content

Commit

Permalink
add CLI command to resync tickets only (there was no path previously …
Browse files Browse the repository at this point in the history
…to not forceResync)
  • Loading branch information
aubin-tchoi committed Dec 12, 2024
1 parent 5fb5cbf commit b4d36f8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
22 changes: 21 additions & 1 deletion connectors/src/connectors/zendesk/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import type {
ZendeskCommandType,
ZendeskCountTicketsResponseType,
} from "@dust-tt/types";
import type { ZendeskResyncTicketsResponseType } from "@dust-tt/types/src";

import { getZendeskSubdomainAndAccessToken } from "@connectors/connectors/zendesk/lib/zendesk_access_token";
import {
fetchZendeskCurrentUser,
fetchZendeskTicketCount,
getZendeskBrandSubdomain,
} from "@connectors/connectors/zendesk/lib/zendesk_api";
import { launchZendeskTicketReSyncWorkflow } from "@connectors/connectors/zendesk/temporal/client";
import { default as topLogger } from "@connectors/logger/logger";
import { ConnectorResource } from "@connectors/resources/connector_resource";
import { ZendeskConfigurationResource } from "@connectors/resources/zendesk_resources";
Expand All @@ -18,7 +20,9 @@ export const zendesk = async ({
command,
args,
}: ZendeskCommandType): Promise<
ZendeskCheckIsAdminResponseType | ZendeskCountTicketsResponseType
| ZendeskCheckIsAdminResponseType
| ZendeskCountTicketsResponseType
| ZendeskResyncTicketsResponseType
> => {
const logger = topLogger.child({ majorCommand: "zendesk", command, args });

Expand Down Expand Up @@ -81,5 +85,21 @@ export const zendesk = async ({
);
return { ticketCount };
}
case "resync-tickets": {
if (!connector) {
throw new Error(`Connector ${connectorId} not found`);
}
const result = await launchZendeskTicketReSyncWorkflow(connector, {
forceResync: args.forceResync || false,
});
if (result.isErr()) {
logger.error(
{ error: result.error },
"Error launching the sync workflow."
);
throw result.error;
}
return { success: true };
}
}
};
21 changes: 21 additions & 0 deletions connectors/src/connectors/zendesk/temporal/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,24 @@ export async function launchZendeskGarbageCollectionWorkflow(

return new Ok(undefined);
}

/**
* Launches a Zendesk workflow that will resync the tickets.
*
* It recreates the signals necessary to resync every brand whose tickets are selected by the user.
*/
export async function launchZendeskTicketReSyncWorkflow(
connector: ConnectorResource,
{ forceResync = false }: { forceResync?: boolean } = {}
): Promise<Result<string, Error>> {
const brandIds = await ZendeskBrandResource.fetchTicketsAllowedBrandIds(
connector.id
);

const result = await launchZendeskSyncWorkflow(connector, {
brandIds,
forceResync,
});

return result.isErr() ? result : new Ok(connector.id.toString());
}
14 changes: 13 additions & 1 deletion types/src/connectors/admin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,16 @@ export type IntercomForceResyncArticlesResponseType = t.TypeOf<
*/
export const ZendeskCommandSchema = t.type({
majorCommand: t.literal("zendesk"),
command: t.union([t.literal("check-is-admin"), t.literal("count-tickets")]),
command: t.union([
t.literal("check-is-admin"),
t.literal("count-tickets"),
t.literal("resync-tickets"),
]),
args: t.type({
connectorId: t.union([t.number, t.undefined]),
brandId: t.union([t.number, t.undefined]),
query: t.union([t.string, t.undefined]),
forceResync: t.boolean,
}),
});
export type ZendeskCommandType = t.TypeOf<typeof ZendeskCommandSchema>;
Expand All @@ -243,6 +248,13 @@ export const ZendeskCountTicketsResponseSchema = t.type({
export type ZendeskCountTicketsResponseType = t.TypeOf<
typeof ZendeskCountTicketsResponseSchema
>;

export const ZendeskResyncTicketsResponseSchema = t.type({
success: t.literal(true),
});
export type ZendeskResyncTicketsResponseType = t.TypeOf<
typeof ZendeskResyncTicketsResponseSchema
>;
/**
* </Zendesk>
*/
Expand Down

0 comments on commit b4d36f8

Please sign in to comment.