Skip to content

Commit

Permalink
[connectors] Remove unnecessary columns in zendesk_tickets (#8854)
Browse files Browse the repository at this point in the history
* ♻️ remove unnecessary columns in zendesk tickets

* 🚩 add migration script

* fix: remove dropped fields from ticket creation

* ♻️ remove `commonTicketData` and equivalents to prevent hiding TS errors
  • Loading branch information
aubin-tchoi authored Nov 22, 2024
1 parent 44f3f72 commit 56e3795
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 49 deletions.
4 changes: 4 additions & 0 deletions connectors/migrations/db/migration_38.sql
Original file line number Diff line number Diff line change
@@ -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";
15 changes: 8 additions & 7 deletions connectors/src/connectors/zendesk/lib/sync_article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,25 @@ 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",
connectorId,
},
});
} 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(
Expand Down
18 changes: 10 additions & 8 deletions connectors/src/connectors/zendesk/lib/sync_category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,25 @@ 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,
permission: "read",
},
});
} else {
await categoryInDb.update(updatableFields);
await categoryInDb.update({
name: category.name || "Category",
url: category.html_url,
description: category.description,
lastUpsertedTs: new Date(currentSyncDateMs),
});
}
}
26 changes: 11 additions & 15 deletions connectors/src/connectors/zendesk/lib/sync_ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,27 @@ 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",
connectorId,
},
});
} 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) {
Expand Down
19 changes: 0 additions & 19 deletions connectors/src/lib/models/zendesk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 56e3795

Please sign in to comment.