"use strict";
const httpError = require("./http-error-mkY8Ke7V.cjs");
const credential = require("./credential-Uug66ge2.cjs");
const index = require("./user-v6mfWQ2d.cjs");
async function checkInstalled(slug, userId) {
  const alreadyInstalled = await credential.CredentialRepository.findByAppIdAndUserId({ appId: slug, userId });
  if (alreadyInstalled) {
    throw new httpError.HttpError({ statusCode: 422, message: "Already installed" });
  }
}
async function createDefaultInstallation({
  appType,
  user,
  slug,
  key = {},
  teamId,
  billingCycleStart,
  paymentStatus,
  subscriptionId
}) {
  const installation = await index.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;
}
exports.checkInstalled = checkInstalled;
exports.createDefaultInstallation = createDefaultInstallation;
