Skip to content

Commit

Permalink
Remove tenants [activities-worker] (#2747)
Browse files Browse the repository at this point in the history
  • Loading branch information
epipav authored Jan 14, 2025
1 parent 768ec10 commit 5c521ee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -98,7 +98,6 @@ export async function createConversations(): Promise<ICreateConversationsResult>
// 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),
Expand All @@ -108,10 +107,10 @@ export async function createConversations(): Promise<ICreateConversationsResult>
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,
Expand Down Expand Up @@ -252,15 +251,9 @@ async function getMinActivityTimestamp(qdbConn: DbConnOrTx): Promise<string | nu
return result.minTimestamp
}

async function generateTitle(
tenantId: string,
segmentId: string,
title: string,
isHtml = false,
): Promise<string> {
async function generateTitle(segmentId: string, title: string, isHtml = false): Promise<string> {
if (!title && getCleanString(title).length === 0) {
const results = await queryConversations(this.qdbStore, {
tenantId,
segmentIds: [segmentId],
countOnly: true,
})
Expand Down Expand Up @@ -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<string> {
async function generateSlug(segmentId: string, title: string): Promise<string> {
// Remove non-standart characters and extra whitespaces
const cleanedTitle = getCleanString(title)

Expand All @@ -305,28 +298,18 @@ 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

const slugCopy = cleanedSlug

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
}
Expand Down
10 changes: 6 additions & 4 deletions services/libs/data-access-layer/src/conversations/sql.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -334,21 +334,18 @@ export async function setConversationToActivity(
export async function doesConversationWithSlugExists(
conn: DbConnOrTx,
slug: string,
tenantId: string,
segmentId: string,
): Promise<boolean> {
const results = await conn.any(
`
select id
from conversations
where
"tenantId" = $(tenantId) and
"segmentId" = $(segmentId) and
slug = $(slug) and
"deletedAt" is null
`,
{
tenantId,
segmentId,
slug,
},
Expand Down Expand Up @@ -377,6 +374,11 @@ export async function queryConversations(
qdbConn: DbConnOrTx,
arg: IQueryConversationsParameters,
): Promise<PageData<IQueryConversationResult>> {
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!')
}
Expand Down
2 changes: 1 addition & 1 deletion services/libs/data-access-layer/src/conversations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5c521ee

Please sign in to comment.