Add workspacePreQueryHook module (#3879)
* rebase * reorganise messaging folders * fix * fix after review * fix yarn lock
This commit is contained in:
@ -0,0 +1 @@
|
||||
export const fetchAllWorkspacesMessagesCronPattern = '*/10 * * * *';
|
||||
@ -0,0 +1,65 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { MessageQueueJob } from 'src/integrations/message-queue/interfaces/message-queue-job.interface';
|
||||
|
||||
import {
|
||||
FeatureFlagEntity,
|
||||
FeatureFlagKeys,
|
||||
} from 'src/core/feature-flag/feature-flag.entity';
|
||||
import { MessageQueue } from 'src/integrations/message-queue/message-queue.constants';
|
||||
import { MessageQueueService } from 'src/integrations/message-queue/services/message-queue.service';
|
||||
import { ConnectedAccountService } from 'src/workspace/messaging/repositories/connected-account/connected-account.service';
|
||||
import {
|
||||
GmailPartialSyncJobData,
|
||||
GmailPartialSyncJob,
|
||||
} from 'src/workspace/messaging/jobs/gmail-partial-sync.job';
|
||||
|
||||
@Injectable()
|
||||
export class FetchAllWorkspacesMessagesJob
|
||||
implements MessageQueueJob<undefined>
|
||||
{
|
||||
constructor(
|
||||
@InjectRepository(FeatureFlagEntity, 'core')
|
||||
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
|
||||
@Inject(MessageQueue.messagingQueue)
|
||||
private readonly messageQueueService: MessageQueueService,
|
||||
private readonly connectedAccountService: ConnectedAccountService,
|
||||
) {}
|
||||
|
||||
async handle(): Promise<void> {
|
||||
const featureFlagsWithMessagingEnabled =
|
||||
await this.featureFlagRepository.findBy({
|
||||
key: FeatureFlagKeys.IsMessagingEnabled,
|
||||
value: true,
|
||||
});
|
||||
|
||||
const workspaceIds = featureFlagsWithMessagingEnabled.map(
|
||||
(featureFlag) => featureFlag.workspaceId,
|
||||
);
|
||||
|
||||
for (const workspaceId of workspaceIds) {
|
||||
await this.fetchWorkspaceMessages(workspaceId);
|
||||
}
|
||||
}
|
||||
|
||||
private async fetchWorkspaceMessages(workspaceId: string): Promise<void> {
|
||||
const connectedAccounts =
|
||||
await this.connectedAccountService.getAll(workspaceId);
|
||||
|
||||
for (const connectedAccount of connectedAccounts) {
|
||||
await this.messageQueueService.add<GmailPartialSyncJobData>(
|
||||
GmailPartialSyncJob.name,
|
||||
{
|
||||
workspaceId,
|
||||
connectedAccountId: connectedAccount.id,
|
||||
},
|
||||
{
|
||||
retryLimit: 2,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -6,7 +6,7 @@ import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
|
||||
import { DataSourceModule } from 'src/metadata/data-source/data-source.module';
|
||||
import { GmailFullSyncCommand } from 'src/workspace/messaging/commands/gmail-full-sync.command';
|
||||
import { GmailPartialSyncCommand } from 'src/workspace/messaging/commands/gmail-partial-sync.command';
|
||||
import { ConnectedAccountModule } from 'src/workspace/messaging/connected-account/connected-account.module';
|
||||
import { ConnectedAccountModule } from 'src/workspace/messaging/repositories/connected-account/connected-account.module';
|
||||
import { StartFetchAllWorkspacesMessagesCronCommand } from 'src/workspace/messaging/commands/start-fetch-all-workspaces-messages.cron.command';
|
||||
import { StopFetchAllWorkspacesMessagesCronCommand } from 'src/workspace/messaging/commands/stop-fetch-all-workspaces-messages.cron.command';
|
||||
|
||||
|
||||
@ -14,7 +14,7 @@ import {
|
||||
GmailFullSyncJobData,
|
||||
GmailFullSyncJob,
|
||||
} from 'src/workspace/messaging/jobs/gmail-full-sync.job';
|
||||
import { ConnectedAccountService } from 'src/workspace/messaging/connected-account/connected-account.service';
|
||||
import { ConnectedAccountService } from 'src/workspace/messaging/repositories/connected-account/connected-account.service';
|
||||
|
||||
interface GmailFullSyncOptions {
|
||||
workspaceId: string;
|
||||
|
||||
@ -14,7 +14,7 @@ import {
|
||||
GmailPartialSyncJob,
|
||||
GmailPartialSyncJobData,
|
||||
} from 'src/workspace/messaging/jobs/gmail-partial-sync.job';
|
||||
import { ConnectedAccountService } from 'src/workspace/messaging/connected-account/connected-account.service';
|
||||
import { ConnectedAccountService } from 'src/workspace/messaging/repositories/connected-account/connected-account.service';
|
||||
|
||||
interface GmailPartialSyncOptions {
|
||||
workspaceId: string;
|
||||
|
||||
@ -4,8 +4,8 @@ import { Command, CommandRunner } from 'nest-commander';
|
||||
|
||||
import { MessageQueue } from 'src/integrations/message-queue/message-queue.constants';
|
||||
import { MessageQueueService } from 'src/integrations/message-queue/services/message-queue.service';
|
||||
import { fetchAllWorkspacesMessagesCronPattern } from 'src/workspace/messaging/crons/fetch-all-workspaces-messages.cron.pattern';
|
||||
import { FetchAllWorkspacesMessagesJob } from 'src/workspace/messaging/crons/fetch-all-workspaces-messages.job';
|
||||
import { fetchAllWorkspacesMessagesCronPattern } from 'src/workspace/messaging/commands/crons/fetch-all-workspaces-messages.cron.pattern';
|
||||
import { FetchAllWorkspacesMessagesJob } from 'src/workspace/messaging/commands/crons/fetch-all-workspaces-messages.job';
|
||||
|
||||
@Command({
|
||||
name: 'fetch-all-workspaces-messages:cron:start',
|
||||
|
||||
@ -4,8 +4,8 @@ import { Command, CommandRunner } from 'nest-commander';
|
||||
|
||||
import { MessageQueue } from 'src/integrations/message-queue/message-queue.constants';
|
||||
import { MessageQueueService } from 'src/integrations/message-queue/services/message-queue.service';
|
||||
import { fetchAllWorkspacesMessagesCronPattern } from 'src/workspace/messaging/crons/fetch-all-workspaces-messages.cron.pattern';
|
||||
import { FetchAllWorkspacesMessagesJob } from 'src/workspace/messaging/crons/fetch-all-workspaces-messages.job';
|
||||
import { fetchAllWorkspacesMessagesCronPattern } from 'src/workspace/messaging/commands/crons/fetch-all-workspaces-messages.cron.pattern';
|
||||
import { FetchAllWorkspacesMessagesJob } from 'src/workspace/messaging/commands/crons/fetch-all-workspaces-messages.job';
|
||||
|
||||
@Command({
|
||||
name: 'fetch-all-workspaces-messages:cron:stop',
|
||||
|
||||
Reference in New Issue
Block a user