Files
cal.diy/packages/features/bookings/services/WrongAssignmentReportService.ts
Benny JooandDevin AI ab21c7f805 refactor: Cal.diy (#28903)
* feat: Cal.diy — community-driven MIT-licensed fork of Cal.com

This squashed commit contains all Cal.diy changes applied on top of calcom/cal.com main:

- Rebrand Cal.com to Cal.diy across the entire codebase
- Remove Enterprise Edition (EE) features, license checks, and AGPL restrictions
- Switch license from AGPL-3.0 to MIT
- Remove docs/ directory (migrated to Nextra at cal.diy)
- Remove dead code: org tests, EE tips, platform nav, premium username, SAML/SSO, etc.
- Clean up .env.example for self-hosted Cal.diy
- Update Docker image references to calcom/cal.diy
- Update README, CONTRIBUTING.md, and issue templates for Cal.diy community fork
- Add PR welcome bot for Cal.diy contributors
- Fix API v2 breaking changes oasdiff ignore entries
- Replace Blacksmith CI runners with default GitHub Actions

3893 files changed, 20789 insertions(+), 411020 deletions(-)

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* refactor: remove org-specific /organizations/:orgId endpoints from API v2 atoms controllers (#1701)

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix: revert Cal.diy Inc to Cal.com, Inc. in license files, copyright notices, and package metadata (#1702)

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* rip out org related comments in api v2

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-04-15 09:52:36 -03:00

159 lines
5.1 KiB
TypeScript

import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks";
import { sendGenericWebhookPayload } from "@calcom/features/webhooks/lib/sendPayload";
import { ErrorWithCode } from "@calcom/lib/errors";
import logger from "@calcom/lib/logger";
import { getTranslation } from "@calcom/i18n/server";
import { Prisma } from "@calcom/prisma/client";
import type { WrongAssignmentReportStatus } from "@calcom/prisma/enums";
import { WebhookTriggerEvents } from "@calcom/prisma/enums";
import type { BookingRepository } from "../repositories/BookingRepository";
import type { WrongAssignmentReportRepository } from "../repositories/WrongAssignmentReportRepository";
export interface IWrongAssignmentReportServiceDeps {
bookingRepo: BookingRepository;
wrongAssignmentReportRepo: WrongAssignmentReportRepository;
}
const log = logger.getSubLogger({ prefix: ["WrongAssignmentReportService"] });
export class WrongAssignmentReportService {
constructor(private deps: IWrongAssignmentReportServiceDeps) {}
async report(input: {
userId: number;
userEmail: string;
userName: string | null;
userLocale: string;
bookingUid: string;
correctAssignee?: string;
additionalNotes: string;
}) {
const { bookingRepo, wrongAssignmentReportRepo } = this.deps;
const t = await getTranslation(input.userLocale, "common");
const alreadyReported = await wrongAssignmentReportRepo.existsByBookingUid(input.bookingUid);
if (alreadyReported) {
throw ErrorWithCode.Factory.BadRequest(t("wrong_assignment_already_reported"));
}
const booking = await bookingRepo.findByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason({
bookingUid: input.bookingUid,
});
if (!booking) {
throw ErrorWithCode.Factory.NotFound("Booking not found");
}
const teamId = booking.eventType?.team?.id ?? null;
const orgId = booking.eventType?.team?.parentId ?? null;
let report: { id: string };
try {
report = await wrongAssignmentReportRepo.createReport({
bookingUid: booking.uid,
reportedById: input.userId,
correctAssignee: input.correctAssignee || null,
additionalNotes: input.additionalNotes,
teamId,
});
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") {
throw ErrorWithCode.Factory.BadRequest(t("wrong_assignment_already_reported"));
}
throw error;
}
await this.sendWebhooks({
booking,
teamId,
orgId,
reporter: { id: input.userId, email: input.userEmail, name: input.userName },
correctAssignee: input.correctAssignee || null,
additionalNotes: input.additionalNotes || null,
});
return {
success: true,
message: t("wrong_assignment_reported"),
reportId: report.id,
};
}
private async sendWebhooks(params: {
booking: NonNullable<
Awaited<
ReturnType<BookingRepository["findByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason"]>
>
>;
teamId: number | null;
orgId: number | null;
reporter: { id: number; email: string; name: string | null };
correctAssignee: string | null;
additionalNotes: string | null;
}) {
const { booking, teamId, orgId, reporter } = params;
const webhookPayload = {
booking: {
uid: booking.uid,
id: booking.id,
title: booking.title,
startTime: booking.startTime,
endTime: booking.endTime,
status: booking.status,
eventType: booking.eventType
? {
id: booking.eventType.id,
title: booking.eventType.title,
slug: booking.eventType.slug,
teamId: teamId,
}
: null,
},
report: {
reportedBy: reporter,
firstAssignmentReason: booking.assignmentReason[0]?.reasonString ?? null,
guest: booking.attendees[0]?.email ?? null,
host: {
email: booking.user?.email ?? null,
name: booking.user?.name ?? null,
},
correctAssignee: params.correctAssignee,
additionalNotes: params.additionalNotes,
},
};
try {
const webhooks = await getWebhooks({
userId: booking.userId,
teamId,
orgId,
triggerEvent: WebhookTriggerEvents.WRONG_ASSIGNMENT_REPORT,
});
const webhookPromises = webhooks.map((webhook) =>
sendGenericWebhookPayload({
secretKey: webhook.secret,
triggerEvent: WebhookTriggerEvents.WRONG_ASSIGNMENT_REPORT,
createdAt: new Date().toISOString(),
webhook,
data: webhookPayload,
}).catch((error) => {
log.error(`Failed to send webhook to ${webhook.subscriberUrl}:`, error);
return { ok: false, status: 0 };
})
);
await Promise.allSettled(webhookPromises);
log.info(`Wrong assignment report webhooks sent for booking ${booking.uid}`, {
teamId,
userId: booking.userId,
webhookCount: webhooks.length,
});
} catch (error) {
log.error("Failed to send wrong assignment webhooks:", error);
}
}
}