Zomars/refactor emails followup (#1216)
This commit is contained in:
1 parent
d76ef4a007
commit
ec2acedf34
10 files changed
+417
-166
No files matched your search
@@ -19,6 +19,8 @@ import {
|
||||
import logger from "@lib/logger";
|
||||
import { VideoCallData } from "@lib/videoClient";
|
||||
|
||||
import { Ensure } from "./types/utils";
|
||||
|
||||
const log = logger.getChildLogger({ prefix: ["[lib] calendarClient"] });
|
||||
|
||||
export type Person = { name: string; email: string; timeZone: string };
|
||||
@@ -61,7 +63,7 @@ export interface CalendarEvent {
|
||||
paymentInfo?: PaymentInfo | null;
|
||||
}
|
||||
|
||||
export interface IntegrationCalendar extends Partial<SelectedCalendar> {
|
||||
export interface IntegrationCalendar extends Ensure<Partial<SelectedCalendar>, "externalId"> {
|
||||
primary?: boolean;
|
||||
name?: string;
|
||||
}
|
||||
@@ -89,11 +91,9 @@ function getCalendarAdapterOrNull(credential: Credential): CalendarApiAdapter |
|
||||
case "office365_calendar":
|
||||
return Office365CalendarApiAdapter(credential);
|
||||
case "caldav_calendar":
|
||||
// FIXME types wrong & type casting should not be needed
|
||||
return new CalDavCalendar(credential) as never as CalendarApiAdapter;
|
||||
return new CalDavCalendar(credential);
|
||||
case "apple_calendar":
|
||||
// FIXME types wrong & type casting should not be needed
|
||||
return new AppleCalendar(credential) as never as CalendarApiAdapter;
|
||||
return new AppleCalendar(credential);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -24,8 +24,6 @@ dayjs.extend(utc);
|
||||
|
||||
const log = logger.getChildLogger({ prefix: ["[[lib] apple calendar"] });
|
||||
|
||||
type EventBusyDate = Record<"start" | "end", Date>;
|
||||
|
||||
export class AppleCalendar implements CalendarApiAdapter {
|
||||
private url: string;
|
||||
private credentials: Record<string, string>;
|
||||
@@ -34,7 +32,7 @@ export class AppleCalendar implements CalendarApiAdapter {
|
||||
|
||||
constructor(credential: Credential) {
|
||||
const decryptedCredential = JSON.parse(
|
||||
symmetricDecrypt(credential.key, process.env.CALENDSO_ENCRYPTION_KEY)
|
||||
symmetricDecrypt(credential.key as string, process.env.CALENDSO_ENCRYPTION_KEY!)
|
||||
);
|
||||
const username = decryptedCredential.username;
|
||||
const password = decryptedCredential.password;
|
||||
@@ -52,12 +50,12 @@ export class AppleCalendar implements CalendarApiAdapter {
|
||||
});
|
||||
}
|
||||
|
||||
convertDate(date: string): number[] {
|
||||
convertDate(date: string): [number, number, number] {
|
||||
return dayjs(date)
|
||||
.utc()
|
||||
.toArray()
|
||||
.slice(0, 6)
|
||||
.map((v, i) => (i === 1 ? v + 1 : v));
|
||||
.map((v, i) => (i === 1 ? v + 1 : v)) as [number, number, number];
|
||||
}
|
||||
|
||||
getDuration(start: string, end: string): DurationObject {
|
||||
@@ -70,11 +68,11 @@ export class AppleCalendar implements CalendarApiAdapter {
|
||||
return attendees.map(({ email, name }) => ({ name, email, partstat: "NEEDS-ACTION" }));
|
||||
}
|
||||
|
||||
async createEvent(event: CalendarEvent): Promise<Record<string, unknown>> {
|
||||
async createEvent(event: CalendarEvent) {
|
||||
try {
|
||||
const calendars = await this.listCalendars();
|
||||
const uid = uuidv4();
|
||||
const { error, value: iCalString } = await createEvent({
|
||||
const { error, value: iCalString } = createEvent({
|
||||
uid,
|
||||
startInputType: "utc",
|
||||
start: this.convertDate(event.startTime),
|
||||
@@ -86,15 +84,9 @@ export class AppleCalendar implements CalendarApiAdapter {
|
||||
attendees: this.getAttendees(event.attendees),
|
||||
});
|
||||
|
||||
if (error) {
|
||||
log.debug("Error creating iCalString");
|
||||
return {};
|
||||
}
|
||||
if (error) throw new Error("Error creating iCalString");
|
||||
|
||||
if (!iCalString) {
|
||||
log.debug("Error creating iCalString");
|
||||
return {};
|
||||
}
|
||||
if (!iCalString) throw new Error("Error creating iCalString");
|
||||
|
||||
await Promise.all(
|
||||
calendars.map((calendar) => {
|
||||
@@ -112,6 +104,9 @@ export class AppleCalendar implements CalendarApiAdapter {
|
||||
return {
|
||||
uid,
|
||||
id: uid,
|
||||
type: "apple_calendar",
|
||||
password: "",
|
||||
url: "",
|
||||
};
|
||||
} catch (reason) {
|
||||
console.error(reason);
|
||||
@@ -132,7 +127,7 @@ export class AppleCalendar implements CalendarApiAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
const { error, value: iCalString } = await createEvent({
|
||||
const { error, value: iCalString } = createEvent({
|
||||
uid,
|
||||
startInputType: "utc",
|
||||
start: this.convertDate(event.startTime),
|
||||
@@ -201,11 +196,7 @@ export class AppleCalendar implements CalendarApiAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
async getAvailability(
|
||||
dateFrom: string,
|
||||
dateTo: string,
|
||||
selectedCalendars: IntegrationCalendar[]
|
||||
): Promise<EventBusyDate[]> {
|
||||
async getAvailability(dateFrom: string, dateTo: string, selectedCalendars: IntegrationCalendar[]) {
|
||||
try {
|
||||
const selectedCalendarIds = selectedCalendars
|
||||
.filter((e) => e.integration === this.integrationName)
|
||||
@@ -229,8 +220,8 @@ export class AppleCalendar implements CalendarApiAdapter {
|
||||
ids.map(async (calId) => {
|
||||
return (await this.getEvents(calId, dateFrom, dateTo)).map((event) => {
|
||||
return {
|
||||
start: event.startDate,
|
||||
end: event.endDate,
|
||||
start: event.startDate.toISOString(),
|
||||
end: event.endDate.toISOString(),
|
||||
};
|
||||
});
|
||||
})
|
||||
@@ -272,8 +263,8 @@ export class AppleCalendar implements CalendarApiAdapter {
|
||||
calId: string,
|
||||
dateFrom: string | null,
|
||||
dateTo: string | null,
|
||||
objectUrls: string[] | null
|
||||
): Promise<unknown[]> {
|
||||
objectUrls?: string[] | null
|
||||
) {
|
||||
try {
|
||||
const objects = await fetchCalendarObjects({
|
||||
calendar: {
|
||||
@@ -290,54 +281,48 @@ export class AppleCalendar implements CalendarApiAdapter {
|
||||
headers: this.headers,
|
||||
});
|
||||
|
||||
const events =
|
||||
objects &&
|
||||
objects?.length > 0 &&
|
||||
objects
|
||||
.map((object) => {
|
||||
if (object?.data) {
|
||||
const jcalData = ICAL.parse(object.data);
|
||||
const vcalendar = new ICAL.Component(jcalData);
|
||||
const vevent = vcalendar.getFirstSubcomponent("vevent");
|
||||
const event = new ICAL.Event(vevent);
|
||||
const events = objects
|
||||
.filter((e) => !!e.data)
|
||||
.map((object) => {
|
||||
const jcalData = ICAL.parse(object.data);
|
||||
const vcalendar = new ICAL.Component(jcalData);
|
||||
const vevent = vcalendar.getFirstSubcomponent("vevent");
|
||||
const event = new ICAL.Event(vevent);
|
||||
|
||||
const calendarTimezone = vcalendar.getFirstSubcomponent("vtimezone")
|
||||
? vcalendar.getFirstSubcomponent("vtimezone").getFirstPropertyValue("tzid")
|
||||
: "";
|
||||
const calendarTimezone =
|
||||
vcalendar.getFirstSubcomponent("vtimezone")?.getFirstPropertyValue("tzid") || "";
|
||||
|
||||
const startDate = calendarTimezone
|
||||
? dayjs(event.startDate).tz(calendarTimezone)
|
||||
: new Date(event.startDate.toUnixTime() * 1000);
|
||||
const endDate = calendarTimezone
|
||||
? dayjs(event.endDate).tz(calendarTimezone)
|
||||
: new Date(event.endDate.toUnixTime() * 1000);
|
||||
const startDate = calendarTimezone
|
||||
? dayjs(event.startDate.toJSDate()).tz(calendarTimezone)
|
||||
: new Date(event.startDate.toUnixTime() * 1000);
|
||||
const endDate = calendarTimezone
|
||||
? dayjs(event.endDate.toJSDate()).tz(calendarTimezone)
|
||||
: new Date(event.endDate.toUnixTime() * 1000);
|
||||
|
||||
return {
|
||||
uid: event.uid,
|
||||
etag: object.etag,
|
||||
url: object.url,
|
||||
summary: event.summary,
|
||||
description: event.description,
|
||||
location: event.location,
|
||||
sequence: event.sequence,
|
||||
startDate,
|
||||
endDate,
|
||||
duration: {
|
||||
weeks: event.duration.weeks,
|
||||
days: event.duration.days,
|
||||
hours: event.duration.hours,
|
||||
minutes: event.duration.minutes,
|
||||
seconds: event.duration.seconds,
|
||||
isNegative: event.duration.isNegative,
|
||||
},
|
||||
organizer: event.organizer,
|
||||
attendees: event.attendees.map((a) => a.getValues()),
|
||||
recurrenceId: event.recurrenceId,
|
||||
timezone: calendarTimezone,
|
||||
};
|
||||
}
|
||||
})
|
||||
.filter((e) => e != null);
|
||||
return {
|
||||
uid: event.uid,
|
||||
etag: object.etag,
|
||||
url: object.url,
|
||||
summary: event.summary,
|
||||
description: event.description,
|
||||
location: event.location,
|
||||
sequence: event.sequence,
|
||||
startDate,
|
||||
endDate,
|
||||
duration: {
|
||||
weeks: event.duration.weeks,
|
||||
days: event.duration.days,
|
||||
hours: event.duration.hours,
|
||||
minutes: event.duration.minutes,
|
||||
seconds: event.duration.seconds,
|
||||
isNegative: event.duration.isNegative,
|
||||
},
|
||||
organizer: event.organizer,
|
||||
attendees: event.attendees.map((a) => a.getValues()),
|
||||
recurrenceId: event.recurrenceId,
|
||||
timezone: calendarTimezone,
|
||||
};
|
||||
});
|
||||
|
||||
return events;
|
||||
} catch (reason) {
|
||||
|
||||
@@ -24,8 +24,6 @@ dayjs.extend(utc);
|
||||
|
||||
const log = logger.getChildLogger({ prefix: ["[lib] caldav"] });
|
||||
|
||||
type EventBusyDate = Record<"start" | "end", Date>;
|
||||
|
||||
export class CalDavCalendar implements CalendarApiAdapter {
|
||||
private url: string;
|
||||
private credentials: Record<string, string>;
|
||||
@@ -34,7 +32,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
||||
|
||||
constructor(credential: Credential) {
|
||||
const decryptedCredential = JSON.parse(
|
||||
symmetricDecrypt(credential.key, process.env.CALENDSO_ENCRYPTION_KEY)
|
||||
symmetricDecrypt(credential.key as string, process.env.CALENDSO_ENCRYPTION_KEY!)
|
||||
);
|
||||
const username = decryptedCredential.username;
|
||||
const url = decryptedCredential.url;
|
||||
@@ -53,12 +51,12 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
||||
});
|
||||
}
|
||||
|
||||
convertDate(date: string): number[] {
|
||||
convertDate(date: string): [number, number, number] {
|
||||
return dayjs(date)
|
||||
.utc()
|
||||
.toArray()
|
||||
.slice(0, 6)
|
||||
.map((v, i) => (i === 1 ? v + 1 : v));
|
||||
.map((v, i) => (i === 1 ? v + 1 : v)) as [number, number, number];
|
||||
}
|
||||
|
||||
getDuration(start: string, end: string): DurationObject {
|
||||
@@ -71,15 +69,14 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
||||
return attendees.map(({ email, name }) => ({ name, email, partstat: "NEEDS-ACTION" }));
|
||||
}
|
||||
|
||||
async createEvent(event: CalendarEvent): Promise<Record<string, unknown>> {
|
||||
async createEvent(event: CalendarEvent) {
|
||||
try {
|
||||
const calendars = await this.listCalendars();
|
||||
const uid = uuidv4();
|
||||
|
||||
const { error, value: iCalString } = await createEvent({
|
||||
const { error, value: iCalString } = createEvent({
|
||||
uid,
|
||||
startInputType: "utc",
|
||||
// FIXME types
|
||||
start: this.convertDate(event.startTime),
|
||||
duration: this.getDuration(event.startTime, event.endTime),
|
||||
title: event.title,
|
||||
@@ -89,15 +86,9 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
||||
attendees: this.getAttendees(event.attendees),
|
||||
});
|
||||
|
||||
if (error) {
|
||||
log.debug("Error creating iCalString");
|
||||
return {};
|
||||
}
|
||||
if (error) throw new Error("Error creating iCalString");
|
||||
|
||||
if (!iCalString) {
|
||||
log.debug("Error creating iCalString");
|
||||
return {};
|
||||
}
|
||||
if (!iCalString) throw new Error("Error creating iCalString");
|
||||
|
||||
await Promise.all(
|
||||
calendars.map((calendar) => {
|
||||
@@ -115,6 +106,9 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
||||
return {
|
||||
uid,
|
||||
id: uid,
|
||||
type: "caldav_calendar",
|
||||
password: "",
|
||||
url: "",
|
||||
};
|
||||
} catch (reason) {
|
||||
log.error(reason);
|
||||
@@ -138,7 +132,6 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
||||
const { error, value: iCalString } = await createEvent({
|
||||
uid,
|
||||
startInputType: "utc",
|
||||
// FIXME - types wrong
|
||||
start: this.convertDate(event.startTime),
|
||||
duration: this.getDuration(event.startTime, event.endTime),
|
||||
title: event.title,
|
||||
@@ -205,12 +198,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME - types wrong
|
||||
async getAvailability(
|
||||
dateFrom: string,
|
||||
dateTo: string,
|
||||
selectedCalendars: IntegrationCalendar[]
|
||||
): Promise<EventBusyDate[]> {
|
||||
async getAvailability(dateFrom: string, dateTo: string, selectedCalendars: IntegrationCalendar[]) {
|
||||
try {
|
||||
const selectedCalendarIds = selectedCalendars
|
||||
.filter((e) => e.integration === this.integrationName)
|
||||
@@ -235,8 +223,8 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
||||
ids.map(async (calId) => {
|
||||
return (await this.getEvents(calId, dateFrom, dateTo)).map((event) => {
|
||||
return {
|
||||
start: event.startDate,
|
||||
end: event.endDate,
|
||||
start: event.startDate.toISOString(),
|
||||
end: event.endDate.toISOString(),
|
||||
};
|
||||
});
|
||||
})
|
||||
@@ -274,7 +262,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
async getEvents(calId: string, dateFrom: string | null, dateTo: string | null): Promise<unknown[]> {
|
||||
async getEvents(calId: string, dateFrom: string | null, dateTo: string | null) {
|
||||
try {
|
||||
const objects = await fetchCalendarObjects({
|
||||
calendar: {
|
||||
@@ -295,50 +283,47 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
||||
}
|
||||
|
||||
const events = objects
|
||||
.filter((e) => !!e.data)
|
||||
.map((object) => {
|
||||
if (object?.data) {
|
||||
const jcalData = ICAL.parse(object.data);
|
||||
const vcalendar = new ICAL.Component(jcalData);
|
||||
const vevent = vcalendar.getFirstSubcomponent("vevent");
|
||||
const event = new ICAL.Event(vevent);
|
||||
const jcalData = ICAL.parse(object.data);
|
||||
const vcalendar = new ICAL.Component(jcalData);
|
||||
const vevent = vcalendar.getFirstSubcomponent("vevent");
|
||||
const event = new ICAL.Event(vevent);
|
||||
|
||||
const calendarTimezone = vcalendar.getFirstSubcomponent("vtimezone")
|
||||
? vcalendar.getFirstSubcomponent("vtimezone").getFirstPropertyValue("tzid")
|
||||
: "";
|
||||
const calendarTimezone =
|
||||
vcalendar.getFirstSubcomponent("vtimezone")?.getFirstPropertyValue("tzid") || "";
|
||||
|
||||
const startDate = calendarTimezone
|
||||
? dayjs(event.startDate).tz(calendarTimezone)
|
||||
: new Date(event.startDate.toUnixTime() * 1000);
|
||||
const endDate = calendarTimezone
|
||||
? dayjs(event.endDate).tz(calendarTimezone)
|
||||
: new Date(event.endDate.toUnixTime() * 1000);
|
||||
const startDate = calendarTimezone
|
||||
? dayjs(event.startDate.toJSDate()).tz(calendarTimezone)
|
||||
: new Date(event.startDate.toUnixTime() * 1000);
|
||||
const endDate = calendarTimezone
|
||||
? dayjs(event.endDate.toJSDate()).tz(calendarTimezone)
|
||||
: new Date(event.endDate.toUnixTime() * 1000);
|
||||
|
||||
return {
|
||||
uid: event.uid,
|
||||
etag: object.etag,
|
||||
url: object.url,
|
||||
summary: event.summary,
|
||||
description: event.description,
|
||||
location: event.location,
|
||||
sequence: event.sequence,
|
||||
startDate,
|
||||
endDate,
|
||||
duration: {
|
||||
weeks: event.duration.weeks,
|
||||
days: event.duration.days,
|
||||
hours: event.duration.hours,
|
||||
minutes: event.duration.minutes,
|
||||
seconds: event.duration.seconds,
|
||||
isNegative: event.duration.isNegative,
|
||||
},
|
||||
organizer: event.organizer,
|
||||
attendees: event.attendees.map((a) => a.getValues()),
|
||||
recurrenceId: event.recurrenceId,
|
||||
timezone: calendarTimezone,
|
||||
};
|
||||
}
|
||||
})
|
||||
.filter((e) => e != null);
|
||||
return {
|
||||
uid: event.uid,
|
||||
etag: object.etag,
|
||||
url: object.url,
|
||||
summary: event.summary,
|
||||
description: event.description,
|
||||
location: event.location,
|
||||
sequence: event.sequence,
|
||||
startDate,
|
||||
endDate,
|
||||
duration: {
|
||||
weeks: event.duration.weeks,
|
||||
days: event.duration.days,
|
||||
hours: event.duration.hours,
|
||||
minutes: event.duration.minutes,
|
||||
seconds: event.duration.seconds,
|
||||
isNegative: event.duration.isNegative,
|
||||
},
|
||||
organizer: event.organizer,
|
||||
attendees: event.attendees.map((a) => a.getValues()),
|
||||
recurrenceId: event.recurrenceId,
|
||||
timezone: calendarTimezone,
|
||||
};
|
||||
});
|
||||
|
||||
return events;
|
||||
} catch (reason) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import { CalendarApiAdapter, CalendarEvent, IntegrationCalendar } from "@lib/cal
|
||||
import prisma from "@lib/prisma";
|
||||
|
||||
export interface ConferenceData {
|
||||
createRequest: calendar_v3.Schema$CreateConferenceRequest;
|
||||
createRequest?: calendar_v3.Schema$CreateConferenceRequest;
|
||||
}
|
||||
|
||||
const googleAuth = (credential: Credential) => {
|
||||
@@ -91,7 +91,19 @@ export const GoogleCalendarApiAdapter = (credential: Credential): CalendarApiAda
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
resolve(Object.values(apires.data.calendars).flatMap((item) => item["busy"]));
|
||||
let result: Prisma.PromiseReturnType<CalendarApiAdapter["getAvailability"]> = [];
|
||||
if (apires?.data.calendars) {
|
||||
result = Object.values(apires.data.calendars).reduce((c, i) => {
|
||||
i.busy?.forEach((busyTime) => {
|
||||
c.push({
|
||||
start: busyTime.start || "",
|
||||
end: busyTime.end || "",
|
||||
});
|
||||
});
|
||||
return c;
|
||||
}, [] as typeof result);
|
||||
}
|
||||
resolve(result);
|
||||
}
|
||||
);
|
||||
})
|
||||
@@ -146,7 +158,14 @@ export const GoogleCalendarApiAdapter = (credential: Credential): CalendarApiAda
|
||||
console.error("There was an error contacting google calendar service: ", err);
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(event.data);
|
||||
return resolve({
|
||||
...event.data,
|
||||
id: event.data.id || "",
|
||||
hangoutLink: event.data.hangoutLink || "",
|
||||
type: "google_calendar",
|
||||
password: "",
|
||||
url: "",
|
||||
});
|
||||
}
|
||||
);
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user