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:
@ -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,
|
||||
|
||||
Reference in New Issue
Block a user