This commit is contained in:
1 parent
50df22928b
commit
7c0ee9f186
38 files changed
+920
-240
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import type { Credentials } from "google-auth-library";
|
||||
|
||||
import { CredentialRepository } from "./credential";
|
||||
@@ -29,6 +30,15 @@ export class GoogleRepository {
|
||||
});
|
||||
}
|
||||
|
||||
static async upsertSelectedCalendar(
|
||||
data: Omit<Prisma.SelectedCalendarUncheckedCreateInput, "integration">
|
||||
) {
|
||||
return await SelectedCalendarRepository.upsert({
|
||||
...data,
|
||||
integration: "google_calendar",
|
||||
});
|
||||
}
|
||||
|
||||
static async findGoogleMeetCredential({ userId }: { userId: number }) {
|
||||
return await CredentialRepository.findFirstByUserIdAndType({
|
||||
userId,
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
||||
|
||||
type SelectedCalendarCreateInput = {
|
||||
credentialId: number;
|
||||
@@ -15,4 +18,108 @@ export class SelectedCalendarRepository {
|
||||
},
|
||||
});
|
||||
}
|
||||
static async upsert(data: Prisma.SelectedCalendarUncheckedCreateInput) {
|
||||
return await prisma.selectedCalendar.upsert({
|
||||
where: {
|
||||
userId_integration_externalId: {
|
||||
userId: data.userId,
|
||||
integration: data.integration,
|
||||
externalId: data.externalId,
|
||||
},
|
||||
},
|
||||
create: { ...data },
|
||||
update: { ...data },
|
||||
});
|
||||
}
|
||||
/** Retrieve calendars that need to be watched */
|
||||
static async getNextBatchToWatch(limit = 100) {
|
||||
// Get selected calendars from users that belong to a team that has calendar cache enabled
|
||||
const oneDayInMS = 24 * 60 * 60 * 1000;
|
||||
const tomorrowTimestamp = String(new Date().getTime() + oneDayInMS);
|
||||
const nextBatch = await prisma.selectedCalendar.findMany({
|
||||
take: limit,
|
||||
where: {
|
||||
user: {
|
||||
teams: {
|
||||
some: {
|
||||
team: {
|
||||
features: {
|
||||
some: {
|
||||
featureId: "calendar-cache",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
// RN we only support google calendar subscriptions for now
|
||||
integration: "google_calendar",
|
||||
OR: [
|
||||
// Either is a calendar pending to be watched
|
||||
{ googleChannelExpiration: null },
|
||||
// Or is a calendar that is about to expire
|
||||
{ googleChannelExpiration: { lt: tomorrowTimestamp } },
|
||||
],
|
||||
},
|
||||
});
|
||||
return nextBatch;
|
||||
}
|
||||
/** Retrieve calendars that are being watched but shouldn't be anymore */
|
||||
static async getNextBatchToUnwatch(limit = 100) {
|
||||
const nextBatch = await prisma.selectedCalendar.findMany({
|
||||
take: limit,
|
||||
where: {
|
||||
user: {
|
||||
teams: {
|
||||
every: {
|
||||
team: {
|
||||
features: {
|
||||
none: {
|
||||
featureId: "calendar-cache",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
// RN we only support google calendar subscriptions for now
|
||||
integration: "google_calendar",
|
||||
googleChannelExpiration: { not: null },
|
||||
},
|
||||
});
|
||||
return nextBatch;
|
||||
}
|
||||
static async delete(data: Prisma.SelectedCalendarUncheckedCreateInput) {
|
||||
return await prisma.selectedCalendar.delete({
|
||||
where: {
|
||||
userId_integration_externalId: {
|
||||
userId: data.userId,
|
||||
externalId: data.externalId,
|
||||
integration: data.integration,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
static async findMany(args: Prisma.SelectedCalendarFindManyArgs) {
|
||||
return await prisma.selectedCalendar.findMany(args);
|
||||
}
|
||||
static async findByGoogleChannelId(googleChannelId: string) {
|
||||
return await prisma.selectedCalendar.findUnique({
|
||||
where: {
|
||||
googleChannelId,
|
||||
},
|
||||
select: {
|
||||
credential: {
|
||||
select: {
|
||||
...credentialForCalendarServiceSelect,
|
||||
selectedCalendars: {
|
||||
orderBy: {
|
||||
externalId: "asc",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import prisma from "@calcom/prisma";
|
||||
import { Prisma } from "@calcom/prisma/client";
|
||||
import type { User as UserType } from "@calcom/prisma/client";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
||||
import { userMetadata } from "@calcom/prisma/zod-utils";
|
||||
import type { UpId, UserProfile } from "@calcom/types/UserProfile";
|
||||
|
||||
@@ -740,4 +741,19 @@ export class UserRepository {
|
||||
},
|
||||
});
|
||||
}
|
||||
static async findUserWithCredentials({ id }: { id: number }) {
|
||||
return await prisma.user.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
select: {
|
||||
credentials: {
|
||||
select: credentialForCalendarServiceSelect,
|
||||
},
|
||||
timeZone: true,
|
||||
id: true,
|
||||
selectedCalendars: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user