refactor: Remove circular deps between @calcom/lib and @calcom/features [2] (#24438)
* move SystemField to features * migrate workflow service * merge two tests for team repository * update imports and migrate team repository * migrate delegation credential repository * migrate credential repository * migrate entityPermissionUtils * migrate hashedLink service and repository * migrate membership service * update imports * remove file * migrate buildEventUrlFromBooking * migrate getAllUserBookings to features * update imports * update organizationMock * migrate slots * migrate date-ranges to schedules dir * migrate getAggregatedAvailability * fix * refactor * migrate useCreateEventType hook to features * migrate assignValueToUser * migrate validateUsername to auth features * migrate system field back to lib * migrate getLabelValueMapFromResponses back to lib * update imports * use relative path * fix type checks * fix * fix * fix tests * update gh codeowners * fix * fix
This commit is contained in:
1 parent
79b319c1dd
commit
e91bf53d80
145 files changed
+412
-405
No files matched your search
@@ -0,0 +1,262 @@
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
|
||||
export type HashedLinkInputType = {
|
||||
link: string;
|
||||
expiresAt?: Date | null;
|
||||
maxUsageCount?: number | null;
|
||||
};
|
||||
|
||||
export const hashedLinkSelect = {
|
||||
id: true,
|
||||
link: true,
|
||||
eventTypeId: true,
|
||||
expiresAt: true,
|
||||
maxUsageCount: true,
|
||||
usageCount: true,
|
||||
eventType: {
|
||||
select: {
|
||||
userId: true,
|
||||
teamId: true,
|
||||
users: {
|
||||
select: {
|
||||
username: true,
|
||||
profiles: {
|
||||
select: {
|
||||
id: true,
|
||||
organizationId: true,
|
||||
username: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
team: {
|
||||
select: {
|
||||
id: true,
|
||||
slug: true,
|
||||
hideBranding: true,
|
||||
parent: {
|
||||
select: {
|
||||
hideBranding: true,
|
||||
},
|
||||
},
|
||||
members: {
|
||||
select: {
|
||||
user: {
|
||||
select: {
|
||||
timeZone: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
take: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
hosts: {
|
||||
select: {
|
||||
user: {
|
||||
select: {
|
||||
timeZone: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
profile: {
|
||||
select: {
|
||||
user: {
|
||||
select: {
|
||||
timeZone: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
owner: {
|
||||
select: {
|
||||
timeZone: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} satisfies Prisma.HashedLinkSelect;
|
||||
|
||||
export class HashedLinkRepository {
|
||||
constructor(private readonly prismaClient: PrismaClient) {}
|
||||
|
||||
static create() {
|
||||
return new HashedLinkRepository(prisma);
|
||||
}
|
||||
|
||||
async deleteLinks(eventTypeId: number, linksToDelete: string[]) {
|
||||
if (linksToDelete.length === 0) return;
|
||||
|
||||
return await this.prismaClient.hashedLink.deleteMany({
|
||||
where: {
|
||||
eventTypeId,
|
||||
link: { in: linksToDelete },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async createLink(
|
||||
eventTypeId: number,
|
||||
linkData: { link: string; expiresAt: Date | null; maxUsageCount?: number | null }
|
||||
) {
|
||||
const data: Prisma.HashedLinkCreateManyInput = {
|
||||
eventTypeId,
|
||||
link: linkData.link,
|
||||
expiresAt: linkData.expiresAt,
|
||||
};
|
||||
|
||||
if (linkData.maxUsageCount && Number.isFinite(linkData.maxUsageCount)) {
|
||||
data.maxUsageCount = linkData.maxUsageCount;
|
||||
}
|
||||
|
||||
return await this.prismaClient.hashedLink.create({ data });
|
||||
}
|
||||
|
||||
async updateLink(
|
||||
eventTypeId: number,
|
||||
linkData: { link: string; expiresAt: Date | null; maxUsageCount?: number | null }
|
||||
) {
|
||||
const updateData: Prisma.HashedLinkUpdateManyMutationInput = {
|
||||
expiresAt: linkData.expiresAt,
|
||||
};
|
||||
|
||||
if (typeof linkData.maxUsageCount === "number" && linkData.maxUsageCount !== null) {
|
||||
updateData.maxUsageCount = linkData.maxUsageCount;
|
||||
}
|
||||
|
||||
return await this.prismaClient.hashedLink.updateMany({
|
||||
where: {
|
||||
eventTypeId,
|
||||
link: linkData.link,
|
||||
},
|
||||
data: updateData,
|
||||
});
|
||||
}
|
||||
|
||||
async findLinksByEventTypeId(eventTypeId: number) {
|
||||
return await this.prismaClient.hashedLink.findMany({
|
||||
where: {
|
||||
eventTypeId,
|
||||
},
|
||||
select: {
|
||||
link: true,
|
||||
expiresAt: true,
|
||||
maxUsageCount: true,
|
||||
usageCount: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findLinkWithEventTypeDetails(linkId: string) {
|
||||
return await this.prismaClient.hashedLink.findUnique({
|
||||
where: {
|
||||
link: linkId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
link: true,
|
||||
expiresAt: true,
|
||||
maxUsageCount: true,
|
||||
usageCount: true,
|
||||
eventTypeId: true,
|
||||
eventType: {
|
||||
select: {
|
||||
teamId: true,
|
||||
userId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findLinkWithDetails(linkId: string) {
|
||||
return await this.prismaClient.hashedLink.findUnique({
|
||||
where: {
|
||||
link: linkId,
|
||||
},
|
||||
select: hashedLinkSelect,
|
||||
});
|
||||
}
|
||||
|
||||
async findLinksWithEventTypeDetails(linkIds: string[]) {
|
||||
return await this.prismaClient.hashedLink.findMany({
|
||||
where: {
|
||||
link: {
|
||||
in: linkIds,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
link: true,
|
||||
expiresAt: true,
|
||||
maxUsageCount: true,
|
||||
usageCount: true,
|
||||
eventTypeId: true,
|
||||
eventType: {
|
||||
select: {
|
||||
teamId: true,
|
||||
userId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findLinkWithValidationData(linkId: string) {
|
||||
return await this.prismaClient.hashedLink.findUnique({
|
||||
where: {
|
||||
link: linkId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
expiresAt: true,
|
||||
maxUsageCount: true,
|
||||
usageCount: true,
|
||||
eventType: {
|
||||
select: {
|
||||
userId: true,
|
||||
teamId: true,
|
||||
hosts: {
|
||||
select: {
|
||||
user: {
|
||||
select: {
|
||||
timeZone: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
profile: {
|
||||
select: {
|
||||
user: {
|
||||
select: {
|
||||
timeZone: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
owner: {
|
||||
select: {
|
||||
timeZone: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async incrementUsage(linkId: number, maxUsageCount: number) {
|
||||
return await this.prismaClient.hashedLink.update({
|
||||
where: {
|
||||
id: linkId,
|
||||
usageCount: { lt: maxUsageCount },
|
||||
},
|
||||
data: {
|
||||
usageCount: { increment: 1 },
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
import {
|
||||
HashedLinkRepository,
|
||||
type HashedLinkInputType,
|
||||
} from "@calcom/features/hashedLink/repositories/hashedLinkRepository";
|
||||
import { MembershipService } from "@calcom/features/membership/services/membershipService";
|
||||
import { ErrorCode } from "@calcom/lib/errorCodes";
|
||||
import { validateHashedLinkData } from "@calcom/lib/hashedLinksUtils";
|
||||
|
||||
type NormalizedLink = {
|
||||
link: string;
|
||||
expiresAt: Date | null;
|
||||
maxUsageCount?: number | null;
|
||||
};
|
||||
|
||||
export class HashedLinkService {
|
||||
constructor(
|
||||
private readonly hashedLinkRepository: HashedLinkRepository = HashedLinkRepository.create(),
|
||||
private readonly membershipService: MembershipService = new MembershipService()
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Normalizes link input to a consistent format
|
||||
* @param input - String link or object with link and options
|
||||
* @returns Normalized link object
|
||||
*/
|
||||
private normalizeLinkInput(input: string | HashedLinkInputType): NormalizedLink {
|
||||
return typeof input === "string"
|
||||
? { link: input, expiresAt: null }
|
||||
: {
|
||||
link: input.link,
|
||||
expiresAt: input.expiresAt ?? null,
|
||||
maxUsageCount: input.maxUsageCount,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles multiple private links operations - orchestrates create, update, and delete
|
||||
* @param eventTypeId - The ID of the event type
|
||||
* @param multiplePrivateLinks - Array of links to create/update (can be strings or objects with expiration)
|
||||
* @param connectedMultiplePrivateLinks - Array of existing link IDs for this event type
|
||||
*/
|
||||
async handleMultiplePrivateLinks({
|
||||
eventTypeId,
|
||||
multiplePrivateLinks,
|
||||
connectedMultiplePrivateLinks,
|
||||
}: {
|
||||
eventTypeId: number;
|
||||
multiplePrivateLinks?: (string | HashedLinkInputType)[];
|
||||
connectedMultiplePrivateLinks: string[];
|
||||
}) {
|
||||
if (!eventTypeId || eventTypeId <= 0) {
|
||||
throw new Error("Invalid event type ID");
|
||||
}
|
||||
|
||||
if (!multiplePrivateLinks || multiplePrivateLinks.length === 0) {
|
||||
await this.hashedLinkRepository.deleteLinks(eventTypeId, connectedMultiplePrivateLinks);
|
||||
return;
|
||||
}
|
||||
|
||||
const normalizedLinks = multiplePrivateLinks.map((input) => this.normalizeLinkInput(input));
|
||||
const currentLinks = normalizedLinks.map((l) => l.link);
|
||||
|
||||
const currentLinksSet = new Set(currentLinks);
|
||||
|
||||
const linksToDelete = connectedMultiplePrivateLinks.filter((link) => !currentLinksSet.has(link));
|
||||
await this.hashedLinkRepository.deleteLinks(eventTypeId, linksToDelete);
|
||||
|
||||
const existingLinksSet = new Set(connectedMultiplePrivateLinks);
|
||||
|
||||
for (const linkData of normalizedLinks) {
|
||||
const exists = existingLinksSet.has(linkData.link);
|
||||
if (!exists) {
|
||||
await this.hashedLinkRepository.createLink(eventTypeId, linkData);
|
||||
} else {
|
||||
await this.hashedLinkRepository.updateLink(eventTypeId, linkData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a link without incrementing usage count
|
||||
* Handles both time-based and usage-based expiration with timezone awareness
|
||||
* @param linkId - The hashed link ID to validate
|
||||
* @returns The validated link data
|
||||
* @throws Error with ErrorCode.PrivateLinkExpired if link is expired or invalid
|
||||
*/
|
||||
async validate(linkId: string) {
|
||||
if (!linkId || typeof linkId !== "string") {
|
||||
throw new Error("Invalid link ID");
|
||||
}
|
||||
|
||||
const hashedLink = await this.hashedLinkRepository.findLinkWithValidationData(linkId);
|
||||
|
||||
if (!hashedLink) {
|
||||
throw new Error(ErrorCode.PrivateLinkExpired);
|
||||
}
|
||||
|
||||
validateHashedLinkData(hashedLink);
|
||||
|
||||
return hashedLink;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a link and increments usage count if valid
|
||||
* Handles both time-based and usage-based expiration with timezone awareness
|
||||
* @param linkId - The hashed link ID to validate
|
||||
* @returns The validated link data
|
||||
* @throws Error with ErrorCode.PrivateLinkExpired if link is expired or invalid
|
||||
*/
|
||||
async validateAndIncrementUsage(linkId: string) {
|
||||
const hashedLink = await this.validate(linkId);
|
||||
if (hashedLink.expiresAt) return hashedLink; // Time-based links don't need usage count increment
|
||||
|
||||
if (hashedLink.maxUsageCount && hashedLink.maxUsageCount > 0) {
|
||||
try {
|
||||
await this.hashedLinkRepository.incrementUsage(hashedLink.id, hashedLink.maxUsageCount);
|
||||
} catch (updateError) {
|
||||
throw new Error(ErrorCode.PrivateLinkExpired);
|
||||
}
|
||||
}
|
||||
|
||||
return hashedLink;
|
||||
}
|
||||
|
||||
async findLinkWithDetails(linkId: string) {
|
||||
const hashedLink = await this.hashedLinkRepository.findLinkWithDetails(linkId);
|
||||
if (!hashedLink) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return hashedLink;
|
||||
}
|
||||
|
||||
async createLinkForEventType(eventTypeId: number, input: string | HashedLinkInputType) {
|
||||
const normalized = this.normalizeLinkInput(input);
|
||||
return this.hashedLinkRepository.createLink(eventTypeId, normalized);
|
||||
}
|
||||
|
||||
async updateLinkForEventType(eventTypeId: number, input: string | HashedLinkInputType) {
|
||||
const normalized = this.normalizeLinkInput(input);
|
||||
return this.hashedLinkRepository.updateLink(eventTypeId, normalized);
|
||||
}
|
||||
|
||||
async deleteLinkForEventType(eventTypeId: number, linkId: string) {
|
||||
return this.hashedLinkRepository.deleteLinks(eventTypeId, [linkId]);
|
||||
}
|
||||
|
||||
async listLinksByEventType(eventTypeId: number) {
|
||||
return this.hashedLinkRepository.findLinksByEventTypeId(eventTypeId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a user has permission to access a link
|
||||
* @param link - Link object with event type information
|
||||
* @param userId - The user ID to check permissions for
|
||||
* @returns true if user has permission, false otherwise
|
||||
*/
|
||||
async checkUserPermissionForLink(
|
||||
link: { eventType: { teamId?: number | null; userId?: number | null } },
|
||||
userId: number
|
||||
): Promise<boolean> {
|
||||
if (!link?.eventType || !userId || userId <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If it's a user event type, check if user owns it
|
||||
if (link.eventType.userId) {
|
||||
return link.eventType.userId === userId;
|
||||
}
|
||||
|
||||
//No user ownership and no team - deny access
|
||||
if (!link.eventType.teamId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const membership = await this.membershipService.checkMembership(link.eventType.teamId, userId);
|
||||
return membership.isMember;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user