-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3889 activate settingsaccountsemailsinboxsettings (#3962)
* update email visibility in settings * improve styling * Add contact auto creation toggle to inbox settings * re move soonpill * update Icon * create job * Add logic to create contacts and companies for message participants without personId and workspaceMemberId * add listener * wip * wip * refactoring * improve structure * Add isContactAutoCreationEnabled method to MessageChannelService * wip * wip * clean * add job * fix bug * contact creation is working * wip * working * improve code * improve typing * resolve conflicts * fix * create company repository * move util * wip * fix
- Loading branch information
1 parent
0b2ffb0
commit 94ad0e3
Showing
27 changed files
with
608 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...wenty-server/src/workspace/messaging/jobs/create-companies-and-contacts-after-sync.job.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
|
||
import { CreateContactsAndCompaniesAfterSyncJobData } from 'packages/twenty-server/dist/src/workspace/messaging/jobs/create-contacts-and-companies-after-sync.job'; | ||
|
||
import { MessageQueueJob } from 'src/integrations/message-queue/interfaces/message-queue-job.interface'; | ||
|
||
import { CreateCompaniesAndContactsService } from 'src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.service'; | ||
import { MessageChannelService } from 'src/workspace/messaging/repositories/message-channel/message-channel.service'; | ||
import { MessageParticipantService } from 'src/workspace/messaging/repositories/message-participant/message-participant.service'; | ||
|
||
export type CreateCompaniesAndContactsAfterSyncJobData = { | ||
workspaceId: string; | ||
messageChannelId: string; | ||
}; | ||
|
||
@Injectable() | ||
export class CreateCompaniesAndContactsAfterSyncJob | ||
implements MessageQueueJob<CreateCompaniesAndContactsAfterSyncJobData> | ||
{ | ||
private readonly logger = new Logger( | ||
CreateCompaniesAndContactsAfterSyncJob.name, | ||
); | ||
constructor( | ||
private readonly createCompaniesAndContactsService: CreateCompaniesAndContactsService, | ||
private readonly messageChannelService: MessageChannelService, | ||
private readonly messageParticipantService: MessageParticipantService, | ||
) {} | ||
|
||
async handle( | ||
data: CreateContactsAndCompaniesAfterSyncJobData, | ||
): Promise<void> { | ||
this.logger.log( | ||
`create contacts and companies after sync for workspace ${data.workspaceId} and messageChannel ${data.messageChannelId}`, | ||
); | ||
const { workspaceId, messageChannelId } = data; | ||
|
||
const isContactAutoCreationEnabled = | ||
await this.messageChannelService.getIsContactAutoCreationEnabledByMessageChannelId( | ||
messageChannelId, | ||
workspaceId, | ||
); | ||
|
||
if (!isContactAutoCreationEnabled) { | ||
return; | ||
} | ||
|
||
const messageParticipantsWithoutPersonIdAndWorkspaceMemberId = | ||
await this.messageParticipantService.getByMessageChannelIdWithoutPersonIdAndWorkspaceMemberId( | ||
messageChannelId, | ||
workspaceId, | ||
); | ||
|
||
await this.createCompaniesAndContactsService.createCompaniesAndContacts( | ||
messageParticipantsWithoutPersonIdAndWorkspaceMemberId, | ||
workspaceId, | ||
); | ||
|
||
await this.messageParticipantService.updateMessageParticipantsAfterPeopleCreation( | ||
messageParticipantsWithoutPersonIdAndWorkspaceMemberId, | ||
workspaceId, | ||
); | ||
|
||
this.logger.log( | ||
`create contacts and companies after sync for workspace ${data.workspaceId} and messageChannel ${data.messageChannelId} done`, | ||
); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...nty-server/src/workspace/messaging/listeners/is-contact-auto-creation-enabled-listener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Injectable, Inject } from '@nestjs/common'; | ||
import { OnEvent } from '@nestjs/event-emitter'; | ||
|
||
import { CreateContactsAndCompaniesAfterSyncJobData } from 'packages/twenty-server/dist/src/workspace/messaging/jobs/create-contacts-and-companies-after-sync.job'; | ||
|
||
import { ObjectRecordUpdateEvent } from 'src/integrations/event-emitter/types/object-record-update.event'; | ||
import { MessageQueue } from 'src/integrations/message-queue/message-queue.constants'; | ||
import { MessageQueueService } from 'src/integrations/message-queue/services/message-queue.service'; | ||
import { MessageChannelObjectMetadata } from 'src/workspace/workspace-sync-metadata/standard-objects/message-channel.object-metadata'; | ||
import { objectRecordChangedProperties as objectRecordUpdateEventChangedProperties } from 'src/integrations/event-emitter/utils/object-record-changed-properties.util'; | ||
import { CreateCompaniesAndContactsAfterSyncJob } from 'src/workspace/messaging/jobs/create-companies-and-contacts-after-sync.job'; | ||
|
||
@Injectable() | ||
export class IsContactAutoCreationEnabledListener { | ||
constructor( | ||
@Inject(MessageQueue.messagingQueue) | ||
private readonly messageQueueService: MessageQueueService, | ||
) {} | ||
|
||
@OnEvent('messageChannel.updated') | ||
handleUpdatedEvent( | ||
payload: ObjectRecordUpdateEvent<MessageChannelObjectMetadata>, | ||
) { | ||
if ( | ||
objectRecordUpdateEventChangedProperties( | ||
payload.previousRecord, | ||
payload.updatedRecord, | ||
).includes('isContactAutoCreationEnabled') && | ||
payload.updatedRecord.isContactAutoCreationEnabled | ||
) { | ||
this.messageQueueService.add<CreateContactsAndCompaniesAfterSyncJobData>( | ||
CreateCompaniesAndContactsAfterSyncJob.name, | ||
{ | ||
workspaceId: payload.workspaceId, | ||
messageChannelId: payload.updatedRecord.id, | ||
}, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/twenty-server/src/workspace/messaging/repositories/company/company.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { CompanyService } from 'src/workspace/messaging/repositories/company/company.service'; | ||
import { WorkspaceDataSourceModule } from 'src/workspace/workspace-datasource/workspace-datasource.module'; | ||
|
||
// TODO: Move outside of the messaging module | ||
@Module({ | ||
imports: [WorkspaceDataSourceModule], | ||
providers: [CompanyService], | ||
exports: [CompanyService], | ||
}) | ||
export class CompanyModule {} |
52 changes: 52 additions & 0 deletions
52
packages/twenty-server/src/workspace/messaging/repositories/company/company.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { EntityManager } from 'typeorm'; | ||
|
||
import { WorkspaceDataSourceService } from 'src/workspace/workspace-datasource/workspace-datasource.service'; | ||
|
||
// TODO: Move outside of the messaging module | ||
@Injectable() | ||
export class CompanyService { | ||
constructor( | ||
private readonly workspaceDataSourceService: WorkspaceDataSourceService, | ||
) {} | ||
|
||
public async getExistingCompaniesByDomainNames( | ||
domainNames: string[], | ||
workspaceId: string, | ||
transactionManager?: EntityManager, | ||
): Promise<{ id: string; domainName: string }[]> { | ||
const dataSourceSchema = | ||
this.workspaceDataSourceService.getSchemaName(workspaceId); | ||
|
||
const existingCompanies = | ||
await this.workspaceDataSourceService.executeRawQuery( | ||
`SELECT id, "domainName" FROM ${dataSourceSchema}.company WHERE "domainName" = ANY($1)`, | ||
[domainNames], | ||
workspaceId, | ||
transactionManager, | ||
); | ||
|
||
return existingCompanies; | ||
} | ||
|
||
public async createCompany( | ||
id: string, | ||
name: string, | ||
domainName: string, | ||
city: string, | ||
workspaceId: string, | ||
transactionManager?: EntityManager, | ||
): Promise<void> { | ||
const dataSourceSchema = | ||
this.workspaceDataSourceService.getSchemaName(workspaceId); | ||
|
||
await this.workspaceDataSourceService.executeRawQuery( | ||
`INSERT INTO ${dataSourceSchema}.company (id, name, "domainName", address) | ||
VALUES ($1, $2, $3, $4)`, | ||
[id, name, domainName, city], | ||
workspaceId, | ||
transactionManager, | ||
); | ||
} | ||
} |
Oops, something went wrong.