refactor: Move SelectedCalendarRepository and WorkflowRelationsRepository from /lib to /features (#25982)
* wip * wip
This commit is contained in:
1 parent
84ecb0e568
commit
03dd0c1501
9 files changed
+7
-8
No files matched your search
@@ -10,8 +10,8 @@ import type {
|
||||
import type { CalendarCacheEventService } from "@calcom/features/calendar-subscription/lib/cache/CalendarCacheEventService";
|
||||
import type { CalendarSyncService } from "@calcom/features/calendar-subscription/lib/sync/CalendarSyncService";
|
||||
import type { FeaturesRepository } from "@calcom/features/flags/features.repository";
|
||||
import type { ISelectedCalendarRepository } from "@calcom/features/selectedCalendar/repositories/SelectedCalendarRepository.interface";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import type { ISelectedCalendarRepository } from "@calcom/lib/server/repository/SelectedCalendarRepository.interface";
|
||||
import type { SelectedCalendar } from "@calcom/prisma/client";
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["CalendarSubscriptionService"] });
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ import type { AdapterFactory } from "@calcom/features/calendar-subscription/adap
|
||||
import type { CalendarCacheEventService } from "@calcom/features/calendar-subscription/lib/cache/CalendarCacheEventService";
|
||||
import type { CalendarSyncService } from "@calcom/features/calendar-subscription/lib/sync/CalendarSyncService";
|
||||
import type { FeaturesRepository } from "@calcom/features/flags/features.repository";
|
||||
import type { ISelectedCalendarRepository } from "@calcom/lib/server/repository/SelectedCalendarRepository.interface";
|
||||
import type { ISelectedCalendarRepository } from "@calcom/features/selectedCalendar/repositories/SelectedCalendarRepository.interface";
|
||||
import type { SelectedCalendar } from "@calcom/prisma/client";
|
||||
|
||||
import { CalendarSubscriptionService } from "../CalendarSubscriptionService";
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
|
||||
export class WorkflowRelationsRepository {
|
||||
constructor(private prismaClient: PrismaClient) {}
|
||||
|
||||
// Team relationship methods
|
||||
async deleteAllActiveOnTeams(workflowId: number) {
|
||||
return await this.prismaClient.workflowsOnTeams.deleteMany({
|
||||
where: { workflowId },
|
||||
});
|
||||
}
|
||||
|
||||
async createActiveOnTeams(workflowId: number, teamIds: number[]) {
|
||||
if (teamIds.length === 0) return;
|
||||
|
||||
return await this.prismaClient.workflowsOnTeams.createMany({
|
||||
data: teamIds.map((teamId) => ({
|
||||
workflowId,
|
||||
teamId,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
async findActiveOnTeams(workflowId: number) {
|
||||
return await this.prismaClient.workflowsOnTeams.findMany({
|
||||
where: { workflowId },
|
||||
select: {
|
||||
teamId: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Routing Forms relationship methods
|
||||
async deleteAllActiveOnRoutingForms(workflowId: number) {
|
||||
return await this.prismaClient.workflowsOnRoutingForms.deleteMany({
|
||||
where: { workflowId },
|
||||
});
|
||||
}
|
||||
|
||||
async createActiveOnRoutingForms(workflowId: number, routingFormIds: string[]) {
|
||||
if (routingFormIds.length === 0) return;
|
||||
|
||||
return await this.prismaClient.workflowsOnRoutingForms.createMany({
|
||||
data: routingFormIds.map((routingFormId) => ({
|
||||
workflowId,
|
||||
routingFormId,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
// Event Types relationship methods
|
||||
async deleteAllActiveOnEventTypes(workflowId: number) {
|
||||
return await this.prismaClient.workflowsOnEventTypes.deleteMany({
|
||||
where: { workflowId },
|
||||
});
|
||||
}
|
||||
|
||||
async createActiveOnEventTypes(workflowId: number, eventTypeIds: number[]) {
|
||||
if (eventTypeIds.length === 0) return;
|
||||
|
||||
return await this.prismaClient.workflowsOnEventTypes.createMany({
|
||||
data: eventTypeIds.map((eventTypeId) => ({
|
||||
workflowId,
|
||||
eventTypeId,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
async findActiveOnEventTypes(workflowId: number) {
|
||||
return await this.prismaClient.workflowsOnEventTypes.findMany({
|
||||
where: { workflowId },
|
||||
select: {
|
||||
eventTypeId: true,
|
||||
eventType: {
|
||||
select: {
|
||||
children: {
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async deleteAllActiveOnRelations(workflowId: number) {
|
||||
return await Promise.all([
|
||||
this.deleteAllActiveOnTeams(workflowId),
|
||||
this.deleteAllActiveOnRoutingForms(workflowId),
|
||||
this.deleteAllActiveOnEventTypes(workflowId),
|
||||
]);
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
import type { Prisma, SelectedCalendar } from "@calcom/prisma/client";
|
||||
|
||||
export interface ISelectedCalendarRepository {
|
||||
/**
|
||||
* Find selected calendar by id
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
findById(id: string): Promise<SelectedCalendar | null>;
|
||||
|
||||
/**
|
||||
* Find selected calendar by channel id
|
||||
*
|
||||
* @param channelId
|
||||
*/
|
||||
findByChannelId(channelId: string): Promise<SelectedCalendar | null>;
|
||||
|
||||
/**
|
||||
* Find next batch of selected calendars
|
||||
* Will check if syncSubscribedAt is null or channelExpiration is greater than current date
|
||||
*
|
||||
* @param take the number of calendars to take
|
||||
* @param integrations the list of integrations
|
||||
*/
|
||||
findNextSubscriptionBatch({
|
||||
take,
|
||||
teamIds,
|
||||
integrations,
|
||||
}: {
|
||||
take: number;
|
||||
teamIds: number[];
|
||||
integrations?: string[];
|
||||
}): Promise<SelectedCalendar[]>;
|
||||
|
||||
/**
|
||||
* Update status of sync for selected calendar
|
||||
*
|
||||
* @param id
|
||||
* @param data
|
||||
*/
|
||||
updateSyncStatus(
|
||||
id: string,
|
||||
data: Pick<
|
||||
Prisma.SelectedCalendarUpdateInput,
|
||||
"syncToken" | "syncedAt" | "syncErrorAt" | "syncErrorCount"
|
||||
>
|
||||
): Promise<SelectedCalendar>;
|
||||
|
||||
/**
|
||||
* Update subscription status for selected calendar
|
||||
*/
|
||||
updateSubscription(
|
||||
id: string,
|
||||
data: Pick<
|
||||
Prisma.SelectedCalendarUpdateInput,
|
||||
| "channelId"
|
||||
| "channelResourceId"
|
||||
| "channelResourceUri"
|
||||
| "channelKind"
|
||||
| "channelExpiration"
|
||||
| "syncSubscribedAt"
|
||||
>
|
||||
): Promise<SelectedCalendar>;
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
import { describe, test, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
import { SelectedCalendarRepository } from "@calcom/features/selectedCalendar/repositories/SelectedCalendarRepository";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import type { Prisma, SelectedCalendar } from "@calcom/prisma/client";
|
||||
|
||||
const mockPrismaClient = {
|
||||
selectedCalendar: {
|
||||
findUnique: vi.fn(),
|
||||
findFirst: vi.fn(),
|
||||
findMany: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
} as unknown as PrismaClient;
|
||||
|
||||
const mockSelectedCalendar: SelectedCalendar = {
|
||||
id: "test-calendar-id",
|
||||
userId: 1,
|
||||
credentialId: 1,
|
||||
integration: "google_calendar",
|
||||
externalId: "[email protected]",
|
||||
eventTypeId: null,
|
||||
delegationCredentialId: null,
|
||||
domainWideDelegationCredentialId: null,
|
||||
googleChannelId: null,
|
||||
googleChannelKind: null,
|
||||
googleChannelResourceId: null,
|
||||
googleChannelResourceUri: null,
|
||||
googleChannelExpiration: null,
|
||||
error: null,
|
||||
lastErrorAt: null,
|
||||
watchAttempts: 0,
|
||||
maxAttempts: 3,
|
||||
unwatchAttempts: 0,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
channelId: "test-channel-id",
|
||||
channelKind: "web_hook",
|
||||
channelResourceId: "test-resource-id",
|
||||
channelResourceUri: "test-resource-uri",
|
||||
channelExpiration: new Date(Date.now() + 86400000),
|
||||
syncSubscribedAt: new Date(),
|
||||
syncToken: "test-sync-token",
|
||||
syncedAt: new Date(),
|
||||
syncErrorAt: null,
|
||||
syncErrorCount: 0,
|
||||
};
|
||||
|
||||
describe("SelectedCalendarRepository", () => {
|
||||
let repository: SelectedCalendarRepository;
|
||||
|
||||
beforeEach(() => {
|
||||
repository = new SelectedCalendarRepository(mockPrismaClient);
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("findById", () => {
|
||||
test("should find selected calendar by id with credential delegation", async () => {
|
||||
const mockCalendarWithCredential = {
|
||||
...mockSelectedCalendar,
|
||||
credential: {
|
||||
delegationCredential: {
|
||||
id: "delegation-id",
|
||||
key: { client_email: "[email protected]" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
vi.mocked(mockPrismaClient.selectedCalendar.findUnique).mockResolvedValue(mockCalendarWithCredential);
|
||||
|
||||
const result = await repository.findById("test-calendar-id");
|
||||
|
||||
expect(mockPrismaClient.selectedCalendar.findUnique).toHaveBeenCalledWith({
|
||||
where: { id: "test-calendar-id" },
|
||||
});
|
||||
|
||||
expect(result).toEqual(mockCalendarWithCredential);
|
||||
});
|
||||
|
||||
test("should return null when calendar not found", async () => {
|
||||
vi.mocked(mockPrismaClient.selectedCalendar.findUnique).mockResolvedValue(null);
|
||||
|
||||
const result = await repository.findById("non-existent-id");
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("findByChannelId", () => {
|
||||
test("should find selected calendar by channel id", async () => {
|
||||
vi.mocked(mockPrismaClient.selectedCalendar.findFirst).mockResolvedValue(mockSelectedCalendar);
|
||||
|
||||
const result = await repository.findByChannelId("test-channel-id");
|
||||
|
||||
expect(mockPrismaClient.selectedCalendar.findFirst).toHaveBeenCalledWith({
|
||||
where: { channelId: "test-channel-id" },
|
||||
});
|
||||
|
||||
expect(result).toEqual(mockSelectedCalendar);
|
||||
});
|
||||
|
||||
test("should return null when calendar not found", async () => {
|
||||
vi.mocked(mockPrismaClient.selectedCalendar.findFirst).mockResolvedValue(null);
|
||||
|
||||
const result = await repository.findByChannelId("non-existent-channel-id");
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("findNextSubscriptionBatch", () => {
|
||||
test("should find next batch of calendars for subscription", async () => {
|
||||
const mockCalendars = [mockSelectedCalendar];
|
||||
vi.mocked(mockPrismaClient.selectedCalendar.findMany).mockResolvedValue(mockCalendars);
|
||||
|
||||
const result = await repository.findNextSubscriptionBatch({
|
||||
take: 10,
|
||||
teamIds: [1, 2, 3],
|
||||
integrations: ["google_calendar", "office365_calendar"],
|
||||
});
|
||||
|
||||
expect(mockPrismaClient.selectedCalendar.findMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
integration: { in: ["google_calendar", "office365_calendar"] },
|
||||
OR: [{ syncSubscribedAt: null }, { channelExpiration: { lte: expect.any(Date) } }],
|
||||
user: {
|
||||
teams: {
|
||||
some: {
|
||||
teamId: { in: [1, 2, 3] },
|
||||
accepted: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
take: 10,
|
||||
});
|
||||
|
||||
expect(result).toEqual(mockCalendars);
|
||||
});
|
||||
|
||||
test("should handle empty integrations array", async () => {
|
||||
const mockCalendars = [mockSelectedCalendar];
|
||||
vi.mocked(mockPrismaClient.selectedCalendar.findMany).mockResolvedValue(mockCalendars);
|
||||
|
||||
const result = await repository.findNextSubscriptionBatch({
|
||||
take: 5,
|
||||
teamIds: [10, 20],
|
||||
integrations: [],
|
||||
});
|
||||
|
||||
expect(mockPrismaClient.selectedCalendar.findMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
integration: { in: [] },
|
||||
OR: [{ syncSubscribedAt: null }, { channelExpiration: { lte: expect.any(Date) } }],
|
||||
user: {
|
||||
teams: {
|
||||
some: {
|
||||
teamId: { in: [10, 20] },
|
||||
accepted: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
take: 5,
|
||||
});
|
||||
|
||||
expect(result).toEqual(mockCalendars);
|
||||
});
|
||||
|
||||
test("should handle empty teamIds array", async () => {
|
||||
const mockCalendars: SelectedCalendar[] = [];
|
||||
vi.mocked(mockPrismaClient.selectedCalendar.findMany).mockResolvedValue(mockCalendars);
|
||||
|
||||
const result = await repository.findNextSubscriptionBatch({
|
||||
take: 10,
|
||||
teamIds: [],
|
||||
integrations: ["google_calendar"],
|
||||
});
|
||||
|
||||
expect(mockPrismaClient.selectedCalendar.findMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
integration: { in: ["google_calendar"] },
|
||||
OR: [{ syncSubscribedAt: null }, { channelExpiration: { lte: expect.any(Date) } }],
|
||||
user: {
|
||||
teams: {
|
||||
some: {
|
||||
teamId: { in: [] },
|
||||
accepted: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
take: 10,
|
||||
});
|
||||
|
||||
expect(result).toEqual(mockCalendars);
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateSyncStatus", () => {
|
||||
test("should update sync status", async () => {
|
||||
const updateData: Pick<
|
||||
Prisma.SelectedCalendarUpdateInput,
|
||||
"syncToken" | "syncedAt" | "syncErrorAt" | "syncErrorCount"
|
||||
> = {
|
||||
syncToken: "new-sync-token",
|
||||
syncedAt: new Date(),
|
||||
syncErrorAt: null,
|
||||
syncErrorCount: 0,
|
||||
};
|
||||
|
||||
const updatedCalendar = {
|
||||
...mockSelectedCalendar,
|
||||
...updateData,
|
||||
};
|
||||
|
||||
vi.mocked(mockPrismaClient.selectedCalendar.update).mockResolvedValue(updatedCalendar);
|
||||
|
||||
const result = await repository.updateSyncStatus("test-calendar-id", updateData);
|
||||
|
||||
expect(mockPrismaClient.selectedCalendar.update).toHaveBeenCalledWith({
|
||||
where: { id: "test-calendar-id" },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
expect(result).toEqual(updatedCalendar);
|
||||
});
|
||||
|
||||
test("should update sync error status", async () => {
|
||||
const updateData: Pick<
|
||||
Prisma.SelectedCalendarUpdateInput,
|
||||
"syncToken" | "syncedAt" | "syncErrorAt" | "syncErrorCount"
|
||||
> = {
|
||||
syncErrorAt: new Date(),
|
||||
syncErrorCount: 1,
|
||||
};
|
||||
|
||||
const updatedCalendar = {
|
||||
...mockSelectedCalendar,
|
||||
...updateData,
|
||||
};
|
||||
|
||||
vi.mocked(mockPrismaClient.selectedCalendar.update).mockResolvedValue(updatedCalendar);
|
||||
|
||||
const result = await repository.updateSyncStatus("test-calendar-id", updateData);
|
||||
|
||||
expect(mockPrismaClient.selectedCalendar.update).toHaveBeenCalledWith({
|
||||
where: { id: "test-calendar-id" },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
expect(result).toEqual(updatedCalendar);
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateSubscription", () => {
|
||||
test("should update subscription status", async () => {
|
||||
const updateData: Pick<
|
||||
Prisma.SelectedCalendarUpdateInput,
|
||||
| "channelId"
|
||||
| "channelResourceId"
|
||||
| "channelResourceUri"
|
||||
| "channelKind"
|
||||
| "channelExpiration"
|
||||
| "syncSubscribedAt"
|
||||
> = {
|
||||
channelId: "new-channel-id",
|
||||
channelResourceId: "new-resource-id",
|
||||
channelResourceUri: "new-resource-uri",
|
||||
channelKind: "web_hook",
|
||||
channelExpiration: new Date(Date.now() + 86400000),
|
||||
syncSubscribedAt: new Date(),
|
||||
};
|
||||
|
||||
const updatedCalendar = {
|
||||
...mockSelectedCalendar,
|
||||
...updateData,
|
||||
};
|
||||
|
||||
vi.mocked(mockPrismaClient.selectedCalendar.update).mockResolvedValue(updatedCalendar);
|
||||
|
||||
const result = await repository.updateSubscription("test-calendar-id", updateData);
|
||||
|
||||
expect(mockPrismaClient.selectedCalendar.update).toHaveBeenCalledWith({
|
||||
where: { id: "test-calendar-id" },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
expect(result).toEqual(updatedCalendar);
|
||||
});
|
||||
|
||||
test("should unsubscribe by setting syncSubscribedAt to null", async () => {
|
||||
const updateData: Pick<
|
||||
Prisma.SelectedCalendarUpdateInput,
|
||||
| "channelId"
|
||||
| "channelResourceId"
|
||||
| "channelResourceUri"
|
||||
| "channelKind"
|
||||
| "channelExpiration"
|
||||
| "syncSubscribedAt"
|
||||
> = {
|
||||
syncSubscribedAt: null,
|
||||
};
|
||||
|
||||
const updatedCalendar = {
|
||||
...mockSelectedCalendar,
|
||||
syncSubscribedAt: null,
|
||||
};
|
||||
|
||||
vi.mocked(mockPrismaClient.selectedCalendar.update).mockResolvedValue(updatedCalendar);
|
||||
|
||||
const result = await repository.updateSubscription("test-calendar-id", updateData);
|
||||
|
||||
expect(mockPrismaClient.selectedCalendar.update).toHaveBeenCalledWith({
|
||||
where: { id: "test-calendar-id" },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
expect(result).toEqual(updatedCalendar);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,74 @@
|
||||
import type { ISelectedCalendarRepository } from "@calcom/features/selectedCalendar/repositories/SelectedCalendarRepository.interface";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
|
||||
export class SelectedCalendarRepository implements ISelectedCalendarRepository {
|
||||
constructor(private prismaClient: PrismaClient) {}
|
||||
|
||||
async findById(id: string) {
|
||||
return this.prismaClient.selectedCalendar.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
async findByChannelId(channelId: string) {
|
||||
return this.prismaClient.selectedCalendar.findFirst({ where: { channelId } });
|
||||
}
|
||||
|
||||
async findNextSubscriptionBatch({
|
||||
take,
|
||||
teamIds,
|
||||
integrations,
|
||||
}: {
|
||||
take: number;
|
||||
teamIds: number[];
|
||||
integrations: string[];
|
||||
}) {
|
||||
return this.prismaClient.selectedCalendar.findMany({
|
||||
where: {
|
||||
integration: { in: integrations },
|
||||
OR: [{ syncSubscribedAt: null }, { channelExpiration: { lte: new Date() } }],
|
||||
user: {
|
||||
teams: {
|
||||
some: {
|
||||
teamId: { in: teamIds },
|
||||
accepted: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
take,
|
||||
});
|
||||
}
|
||||
|
||||
async updateSyncStatus(
|
||||
id: string,
|
||||
data: Pick<
|
||||
Prisma.SelectedCalendarUpdateInput,
|
||||
"syncToken" | "syncedAt" | "syncErrorAt" | "syncErrorCount"
|
||||
>
|
||||
) {
|
||||
return this.prismaClient.selectedCalendar.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
async updateSubscription(
|
||||
id: string,
|
||||
data: Pick<
|
||||
Prisma.SelectedCalendarUpdateInput,
|
||||
| "channelId"
|
||||
| "channelResourceId"
|
||||
| "channelResourceUri"
|
||||
| "channelKind"
|
||||
| "channelExpiration"
|
||||
| "syncSubscribedAt"
|
||||
>
|
||||
) {
|
||||
return this.prismaClient.selectedCalendar.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user