feat: blocklist table (#24459)

* feat: blocklist table

* feat: blocklist table

* refactor: feedback

* chore: add select

* UI improvements

* chore: remove un unsed

---------

Co-authored-by: CarinaWolli <[email protected]>
This commit is contained in:
Udit TakkarandCarinaWolli authored and GitHub committed 2025-10-15 11:20:49 +00:00
1 parent 17ab088af8
commit 5a59bb86cd
30 files changed
+2209 -24

No files matched your search

+8
View File
@@ -4,6 +4,14 @@ import { z } from "zod";
export const emailRegex =
/^(?!\.)(?!.*\.\.)([A-Z0-9_+-\.']*)[A-Z0-9_+'-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
/**
* Domain regex for watchlist entries - requires @ prefix
* Supports international domains with Unicode characters
* Examples: @example.com, @münchen.de, @example.co.uk
*/
export const domainWithAtRegex =
/^@[a-zA-Z0-9\u00a1-\uffff]([a-zA-Z0-9\u00a1-\uffff-]*[a-zA-Z0-9\u00a1-\uffff])?(\.[a-zA-Z0-9\u00a1-\uffff]([a-zA-Z0-9\u00a1-\uffff-]*[a-zA-Z0-9\u00a1-\uffff])?)*$/;
/**
* RFC 5321 Section 4.5.3.1.3 specifies:
* - Maximum email address length: 254 characters
@@ -0,0 +1,67 @@
import type { WatchlistType, WatchlistAction, WatchlistSource } from "@calcom/prisma/enums";
export interface WatchlistEntry {
id: string;
type: WatchlistType;
value: string;
action: WatchlistAction;
description: string | null;
organizationId: number | null;
isGlobal: boolean;
source: WatchlistSource;
createdAt?: Date;
lastUpdatedAt?: Date;
}
export interface WatchlistAuditEntry {
id: string;
watchlistId: string;
type: WatchlistType;
value: string;
description: string | null;
action: WatchlistAction;
eventType?: string;
changedByUserId: number | null;
changedAt: Date;
}
export interface CreateWatchlistInput {
type: WatchlistType;
value: string;
organizationId: number;
action: WatchlistAction;
description?: string;
userId: number;
}
export interface CheckWatchlistInput {
type: WatchlistType;
value: string;
organizationId: number;
}
export interface FindAllEntriesInput {
organizationId: number;
limit: number;
offset: number;
searchTerm?: string;
filters?: {
type?: WatchlistType;
};
}
export interface IWatchlistRepository {
createEntry(params: CreateWatchlistInput): Promise<WatchlistEntry>;
checkExists(params: CheckWatchlistInput): Promise<WatchlistEntry | null>;
findAllEntries(params: FindAllEntriesInput): Promise<{
rows: (WatchlistEntry & {
audits?: { changedByUserId: number | null }[];
})[];
meta: { totalRowCount: number };
}>;
findEntryWithAudit(id: string): Promise<{
entry: WatchlistEntry | null;
auditHistory: WatchlistAuditEntry[];
}>;
deleteEntry(id: string, userId: number): Promise<void>;
}
@@ -0,0 +1,211 @@
import type { PrismaClient } from "@calcom/prisma";
import { WatchlistSource } from "@calcom/prisma/enums";
import type {
IWatchlistRepository,
CreateWatchlistInput,
CheckWatchlistInput,
WatchlistEntry,
FindAllEntriesInput,
WatchlistAuditEntry,
} from "./watchlist.interface";
export class WatchlistRepository implements IWatchlistRepository {
constructor(private readonly prismaClient: PrismaClient) {}
async createEntry(params: CreateWatchlistInput): Promise<WatchlistEntry> {
const existing = await this.checkExists({
type: params.type,
value: params.value,
organizationId: params.organizationId,
});
if (existing) {
throw new Error("Watchlist entry already exists for this organization");
}
const watchlist = await this.prismaClient.$transaction(async (tx) => {
const created = await tx.watchlist.create({
data: {
type: params.type,
value: params.value,
organizationId: params.organizationId,
action: params.action,
description: params.description,
source: WatchlistSource.MANUAL,
isGlobal: false,
},
});
await tx.watchlistAudit.create({
data: {
watchlistId: created.id,
type: params.type,
value: params.value,
description: params.description,
action: params.action,
changedByUserId: params.userId,
},
});
return created;
});
return watchlist;
}
async checkExists(params: CheckWatchlistInput): Promise<WatchlistEntry | null> {
const entry = await this.prismaClient.watchlist.findUnique({
where: {
type_value_organizationId: {
type: params.type,
value: params.value,
organizationId: params.organizationId,
},
},
});
return entry;
}
async findAllEntries(params: FindAllEntriesInput): Promise<{
rows: (WatchlistEntry & { audits?: { changedByUserId: number | null }[] })[];
meta: { totalRowCount: number };
}> {
const where = {
organizationId: params.organizationId,
...(params.searchTerm && {
value: {
contains: params.searchTerm,
mode: "insensitive" as const,
},
}),
...(params.filters?.type && {
type: params.filters.type,
}),
};
const [rows, totalRowCount] = await Promise.all([
this.prismaClient.watchlist.findMany({
where,
take: params.limit,
skip: params.offset,
orderBy: {
lastUpdatedAt: "desc",
},
select: {
id: true,
type: true,
value: true,
action: true,
description: true,
organizationId: true,
isGlobal: true,
source: true,
lastUpdatedAt: true,
audits: {
take: 1,
orderBy: {
changedAt: "desc",
},
select: {
changedByUserId: true,
},
},
},
}),
this.prismaClient.watchlist.count({ where }),
]);
return {
rows,
meta: { totalRowCount },
};
}
async findEntryWithAudit(id: string): Promise<{
entry: WatchlistEntry | null;
auditHistory: WatchlistAuditEntry[];
}> {
const entry = await this.prismaClient.watchlist.findUnique({
where: { id },
select: {
id: true,
type: true,
value: true,
action: true,
description: true,
organizationId: true,
isGlobal: true,
source: true,
lastUpdatedAt: true,
audits: {
select: {
id: true,
watchlistId: true,
type: true,
value: true,
description: true,
action: true,
changedByUserId: true,
changedAt: true,
},
orderBy: {
changedAt: "desc",
},
},
},
});
return {
entry: entry
? {
id: entry.id,
type: entry.type,
value: entry.value,
action: entry.action,
description: entry.description ?? null,
organizationId: entry.organizationId ?? null,
isGlobal: entry.isGlobal,
source: entry.source,
lastUpdatedAt: entry.lastUpdatedAt,
}
: null,
auditHistory: entry?.audits || [],
};
}
async deleteEntry(id: string, userId: number): Promise<void> {
const existing = await this.prismaClient.watchlist.findUnique({
where: { id },
select: {
id: true,
type: true,
value: true,
description: true,
action: true,
},
});
if (!existing) {
throw new Error("Watchlist entry not found");
}
await this.prismaClient.$transaction(async (tx) => {
await tx.watchlistAudit.create({
data: {
watchlistId: id,
type: existing.type,
value: existing.value,
description: existing.description,
action: existing.action,
changedByUserId: userId,
},
});
await tx.watchlist.delete({
where: { id },
});
});
}
}