Add fetch connected account job (#3313)
* Add fetch connected account job * add featureFlag check
This commit is contained in:
+51
-10
@@ -1,6 +1,11 @@
|
||||
import { Command, CommandRunner, Option } from 'nest-commander';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { FetchWorkspaceMessagesService } from 'src/workspace/messaging/services/fetch-workspace-messages.service';
|
||||
import { Command, CommandRunner, Option } from 'nest-commander';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { FeatureFlagEntity } from 'src/core/feature-flag/feature-flag.entity';
|
||||
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
|
||||
import { DataSourceService } from 'src/metadata/data-source/data-source.service';
|
||||
import { MessagingProducer } from 'src/workspace/messaging/producers/messaging-producer';
|
||||
|
||||
interface FetchWorkspaceMessagesOptions {
|
||||
@@ -13,8 +18,12 @@ interface FetchWorkspaceMessagesOptions {
|
||||
})
|
||||
export class FetchWorkspaceMessagesCommand extends CommandRunner {
|
||||
constructor(
|
||||
private readonly fetchWorkspaceMessagesService: FetchWorkspaceMessagesService,
|
||||
private readonly dataSourceService: DataSourceService,
|
||||
private readonly typeORMService: TypeORMService,
|
||||
private readonly messagingProducer: MessagingProducer,
|
||||
|
||||
@InjectRepository(FeatureFlagEntity, 'core')
|
||||
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
@@ -23,14 +32,17 @@ export class FetchWorkspaceMessagesCommand extends CommandRunner {
|
||||
_passedParam: string[],
|
||||
options: FetchWorkspaceMessagesOptions,
|
||||
): Promise<void> {
|
||||
await this.messagingProducer.enqueueFetchMessages(
|
||||
{ workspaceId: options.workspaceId },
|
||||
options.workspaceId,
|
||||
);
|
||||
const isMessagingEnabled = await this.featureFlagRepository.findOneBy({
|
||||
workspaceId: options.workspaceId,
|
||||
key: 'IS_MESSAGING_ENABLED',
|
||||
value: true,
|
||||
});
|
||||
|
||||
await this.fetchWorkspaceMessagesService.fetchWorkspaceMessages(
|
||||
options.workspaceId,
|
||||
);
|
||||
if (!isMessagingEnabled) {
|
||||
throw new Error('Messaging is not enabled for this workspace');
|
||||
}
|
||||
|
||||
await this.fetchWorkspaceMessages(options.workspaceId);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -43,4 +55,33 @@ export class FetchWorkspaceMessagesCommand extends CommandRunner {
|
||||
parseWorkspaceId(value: string): string {
|
||||
return value;
|
||||
}
|
||||
|
||||
private async fetchWorkspaceMessages(workspaceId: string): Promise<void> {
|
||||
const dataSourceMetadata =
|
||||
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceIdOrFail(
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
const workspaceDataSource =
|
||||
await this.typeORMService.connectToDataSource(dataSourceMetadata);
|
||||
|
||||
if (!workspaceDataSource) {
|
||||
throw new Error('No workspace data source found');
|
||||
}
|
||||
|
||||
const connectedAccounts = await workspaceDataSource?.query(
|
||||
`SELECT * FROM ${dataSourceMetadata.schema}."connectedAccount" WHERE "provider" = 'gmail'`,
|
||||
);
|
||||
|
||||
if (!connectedAccounts || connectedAccounts.length === 0) {
|
||||
throw new Error('No connected account found');
|
||||
}
|
||||
|
||||
for (const connectedAccount of connectedAccounts) {
|
||||
await this.messagingProducer.enqueueFetchAllMessagesFromConnectedAccount(
|
||||
{ workspaceId, connectedAccountId: connectedAccount.id },
|
||||
`${workspaceId}-${connectedAccount.id}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user