From 56e3795a47c61507b78847d1ce00b7d9890b7fdd Mon Sep 17 00:00:00 2001 From: Aubin <60398825+aubin-tchoi@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:56:10 +0100 Subject: [PATCH] [connectors] Remove unnecessary columns in zendesk_tickets (#8854) * :recycle: remove unnecessary columns in zendesk tickets * :triangular_flag_on_post: add migration script * fix: remove dropped fields from ticket creation * :recycle: remove `commonTicketData` and equivalents to prevent hiding TS errors --- connectors/migrations/db/migration_38.sql | 4 +++ .../connectors/zendesk/lib/sync_article.ts | 15 ++++++----- .../connectors/zendesk/lib/sync_category.ts | 18 +++++++------ .../src/connectors/zendesk/lib/sync_ticket.ts | 26 ++++++++----------- connectors/src/lib/models/zendesk.ts | 19 -------------- 5 files changed, 33 insertions(+), 49 deletions(-) create mode 100644 connectors/migrations/db/migration_38.sql diff --git a/connectors/migrations/db/migration_38.sql b/connectors/migrations/db/migration_38.sql new file mode 100644 index 000000000000..882e9e6bb152 --- /dev/null +++ b/connectors/migrations/db/migration_38.sql @@ -0,0 +1,4 @@ +-- Migration created on Nov 22, 2024 +ALTER TABLE "zendesk_tickets" DROP COLUMN "assigneeId"; +ALTER TABLE "zendesk_tickets" DROP COLUMN "groupId"; +ALTER TABLE "zendesk_tickets" DROP COLUMN "organizationId"; diff --git a/connectors/src/connectors/zendesk/lib/sync_article.ts b/connectors/src/connectors/zendesk/lib/sync_article.ts index 84dd390ea9bc..221e46f1406d 100644 --- a/connectors/src/connectors/zendesk/lib/sync_article.ts +++ b/connectors/src/connectors/zendesk/lib/sync_article.ts @@ -84,16 +84,13 @@ export async function syncArticle({ !articleInDb.lastUpsertedTs || articleInDb.lastUpsertedTs < updatedAtDate; // upserting if the article was updated after the last upsert - const updatableFields = { - categoryId: category.categoryId, // an article can be moved from one category to another, which does not apply to brands - name: article.name, - url: article.html_url, - }; // we either create a new article or update the existing one if (!articleInDb) { articleInDb = await ZendeskArticleResource.makeNew({ blob: { - ...updatableFields, + categoryId: category.categoryId, // an article can be moved from one category to another, which does not apply to brands + name: article.name, + url: article.html_url, articleId: article.id, brandId: category.brandId, permission: "read", @@ -101,7 +98,11 @@ export async function syncArticle({ }, }); } else { - await articleInDb.update(updatableFields); + await articleInDb.update({ + categoryId: category.categoryId, // an article can be moved from one category to another, which does not apply to brands + name: article.name, + url: article.html_url, + }); } logger.info( diff --git a/connectors/src/connectors/zendesk/lib/sync_category.ts b/connectors/src/connectors/zendesk/lib/sync_category.ts index 74faf97d476c..a0cf5bd120d3 100644 --- a/connectors/src/connectors/zendesk/lib/sync_category.ts +++ b/connectors/src/connectors/zendesk/lib/sync_category.ts @@ -63,16 +63,13 @@ export async function syncCategory({ connectorId, categoryId: category.id, }); - const updatableFields = { - name: category.name || "Category", - url: category.html_url, - description: category.description, - lastUpsertedTs: new Date(currentSyncDateMs), - }; if (!categoryInDb) { await ZendeskCategoryResource.makeNew({ blob: { - ...updatableFields, + name: category.name || "Category", + url: category.html_url, + description: category.description, + lastUpsertedTs: new Date(currentSyncDateMs), connectorId, brandId, categoryId: category.id, @@ -80,6 +77,11 @@ export async function syncCategory({ }, }); } else { - await categoryInDb.update(updatableFields); + await categoryInDb.update({ + name: category.name || "Category", + url: category.html_url, + description: category.description, + lastUpsertedTs: new Date(currentSyncDateMs), + }); } } diff --git a/connectors/src/connectors/zendesk/lib/sync_ticket.ts b/connectors/src/connectors/zendesk/lib/sync_ticket.ts index 8ed0733de503..55a67db3b2d6 100644 --- a/connectors/src/connectors/zendesk/lib/sync_ticket.ts +++ b/connectors/src/connectors/zendesk/lib/sync_ticket.ts @@ -85,23 +85,13 @@ export async function syncTicket({ !ticketInDb.lastUpsertedTs || ticketInDb.lastUpsertedTs < updatedAtDate; - // ticket.url is the json api url, we need to convert it to the web url - const ticketUrl = ticket.url.replace("/api/v2/", "/").replace(".json", ""); - - const commonTicketData = { - subject: ticket.subject, - url: ticketUrl, - assigneeId: ticket.assignee_id, - groupId: ticket.group_id, - organizationId: ticket.organization_id, - lastUpsertedTs: new Date(currentSyncDateMs), - ticketUpdatedAt: updatedAtDate, - }; - if (!ticketInDb) { ticketInDb = await ZendeskTicketResource.makeNew({ blob: { - ...commonTicketData, + subject: ticket.subject, + url: ticket.url.replace("/api/v2/", "/").replace(".json", ""), // converting the API URL into the web URL + lastUpsertedTs: new Date(currentSyncDateMs), + ticketUpdatedAt: updatedAtDate, ticketId: ticket.id, brandId, permission: "read", @@ -109,7 +99,13 @@ export async function syncTicket({ }, }); } else { - await ticketInDb.update({ ...commonTicketData, permission: "read" }); + await ticketInDb.update({ + subject: ticket.subject, + url: ticket.url.replace("/api/v2/", "/").replace(".json", ""), // converting the API URL into the web URL + lastUpsertedTs: new Date(currentSyncDateMs), + ticketUpdatedAt: updatedAtDate, + permission: "read", + }); } if (!shouldPerformUpsertion) { diff --git a/connectors/src/lib/models/zendesk.ts b/connectors/src/lib/models/zendesk.ts index 1752ead95043..20abf9f7d972 100644 --- a/connectors/src/lib/models/zendesk.ts +++ b/connectors/src/lib/models/zendesk.ts @@ -397,10 +397,6 @@ export class ZendeskTicket extends Model< declare brandId: number; declare permission: "read" | "none"; - declare assigneeId: number | null; - declare groupId: number | null; - declare organizationId: number | null; - declare subject: string; declare url: string; @@ -445,21 +441,6 @@ ZendeskTicket.init( allowNull: false, validate: { throwOnUnsafeInteger }, }, - groupId: { - type: DataTypes.BIGINT, - allowNull: true, - validate: { throwOnUnsafeInteger }, - }, - assigneeId: { - type: DataTypes.BIGINT, - allowNull: true, - validate: { throwOnUnsafeInteger }, - }, - organizationId: { - type: DataTypes.BIGINT, - allowNull: true, - validate: { throwOnUnsafeInteger }, - }, permission: { type: DataTypes.STRING, allowNull: false,