* 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
59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import { HttpError } from "@calcom/lib/http-error";
|
|
import { CredentialRepository } from "@calcom/features/credentials/repositories/CredentialRepository";
|
|
import prisma from "@calcom/prisma";
|
|
import type { Prisma } from "@calcom/prisma/client";
|
|
import type { UserProfile } from "@calcom/types/UserProfile";
|
|
|
|
export async function checkInstalled(slug: string, userId: number) {
|
|
const alreadyInstalled = await CredentialRepository.findByAppIdAndUserId({ appId: slug, userId });
|
|
if (alreadyInstalled) {
|
|
throw new HttpError({ statusCode: 422, message: "Already installed" });
|
|
}
|
|
}
|
|
|
|
export async function isAppInstalled({ appId, userId }: { appId: string; userId: number }) {
|
|
const alreadyInstalled = await CredentialRepository.findByAppIdAndUserId({ appId, userId });
|
|
return !!alreadyInstalled;
|
|
}
|
|
|
|
type InstallationArgs = {
|
|
appType: string;
|
|
user: {
|
|
id: number;
|
|
profile?: UserProfile;
|
|
};
|
|
slug: string;
|
|
key?: Prisma.InputJsonValue;
|
|
teamId?: number;
|
|
subscriptionId?: string | null;
|
|
paymentStatus?: string | null;
|
|
billingCycleStart?: number | null;
|
|
};
|
|
|
|
export async function createDefaultInstallation({
|
|
appType,
|
|
user,
|
|
slug,
|
|
key = {},
|
|
teamId,
|
|
billingCycleStart,
|
|
paymentStatus,
|
|
subscriptionId,
|
|
}: InstallationArgs) {
|
|
const installation = await prisma.credential.create({
|
|
data: {
|
|
type: appType,
|
|
key,
|
|
...(teamId ? { teamId } : { userId: user.id }),
|
|
appId: slug,
|
|
subscriptionId,
|
|
paymentStatus,
|
|
billingCycleStart,
|
|
},
|
|
});
|
|
if (!installation) {
|
|
throw new Error(`Unable to create user credential for type ${appType}`);
|
|
}
|
|
return installation;
|
|
}
|