fix code validation when the google calendar integration does not have all permission (#1425)
Co-authored-by: Edward Fernandez <edward.fernandez@rappi.com>
This commit is contained in:
parent
bd2a795d7a
commit
6e7359ae96
5 changed files with 28 additions and 15 deletions
|
@ -9,7 +9,7 @@ import notEmpty from "@lib/notEmpty";
|
||||||
|
|
||||||
import { ALL_INTEGRATIONS } from "../getIntegrations";
|
import { ALL_INTEGRATIONS } from "../getIntegrations";
|
||||||
import { CALENDAR_INTEGRATIONS_TYPES } from "./constants/generals";
|
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 { Calendar, CalendarEvent } from "./interfaces/Calendar";
|
||||||
import AppleCalendarService from "./services/AppleCalendarService";
|
import AppleCalendarService from "./services/AppleCalendarService";
|
||||||
import CalDavCalendarService from "./services/CalDavCalendarService";
|
import CalDavCalendarService from "./services/CalDavCalendarService";
|
||||||
|
@ -111,9 +111,12 @@ export const getBusyCalendarTimes = async (
|
||||||
.map((credential) => getCalendar(credential))
|
.map((credential) => getCalendar(credential))
|
||||||
.filter(notEmpty);
|
.filter(notEmpty);
|
||||||
|
|
||||||
const results = await Promise.all(
|
let results: EventBusyDate[][] = [];
|
||||||
calendars.map((c) => c.getAvailability(dateFrom, dateTo, selectedCalendars))
|
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), []);
|
return results.reduce((acc, availability) => acc.concat(availability), []);
|
||||||
};
|
};
|
||||||
|
|
|
@ -31,8 +31,7 @@ export default abstract class BaseCalendarService implements Calendar {
|
||||||
private credentials: Record<string, string> = {};
|
private credentials: Record<string, string> = {};
|
||||||
private headers: Record<string, string> = {};
|
private headers: Record<string, string> = {};
|
||||||
protected integrationName = "";
|
protected integrationName = "";
|
||||||
|
private log: typeof logger;
|
||||||
log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] });
|
|
||||||
|
|
||||||
constructor(credential: Credential, integrationName: string, url?: string) {
|
constructor(credential: Credential, integrationName: string, url?: string) {
|
||||||
this.integrationName = integrationName;
|
this.integrationName = integrationName;
|
||||||
|
@ -47,6 +46,8 @@ export default abstract class BaseCalendarService implements Calendar {
|
||||||
|
|
||||||
this.credentials = { username, password };
|
this.credentials = { username, password };
|
||||||
this.headers = getBasicAuthHeaders({ username, password });
|
this.headers = getBasicAuthHeaders({ username, password });
|
||||||
|
|
||||||
|
this.log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] });
|
||||||
}
|
}
|
||||||
|
|
||||||
async createEvent(event: CalendarEvent): Promise<NewCalendarEventType> {
|
async createEvent(event: CalendarEvent): Promise<NewCalendarEventType> {
|
||||||
|
|
|
@ -17,13 +17,14 @@ export default class GoogleCalendarService implements Calendar {
|
||||||
private url = "";
|
private url = "";
|
||||||
private integrationName = "";
|
private integrationName = "";
|
||||||
private auth: { getToken: () => Promise<MyGoogleAuth> };
|
private auth: { getToken: () => Promise<MyGoogleAuth> };
|
||||||
|
private log: typeof logger;
|
||||||
log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] });
|
|
||||||
|
|
||||||
constructor(credential: Credential) {
|
constructor(credential: Credential) {
|
||||||
this.integrationName = CALENDAR_INTEGRATIONS_TYPES.google;
|
this.integrationName = CALENDAR_INTEGRATIONS_TYPES.google;
|
||||||
|
|
||||||
this.auth = this.googleAuth(credential);
|
this.auth = this.googleAuth(credential);
|
||||||
|
|
||||||
|
this.log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] });
|
||||||
}
|
}
|
||||||
|
|
||||||
private googleAuth = (credential: Credential) => {
|
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);
|
this.log.error("There was an error contacting google calendar service: ", err);
|
||||||
|
|
||||||
reject(err);
|
reject(err);
|
||||||
|
|
|
@ -17,13 +17,14 @@ const MS_GRAPH_CLIENT_SECRET = process.env.MS_GRAPH_CLIENT_SECRET || "";
|
||||||
export default class Office365CalendarService implements Calendar {
|
export default class Office365CalendarService implements Calendar {
|
||||||
private url = "";
|
private url = "";
|
||||||
private integrationName = "";
|
private integrationName = "";
|
||||||
|
private log: typeof logger;
|
||||||
auth: { getToken: () => Promise<string> };
|
auth: { getToken: () => Promise<string> };
|
||||||
|
|
||||||
log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] });
|
|
||||||
|
|
||||||
constructor(credential: Credential) {
|
constructor(credential: Credential) {
|
||||||
this.integrationName = CALENDAR_INTEGRATIONS_TYPES.office365;
|
this.integrationName = CALENDAR_INTEGRATIONS_TYPES.office365;
|
||||||
this.auth = this.o365Auth(credential);
|
this.auth = this.o365Auth(credential);
|
||||||
|
|
||||||
|
this.log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] });
|
||||||
}
|
}
|
||||||
|
|
||||||
async createEvent(event: CalendarEvent): Promise<NewCalendarEventType> {
|
async createEvent(event: CalendarEvent): Promise<NewCalendarEventType> {
|
||||||
|
|
|
@ -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" });
|
res.status(401).json({ message: "You must be logged in to do this" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (typeof code !== "string") {
|
if (code && typeof code !== "string") {
|
||||||
res.status(400).json({ message: "`code` must be a string" });
|
res.status(400).json({ message: "`code` must be a string" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -30,10 +30,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||||
|
|
||||||
const { client_secret, client_id } = JSON.parse(credentials).web;
|
const { client_secret, client_id } = JSON.parse(credentials).web;
|
||||||
const redirect_uri = BASE_URL + "/api/integrations/googlecalendar/callback";
|
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({
|
await prisma.credential.create({
|
||||||
data: {
|
data: {
|
||||||
type: "google_calendar",
|
type: "google_calendar",
|
||||||
|
|
Loading…
Reference in a new issue