fix: multiple twenty orm issues & show an example of use (#5439)
This PR is fixing some issues and adding enhancement in TwentyORM: - [x] Composite fields in nested relations are not formatted properly - [x] Passing operators like `Any` in `where` condition is breaking the query - [x] Ability to auto load workspace-entities based on a regex path I've also introduced an example of use for `CalendarEventService`: https://github.com/twentyhq/twenty/pull/5439/files#diff-3a7dffc0dea57345d10e70c648e911f98fe237248bcea124dafa9c8deb1db748R15
This commit is contained in:
@ -24,9 +24,9 @@ import { FileUploadModule } from 'src/engine/core-modules/file/file-upload/file-
|
||||
import { MicrosoftAuthController } from 'src/engine/core-modules/auth/controllers/microsoft-auth.controller';
|
||||
import { AppTokenService } from 'src/engine/core-modules/app-token/services/app-token.service';
|
||||
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 { MessageChannelObjectMetadata } from 'src/modules/messaging/standard-objects/message-channel.object-metadata';
|
||||
import { CalendarChannelObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-channel.object-metadata';
|
||||
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||
import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/standard-objects/message-channel.workspace-entity';
|
||||
import { CalendarChannelWorkspaceEntity } from 'src/modules/calendar/standard-objects/calendar-channel.workspace-entity';
|
||||
|
||||
import { AuthResolver } from './auth.resolver';
|
||||
|
||||
@ -57,9 +57,9 @@ const jwtModule = JwtModule.registerAsync({
|
||||
'core',
|
||||
),
|
||||
ObjectMetadataRepositoryModule.forFeature([
|
||||
ConnectedAccountObjectMetadata,
|
||||
MessageChannelObjectMetadata,
|
||||
CalendarChannelObjectMetadata,
|
||||
ConnectedAccountWorkspaceEntity,
|
||||
MessageChannelWorkspaceEntity,
|
||||
CalendarChannelWorkspaceEntity,
|
||||
]),
|
||||
HttpModule,
|
||||
UserWorkspaceModule,
|
||||
|
||||
@ -17,20 +17,20 @@ import {
|
||||
} from 'src/modules/calendar/jobs/google-calendar-sync.job';
|
||||
import { CalendarChannelRepository } from 'src/modules/calendar/repositories/calendar-channel.repository';
|
||||
import {
|
||||
CalendarChannelObjectMetadata,
|
||||
CalendarChannelWorkspaceEntity,
|
||||
CalendarChannelVisibility,
|
||||
} from 'src/modules/calendar/standard-objects/calendar-channel.object-metadata';
|
||||
} from 'src/modules/calendar/standard-objects/calendar-channel.workspace-entity';
|
||||
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
|
||||
import {
|
||||
ConnectedAccountObjectMetadata,
|
||||
ConnectedAccountWorkspaceEntity,
|
||||
ConnectedAccountProvider,
|
||||
} from 'src/modules/connected-account/standard-objects/connected-account.object-metadata';
|
||||
} from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||
import { MessageChannelRepository } from 'src/modules/messaging/repositories/message-channel.repository';
|
||||
import {
|
||||
MessageChannelObjectMetadata,
|
||||
MessageChannelWorkspaceEntity,
|
||||
MessageChannelType,
|
||||
MessageChannelVisibility,
|
||||
} from 'src/modules/messaging/standard-objects/message-channel.object-metadata';
|
||||
} from 'src/modules/messaging/standard-objects/message-channel.workspace-entity';
|
||||
import {
|
||||
GmailFullSyncJobData,
|
||||
GmailFullSyncJob,
|
||||
@ -48,11 +48,11 @@ export class GoogleAPIsService {
|
||||
private readonly environmentService: EnvironmentService,
|
||||
@InjectRepository(FeatureFlagEntity, 'core')
|
||||
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
|
||||
@InjectObjectMetadataRepository(ConnectedAccountObjectMetadata)
|
||||
@InjectObjectMetadataRepository(ConnectedAccountWorkspaceEntity)
|
||||
private readonly connectedAccountRepository: ConnectedAccountRepository,
|
||||
@InjectObjectMetadataRepository(MessageChannelObjectMetadata)
|
||||
@InjectObjectMetadataRepository(MessageChannelWorkspaceEntity)
|
||||
private readonly messageChannelRepository: MessageChannelRepository,
|
||||
@InjectObjectMetadataRepository(CalendarChannelObjectMetadata)
|
||||
@InjectObjectMetadataRepository(CalendarChannelWorkspaceEntity)
|
||||
private readonly calendarChannelRepository: CalendarChannelRepository,
|
||||
) {}
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
||||
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
|
||||
import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event';
|
||||
import { WorkspaceMemberObjectMetadata } from 'src/modules/workspace-member/standard-objects/workspace-member.object-metadata';
|
||||
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
||||
import {
|
||||
UpdateSubscriptionJob,
|
||||
UpdateSubscriptionJobData,
|
||||
@ -22,7 +22,7 @@ export class BillingWorkspaceMemberListener {
|
||||
@OnEvent('workspaceMember.created')
|
||||
@OnEvent('workspaceMember.deleted')
|
||||
async handleCreateOrDeleteEvent(
|
||||
payload: ObjectRecordCreateEvent<WorkspaceMemberObjectMetadata>,
|
||||
payload: ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>,
|
||||
) {
|
||||
if (!this.environmentService.get('IS_BILLING_ENABLED')) {
|
||||
return;
|
||||
|
||||
@ -1,12 +1,20 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
|
||||
import { UserModule } from 'src/engine/core-modules/user/user.module';
|
||||
import { TimelineCalendarEventResolver } from 'src/engine/core-modules/calendar/timeline-calendar-event.resolver';
|
||||
import { TimelineCalendarEventService } from 'src/engine/core-modules/calendar/timeline-calendar-event.service';
|
||||
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
|
||||
import { CalendarEventWorkspaceEntity } from 'src/modules/calendar/standard-objects/calendar-event.workspace-entity';
|
||||
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||
|
||||
@Module({
|
||||
imports: [WorkspaceDataSourceModule, UserModule],
|
||||
imports: [
|
||||
TwentyORMModule.forFeature([
|
||||
CalendarEventWorkspaceEntity,
|
||||
PersonWorkspaceEntity,
|
||||
]),
|
||||
UserModule,
|
||||
],
|
||||
exports: [],
|
||||
providers: [TimelineCalendarEventResolver, TimelineCalendarEventService],
|
||||
})
|
||||
|
||||
@ -5,14 +5,11 @@ import { Max } from 'class-validator';
|
||||
|
||||
import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
import { AuthUser } from 'src/engine/decorators/auth/auth-user.decorator';
|
||||
import { AuthWorkspace } from 'src/engine/decorators/auth/auth-workspace.decorator';
|
||||
import { JwtAuthGuard } from 'src/engine/guards/jwt.auth.guard';
|
||||
import { TIMELINE_CALENDAR_EVENTS_MAX_PAGE_SIZE } from 'src/engine/core-modules/calendar/constants/calendar.constants';
|
||||
import { TimelineCalendarEventsWithTotal } from 'src/engine/core-modules/calendar/dtos/timeline-calendar-events-with-total.dto';
|
||||
import { TimelineCalendarEventService } from 'src/engine/core-modules/calendar/timeline-calendar-event.service';
|
||||
import { UserService } from 'src/engine/core-modules/user/services/user.service';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { NotFoundError } from 'src/engine/utils/graphql-errors.util';
|
||||
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
|
||||
|
||||
@ArgsType()
|
||||
@ -51,21 +48,12 @@ export class TimelineCalendarEventResolver {
|
||||
|
||||
@Query(() => TimelineCalendarEventsWithTotal)
|
||||
async getTimelineCalendarEventsFromPersonId(
|
||||
@AuthWorkspace() { id: workspaceId }: Workspace,
|
||||
@AuthUser() user: User,
|
||||
@Args()
|
||||
{ personId, page, pageSize }: GetTimelineCalendarEventsFromPersonIdArgs,
|
||||
) {
|
||||
const workspaceMember = await this.userService.loadWorkspaceMember(user);
|
||||
|
||||
if (!workspaceMember) {
|
||||
throw new NotFoundError('Workspace member not found');
|
||||
}
|
||||
|
||||
const timelineCalendarEvents =
|
||||
await this.timelineCalendarEventService.getCalendarEventsFromPersonIds(
|
||||
workspaceMember.id,
|
||||
workspaceId,
|
||||
[personId],
|
||||
page,
|
||||
pageSize,
|
||||
@ -76,21 +64,12 @@ export class TimelineCalendarEventResolver {
|
||||
|
||||
@Query(() => TimelineCalendarEventsWithTotal)
|
||||
async getTimelineCalendarEventsFromCompanyId(
|
||||
@AuthWorkspace() { id: workspaceId }: Workspace,
|
||||
@AuthUser() user: User,
|
||||
@Args()
|
||||
{ companyId, page, pageSize }: GetTimelineCalendarEventsFromCompanyIdArgs,
|
||||
) {
|
||||
const workspaceMember = await this.userService.loadWorkspaceMember(user);
|
||||
|
||||
if (!workspaceMember) {
|
||||
throw new NotFoundError('Workspace member not found');
|
||||
}
|
||||
|
||||
const timelineCalendarEvents =
|
||||
await this.timelineCalendarEventService.getCalendarEventsFromCompanyId(
|
||||
workspaceMember.id,
|
||||
workspaceId,
|
||||
companyId,
|
||||
page,
|
||||
pageSize,
|
||||
|
||||
@ -1,272 +1,159 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import groupBy from 'lodash.groupby';
|
||||
import { Any } from 'typeorm';
|
||||
import omit from 'lodash.omit';
|
||||
|
||||
import { TIMELINE_CALENDAR_EVENTS_DEFAULT_PAGE_SIZE } from 'src/engine/core-modules/calendar/constants/calendar.constants';
|
||||
import { TimelineCalendarEventParticipant } from 'src/engine/core-modules/calendar/dtos/timeline-calendar-event-participant.dto';
|
||||
import {
|
||||
TimelineCalendarEvent,
|
||||
TimelineCalendarEventVisibility,
|
||||
} from 'src/engine/core-modules/calendar/dtos/timeline-calendar-event.dto';
|
||||
import { TimelineCalendarEventVisibility } from 'src/engine/core-modules/calendar/dtos/timeline-calendar-event.dto';
|
||||
import { TimelineCalendarEventsWithTotal } from 'src/engine/core-modules/calendar/dtos/timeline-calendar-events-with-total.dto';
|
||||
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
|
||||
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record';
|
||||
import { CalendarEventParticipantObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-event-participant.object-metadata';
|
||||
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
|
||||
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
||||
import { CalendarEventWorkspaceEntity } from 'src/modules/calendar/standard-objects/calendar-event.workspace-entity';
|
||||
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||
|
||||
type TimelineCalendarEventParticipantWithPersonInformation =
|
||||
ObjectRecord<CalendarEventParticipantObjectMetadata> & {
|
||||
personFirstName: string;
|
||||
personLastName: string;
|
||||
personAvatarUrl: string;
|
||||
workspaceMemberFirstName: string;
|
||||
workspaceMemberLastName: string;
|
||||
workspaceMemberAvatarUrl: string;
|
||||
};
|
||||
@Injectable()
|
||||
export class TimelineCalendarEventService {
|
||||
constructor(
|
||||
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
|
||||
@InjectWorkspaceRepository(CalendarEventWorkspaceEntity)
|
||||
private readonly calendarEventRepository: WorkspaceRepository<CalendarEventWorkspaceEntity>,
|
||||
@InjectWorkspaceRepository(PersonWorkspaceEntity)
|
||||
private readonly personRepository: WorkspaceRepository<PersonWorkspaceEntity>,
|
||||
) {}
|
||||
|
||||
// TODO: Align return type with the entities to avoid mapping
|
||||
async getCalendarEventsFromPersonIds(
|
||||
workspaceMemberId: string,
|
||||
workspaceId: string,
|
||||
personIds: string[],
|
||||
page = 1,
|
||||
pageSize: number = TIMELINE_CALENDAR_EVENTS_DEFAULT_PAGE_SIZE,
|
||||
): Promise<TimelineCalendarEventsWithTotal> {
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const dataSourceSchema =
|
||||
this.workspaceDataSourceService.getSchemaName(workspaceId);
|
||||
const calendarEventIds = await this.calendarEventRepository.find({
|
||||
where: {
|
||||
calendarEventParticipants: {
|
||||
person: {
|
||||
id: Any(personIds),
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
startsAt: true,
|
||||
},
|
||||
skip: offset,
|
||||
take: pageSize,
|
||||
order: {
|
||||
startsAt: 'DESC',
|
||||
},
|
||||
});
|
||||
|
||||
const calendarEvents: Omit<TimelineCalendarEvent, 'participants'>[] =
|
||||
await this.workspaceDataSourceService.executeRawQuery(
|
||||
`SELECT
|
||||
"calendarEvent".*
|
||||
FROM
|
||||
${dataSourceSchema}."calendarEvent" "calendarEvent"
|
||||
LEFT JOIN
|
||||
${dataSourceSchema}."calendarEventParticipant" "calendarEventParticipant" ON "calendarEvent".id = "calendarEventParticipant"."calendarEventId"
|
||||
LEFT JOIN
|
||||
${dataSourceSchema}."person" "person" ON "calendarEventParticipant"."personId" = "person".id
|
||||
WHERE
|
||||
"calendarEventParticipant"."personId" = ANY($1)
|
||||
GROUP BY
|
||||
"calendarEvent".id
|
||||
ORDER BY
|
||||
"calendarEvent"."startsAt" DESC
|
||||
LIMIT $2
|
||||
OFFSET $3`,
|
||||
[personIds, pageSize, offset],
|
||||
workspaceId,
|
||||
);
|
||||
const ids = calendarEventIds.map(({ id }) => id);
|
||||
|
||||
if (!calendarEvents) {
|
||||
if (ids.length <= 0) {
|
||||
return {
|
||||
totalNumberOfCalendarEvents: 0,
|
||||
timelineCalendarEvents: [],
|
||||
};
|
||||
}
|
||||
|
||||
const calendarEventParticipants: TimelineCalendarEventParticipantWithPersonInformation[] =
|
||||
await this.workspaceDataSourceService.executeRawQuery(
|
||||
`SELECT
|
||||
"calendarEventParticipant".*,
|
||||
"person"."nameFirstName" as "personFirstName",
|
||||
"person"."nameLastName" as "personLastName",
|
||||
"person"."avatarUrl" as "personAvatarUrl",
|
||||
"workspaceMember"."nameFirstName" as "workspaceMemberFirstName",
|
||||
"workspaceMember"."nameLastName" as "workspaceMemberLastName",
|
||||
"workspaceMember"."avatarUrl" as "workspaceMemberAvatarUrl"
|
||||
FROM
|
||||
${dataSourceSchema}."calendarEventParticipant" "calendarEventParticipant"
|
||||
LEFT JOIN
|
||||
${dataSourceSchema}."person" "person" ON "calendarEventParticipant"."personId" = "person".id
|
||||
LEFT JOIN
|
||||
${dataSourceSchema}."workspaceMember" "workspaceMember" ON "calendarEventParticipant"."workspaceMemberId" = "workspaceMember".id
|
||||
WHERE
|
||||
"calendarEventParticipant"."calendarEventId" = ANY($1)`,
|
||||
[calendarEvents.map((event) => event.id)],
|
||||
workspaceId,
|
||||
);
|
||||
// We've split the query into two parts, because we want to fetch all the participants without any filtering
|
||||
const [events, total] = await this.calendarEventRepository.findAndCount({
|
||||
where: {
|
||||
id: Any(ids),
|
||||
},
|
||||
relations: {
|
||||
calendarEventParticipants: {
|
||||
person: true,
|
||||
workspaceMember: true,
|
||||
},
|
||||
calendarChannelEventAssociations: {
|
||||
calendarChannel: true,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const formattedCalendarEventParticipants: TimelineCalendarEventParticipant[] =
|
||||
calendarEventParticipants.map((participant) => {
|
||||
const firstName =
|
||||
participant.personFirstName ||
|
||||
participant.workspaceMemberFirstName ||
|
||||
'';
|
||||
// Keep events in the same order as they ids were returned
|
||||
const orderedEvents = events.sort(
|
||||
(a, b) => ids.indexOf(a.id) - ids.indexOf(b.id),
|
||||
);
|
||||
|
||||
const lastName =
|
||||
participant.personLastName ||
|
||||
participant.workspaceMemberLastName ||
|
||||
'';
|
||||
|
||||
const displayName =
|
||||
firstName || participant.displayName || participant.handle;
|
||||
|
||||
const avatarUrl =
|
||||
participant.personAvatarUrl ||
|
||||
participant.workspaceMemberAvatarUrl ||
|
||||
'';
|
||||
|
||||
return {
|
||||
calendarEventId: participant.calendarEventId,
|
||||
personId: participant.personId,
|
||||
workspaceMemberId: participant.workspaceMemberId,
|
||||
firstName,
|
||||
lastName,
|
||||
displayName,
|
||||
avatarUrl,
|
||||
const timelineCalendarEvents = orderedEvents.map((event) => {
|
||||
const participants = event.calendarEventParticipants.map(
|
||||
(participant) => ({
|
||||
calendarEventId: event.id,
|
||||
personId: participant.person?.id,
|
||||
workspaceMemberId: participant.workspaceMember?.id,
|
||||
firstName:
|
||||
participant.person?.name.firstName ||
|
||||
participant.workspaceMember?.name.firstName ||
|
||||
'',
|
||||
lastName:
|
||||
participant.person?.name.lastName ||
|
||||
participant.workspaceMember?.name.lastName ||
|
||||
'',
|
||||
displayName:
|
||||
participant.person?.name.firstName ||
|
||||
participant.person?.name.lastName ||
|
||||
participant.workspaceMember?.name.firstName ||
|
||||
participant.workspaceMember?.name.lastName ||
|
||||
'',
|
||||
avatarUrl:
|
||||
participant.person?.avatarUrl ||
|
||||
participant.workspaceMember?.avatarUrl ||
|
||||
'',
|
||||
handle: participant.handle,
|
||||
};
|
||||
});
|
||||
|
||||
const calendarEventParticipantsByEventId: {
|
||||
[calendarEventId: string]: TimelineCalendarEventParticipant[];
|
||||
} = groupBy(formattedCalendarEventParticipants, 'calendarEventId');
|
||||
|
||||
const totalNumberOfCalendarEvents: { count: number }[] =
|
||||
await this.workspaceDataSourceService.executeRawQuery(
|
||||
`
|
||||
SELECT
|
||||
COUNT(DISTINCT "calendarEventId")
|
||||
FROM
|
||||
${dataSourceSchema}."calendarEventParticipant" "calendarEventParticipant"
|
||||
WHERE
|
||||
"calendarEventParticipant"."personId" = ANY($1)
|
||||
`,
|
||||
[personIds],
|
||||
workspaceId,
|
||||
}),
|
||||
);
|
||||
|
||||
const timelineCalendarEvents = calendarEvents.map((event) => {
|
||||
const participants = calendarEventParticipantsByEventId[event.id] || [];
|
||||
const visibility = event.calendarChannelEventAssociations.some(
|
||||
(association) => association.calendarChannel.visibility === 'METADATA',
|
||||
)
|
||||
? TimelineCalendarEventVisibility.METADATA
|
||||
: TimelineCalendarEventVisibility.SHARE_EVERYTHING;
|
||||
|
||||
return {
|
||||
...event,
|
||||
...omit(event, [
|
||||
'calendarEventParticipants',
|
||||
'calendarChannelEventAssociations',
|
||||
]),
|
||||
startsAt: event.startsAt as unknown as Date,
|
||||
endsAt: event.endsAt as unknown as Date,
|
||||
participants,
|
||||
visibility,
|
||||
};
|
||||
});
|
||||
|
||||
const calendarEventIdsWithWorkspaceMemberInParticipants =
|
||||
await this.workspaceDataSourceService.executeRawQuery(
|
||||
`
|
||||
SELECT
|
||||
"calendarEventId"
|
||||
FROM
|
||||
${dataSourceSchema}."calendarEventParticipant" "calendarEventParticipant"
|
||||
WHERE
|
||||
"calendarEventParticipant"."workspaceMemberId" = $1
|
||||
`,
|
||||
[workspaceMemberId],
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
const calendarEventIdsWithWorkspaceMemberInParticipantsFormatted =
|
||||
calendarEventIdsWithWorkspaceMemberInParticipants.map(
|
||||
(event: { calendarEventId: string }) => event.calendarEventId,
|
||||
);
|
||||
|
||||
const calendarEventIdsToFetchVisibilityFor = timelineCalendarEvents
|
||||
.filter(
|
||||
(event) =>
|
||||
!calendarEventIdsWithWorkspaceMemberInParticipantsFormatted.includes(
|
||||
event.id,
|
||||
),
|
||||
)
|
||||
.map((event) => event.id);
|
||||
|
||||
const calendarEventIdsForWhichVisibilityIsMetadata:
|
||||
| {
|
||||
id: string;
|
||||
}[]
|
||||
| undefined = await this.workspaceDataSourceService.executeRawQuery(
|
||||
`
|
||||
SELECT
|
||||
"calendarChannelEventAssociation"."calendarEventId" AS "id"
|
||||
FROM
|
||||
${dataSourceSchema}."calendarChannel" "calendarChannel"
|
||||
LEFT JOIN
|
||||
${dataSourceSchema}."calendarChannelEventAssociation" "calendarChannelEventAssociation" ON "calendarChannel".id = "calendarChannelEventAssociation"."calendarChannelId"
|
||||
WHERE
|
||||
"calendarChannelEventAssociation"."calendarEventId" = ANY($1)
|
||||
AND
|
||||
"calendarChannel"."visibility" = 'METADATA'
|
||||
`,
|
||||
[calendarEventIdsToFetchVisibilityFor],
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
if (!calendarEventIdsForWhichVisibilityIsMetadata) {
|
||||
throw new Error('Failed to fetch calendar event visibility');
|
||||
}
|
||||
|
||||
const calendarEventIdsForWhichVisibilityIsMetadataMap = new Map(
|
||||
calendarEventIdsForWhichVisibilityIsMetadata.map((event) => [
|
||||
event.id,
|
||||
TimelineCalendarEventVisibility.METADATA,
|
||||
]),
|
||||
);
|
||||
|
||||
timelineCalendarEvents.forEach((event) => {
|
||||
event.visibility =
|
||||
calendarEventIdsForWhichVisibilityIsMetadataMap.get(event.id) ??
|
||||
TimelineCalendarEventVisibility.SHARE_EVERYTHING;
|
||||
|
||||
if (event.visibility === TimelineCalendarEventVisibility.METADATA) {
|
||||
event.title = '';
|
||||
event.description = '';
|
||||
event.location = '';
|
||||
event.conferenceSolution = '';
|
||||
event.conferenceLink = { label: '', url: '' };
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
totalNumberOfCalendarEvents: totalNumberOfCalendarEvents[0].count,
|
||||
totalNumberOfCalendarEvents: total,
|
||||
timelineCalendarEvents,
|
||||
};
|
||||
}
|
||||
|
||||
async getCalendarEventsFromCompanyId(
|
||||
workspaceMemberId: string,
|
||||
workspaceId: string,
|
||||
companyId: string,
|
||||
page = 1,
|
||||
pageSize: number = TIMELINE_CALENDAR_EVENTS_DEFAULT_PAGE_SIZE,
|
||||
): Promise<TimelineCalendarEventsWithTotal> {
|
||||
const dataSourceSchema =
|
||||
this.workspaceDataSourceService.getSchemaName(workspaceId);
|
||||
const personIds = await this.personRepository.find({
|
||||
where: {
|
||||
company: {
|
||||
id: companyId,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
|
||||
const personIds = await this.workspaceDataSourceService.executeRawQuery(
|
||||
`
|
||||
SELECT
|
||||
p."id"
|
||||
FROM
|
||||
${dataSourceSchema}."person" p
|
||||
WHERE
|
||||
p."companyId" = $1
|
||||
`,
|
||||
[companyId],
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
if (!personIds) {
|
||||
if (personIds.length <= 0) {
|
||||
return {
|
||||
totalNumberOfCalendarEvents: 0,
|
||||
timelineCalendarEvents: [],
|
||||
};
|
||||
}
|
||||
|
||||
const formattedPersonIds = personIds.map(
|
||||
(personId: { id: string }) => personId.id,
|
||||
);
|
||||
const formattedPersonIds = personIds.map(({ id }) => id);
|
||||
|
||||
const messageThreads = await this.getCalendarEventsFromPersonIds(
|
||||
workspaceMemberId,
|
||||
workspaceId,
|
||||
formattedPersonIds,
|
||||
page,
|
||||
pageSize,
|
||||
|
||||
@ -10,6 +10,7 @@ import { TimelineMessagingModule } from 'src/engine/core-modules/messaging/timel
|
||||
import { TimelineCalendarEventModule } from 'src/engine/core-modules/calendar/timeline-calendar-event.module';
|
||||
import { BillingModule } from 'src/engine/core-modules/billing/billing.module';
|
||||
import { HealthModule } from 'src/engine/core-modules/health/health.module';
|
||||
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
|
||||
|
||||
import { AnalyticsModule } from './analytics/analytics.module';
|
||||
import { FileModule } from './file/file.module';
|
||||
@ -17,6 +18,9 @@ import { ClientConfigModule } from './client-config/client-config.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TwentyORMModule.register({
|
||||
workspaceEntities: ['dist/src/**/*.workspace-entity{.ts,.js}'],
|
||||
}),
|
||||
HealthModule,
|
||||
AnalyticsModule,
|
||||
AuthModule,
|
||||
|
||||
@ -10,7 +10,7 @@ import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-
|
||||
import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
|
||||
import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event';
|
||||
import { WorkspaceMemberObjectMetadata } from 'src/modules/workspace-member/standard-objects/workspace-member.object-metadata';
|
||||
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
||||
import { assert } from 'src/utils/assert';
|
||||
|
||||
export class UserWorkspaceService extends TypeOrmQueryService<UserWorkspace> {
|
||||
@ -59,7 +59,7 @@ export class UserWorkspaceService extends TypeOrmQueryService<UserWorkspace> {
|
||||
`Error while creating workspace member ${user.email} on workspace ${workspaceId}`,
|
||||
);
|
||||
const payload =
|
||||
new ObjectRecordCreateEvent<WorkspaceMemberObjectMetadata>();
|
||||
new ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>();
|
||||
|
||||
payload.workspaceId = workspaceId;
|
||||
payload.properties = {
|
||||
|
||||
@ -10,7 +10,7 @@ import { WorkspaceMember } from 'src/engine/core-modules/user/dtos/workspace-mem
|
||||
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
|
||||
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
|
||||
import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity';
|
||||
import { WorkspaceMemberObjectMetadata } from 'src/modules/workspace-member/standard-objects/workspace-member.object-metadata';
|
||||
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
||||
import { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event';
|
||||
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record';
|
||||
import { WorkspaceService } from 'src/engine/core-modules/workspace/services/workspace.service';
|
||||
@ -113,7 +113,7 @@ export class UserService extends TypeOrmQueryService<User> {
|
||||
`SELECT * FROM ${dataSourceMetadata.schema}."workspaceMember"`,
|
||||
);
|
||||
const workspaceMember = workspaceMembers.filter(
|
||||
(member: ObjectRecord<WorkspaceMemberObjectMetadata>) =>
|
||||
(member: ObjectRecord<WorkspaceMemberWorkspaceEntity>) =>
|
||||
member.userId === userId,
|
||||
)?.[0];
|
||||
|
||||
@ -129,7 +129,7 @@ export class UserService extends TypeOrmQueryService<User> {
|
||||
`DELETE FROM ${dataSourceMetadata.schema}."workspaceMember" WHERE "userId" = '${userId}'`,
|
||||
);
|
||||
const payload =
|
||||
new ObjectRecordDeleteEvent<WorkspaceMemberObjectMetadata>();
|
||||
new ObjectRecordDeleteEvent<WorkspaceMemberWorkspaceEntity>();
|
||||
|
||||
payload.workspaceId = workspaceId;
|
||||
payload.properties = {
|
||||
|
||||
@ -3,7 +3,7 @@ import { OnEvent } from '@nestjs/event-emitter';
|
||||
|
||||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
||||
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
|
||||
import { WorkspaceMemberObjectMetadata } from 'src/modules/workspace-member/standard-objects/workspace-member.object-metadata';
|
||||
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
||||
import { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event';
|
||||
import {
|
||||
HandleWorkspaceMemberDeletedJob,
|
||||
@ -19,7 +19,7 @@ export class WorkspaceWorkspaceMemberListener {
|
||||
|
||||
@OnEvent('workspaceMember.deleted')
|
||||
async handleDeleteEvent(
|
||||
payload: ObjectRecordDeleteEvent<WorkspaceMemberObjectMetadata>,
|
||||
payload: ObjectRecordDeleteEvent<WorkspaceMemberWorkspaceEntity>,
|
||||
) {
|
||||
const userId = payload.properties.before.userId;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user