diff --git a/front/lib/resources/tracker_resource.ts b/front/lib/resources/tracker_resource.ts index 56496c3e64201..ef55a715f2356 100644 --- a/front/lib/resources/tracker_resource.ts +++ b/front/lib/resources/tracker_resource.ts @@ -377,12 +377,26 @@ export class TrackerConfigurationResource extends ResourceWithSpace { return this.baseFetch(auth, { where: { vaultId: space.id, }, + includeDeleted, + }); + } + + static async listByWorkspace( + auth: Authenticator, + { includeDeleted }: { includeDeleted?: boolean } = {} + ): Promise { + return this.baseFetch(auth, { + where: { + workspaceId: auth.getNonNullableWorkspace().id, + }, + includeDeleted, }); } @@ -438,6 +452,7 @@ export class TrackerConfigurationResource extends ResourceWithSpace validDsViewIds.has(c.dataSourceViewId)); - // Fetch the associated tracker configurations // Fetch the associated tracker configurations const trackerIds = _.uniq( dsConfigs.map((config) => config.trackerConfigurationId) @@ -556,19 +570,30 @@ export class TrackerConfigurationResource extends ResourceWithSpace> { + const workspaceId = auth.getNonNullableWorkspace().id; const deletedCount = await frontSequelize.transaction(async (t) => { - // TODO Daph: Delete all related resources. - // await TrackerDataSourceConfigurationResource.deleteAllByTrackerId(this.id, t); - // await TrackerGenerationResource.deleteAllByTrackerId(this.id, t); - + await TrackerGenerationModel.destroy({ + where: { + trackerConfigurationId: this.id, + workspaceId, + }, + transaction: t, + hardDelete: true, + }); + await TrackerDataSourceConfigurationModel.destroy({ + where: { + trackerConfigurationId: this.id, + workspaceId, + }, + transaction: t, + hardDelete: true, + }); return TrackerConfigurationModel.destroy({ where: { - workspaceId: auth.getNonNullableWorkspace().id, id: this.id, + workspaceId, }, transaction: t, - // Use 'hardDelete: true' to ensure the record is permanently deleted from the database, - // bypassing the soft deletion in place. hardDelete: true, }); }); @@ -579,12 +604,32 @@ export class TrackerConfigurationResource extends ResourceWithSpace> { - const deletedCount = await TrackerConfigurationModel.destroy({ - where: { - workspaceId: auth.getNonNullableWorkspace().id, - id: this.id, - }, - hardDelete: false, + const workspaceId = auth.getNonNullableWorkspace().id; + const deletedCount = await frontSequelize.transaction(async (t) => { + await TrackerGenerationModel.destroy({ + where: { + trackerConfigurationId: this.id, + workspaceId, + }, + transaction: t, + hardDelete: false, + }); + await TrackerDataSourceConfigurationModel.destroy({ + where: { + trackerConfigurationId: this.id, + workspaceId, + }, + transaction: t, + hardDelete: false, + }); + return TrackerConfigurationModel.destroy({ + where: { + id: this.id, + workspaceId, + }, + transaction: t, + hardDelete: false, + }); }); return new Ok(deletedCount); diff --git a/front/poke/temporal/activities.ts b/front/poke/temporal/activities.ts index 48bfb36632247..47f37fc52661b 100644 --- a/front/poke/temporal/activities.ts +++ b/front/poke/temporal/activities.ts @@ -65,6 +65,7 @@ import { LabsTranscriptsHistoryModel, } from "@app/lib/resources/storage/models/labs_transcripts"; import { UserMetadataModel } from "@app/lib/resources/storage/models/user"; +import { TrackerConfigurationResource } from "@app/lib/resources/tracker_resource"; import { UserResource } from "@app/lib/resources/user_resource"; import { renderLightWorkspaceType } from "@app/lib/workspace"; import logger from "@app/logger/logger"; @@ -507,6 +508,21 @@ export async function deleteRunOnDustAppsActivity({ } } +export const deleteTrackersActivity = async ({ + workspaceId, +}: { + workspaceId: string; +}) => { + const auth = await Authenticator.internalAdminForWorkspace(workspaceId); + const trackers = await TrackerConfigurationResource.listByWorkspace(auth, { + includeDeleted: true, + }); + + for (const tracker of trackers) { + await tracker.delete(auth, { hardDelete: true }); + } +}; + export async function deleteMembersActivity({ workspaceId, }: { diff --git a/front/poke/temporal/workflows.ts b/front/poke/temporal/workflows.ts index decc1a3709707..f260940ad0538 100644 --- a/front/poke/temporal/workflows.ts +++ b/front/poke/temporal/workflows.ts @@ -10,6 +10,7 @@ const activityProxies = proxyActivities({ const { deleteAgentsActivity, deleteAppsActivity, + deleteTrackersActivity, deleteConversationsActivity, deleteMembersActivity, deleteRunOnDustAppsActivity, @@ -54,6 +55,7 @@ export async function deleteWorkspaceWorkflow({ await deleteAgentsActivity({ workspaceId }); await deleteAppsActivity({ workspaceId }); await deleteRunOnDustAppsActivity({ workspaceId }); + await deleteTrackersActivity({ workspaceId }); await deleteMembersActivity({ workspaceId }); await deleteSpacesActivity({ workspaceId }); await deleteTranscriptsActivity({ workspaceId }); diff --git a/front/temporal/scrub_workspace/activities.ts b/front/temporal/scrub_workspace/activities.ts index b6d69dd9ee8c1..d29fba106d4e9 100644 --- a/front/temporal/scrub_workspace/activities.ts +++ b/front/temporal/scrub_workspace/activities.ts @@ -29,6 +29,7 @@ import { subscriptionForWorkspaces } from "@app/lib/plans/subscription"; import { DataSourceResource } from "@app/lib/resources/data_source_resource"; import { MembershipResource } from "@app/lib/resources/membership_resource"; import { SpaceResource } from "@app/lib/resources/space_resource"; +import { TrackerConfigurationResource } from "@app/lib/resources/tracker_resource"; import { UserResource } from "@app/lib/resources/user_resource"; import { CustomerioServerSideTracking } from "@app/lib/tracking/customerio/server"; import { renderLightWorkspaceType } from "@app/lib/workspace"; @@ -89,6 +90,7 @@ export async function scrubWorkspaceData({ const auth = await Authenticator.internalAdminForWorkspace(workspaceId); await deleteAllConversations(auth); await archiveAssistants(auth); + await deleteTrackers(auth); await deleteDatasources(auth); await deleteSpaces(auth); await cleanupCustomerio(auth); @@ -151,6 +153,20 @@ async function archiveAssistants(auth: Authenticator) { } } +async function deleteTrackers(auth: Authenticator) { + const workspace = auth.workspace(); + if (!workspace) { + throw new Error("No workspace found"); + } + + const trackers = await TrackerConfigurationResource.listByWorkspace(auth, { + includeDeleted: true, + }); + for (const tracker of trackers) { + await tracker.delete(auth, { hardDelete: true }); + } +} + async function deleteDatasources(auth: Authenticator) { const globalAndSystemSpaces = await SpaceResource.listWorkspaceDefaultSpaces( auth,