fix code validation when the google calendar integration does not have all permission (#1425)

Co-authored-by: Edward Fernandez <edward.fernandez@rappi.com>
pull/1426/head
Edward Fernández 2022-01-06 17:06:31 -05:00 committed by GitHub
parent bd2a795d7a
commit 6e7359ae96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 15 deletions

View File

@ -9,7 +9,7 @@ import notEmpty from "@lib/notEmpty";
import { ALL_INTEGRATIONS } from "../getIntegrations";
import { CALENDAR_INTEGRATIONS_TYPES } from "./constants/generals";
import { CalendarServiceType } from "./constants/types";
import { CalendarServiceType, EventBusyDate } from "./constants/types";
import { Calendar, CalendarEvent } from "./interfaces/Calendar";
import AppleCalendarService from "./services/AppleCalendarService";
import CalDavCalendarService from "./services/CalDavCalendarService";
@ -111,9 +111,12 @@ export const getBusyCalendarTimes = async (
.map((credential) => getCalendar(credential))
.filter(notEmpty);
const results = await Promise.all(
calendars.map((c) => c.getAvailability(dateFrom, dateTo, selectedCalendars))
);
let results: EventBusyDate[][] = [];
try {
results = await Promise.all(calendars.map((c) => c.getAvailability(dateFrom, dateTo, selectedCalendars)));
} catch (error) {
log.warn(error);
}
return results.reduce((acc, availability) => acc.concat(availability), []);
};

View File

@ -31,8 +31,7 @@ export default abstract class BaseCalendarService implements Calendar {
private credentials: Record<string, string> = {};
private headers: Record<string, string> = {};
protected integrationName = "";
log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] });
private log: typeof logger;
constructor(credential: Credential, integrationName: string, url?: string) {
this.integrationName = integrationName;
@ -47,6 +46,8 @@ export default abstract class BaseCalendarService implements Calendar {
this.credentials = { username, password };
this.headers = getBasicAuthHeaders({ username, password });
this.log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] });
}
async createEvent(event: CalendarEvent): Promise<NewCalendarEventType> {

View File

@ -17,13 +17,14 @@ export default class GoogleCalendarService implements Calendar {
private url = "";
private integrationName = "";
private auth: { getToken: () => Promise<MyGoogleAuth> };
log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] });
private log: typeof logger;
constructor(credential: Credential) {
this.integrationName = CALENDAR_INTEGRATIONS_TYPES.google;
this.auth = this.googleAuth(credential);
this.log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] });
}
private googleAuth = (credential: Credential) => {
@ -299,7 +300,7 @@ export default class GoogleCalendarService implements Calendar {
}) || []
);
})
.catch((err) => {
.catch((err: Error) => {
this.log.error("There was an error contacting google calendar service: ", err);
reject(err);

View File

@ -17,13 +17,14 @@ const MS_GRAPH_CLIENT_SECRET = process.env.MS_GRAPH_CLIENT_SECRET || "";
export default class Office365CalendarService implements Calendar {
private url = "";
private integrationName = "";
private log: typeof logger;
auth: { getToken: () => Promise<string> };
log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] });
constructor(credential: Credential) {
this.integrationName = CALENDAR_INTEGRATIONS_TYPES.office365;
this.auth = this.o365Auth(credential);
this.log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] });
}
async createEvent(event: CalendarEvent): Promise<NewCalendarEventType> {

View File

@ -19,7 +19,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
res.status(401).json({ message: "You must be logged in to do this" });
return;
}
if (typeof code !== "string") {
if (code && typeof code !== "string") {
res.status(400).json({ message: "`code` must be a string" });
return;
}
@ -30,10 +30,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const { client_secret, client_id } = JSON.parse(credentials).web;
const redirect_uri = BASE_URL + "/api/integrations/googlecalendar/callback";
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri);
const token = await oAuth2Client.getToken(code);
const key = token.res?.data;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri);
let key = "";
if (code) {
const token = await oAuth2Client.getToken(code);
key = token.res?.data;
}
await prisma.credential.create({
data: {
type: "google_calendar",