Stop relying on sendgrid client pkg
This commit is contained in:
parent
f94aaf50fb
commit
dd61851572
|
@ -8,7 +8,10 @@ const log = logger.getChildLogger({ prefix: ["CalendarManager"] });
|
||||||
|
|
||||||
export const getCalendar = (credential: CredentialPayload | null): Calendar | null => {
|
export const getCalendar = (credential: CredentialPayload | null): Calendar | null => {
|
||||||
if (!credential || !credential.key) return null;
|
if (!credential || !credential.key) return null;
|
||||||
const { type: calendarType } = credential;
|
let { type: calendarType } = credential;
|
||||||
|
if (calendarType === "sendgrid_other_calendar") {
|
||||||
|
calendarType = "sendgrid";
|
||||||
|
}
|
||||||
const calendarApp = appStore[calendarType.split("_").join("") as keyof typeof appStore];
|
const calendarApp = appStore[calendarType.split("_").join("") as keyof typeof appStore];
|
||||||
if (!(calendarApp && "lib" in calendarApp && "CalendarService" in calendarApp.lib)) {
|
if (!(calendarApp && "lib" in calendarApp && "CalendarService" in calendarApp.lib)) {
|
||||||
log.warn(`calendar of type ${calendarType} is not implemented`);
|
log.warn(`calendar of type ${calendarType} is not implemented`);
|
||||||
|
|
|
@ -28,8 +28,8 @@ function useAddAppMutation(_type: App["type"] | null, options?: Parameters<typeo
|
||||||
isOmniInstall = variables.isOmniInstall;
|
isOmniInstall = variables.isOmniInstall;
|
||||||
type = variables.type;
|
type = variables.type;
|
||||||
}
|
}
|
||||||
if (type?.endsWith("_other_calendar")) {
|
if (type === "sendgrid_other_calendar") {
|
||||||
type = type.split("_other_calendar")[0];
|
type = "sendgrid";
|
||||||
}
|
}
|
||||||
const state: IntegrationOAuthCallbackState = {
|
const state: IntegrationOAuthCallbackState = {
|
||||||
returnTo:
|
returnTo:
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
jest.mock("@calcom/lib/logger", () => ({
|
jest.mock("@calcom/lib/logger", () => ({
|
||||||
default: {
|
debug: jest.fn(),
|
||||||
getChildLogger: () => ({
|
error: jest.fn(),
|
||||||
debug: jest.fn(),
|
log: jest.fn(),
|
||||||
error: jest.fn(),
|
getChildLogger: jest.fn(),
|
||||||
log: jest.fn(),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
jest.mock("@calcom/lib/crypto", () => ({
|
jest.mock("@calcom/lib/crypto", () => ({
|
||||||
|
|
|
@ -7,6 +7,14 @@ import {
|
||||||
} from "@calcom/lib/CloseComeUtils";
|
} from "@calcom/lib/CloseComeUtils";
|
||||||
import type { CalendarEvent } from "@calcom/types/Calendar";
|
import type { CalendarEvent } from "@calcom/types/Calendar";
|
||||||
|
|
||||||
|
jest.mock("@calcom/lib/CloseCom", () => ({
|
||||||
|
default: class {
|
||||||
|
constructor() {
|
||||||
|
/* Mock */
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
jest.resetAllMocks();
|
jest.resetAllMocks();
|
||||||
});
|
});
|
||||||
|
@ -160,7 +168,7 @@ test("prepare data to create custom activity type instance: two attendees, no ad
|
||||||
const event = {
|
const event = {
|
||||||
attendees,
|
attendees,
|
||||||
startTime: now.toISOString(),
|
startTime: now.toISOString(),
|
||||||
} as unknown as CalendarEvent;
|
} as CalendarEvent;
|
||||||
|
|
||||||
CloseCom.prototype.activity = {
|
CloseCom.prototype.activity = {
|
||||||
type: {
|
type: {
|
||||||
|
|
|
@ -7,8 +7,7 @@
|
||||||
"description": "SendGrid delivers your transactional and marketing emails through the world's largest cloud-based email delivery platform.",
|
"description": "SendGrid delivers your transactional and marketing emails through the world's largest cloud-based email delivery platform.",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@calcom/lib": "*",
|
"@calcom/lib": "*",
|
||||||
"@calcom/prisma": "*",
|
"@calcom/prisma": "*"
|
||||||
"@sendgrid/client": "^7.7.0"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@calcom/types": "*"
|
"@calcom/types": "*"
|
||||||
|
|
|
@ -869,7 +869,8 @@ async function handler(req: NextApiRequest & { userId?: number | undefined }) {
|
||||||
|
|
||||||
results = createManager.results;
|
results = createManager.results;
|
||||||
referencesToCreate = createManager.referencesToCreate;
|
referencesToCreate = createManager.referencesToCreate;
|
||||||
if (results.length > 0 && results.every((res) => !res.success)) {
|
const nonAppResults = results.filter((res) => !res.appName);
|
||||||
|
if (nonAppResults.length > 0 && nonAppResults.every((res) => !res.success)) {
|
||||||
const error = {
|
const error = {
|
||||||
errorCode: "BookingCreatingMeetingFailed",
|
errorCode: "BookingCreatingMeetingFailed",
|
||||||
message: "Booking failed",
|
message: "Booking failed",
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import client from "@sendgrid/client";
|
|
||||||
|
|
||||||
import logger from "@calcom/lib/logger";
|
import logger from "@calcom/lib/logger";
|
||||||
|
|
||||||
export type SendgridFieldOptions = [string, string][];
|
export type SendgridFieldOptions = [string, string][];
|
||||||
|
@ -48,11 +46,14 @@ const environmentApiKey = process.env.SENDGRID_SYNC_API_KEY || "";
|
||||||
*/
|
*/
|
||||||
export default class Sendgrid {
|
export default class Sendgrid {
|
||||||
private log: typeof logger;
|
private log: typeof logger;
|
||||||
|
private apiKey: string;
|
||||||
|
private apiUrl: string;
|
||||||
|
|
||||||
constructor(providedApiKey = "") {
|
constructor(providedApiKey = "") {
|
||||||
this.log = logger.getChildLogger({ prefix: [`[[lib] sendgrid`] });
|
this.log = logger.getChildLogger({ prefix: [`[lib] sendgrid`] });
|
||||||
if (!providedApiKey && !environmentApiKey) throw Error("Sendgrid Api Key not present");
|
if (!providedApiKey && !environmentApiKey) throw Error("Sendgrid Api Key not present");
|
||||||
client.setApiKey(providedApiKey || environmentApiKey);
|
this.apiKey = providedApiKey || environmentApiKey;
|
||||||
|
this.apiUrl = "https://api.sendgrid.com";
|
||||||
}
|
}
|
||||||
|
|
||||||
public username = async () => {
|
public username = async () => {
|
||||||
|
@ -63,12 +64,28 @@ export default class Sendgrid {
|
||||||
return username;
|
return username;
|
||||||
};
|
};
|
||||||
|
|
||||||
public async sendgridRequest<R>(data: any): Promise<R> {
|
public async sendgridRequest<R>(requestData: { url: string; method: string; body?: unknown }): Promise<R> {
|
||||||
this.log.debug("sendgridRequest:request", data);
|
this.log.debug("sendgridRequest:request", requestData);
|
||||||
const results = await client.request(data);
|
const results = await fetch(`${this.apiUrl}${requestData.url}`, {
|
||||||
|
method: requestData.method,
|
||||||
|
headers: new Headers({
|
||||||
|
Authorization: `Bearer ${this.apiKey}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
}),
|
||||||
|
...(requestData.body ? { body: JSON.stringify(requestData.body) } : {}),
|
||||||
|
})
|
||||||
|
.then(async (res) => {
|
||||||
|
const jsonRes = await res.json();
|
||||||
|
if (jsonRes.errors) {
|
||||||
|
throw Error(`Sendgrid request error: ${jsonRes.errors[0]}`);
|
||||||
|
}
|
||||||
|
return jsonRes;
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
throw Error(`Sendgrid request error: ${error}`);
|
||||||
|
});
|
||||||
this.log.debug("sendgridRequest:results", results);
|
this.log.debug("sendgridRequest:results", results);
|
||||||
if (results[1].errors) throw Error(`Sendgrid request error: ${results[1].errors}`);
|
return results;
|
||||||
return results[1];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getSendgridContactId(email: string) {
|
public async getSendgridContactId(email: string) {
|
||||||
|
|
|
@ -14,7 +14,6 @@
|
||||||
"@calcom/config": "*",
|
"@calcom/config": "*",
|
||||||
"@calcom/dayjs": "*",
|
"@calcom/dayjs": "*",
|
||||||
"@prisma/client": "^4.2.1",
|
"@prisma/client": "^4.2.1",
|
||||||
"@sendgrid/client": "^7.7.0",
|
|
||||||
"@vercel/og": "^0.0.19",
|
"@vercel/og": "^0.0.19",
|
||||||
"bcryptjs": "^2.4.3",
|
"bcryptjs": "^2.4.3",
|
||||||
"ical.js": "^1.4.0",
|
"ical.js": "^1.4.0",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user