mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-04 11:30:38 -05:00
02b25138ef
Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com>
68 lines
2.0 KiB
TypeScript
68 lines
2.0 KiB
TypeScript
"use server";
|
|
|
|
import { authenticatedActionClient } from "@/lib/utils/action-client";
|
|
import { checkAuthorizationUpdated } from "@/lib/utils/action-client-middleware";
|
|
import {
|
|
getOrganizationIdFromApiKeyId,
|
|
getOrganizationIdFromEnvironmentId,
|
|
getProjectIdFromApiKeyId,
|
|
getProjectIdFromEnvironmentId,
|
|
} from "@/lib/utils/helper";
|
|
import { createApiKey, deleteApiKey } from "@/modules/projects/settings/api-keys/lib/api-key";
|
|
import { z } from "zod";
|
|
import { ZId } from "@formbricks/types/common";
|
|
import { ZApiKeyCreateInput } from "./types/api-keys";
|
|
|
|
const ZDeleteApiKeyAction = z.object({
|
|
id: ZId,
|
|
});
|
|
|
|
export const deleteApiKeyAction = authenticatedActionClient
|
|
.schema(ZDeleteApiKeyAction)
|
|
.action(async ({ ctx, parsedInput }) => {
|
|
await checkAuthorizationUpdated({
|
|
userId: ctx.user.id,
|
|
organizationId: await getOrganizationIdFromApiKeyId(parsedInput.id),
|
|
access: [
|
|
{
|
|
type: "organization",
|
|
roles: ["owner", "manager"],
|
|
},
|
|
{
|
|
type: "projectTeam",
|
|
minPermission: "manage",
|
|
projectId: await getProjectIdFromApiKeyId(parsedInput.id),
|
|
},
|
|
],
|
|
});
|
|
|
|
return await deleteApiKey(parsedInput.id);
|
|
});
|
|
|
|
const ZCreateApiKeyAction = z.object({
|
|
environmentId: ZId,
|
|
apiKeyData: ZApiKeyCreateInput,
|
|
});
|
|
|
|
export const createApiKeyAction = authenticatedActionClient
|
|
.schema(ZCreateApiKeyAction)
|
|
.action(async ({ ctx, parsedInput }) => {
|
|
await checkAuthorizationUpdated({
|
|
userId: ctx.user.id,
|
|
organizationId: await getOrganizationIdFromEnvironmentId(parsedInput.environmentId),
|
|
access: [
|
|
{
|
|
type: "organization",
|
|
roles: ["owner", "manager"],
|
|
},
|
|
{
|
|
type: "projectTeam",
|
|
minPermission: "manage",
|
|
projectId: await getProjectIdFromEnvironmentId(parsedInput.environmentId),
|
|
},
|
|
],
|
|
});
|
|
|
|
return await createApiKey(parsedInput.environmentId, parsedInput.apiKeyData);
|
|
});
|