Reorganise calendar module (#6089)

Refactor Calendar into functional sub modules
<img width="437" alt="image"
src="https://github.com/twentyhq/twenty/assets/12035771/d9de3285-a226-4fe8-b3ef-2d8a21def2a5">

---------

Co-authored-by: bosiraphael <raphael.bosi@gmail.com>
This commit is contained in:
Charles Bochet
2024-07-02 13:55:11 +02:00
committed by GitHub
parent ea7d52fba8
commit f8dd2cc733
66 changed files with 267 additions and 401 deletions

View File

@ -0,0 +1,20 @@
import { calendar_v3 as calendarV3 } from 'googleapis';
import { isEmailBlocklisted } from 'src/modules/calendar-messaging-participant/utils/is-email-blocklisted.util';
export const filterOutBlocklistedEvents = (
calendarChannelHandle: string,
events: calendarV3.Schema$Event[],
blocklist: string[],
) => {
return events.filter((event) => {
if (!event.attendees) {
return true;
}
return event.attendees.every(
(attendee) =>
!isEmailBlocklisted(calendarChannelHandle, attendee.email, blocklist),
);
});
};

View File

@ -0,0 +1,55 @@
import { calendar_v3 as calendarV3 } from 'googleapis';
import { v4 } from 'uuid';
import { CalendarEventWithParticipants } from 'src/modules/calendar/common/types/calendar-event';
import { CalendarEventParticipantResponseStatus } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
export const formatGoogleCalendarEvent = (
event: calendarV3.Schema$Event,
iCalUIDCalendarEventIdMap: Map<string, string>,
): CalendarEventWithParticipants => {
const id =
(event.iCalUID && iCalUIDCalendarEventIdMap.get(event.iCalUID)) ?? v4();
const formatResponseStatus = (status: string | null | undefined) => {
switch (status) {
case 'accepted':
return CalendarEventParticipantResponseStatus.ACCEPTED;
case 'declined':
return CalendarEventParticipantResponseStatus.DECLINED;
case 'tentative':
return CalendarEventParticipantResponseStatus.TENTATIVE;
default:
return CalendarEventParticipantResponseStatus.NEEDS_ACTION;
}
};
return {
id,
title: event.summary ?? '',
isCanceled: event.status === 'cancelled',
isFullDay: event.start?.dateTime == null,
startsAt: event.start?.dateTime ?? event.start?.date ?? null,
endsAt: event.end?.dateTime ?? event.end?.date ?? null,
externalId: event.id ?? '',
externalCreatedAt: event.created ?? null,
externalUpdatedAt: event.updated ?? null,
description: event.description ?? '',
location: event.location ?? '',
iCalUID: event.iCalUID ?? '',
conferenceSolution:
event.conferenceData?.conferenceSolution?.key?.type ?? '',
conferenceLinkLabel: event.conferenceData?.entryPoints?.[0]?.uri ?? '',
conferenceLinkUrl: event.conferenceData?.entryPoints?.[0]?.uri ?? '',
recurringEventExternalId: event.recurringEventId ?? '',
participants:
event.attendees?.map((attendee) => ({
calendarEventId: id,
iCalUID: event.iCalUID ?? '',
handle: attendee.email ?? '',
displayName: attendee.displayName ?? '',
isOrganizer: attendee.organizer === true,
responseStatus: formatResponseStatus(attendee.responseStatus),
})) ?? [],
};
};

View File

@ -0,0 +1,56 @@
export const valuesStringForBatchRawQuery = (
values: {
[key: string]: any;
}[],
typesArray: string[] = [],
) => {
const castedValues = values.reduce((acc, _, rowIndex) => {
const numberOfColumns = typesArray.length;
const rowValues = Array.from(
{ length: numberOfColumns },
(_, columnIndex) => {
const placeholder = `$${rowIndex * numberOfColumns + columnIndex + 1}`;
const typeCast = typesArray[columnIndex]
? `::${typesArray[columnIndex]}`
: '';
return `${placeholder}${typeCast}`;
},
).join(', ');
acc.push(`(${rowValues})`);
return acc;
}, [] as string[]);
return castedValues.join(', ');
};
export const getFlattenedValuesAndValuesStringForBatchRawQuery = (
values: {
[key: string]: any;
}[],
keyTypeMap: {
[key: string]: string;
},
): {
flattenedValues: any[];
valuesString: string;
} => {
const keysToInsert = Object.keys(keyTypeMap);
const flattenedValues = values.flatMap((value) =>
keysToInsert.map((key) => value[key]),
);
const valuesString = valuesStringForBatchRawQuery(
values,
Object.values(keyTypeMap),
);
return {
flattenedValues,
valuesString,
};
};