mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-03 11:30:50 -05:00
8b5328aa74
Co-authored-by: Johannes <johannes@formbricks.com> Co-authored-by: Johannes <72809645+jobenjada@users.noreply.github.com> Co-authored-by: Matthias Nannt <mail@matthiasnannt.com> Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com>
80 lines
3.0 KiB
TypeScript
80 lines
3.0 KiB
TypeScript
"use server";
|
|
|
|
import { getServerSession } from "next-auth";
|
|
|
|
import { authOptions } from "@formbricks/lib/authOptions";
|
|
import {
|
|
createLanguage,
|
|
deleteLanguage,
|
|
getSurveysUsingGivenLanguage,
|
|
updateLanguage,
|
|
} from "@formbricks/lib/language/service";
|
|
import { canUserAccessProduct, verifyUserRoleAccess } from "@formbricks/lib/product/auth";
|
|
import { getProduct } from "@formbricks/lib/product/service";
|
|
import { AuthorizationError } from "@formbricks/types/errors";
|
|
import { TLanguageInput } from "@formbricks/types/product";
|
|
|
|
export async function createLanguageAction(
|
|
productId: string,
|
|
environmentId: string,
|
|
languageInput: TLanguageInput
|
|
) {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session) throw new AuthorizationError("Not authorized");
|
|
|
|
const isAuthorized = await canUserAccessProduct(session.user?.id, productId);
|
|
if (!isAuthorized) throw new AuthorizationError("Not authorized");
|
|
|
|
const product = await getProduct(productId);
|
|
|
|
const { hasCreateOrUpdateAccess } = await verifyUserRoleAccess(product!.teamId, session.user?.id);
|
|
if (!hasCreateOrUpdateAccess) throw new AuthorizationError("Not authorized");
|
|
|
|
return await createLanguage(productId, environmentId, languageInput);
|
|
}
|
|
|
|
export async function deleteLanguageAction(productId: string, environmentId: string, languageId: string) {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session) throw new AuthorizationError("Not authorized");
|
|
|
|
const isAuthorized = await canUserAccessProduct(session.user?.id, productId);
|
|
if (!isAuthorized) throw new AuthorizationError("Not authorized");
|
|
|
|
const product = await getProduct(productId);
|
|
|
|
const { hasCreateOrUpdateAccess } = await verifyUserRoleAccess(product!.teamId, session.user?.id);
|
|
if (!hasCreateOrUpdateAccess) throw new AuthorizationError("Not authorized");
|
|
|
|
return await deleteLanguage(environmentId, languageId);
|
|
}
|
|
|
|
export async function getSurveysUsingGivenLanguageAction(productId: string, languageId: string) {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session) throw new AuthorizationError("Not authorized");
|
|
|
|
const isAuthorized = await canUserAccessProduct(session.user?.id, productId);
|
|
if (!isAuthorized) throw new AuthorizationError("Not authorized");
|
|
|
|
return await getSurveysUsingGivenLanguage(languageId);
|
|
}
|
|
|
|
export async function updateLanguageAction(
|
|
productId: string,
|
|
environmentId: string,
|
|
languageId: string,
|
|
languageInput: TLanguageInput
|
|
) {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session) throw new AuthorizationError("Not authorized");
|
|
|
|
const isAuthorized = await canUserAccessProduct(session.user?.id, productId);
|
|
if (!isAuthorized) throw new AuthorizationError("Not authorized");
|
|
|
|
const product = await getProduct(productId);
|
|
|
|
const { hasCreateOrUpdateAccess } = await verifyUserRoleAccess(product!.teamId, session.user?.id);
|
|
if (!hasCreateOrUpdateAccess) throw new AuthorizationError("Not authorized");
|
|
|
|
return await updateLanguage(environmentId, languageId, languageInput);
|
|
}
|