feat: make.com/integromat app (#8897)
* start make app integration * setup integration * add relevant env vars * update app metadata * import setup route in app setups * fix typo * add app store imports * fix module import error * update make readme * move scheduler to app-store utils * move add subscription to node scheduler * move delete subscription to scheduler * subscribe unsubscribe in zapier using common nodeScheduler * fix lint errors * revert settings.json * update icon * add app screenshots * fix app description * fix type errors * update app code * Delete .gitkeep * delete unused template files * get app invite link from env vars * chore: handle error, cleanup readme, address review comments * fix: update link in readme * revert yarn.lock * fix type errors * Update packages/prisma/seed-app-store.ts * Update .env.appStore.example * Update .env.appStore.example * update app readme * fix param name in deleteSubcription * fix listBookings handler * Update turbo.json * use default installation handler to install app * use logger for console logs * Fix inviteLink reading * fix app setup handler * Fix type issue * always show app invite link * fix type error * add make invite link --------- Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: Carina Wollendorfer <[email protected]> Co-authored-by: Hariom Balhara <[email protected]> Co-authored-by: CarinaWolli <[email protected]>
This commit is contained in:
33 files changed
+697
-170
No files matched your search
@@ -1,5 +1,205 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import { v4 } from "uuid";
|
||||
|
||||
import { getHumanReadableLocationValue } from "@calcom/core/location";
|
||||
import { getCalEventResponses } from "@calcom/features/bookings/lib/getCalEventResponses";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { getTranslation } from "@calcom/lib/server";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { WebhookTriggerEvents } from "@calcom/prisma/enums";
|
||||
import type { ApiKey } from "@calcom/prisma/client";
|
||||
import { BookingStatus, WebhookTriggerEvents } from "@calcom/prisma/enums";
|
||||
|
||||
const log = logger.getChildLogger({ prefix: ["[node-scheduler]"] });
|
||||
|
||||
export async function addSubscription({
|
||||
appApiKey,
|
||||
triggerEvent,
|
||||
subscriberUrl,
|
||||
appId,
|
||||
}: {
|
||||
appApiKey: ApiKey;
|
||||
triggerEvent: WebhookTriggerEvents;
|
||||
subscriberUrl: string;
|
||||
appId: string;
|
||||
}) {
|
||||
try {
|
||||
const createSubscription = await prisma.webhook.create({
|
||||
data: {
|
||||
id: v4(),
|
||||
userId: appApiKey.userId,
|
||||
teamId: appApiKey.teamId,
|
||||
eventTriggers: [triggerEvent],
|
||||
subscriberUrl,
|
||||
active: true,
|
||||
appId: appId,
|
||||
},
|
||||
});
|
||||
|
||||
if (triggerEvent === WebhookTriggerEvents.MEETING_ENDED) {
|
||||
//schedule job for already existing bookings
|
||||
const where: Prisma.BookingWhereInput = {};
|
||||
if (appApiKey.teamId) where.eventType = { teamId: appApiKey.teamId };
|
||||
else where.userId = appApiKey.userId;
|
||||
const bookings = await prisma.booking.findMany({
|
||||
where: {
|
||||
...where,
|
||||
startTime: {
|
||||
gte: new Date(),
|
||||
},
|
||||
status: BookingStatus.ACCEPTED,
|
||||
},
|
||||
});
|
||||
|
||||
for (const booking of bookings) {
|
||||
scheduleTrigger(booking, createSubscription.subscriberUrl, {
|
||||
id: createSubscription.id,
|
||||
appId: createSubscription.appId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return createSubscription;
|
||||
} catch (error) {
|
||||
log.error(`Error creating subscription for user ${appApiKey.userId} and appId ${appApiKey.appId}.`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteSubscription({
|
||||
appApiKey,
|
||||
webhookId,
|
||||
appId,
|
||||
}: {
|
||||
appApiKey: ApiKey;
|
||||
webhookId: string;
|
||||
appId: string;
|
||||
}) {
|
||||
try {
|
||||
const webhook = await prisma.webhook.findFirst({
|
||||
where: {
|
||||
id: webhookId,
|
||||
},
|
||||
});
|
||||
|
||||
if (webhook?.eventTriggers.includes(WebhookTriggerEvents.MEETING_ENDED)) {
|
||||
const where: Prisma.BookingWhereInput = {};
|
||||
if (appApiKey.teamId) where.eventType = { teamId: appApiKey.teamId };
|
||||
else where.userId = appApiKey.userId;
|
||||
const bookingsWithScheduledJobs = await prisma.booking.findMany({
|
||||
where: {
|
||||
...where,
|
||||
scheduledJobs: {
|
||||
isEmpty: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
for (const booking of bookingsWithScheduledJobs) {
|
||||
const updatedScheduledJobs = booking.scheduledJobs.filter(
|
||||
(scheduledJob) => scheduledJob !== `${appId}_${webhook.id}`
|
||||
);
|
||||
await prisma.booking.update({
|
||||
where: {
|
||||
id: booking.id,
|
||||
},
|
||||
data: {
|
||||
scheduledJobs: updatedScheduledJobs,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const deleteWebhook = await prisma.webhook.delete({
|
||||
where: {
|
||||
id: webhookId,
|
||||
},
|
||||
});
|
||||
if (!deleteWebhook) {
|
||||
throw new Error(`Unable to delete webhook ${webhookId}`);
|
||||
}
|
||||
return deleteWebhook;
|
||||
} catch (err) {
|
||||
log.error(
|
||||
`Error deleting subscription for user ${appApiKey.userId}, webhookId ${webhookId}, appId ${appId}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function listBookings(appApiKey: ApiKey) {
|
||||
try {
|
||||
const where: Prisma.BookingWhereInput = {};
|
||||
if (appApiKey.teamId) {
|
||||
where.eventType = {
|
||||
OR: [{ teamId: appApiKey.teamId }, { parent: { teamId: appApiKey.teamId } }],
|
||||
};
|
||||
} else {
|
||||
where.userId = appApiKey.userId;
|
||||
}
|
||||
const bookings = await prisma.booking.findMany({
|
||||
take: 3,
|
||||
where: where,
|
||||
orderBy: {
|
||||
id: "desc",
|
||||
},
|
||||
select: {
|
||||
title: true,
|
||||
description: true,
|
||||
customInputs: true,
|
||||
responses: true,
|
||||
startTime: true,
|
||||
endTime: true,
|
||||
location: true,
|
||||
cancellationReason: true,
|
||||
status: true,
|
||||
user: {
|
||||
select: {
|
||||
username: true,
|
||||
name: true,
|
||||
email: true,
|
||||
timeZone: true,
|
||||
locale: true,
|
||||
},
|
||||
},
|
||||
eventType: {
|
||||
select: {
|
||||
title: true,
|
||||
description: true,
|
||||
requiresConfirmation: true,
|
||||
price: true,
|
||||
currency: true,
|
||||
length: true,
|
||||
bookingFields: true,
|
||||
team: true,
|
||||
},
|
||||
},
|
||||
attendees: {
|
||||
select: {
|
||||
name: true,
|
||||
email: true,
|
||||
timeZone: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
if (bookings.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const t = await getTranslation(bookings[0].user?.locale ?? "en", "common");
|
||||
|
||||
const updatedBookings = bookings.map((booking) => {
|
||||
return {
|
||||
...booking,
|
||||
...getCalEventResponses({
|
||||
bookingFields: booking.eventType?.bookingFields ?? null,
|
||||
booking,
|
||||
}),
|
||||
location: getHumanReadableLocationValue(booking.location || "", t),
|
||||
};
|
||||
});
|
||||
|
||||
return updatedBookings;
|
||||
} catch (err) {
|
||||
log.error(`Error retrieving list of bookings for user ${appApiKey.userId} and appId ${appApiKey.appId}.`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function scheduleTrigger(
|
||||
booking: { id: number; endTime: Date; scheduledJobs: string[] },
|
||||
|
||||
Reference in new issue
Block a user