From 5c521ee5da7362097acc27ee858432838079d323 Mon Sep 17 00:00:00 2001 From: anilb Date: Tue, 14 Jan 2025 10:16:38 +0100 Subject: [PATCH] Remove tenants [activities-worker] (#2747) --- .../src/activities/createConversations.ts | 35 +++++-------------- .../src/conversations/sql.ts | 10 +++--- .../src/conversations/types.ts | 2 +- 3 files changed, 16 insertions(+), 31 deletions(-) diff --git a/services/apps/activities_worker/src/activities/createConversations.ts b/services/apps/activities_worker/src/activities/createConversations.ts index 7acf38c46f..d71120c80d 100644 --- a/services/apps/activities_worker/src/activities/createConversations.ts +++ b/services/apps/activities_worker/src/activities/createConversations.ts @@ -1,6 +1,6 @@ import { convert as convertHtmlToText } from 'html-to-text' -import { generateUUIDv4, getCleanString, partition } from '@crowd/common' +import { generateUUIDv4, getCleanString, getDefaultTenantId, partition } from '@crowd/common' import { ALL_COLUMNS_TO_SELECT, doesConversationWithSlugExists, @@ -98,7 +98,6 @@ export async function createConversations(): Promise // if not then create a new conversation conversationId = generateUUIDv4() const conversationTitle = await generateTitle( - row.parent.tenantId, row.parent.egmentId, row.parent.title || row.parent.body, hasHtmlActivities(row.parent.platform), @@ -108,10 +107,10 @@ export async function createConversations(): Promise conversationsToCreate[row.parent.sourceId] = { id: conversationId, title: conversationTitle, - slug: await generateSlug(row.parent.tenantId, row.parent.segmentId, conversationTitle), + slug: await generateSlug(row.parent.segmentId, conversationTitle), published: true, timestamp: row.parent.timestamp || row.child.timestamp, - tenantId: row.parent.tenantId, + tenantId: getDefaultTenantId(), segmentId: row.parent.segmentId, createdById: null, updatedById: null, @@ -252,15 +251,9 @@ async function getMinActivityTimestamp(qdbConn: DbConnOrTx): Promise { +async function generateTitle(segmentId: string, title: string, isHtml = false): Promise { if (!title && getCleanString(title).length === 0) { const results = await queryConversations(this.qdbStore, { - tenantId, segmentIds: [segmentId], countOnly: true, }) @@ -288,7 +281,7 @@ function hasHtmlActivities(platform: PlatformType): boolean { } const MAX_SLUG_WORD_LENGTH = 10 -async function generateSlug(tenantId: string, segmentId: string, title: string): Promise { +async function generateSlug(segmentId: string, title: string): Promise { // Remove non-standart characters and extra whitespaces const cleanedTitle = getCleanString(title) @@ -305,15 +298,10 @@ async function generateSlug(tenantId: string, segmentId: string, title: string): // remove trailing dash cleanedSlug = cleanedSlug.replace(/-$/gi, '') - // check generated slug already exists in tenant - let slugExists = await doesConversationWithSlugExists( - this.qdbStore, - cleanedSlug, - tenantId, - segmentId, - ) + // check generated slug already exists in segment + let slugExists = await doesConversationWithSlugExists(this.qdbStore, cleanedSlug, segmentId) - // generated slug already exists in the tenant, start adding suffixes and re-check + // generated slug already exists in the segment, start adding suffixes and re-check if (slugExists) { let suffix = 1 @@ -321,12 +309,7 @@ async function generateSlug(tenantId: string, segmentId: string, title: string): while (slugExists) { const suffixedSlug = `${slugCopy}-${suffix}` - slugExists = await doesConversationWithSlugExists( - this.qdbStore, - cleanedSlug, - tenantId, - segmentId, - ) + slugExists = await doesConversationWithSlugExists(this.qdbStore, cleanedSlug, segmentId) suffix += 1 cleanedSlug = suffixedSlug } diff --git a/services/libs/data-access-layer/src/conversations/sql.ts b/services/libs/data-access-layer/src/conversations/sql.ts index 013a6c0461..b4ddec364f 100644 --- a/services/libs/data-access-layer/src/conversations/sql.ts +++ b/services/libs/data-access-layer/src/conversations/sql.ts @@ -1,7 +1,7 @@ import { convert as convertHtmlToText } from 'html-to-text' import merge from 'lodash.merge' -import { RawQueryParser, generateUUIDv4, getEnv } from '@crowd/common' +import { RawQueryParser, generateUUIDv4, getDefaultTenantId, getEnv } from '@crowd/common' import { DbConnOrTx } from '@crowd/database' import { ActivityDisplayService } from '@crowd/integrations' import { ActivityDisplayVariant, PageData, PlatformType } from '@crowd/types' @@ -334,7 +334,6 @@ export async function setConversationToActivity( export async function doesConversationWithSlugExists( conn: DbConnOrTx, slug: string, - tenantId: string, segmentId: string, ): Promise { const results = await conn.any( @@ -342,13 +341,11 @@ export async function doesConversationWithSlugExists( select id from conversations where - "tenantId" = $(tenantId) and "segmentId" = $(segmentId) and slug = $(slug) and "deletedAt" is null `, { - tenantId, segmentId, slug, }, @@ -377,6 +374,11 @@ export async function queryConversations( qdbConn: DbConnOrTx, arg: IQueryConversationsParameters, ): Promise> { + if (arg.tenantId === undefined) { + // fall back to default tenant + arg.tenantId = getDefaultTenantId() + } + if (arg.tenantId === undefined || arg.segmentIds === undefined || arg.segmentIds.length === 0) { throw new Error('tenantId and segmentIds are required to query conversations!') } diff --git a/services/libs/data-access-layer/src/conversations/types.ts b/services/libs/data-access-layer/src/conversations/types.ts index 27cbbe37f3..2015b353f2 100644 --- a/services/libs/data-access-layer/src/conversations/types.ts +++ b/services/libs/data-access-layer/src/conversations/types.ts @@ -39,7 +39,7 @@ export interface IConversationWithActivities extends IDbConversation { } export interface IQueryConversationsParameters { - tenantId: string + tenantId?: string segmentIds: string[] // eslint-disable-next-line @typescript-eslint/no-explicit-any filter?: any