117 lines
2.7 KiB
TypeScript
117 lines
2.7 KiB
TypeScript
import type { Workflow } from "@prisma/client";
|
|
|
|
import { isSMSAction } from "@calcom/ee/workflows/lib/actionHelperFunctions";
|
|
import {
|
|
getSmsReminderNumberField,
|
|
getSmsReminderNumberSource,
|
|
SMS_REMINDER_NUMBER_FIELD,
|
|
} from "@calcom/features/bookings/lib/getBookingFields";
|
|
import { removeBookingField, upsertBookingField } from "@calcom/features/eventtypes/lib/bookingFieldsManager";
|
|
import { SENDER_ID, SENDER_NAME } from "@calcom/lib/constants";
|
|
import type PrismaType from "@calcom/prisma";
|
|
import type { WorkflowStep } from "@calcom/prisma/client";
|
|
import { MembershipRole } from "@calcom/prisma/enums";
|
|
|
|
export function getSender(
|
|
step: Pick<WorkflowStep, "action" | "sender"> & { senderName: string | null | undefined }
|
|
) {
|
|
return isSMSAction(step.action) ? step.sender || SENDER_ID : step.senderName || SENDER_NAME;
|
|
}
|
|
|
|
export async function isAuthorized(
|
|
workflow: Pick<Workflow, "id" | "teamId" | "userId"> | null,
|
|
prisma: typeof PrismaType,
|
|
currentUserId: number,
|
|
readOnly?: boolean
|
|
) {
|
|
if (!workflow) {
|
|
return false;
|
|
}
|
|
|
|
if (!readOnly) {
|
|
const userWorkflow = await prisma.workflow.findFirst({
|
|
where: {
|
|
id: workflow.id,
|
|
OR: [
|
|
{ userId: currentUserId },
|
|
{
|
|
team: {
|
|
members: {
|
|
some: {
|
|
userId: currentUserId,
|
|
accepted: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
});
|
|
if (userWorkflow) return true;
|
|
}
|
|
|
|
const userWorkflow = await prisma.workflow.findFirst({
|
|
where: {
|
|
id: workflow.id,
|
|
OR: [
|
|
{ userId: currentUserId },
|
|
{
|
|
team: {
|
|
members: {
|
|
some: {
|
|
userId: currentUserId,
|
|
accepted: true,
|
|
NOT: {
|
|
role: MembershipRole.MEMBER,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
if (userWorkflow) return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
export async function upsertSmsReminderFieldForBooking({
|
|
workflowId,
|
|
eventTypeId,
|
|
isSmsReminderNumberRequired,
|
|
}: {
|
|
workflowId: number;
|
|
isSmsReminderNumberRequired: boolean;
|
|
eventTypeId: number;
|
|
}) {
|
|
await upsertBookingField(
|
|
getSmsReminderNumberField(),
|
|
getSmsReminderNumberSource({
|
|
workflowId,
|
|
isSmsReminderNumberRequired,
|
|
}),
|
|
eventTypeId
|
|
);
|
|
}
|
|
|
|
export async function removeSmsReminderFieldForBooking({
|
|
workflowId,
|
|
eventTypeId,
|
|
}: {
|
|
workflowId: number;
|
|
eventTypeId: number;
|
|
}) {
|
|
await removeBookingField(
|
|
{
|
|
name: SMS_REMINDER_NUMBER_FIELD,
|
|
},
|
|
{
|
|
id: "" + workflowId,
|
|
type: "workflow",
|
|
},
|
|
eventTypeId
|
|
);
|
|
}
|