Skip to content

Commit

Permalink
[backend] rename repository services and replace repository modules b…
Browse files Browse the repository at this point in the history
…y dynamicModule (twentyhq#4536)

* rename database services to repository

* refactor more repositories

* more refactoring

* followup

* remove unused imports

* fix

* fix

* Fix calendar listener being called when flag is off

* remove folders
  • Loading branch information
Weiko authored Mar 18, 2024
1 parent 29537ec commit fcc1bc8
Show file tree
Hide file tree
Showing 79 changed files with 1,080 additions and 776 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@ import { ObjectMetadataModule } from 'src/engine-metadata/object-metadata/object
import { DataSourceModule } from 'src/engine-metadata/data-source/data-source.module';
import { CleanInactiveWorkspaceJob } from 'src/engine/workspace-manager/workspace-cleaner/crons/clean-inactive-workspace.job';
import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
import { MessagingModule } from 'src/modules/messaging/messaging.module';
import { GmailPartialSyncJob } from 'src/modules/messaging/jobs/gmail-partial-sync.job';
import { EmailSenderJob } from 'src/engine/integrations/email/email-sender.job';
import { UserModule } from 'src/engine/modules/user/user.module';
import { EnvironmentModule } from 'src/engine/integrations/environment/environment.module';
import { FetchAllWorkspacesMessagesJob } from 'src/modules/messaging/commands/crons/fetch-all-workspaces-messages.job';
import { ConnectedAccountModule } from 'src/modules/connected-account/repositories/connected-account/connected-account.module';
import { MatchMessageParticipantJob } from 'src/modules/messaging/jobs/match-message-participant.job';
import { CreateCompaniesAndContactsAfterSyncJob } from 'src/modules/messaging/jobs/create-companies-and-contacts-after-sync.job';
import { CreateCompaniesAndContactsModule } from 'src/modules/connected-account/auto-companies-and-contacts-creation/create-company-and-contact/create-company-and-contact.module';
import { MessageChannelModule } from 'src/modules/messaging/repositories/message-channel/message-channel.module';
import { MessageParticipantModule } from 'src/modules/messaging/repositories/message-participant/message-participant.module';
import { DataSeedDemoWorkspaceModule } from 'src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace.module';
import { DataSeedDemoWorkspaceJob } from 'src/database/commands/data-seed-demo-workspace/jobs/data-seed-demo-workspace.job';
import { DeleteConnectedAccountAssociatedMessagingDataJob } from 'src/modules/messaging/jobs/delete-connected-account-associated-messaging-data.job';
Expand All @@ -33,27 +29,31 @@ import { UserWorkspaceModule } from 'src/engine/modules/user-workspace/user-work
import { StripeModule } from 'src/engine/modules/billing/stripe/stripe.module';
import { Workspace } from 'src/engine/modules/workspace/workspace.entity';
import { FeatureFlagEntity } from 'src/engine/modules/feature-flag/feature-flag.entity';
import { CalendarModule } from 'src/modules/calendar/calendar.module';
import { DataSourceEntity } from 'src/engine-metadata/data-source/data-source.entity';
import { GoogleCalendarFullSyncJob } from 'src/modules/calendar/jobs/google-calendar-full-sync.job';
import { CalendarEventCleanerModule } from 'src/modules/calendar/services/calendar-event-cleaner/calendar-event-cleaner.module';
import { RecordPositionBackfillJob } from 'src/engine/api/graphql/workspace-query-runner/jobs/record-position-backfill.job';
import { RecordPositionBackfillModule } from 'src/engine/api/graphql/workspace-query-runner/services/record-position-backfill-module';
import { DeleteConnectedAccountAssociatedCalendarDataJob } from 'src/modules/messaging/jobs/delete-connected-account-associated-calendar-data.job';
import { GoogleCalendarFullSyncModule } from 'src/modules/calendar/services/google-calendar-full-sync.module';
import { GoogleAPIRefreshAccessTokenModule } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.module';
import { GmailFullSyncModule } from 'src/modules/messaging/services/gmail-full-sync/gmail-full-sync.module';
import { GmailPartialSyncModule } from 'src/modules/messaging/services/gmail-partial-sync/gmail-partial-sync.module';
import { MessageParticipantModule } from 'src/modules/messaging/services/message-participant/message-participant.module';
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata';
import { MessageParticipantObjectMetadata } from 'src/modules/messaging/standard-objects/message-participant.object-metadata';
import { MessageChannelObjectMetadata } from 'src/modules/messaging/standard-objects/message-channel.object-metadata';

@Module({
imports: [
BillingModule,
DataSourceModule,
ConnectedAccountModule,
CreateCompaniesAndContactsModule,
DataSeedDemoWorkspaceModule,
EnvironmentModule,
HttpModule,
MessagingModule,
MessageParticipantModule,
MessageChannelModule,
CalendarModule,
GoogleCalendarFullSyncModule,
ObjectMetadataModule,
StripeModule,
ThreadCleanerModule,
Expand All @@ -65,6 +65,15 @@ import { DeleteConnectedAccountAssociatedCalendarDataJob } from 'src/modules/mes
UserWorkspaceModule,
WorkspaceDataSourceModule,
RecordPositionBackfillModule,
GoogleAPIRefreshAccessTokenModule,
GmailFullSyncModule,
GmailPartialSyncModule,
MessageParticipantModule,
ObjectMetadataRepositoryModule.forFeature([
ConnectedAccountObjectMetadata,
MessageParticipantObjectMetadata,
MessageChannelObjectMetadata,
]),
],
providers: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { CalendarChannelEventAssociationRepository } from 'src/modules/calendar/repositories/calendar-channel-event-association.repository';
import { CalendarChannelRepository } from 'src/modules/calendar/repositories/calendar-channel.repository';
import { CalendarEventAttendeeRepository } from 'src/modules/calendar/repositories/calendar-event-attendee.repository';
import { CalendarEventRepository } from 'src/modules/calendar/repositories/calendar-event.repository';
import { CompanyRepository } from 'src/modules/company/repositories/company.repository';
import { BlocklistRepository } from 'src/modules/connected-account/repositories/blocklist.repository';
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
import { MessageChannelMessageAssociationRepository } from 'src/modules/messaging/repositories/message-channel-message-association.repository';
import { MessageChannelRepository } from 'src/modules/messaging/repositories/message-channel.repository';
import { MessageParticipantRepository } from 'src/modules/messaging/repositories/message-participant.repository';
import { MessageThreadRepository } from 'src/modules/messaging/repositories/message-thread.repository';
import { MessageRepository } from 'src/modules/messaging/repositories/message.repository';
import { PersonRepository } from 'src/modules/person/repositories/person.repository';
import { WorkspaceMemberRepository } from 'src/modules/workspace-member/repositories/workspace-member.repository';

export const metadataToRepositoryMapping = {
BlocklistObjectMetadata: BlocklistRepository,
CalendarChannelEventAssociationObjectMetadata:
CalendarChannelEventAssociationRepository,
CalendarChannelObjectMetadata: CalendarChannelRepository,
CalendarEventAttendeeObjectMetadata: CalendarEventAttendeeRepository,
CalendarEventObjectMetadata: CalendarEventRepository,
CompanyObjectMetadata: CompanyRepository,
ConnectedAccountObjectMetadata: ConnectedAccountRepository,
MessageChannelMessageAssociationObjectMetadata:
MessageChannelMessageAssociationRepository,
MessageChannelObjectMetadata: MessageChannelRepository,
MessageObjectMetadata: MessageRepository,
MessageParticipantObjectMetadata: MessageParticipantRepository,
MessageThreadObjectMetadata: MessageThreadRepository,
PersonObjectMetadata: PersonRepository,
WorkspaceMemberObjectMetadata: WorkspaceMemberRepository,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Inject } from '@nestjs/common';

import { convertClassNameToObjectMetadataName } from 'src/engine/workspace-manager/workspace-sync-metadata/utils/convert-class-to-object-metadata-name.util';
import { capitalize } from 'src/utils/capitalize';

export const InjectObjectMetadataRepository = (objectMetadata: any) => {
const token = `${capitalize(
convertClassNameToObjectMetadataName(objectMetadata.name),
)}Repository`;

return Inject(token);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Global, Module, DynamicModule, Provider } from '@nestjs/common';

import { metadataToRepositoryMapping } from 'src/engine/object-metadata-repository/metadata-to-repository.mapping';
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { convertClassNameToObjectMetadataName } from 'src/engine/workspace-manager/workspace-sync-metadata/utils/convert-class-to-object-metadata-name.util';
import { capitalize } from 'src/utils/capitalize';

@Global()
@Module({})
export class ObjectMetadataRepositoryModule {
static forFeature(objectMetadatas): DynamicModule {
const providers: Provider[] = objectMetadatas.map((objectMetadata) => {
const repositoryClass = metadataToRepositoryMapping[objectMetadata.name];

if (!repositoryClass) {
throw new Error(`No repository found for ${objectMetadata.name}`);
}

return {
provide: `${capitalize(
convertClassNameToObjectMetadataName(objectMetadata.name),
)}Repository`,
useFactory: (
workspaceDataSourceService: WorkspaceDataSourceService,
) => {
return new repositoryClass(workspaceDataSourceService);
},
inject: [WorkspaceDataSourceService],
};
});

return {
module: ObjectMetadataRepositoryModule,
imports: [WorkspaceDataSourceModule],
providers: [...providers],
exports: providers,
};
}
}
39 changes: 3 additions & 36 deletions packages/twenty-server/src/modules/calendar/calendar.module.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,8 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { FeatureFlagEntity } from 'src/engine/modules/feature-flag/feature-flag.entity';
import { EnvironmentModule } from 'src/engine/integrations/environment/environment.module';
import { CreateCompaniesAndContactsModule } from 'src/modules/connected-account/auto-companies-and-contacts-creation/create-company-and-contact/create-company-and-contact.module';
import { BlocklistModule } from 'src/modules/connected-account/repositories/blocklist/blocklist.module';
import { ConnectedAccountModule } from 'src/modules/connected-account/repositories/connected-account/connected-account.module';
import { CalendarChannelEventAssociationModule } from 'src/modules/calendar/repositories/calendar-channel-event-association/calendar-channel-event-assocation.module';
import { CalendarChannelModule } from 'src/modules/calendar/repositories/calendar-channel/calendar-channel.module';
import { CalendarEventAttendeeModule } from 'src/modules/calendar/repositories/calendar-event-attendee/calendar-event-attendee.module';
import { CalendarEventModule } from 'src/modules/calendar/repositories/calendar-event/calendar-event.module';
import { CalendarEventCleanerModule } from 'src/modules/calendar/services/calendar-event-cleaner/calendar-event-cleaner.module';
import { GoogleCalendarFullSyncService } from 'src/modules/calendar/services/google-calendar-full-sync.service';
import { GoogleCalendarClientProvider } from 'src/modules/calendar/services/providers/google-calendar/google-calendar.provider';
import { CompanyModule } from 'src/modules/messaging/repositories/company/company.module';
import { PersonModule } from 'src/modules/person/repositories/person/person.module';
import { WorkspaceMemberModule } from 'src/modules/workspace-member/repositories/workspace-member/workspace-member.module';
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';

@Module({
imports: [
EnvironmentModule,
WorkspaceDataSourceModule,
ConnectedAccountModule,
CalendarChannelModule,
CalendarChannelEventAssociationModule,
CalendarEventModule,
CalendarEventAttendeeModule,
CreateCompaniesAndContactsModule,
WorkspaceMemberModule,
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
CompanyModule,
PersonModule,
BlocklistModule,
CalendarEventCleanerModule,
],
providers: [GoogleCalendarFullSyncService, GoogleCalendarClientProvider],
exports: [GoogleCalendarFullSyncService],
imports: [],
providers: [],
exports: [],
})
export class CalendarModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { Command, CommandRunner, Option } from 'nest-commander';

import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { ConnectedAccountService } from 'src/modules/connected-account/repositories/connected-account/connected-account.service';
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
import {
GoogleCalendarFullSyncJobData,
GoogleCalendarFullSyncJob,
} from 'src/modules/calendar/jobs/google-calendar-full-sync.job';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata';

interface GoogleCalendarFullSyncOptions {
workspaceId: string;
Expand All @@ -23,7 +25,8 @@ export class GoogleCalendarFullSyncCommand extends CommandRunner {
constructor(
@Inject(MessageQueue.messagingQueue)
private readonly messageQueueService: MessageQueueService,
private readonly connectedAccountService: ConnectedAccountService,
@InjectObjectMetadataRepository(ConnectedAccountObjectMetadata)
private readonly connectedAccountRepository: ConnectedAccountRepository,
) {
super();
}
Expand All @@ -48,7 +51,7 @@ export class GoogleCalendarFullSyncCommand extends CommandRunner {

private async fetchWorkspaceCalendars(workspaceId: string): Promise<void> {
const connectedAccounts =
await this.connectedAccountService.getAll(workspaceId);
await this.connectedAccountRepository.getAll(workspaceId);

for (const connectedAccount of connectedAccounts) {
await this.messageQueueService.add<GoogleCalendarFullSyncJobData>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { FeatureFlagEntity } from 'src/engine/modules/feature-flag/feature-flag.entity';
import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
import { DataSourceModule } from 'src/engine-metadata/data-source/data-source.module';
import { ConnectedAccountModule } from 'src/modules/connected-account/repositories/connected-account/connected-account.module';
import { GoogleCalendarFullSyncCommand } from 'src/modules/calendar/commands/google-calendar-full-sync.command';
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata';

@Module({
imports: [
DataSourceModule,
TypeORMModule,
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
ConnectedAccountModule,
ObjectMetadataRepositoryModule.forFeature([ConnectedAccountObjectMetadata]),
],
providers: [GoogleCalendarFullSyncCommand],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';

import { MessageQueueJob } from 'src/engine/integrations/message-queue/interfaces/message-queue-job.interface';

import { GoogleAPIsRefreshAccessTokenService } from 'src/modules/connected-account/services/google-apis-refresh-access-token.service';
import { GoogleAPIRefreshAccessTokenService } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.service';
import { GoogleCalendarFullSyncService } from 'src/modules/calendar/services/google-calendar-full-sync.service';

export type GoogleCalendarFullSyncJobData = {
Expand All @@ -18,7 +18,7 @@ export class GoogleCalendarFullSyncJob
private readonly logger = new Logger(GoogleCalendarFullSyncJob.name);

constructor(
private readonly googleAPIsRefreshAccessTokenService: GoogleAPIsRefreshAccessTokenService,
private readonly googleAPIsRefreshAccessTokenService: GoogleAPIRefreshAccessTokenService,
private readonly googleCalendarFullSyncService: GoogleCalendarFullSyncService,
) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CalendarChannelEventAssociationObjectMetadata } from 'src/modules/calen
import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/utils/getFlattenedValuesAndValuesStringForBatchRawQuery.util';

@Injectable()
export class CalendarChannelEventAssociationService {
export class CalendarChannelEventAssociationRepository {
constructor(
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
) {}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CalendarChannelObjectMetadata } from 'src/modules/calendar/standard-obj
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record';

@Injectable()
export class CalendarChannelService {
export class CalendarChannelRepository {
constructor(
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
) {}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/c
import { CalendarEventAttendee } from 'src/modules/calendar/types/calendar-event';

@Injectable()
export class CalendarEventAttendeeService {
export class CalendarEventAttendeeRepository {
constructor(
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
) {}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { CalendarEvent } from 'src/modules/calendar/types/calendar-event';
import { CalendarEventAttendeeObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-event-attendee.object-metadata';

@Injectable()
export class CalendarEventService {
export class CalendarEventRepository {
constructor(
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
) {}
Expand Down
Loading

0 comments on commit fcc1bc8

Please sign in to comment.