feat: drop calendar repository (#5824)
This PR is replacing and removing all the raw queries and repositories with the new `TwentyORM` and injection system using `@InjectWorkspaceRepository`. Some logic that was contained inside repositories has been moved to the services. In this PR we're only replacing repositories for calendar feature. --------- Co-authored-by: Weiko <corentin@twenty.com> Co-authored-by: bosiraphael <raphael.bosi@gmail.com> Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -1,14 +1,17 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
|
||||
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
|
||||
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
|
||||
import { AddPersonIdAndWorkspaceMemberIdModule } from 'src/modules/calendar-messaging-participant/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.module';
|
||||
import { CalendarEventParticipantService } from 'src/modules/calendar/services/calendar-event-participant/calendar-event-participant.service';
|
||||
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/standard-objects/calendar-event-participant.workspace-entity';
|
||||
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
WorkspaceDataSourceModule,
|
||||
TwentyORMModule.forFeature([CalendarEventParticipantWorkspaceEntity]),
|
||||
ObjectMetadataRepositoryModule.forFeature([PersonWorkspaceEntity]),
|
||||
AddPersonIdAndWorkspaceMemberIdModule,
|
||||
],
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
|
||||
import { EntityManager } from 'typeorm';
|
||||
import { Any, EntityManager } from 'typeorm';
|
||||
|
||||
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
|
||||
import { PersonRepository } from 'src/modules/person/repositories/person.repository';
|
||||
@ -9,17 +9,18 @@ import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/perso
|
||||
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
|
||||
import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/utils/get-flattened-values-and-values-string-for-batch-raw-query.util';
|
||||
import { CalendarEventParticipant } from 'src/modules/calendar/types/calendar-event';
|
||||
import { CalendarEventParticipantRepository } from 'src/modules/calendar/repositories/calendar-event-participant.repository';
|
||||
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/standard-objects/calendar-event-participant.workspace-entity';
|
||||
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
|
||||
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record';
|
||||
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
|
||||
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
||||
|
||||
@Injectable()
|
||||
export class CalendarEventParticipantService {
|
||||
constructor(
|
||||
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
|
||||
@InjectObjectMetadataRepository(CalendarEventParticipantWorkspaceEntity)
|
||||
private readonly calendarEventParticipantRepository: CalendarEventParticipantRepository,
|
||||
@InjectWorkspaceRepository(CalendarEventParticipantWorkspaceEntity)
|
||||
private readonly calendarEventParticipantRepository: WorkspaceRepository<CalendarEventParticipantWorkspaceEntity>,
|
||||
@InjectObjectMetadataRepository(PersonWorkspaceEntity)
|
||||
private readonly personRepository: PersonRepository,
|
||||
private readonly addPersonIdAndWorkspaceMemberIdService: AddPersonIdAndWorkspaceMemberIdService,
|
||||
@ -31,11 +32,11 @@ export class CalendarEventParticipantService {
|
||||
workspaceId: string,
|
||||
transactionManager?: EntityManager,
|
||||
): Promise<ObjectRecord<CalendarEventParticipantWorkspaceEntity>[]> {
|
||||
const participants =
|
||||
await this.calendarEventParticipantRepository.getByHandles(
|
||||
createdPeople.map((person) => person.email),
|
||||
workspaceId,
|
||||
);
|
||||
const participants = await this.calendarEventParticipantRepository.find({
|
||||
where: {
|
||||
handle: Any(createdPeople.map((person) => person.email)),
|
||||
},
|
||||
});
|
||||
|
||||
if (!participants) return [];
|
||||
|
||||
@ -132,33 +133,50 @@ export class CalendarEventParticipantService {
|
||||
workspaceMemberId?: string,
|
||||
) {
|
||||
const calendarEventParticipantsToUpdate =
|
||||
await this.calendarEventParticipantRepository.getByHandles(
|
||||
[email],
|
||||
workspaceId,
|
||||
);
|
||||
await this.calendarEventParticipantRepository.find({
|
||||
where: {
|
||||
handle: email,
|
||||
},
|
||||
});
|
||||
|
||||
const calendarEventParticipantIdsToUpdate =
|
||||
calendarEventParticipantsToUpdate.map((participant) => participant.id);
|
||||
|
||||
if (personId) {
|
||||
await this.calendarEventParticipantRepository.update(
|
||||
{
|
||||
id: Any(calendarEventParticipantIdsToUpdate),
|
||||
},
|
||||
{
|
||||
person: {
|
||||
id: personId,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const updatedCalendarEventParticipants =
|
||||
await this.calendarEventParticipantRepository.updateParticipantsPersonIdAndReturn(
|
||||
calendarEventParticipantIdsToUpdate,
|
||||
personId,
|
||||
workspaceId,
|
||||
);
|
||||
await this.calendarEventParticipantRepository.find({
|
||||
where: {
|
||||
id: Any(calendarEventParticipantIdsToUpdate),
|
||||
},
|
||||
});
|
||||
|
||||
this.eventEmitter.emit(`calendarEventParticipant.matched`, {
|
||||
workspaceId,
|
||||
userId: null,
|
||||
workspaceMemberId: null,
|
||||
calendarEventParticipants: updatedCalendarEventParticipants,
|
||||
});
|
||||
}
|
||||
if (workspaceMemberId) {
|
||||
await this.calendarEventParticipantRepository.updateParticipantsWorkspaceMemberId(
|
||||
calendarEventParticipantIdsToUpdate,
|
||||
workspaceMemberId,
|
||||
workspaceId,
|
||||
await this.calendarEventParticipantRepository.update(
|
||||
{
|
||||
id: Any(calendarEventParticipantIdsToUpdate),
|
||||
},
|
||||
{
|
||||
workspaceMember: {
|
||||
id: workspaceMemberId,
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -170,15 +188,23 @@ export class CalendarEventParticipantService {
|
||||
workspaceMemberId?: string,
|
||||
) {
|
||||
if (personId) {
|
||||
await this.calendarEventParticipantRepository.removePersonIdByHandle(
|
||||
handle,
|
||||
workspaceId,
|
||||
await this.calendarEventParticipantRepository.update(
|
||||
{
|
||||
handle,
|
||||
},
|
||||
{
|
||||
person: null,
|
||||
},
|
||||
);
|
||||
}
|
||||
if (workspaceMemberId) {
|
||||
await this.calendarEventParticipantRepository.removeWorkspaceMemberIdByHandle(
|
||||
handle,
|
||||
workspaceId,
|
||||
await this.calendarEventParticipantRepository.update(
|
||||
{
|
||||
handle,
|
||||
},
|
||||
{
|
||||
workspaceMember: null,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user