Files
formbricks/packages/ee/multiLanguage/lib/actions.ts
T
Dhruwang Jariwala 8b5328aa74 feat: Multi language Surveys (#1630)
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>
2024-03-18 19:02:18 +00:00

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);
}