Skip to content

Commit 2d0c86d

Browse files
committed
add segment ids
1 parent 376d4a5 commit 2d0c86d

File tree

4 files changed

+86
-34
lines changed

4 files changed

+86
-34
lines changed

services/apps/data_sink_worker/src/service/activity.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export default class ActivityService extends LoggerBase {
8080
return id
8181
})
8282
await this.nodejsWorkerEmitter.processAutomationForNewActivity(tenantId, id, segmentId)
83-
const affectedIds = await this.conversationService.processActivity(tenantId, id)
83+
const affectedIds = await this.conversationService.processActivity(tenantId, segmentId, id)
8484

8585
if (fireSync) {
8686
await this.searchSyncWorkerEmitter.triggerMemberSync(tenantId, activity.memberId)
@@ -158,7 +158,7 @@ export default class ActivityService extends LoggerBase {
158158
})
159159

160160
if (updated) {
161-
await this.conversationService.processActivity(tenantId, id)
161+
await this.conversationService.processActivity(tenantId, segmentId, id)
162162

163163
if (fireSync) {
164164
await this.searchSyncWorkerEmitter.triggerMemberSync(tenantId, activity.memberId)

services/libs/conversations/src/repo/conversation.data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const getInsertConversationColumnSet = (instance: DbInstance): DbColumnSe
2727
if (insertConversationColumnSet) return insertConversationColumnSet
2828

2929
insertConversationColumnSet = new instance.helpers.ColumnSet(
30-
['id', 'title', 'slug', 'published', 'tenantId', 'createdAt', 'updatedAt'],
30+
['id', 'title', 'slug', 'published', 'tenantId', 'segmentId', 'createdAt', 'updatedAt'],
3131
{
3232
table: {
3333
table: 'conversations',

services/libs/conversations/src/repo/conversation.repo.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class ConversationRepository extends RepositoryBase<ConversationRepositor
1818

1919
public async createConversation(
2020
tenantId: string,
21+
segmentId: string,
2122
title: string,
2223
published: boolean,
2324
slug: string,
@@ -28,6 +29,7 @@ export class ConversationRepository extends RepositoryBase<ConversationRepositor
2829
{
2930
id,
3031
tenantId,
32+
segmentId,
3133
title,
3234
published,
3335
slug,
@@ -74,12 +76,17 @@ export class ConversationRepository extends RepositoryBase<ConversationRepositor
7476
}
7577
}
7678

77-
public async checkSlugExists(tenantId: string, slug: string): Promise<boolean> {
79+
public async checkSlugExists(
80+
tenantId: string,
81+
segmentId: string,
82+
slug: string,
83+
): Promise<boolean> {
7884
const results = await this.db().any(
79-
`select id from conversations where "tenantId" = $(tenantId) and slug = $(slug)`,
85+
`select id from conversations where "tenantId" = $(tenantId) and slug = $(slug) and "segmentId" = $(segmentId)`,
8086
{
8187
tenantId,
8288
slug,
89+
segmentId,
8390
},
8491
)
8592

@@ -90,33 +97,44 @@ export class ConversationRepository extends RepositoryBase<ConversationRepositor
9097
return false
9198
}
9299

93-
public async getConversation(tenantId: string, id: string): Promise<IDbConversation | null> {
100+
public async getConversation(
101+
tenantId: string,
102+
segmentId: string,
103+
id: string,
104+
): Promise<IDbConversation | null> {
94105
const result = await this.db().oneOrNone(
95-
`select id, published from conversations where "tenantId" = $(tenantId) and id = $(id)`,
106+
`select id, published from conversations where "tenantId" = $(tenantId) and "segmentId" = $(segmentId) and id = $(id)`,
96107
{
97108
tenantId,
98109
id,
110+
segmentId,
99111
},
100112
)
101113

102114
return result
103115
}
104116

105-
public async getActivityData(tenantId: string, activityId: string): Promise<IDbActivityInfo> {
117+
public async getActivityData(
118+
tenantId: string,
119+
segmentId: string,
120+
activityId: string,
121+
): Promise<IDbActivityInfo> {
106122
const results = await this.db().one(
107123
`select id, "conversationId", "sourceId", "sourceParentId", "parentId", platform, body, title, channel
108124
from activities
109-
where "tenantId" = $(tenantId) and id = $(activityId)`,
125+
where "tenantId" = $(tenantId) and id = $(activityId) and "segmentId" = $(segmentId)`,
110126
{
111127
tenantId,
112128
activityId,
129+
segmentId,
113130
},
114131
)
115132
return results
116133
}
117134

118135
public async getActivities(
119136
tenantId: string,
137+
segmentId: string,
120138
sourceParentId: string,
121139
page: number,
122140
perPage: number,
@@ -125,12 +143,13 @@ export class ConversationRepository extends RepositoryBase<ConversationRepositor
125143
`
126144
select id, "conversationId", "sourceId", "sourceParentId", "parentId", platform, body, title, channel
127145
from activities
128-
where "tenantId" = $(tenantId) and "sourceParentId" = $(sourceParentId)
146+
where "tenantId" = $(tenantId) and "sourceParentId" = $(sourceParentId) and "segmentId" = $(segmentId)
129147
limit ${perPage} offset ${(page - 1) * perPage}
130148
`,
131149
{
132150
tenantId,
133151
sourceParentId,
152+
segmentId,
134153
},
135154
)
136155

@@ -139,6 +158,7 @@ export class ConversationRepository extends RepositoryBase<ConversationRepositor
139158

140159
public async setConversationTitleAndSlug(
141160
tenantId: string,
161+
segmentId: string,
142162
id: string,
143163
title: string,
144164
slug: string,
@@ -147,12 +167,13 @@ export class ConversationRepository extends RepositoryBase<ConversationRepositor
147167
`update conversations
148168
set title = $(title),
149169
slug = $(slug)
150-
where id = $(id) and "tenantId" = $(tenantId)`,
170+
where id = $(id) and "tenantId" = $(tenantId) and "segmentId" = $(segmentId)`,
151171
{
152172
tenantId,
153173
id,
154174
title,
155175
slug,
176+
segmentId,
156177
},
157178
)
158179

@@ -161,26 +182,29 @@ export class ConversationRepository extends RepositoryBase<ConversationRepositor
161182

162183
public async setActivityConversationId(
163184
tenantId: string,
185+
segmentId: string,
164186
activityId: string,
165187
conversationId: string,
166188
): Promise<void> {
167189
const result = await this.db().result(
168-
`update activities set "conversationId" = $(conversationId) where id = $(activityId) and "tenantId" = $(tenantId)`,
190+
`update activities set "conversationId" = $(conversationId) where id = $(activityId) and "tenantId" = $(tenantId) and "segmentId" = $(segmentId)`,
169191
{
170192
activityId,
171193
conversationId,
172194
tenantId,
195+
segmentId,
173196
},
174197
)
175198

176199
this.checkUpdateRowCount(result.rowCount, 1)
177200
}
178201

179-
public async getConversationCount(tenantId: string): Promise<number> {
202+
public async getConversationCount(tenantId: string, segmentId: string): Promise<number> {
180203
const result = await this.db().one(
181-
`select count(id) as count from conversations where "tenantId" = $(tenantId)`,
204+
`select count(id) as count from conversations where "tenantId" = $(tenantId) and "segmentId" = $(segmentId)`,
182205
{
183206
tenantId,
207+
segmentId,
184208
},
185209
)
186210

services/libs/conversations/src/service/conversation.service.ts

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ export class ConversationService extends LoggerBase {
1717

1818
private async getConversation(
1919
tenantId: string,
20+
segmentId: string,
2021
id: string,
2122
repo: ConversationRepository,
2223
): Promise<IDbConversation> {
23-
const conversation = await repo.getConversation(tenantId, id)
24+
const conversation = await repo.getConversation(tenantId, segmentId, id)
2425

2526
if (!conversation) {
2627
throw new Error(`Conversation ${id} does not exist!`)
@@ -29,10 +30,15 @@ export class ConversationService extends LoggerBase {
2930
return conversation
3031
}
3132

32-
public async generateTitle(tenantId: string, title: string, isHtml = false): Promise<string> {
33+
public async generateTitle(
34+
tenantId: string,
35+
segmentId: string,
36+
title: string,
37+
isHtml = false,
38+
): Promise<string> {
3339
if (!title && getCleanString(title).length === 0) {
3440
const repo = new ConversationRepository(this.store, this.log)
35-
const count = await repo.getConversationCount(tenantId)
41+
const count = await repo.getConversationCount(tenantId, segmentId)
3642

3743
return `conversation-${count}`
3844
}
@@ -48,7 +54,7 @@ export class ConversationService extends LoggerBase {
4854
}
4955

5056
static readonly MAX_SLUG_WORD_LENGTH = 10
51-
public async generateSlug(tenantId: string, title: string): Promise<string> {
57+
public async generateSlug(tenantId: string, segmentId: string, title: string): Promise<string> {
5258
// Remove non-standart characters and extra whitespaces
5359
const cleanedTitle = getCleanString(title)
5460

@@ -68,7 +74,7 @@ export class ConversationService extends LoggerBase {
6874
// check generated slug already exists in tenant
6975
const repo = new ConversationRepository(this.store, this.log)
7076

71-
let slugExists = await repo.checkSlugExists(tenantId, cleanedSlug)
77+
let slugExists = await repo.checkSlugExists(tenantId, segmentId, cleanedSlug)
7278

7379
// generated slug already exists in the tenant, start adding suffixes and re-check
7480
if (slugExists) {
@@ -78,7 +84,7 @@ export class ConversationService extends LoggerBase {
7884

7985
while (slugExists) {
8086
const suffixedSlug = `${slugCopy}-${suffix}`
81-
slugExists = await repo.checkSlugExists(tenantId, cleanedSlug)
87+
slugExists = await repo.checkSlugExists(tenantId, segmentId, cleanedSlug)
8288
suffix += 1
8389
cleanedSlug = suffixedSlug
8490
}
@@ -88,23 +94,27 @@ export class ConversationService extends LoggerBase {
8894
}
8995

9096
// returns activity ids that were changed
91-
public async processActivity(tenantId: string, activityId: string): Promise<string[]> {
97+
public async processActivity(
98+
tenantId: string,
99+
segmentId: string,
100+
activityId: string,
101+
): Promise<string[]> {
92102
const repo = new ConversationRepository(this.store, this.log)
93103

94-
const activity = await repo.getActivityData(tenantId, activityId)
104+
const activity = await repo.getActivityData(tenantId, segmentId, activityId)
95105

96106
if (activity.parentId) {
97-
const parent = await repo.getActivityData(tenantId, activity.parentId)
98-
return await this.addToConversation(tenantId, activity, parent)
107+
const parent = await repo.getActivityData(tenantId, segmentId, activity.parentId)
108+
return await this.addToConversation(tenantId, segmentId, activity, parent)
99109
} else {
100110
const ids: string[] = []
101111
await processPaginated(
102112
async (page) => {
103-
return repo.getActivities(tenantId, activity.sourceId, page, 10)
113+
return repo.getActivities(tenantId, segmentId, activity.sourceId, page, 10)
104114
},
105115
async (activities) => {
106116
for (const child of activities) {
107-
const results = await this.addToConversation(tenantId, child, activity)
117+
const results = await this.addToConversation(tenantId, segmentId, child, activity)
108118
ids.push(...results)
109119
}
110120
},
@@ -116,6 +126,7 @@ export class ConversationService extends LoggerBase {
116126

117127
public async addToConversation(
118128
tenantId: string,
129+
segmentId: string,
119130
child: IDbActivityInfo,
120131
parent: IDbActivityInfo,
121132
): Promise<string[]> {
@@ -133,43 +144,59 @@ export class ConversationService extends LoggerBase {
133144

134145
// check if parent is in a conversation already
135146
if (parent.conversationId) {
136-
conversation = await this.getConversation(tenantId, parent.conversationId, txRepo)
137-
await txRepo.setActivityConversationId(tenantId, child.id, parent.conversationId)
147+
conversation = await this.getConversation(
148+
tenantId,
149+
segmentId,
150+
parent.conversationId,
151+
txRepo,
152+
)
153+
await txRepo.setActivityConversationId(tenantId, segmentId, child.id, parent.conversationId)
138154
affectedIds.push(child.id)
139155
}
140156
// if child is already in a conversation
141157
else if (child.conversationId) {
142-
conversation = await this.getConversation(tenantId, child.conversationId, txRepo)
158+
conversation = await this.getConversation(tenantId, segmentId, child.conversationId, txRepo)
143159

144160
if (!conversation.published) {
145161
const txService = new ConversationService(txStore, this.log)
146162
const newConversationTitle = await txService.generateTitle(
147163
tenantId,
164+
segmentId,
148165
parent.title || parent.body,
149166
ConversationService.hasHtmlActivities(parent.platform as PlatformType),
150167
)
151168

152-
const newConversationSlug = await txService.generateSlug(tenantId, newConversationTitle)
169+
const newConversationSlug = await txService.generateSlug(
170+
tenantId,
171+
segmentId,
172+
newConversationTitle,
173+
)
153174

154175
await txRepo.setConversationTitleAndSlug(
155176
tenantId,
177+
segmentId,
156178
conversation.id,
157179
newConversationTitle,
158180
newConversationSlug,
159181
)
160182
}
161183

162-
await txRepo.setActivityConversationId(tenantId, parent.id, conversation.id)
184+
await txRepo.setActivityConversationId(tenantId, segmentId, parent.id, conversation.id)
163185
affectedIds.push(parent.id)
164186
} else {
165187
// create a new conversation
166188
const txService = new ConversationService(txStore, this.log)
167189
const conversationTitle = await txService.generateTitle(
168190
tenantId,
191+
segmentId,
169192
parent.title || parent.body,
170193
ConversationService.hasHtmlActivities(parent.platform as PlatformType),
171194
)
172-
const conversationSlug = await txService.generateSlug(tenantId, conversationTitle)
195+
const conversationSlug = await txService.generateSlug(
196+
tenantId,
197+
segmentId,
198+
conversationTitle,
199+
)
173200
const conversationSettings = await txRepo.getConversationSettings(tenantId)
174201
const channel = ConversationService.getChannelFromActivity(
175202
parent.platform as PlatformType,
@@ -184,6 +211,7 @@ export class ConversationService extends LoggerBase {
184211

185212
const conversationId = await txRepo.createConversation(
186213
tenantId,
214+
segmentId,
187215
conversationTitle,
188216
published,
189217
conversationSlug,
@@ -194,8 +222,8 @@ export class ConversationService extends LoggerBase {
194222
published,
195223
}
196224

197-
await txRepo.setActivityConversationId(tenantId, parent.id, conversationId)
198-
await txRepo.setActivityConversationId(tenantId, child.id, conversationId)
225+
await txRepo.setActivityConversationId(tenantId, segmentId, parent.id, conversationId)
226+
await txRepo.setActivityConversationId(tenantId, segmentId, child.id, conversationId)
199227
affectedIds.push(parent.id)
200228
affectedIds.push(child.id)
201229
}

0 commit comments

Comments
 (0)