first commit
This commit is contained in:
14
calcom/packages/features/tasker/tasks/index.ts
Normal file
14
calcom/packages/features/tasker/tasks/index.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import type { TaskHandler, TaskTypes } from "../tasker";
|
||||
|
||||
/**
|
||||
* This is a map of all the tasks that the Tasker can handle.
|
||||
* The keys are the TaskTypes and the values are the task handlers.
|
||||
* The task handlers are imported dynamically to avoid circular dependencies.
|
||||
*/
|
||||
const tasks: Record<TaskTypes, () => Promise<TaskHandler>> = {
|
||||
sendEmail: () => import("./sendEmail").then((module) => module.sendEmail),
|
||||
sendWebhook: () => import("./sendWebook").then((module) => module.sendWebhook),
|
||||
sendSms: () => Promise.resolve(() => Promise.reject(new Error("Not implemented"))),
|
||||
};
|
||||
|
||||
export default tasks;
|
||||
25
calcom/packages/features/tasker/tasks/sendEmail.ts
Normal file
25
calcom/packages/features/tasker/tasks/sendEmail.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { z } from "zod";
|
||||
|
||||
const sendEmailPayloadSchema = z.object({
|
||||
/** */
|
||||
to: z.string(),
|
||||
/** The email template to send */
|
||||
template: z.string(),
|
||||
payload: z.string(),
|
||||
});
|
||||
|
||||
export async function sendEmail(payload: string): Promise<void> {
|
||||
try {
|
||||
const parsedPayload = sendEmailPayloadSchema.parse(JSON.parse(payload));
|
||||
console.log(parsedPayload);
|
||||
const emails = await import("@calcom/emails");
|
||||
const email = emails[parsedPayload.template as keyof typeof emails];
|
||||
if (!email) throw new Error("Invalid email template");
|
||||
// @ts-expect-error - TODO bring back email type safety
|
||||
await email(parsedPayload.to);
|
||||
} catch (error) {
|
||||
// ... handle error
|
||||
console.error(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
29
calcom/packages/features/tasker/tasks/sendWebook.ts
Normal file
29
calcom/packages/features/tasker/tasks/sendWebook.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import sendPayload from "@calcom/features/webhooks/lib/sendPayload";
|
||||
|
||||
const sendWebhookPayloadSchema = z.object({
|
||||
secretKey: z.string().nullable(),
|
||||
triggerEvent: z.string(),
|
||||
createdAt: z.string(),
|
||||
webhook: z.object({
|
||||
subscriberUrl: z.string().url(),
|
||||
appId: z.string().nullable(),
|
||||
payloadTemplate: z.string().nullable(),
|
||||
}),
|
||||
// TODO: Define the data schema
|
||||
data: z.any(),
|
||||
});
|
||||
|
||||
export async function sendWebhook(payload: string): Promise<void> {
|
||||
try {
|
||||
const { secretKey, triggerEvent, createdAt, webhook, data } = sendWebhookPayloadSchema.parse(
|
||||
JSON.parse(payload)
|
||||
);
|
||||
await sendPayload(secretKey, triggerEvent, createdAt, webhook, data);
|
||||
} catch (error) {
|
||||
// ... handle error
|
||||
console.error(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user