fix: functionality

This commit is contained in:
Piyush Gupta
2025-06-24 18:12:58 +05:30
parent ea8de20ff4
commit 907899c3d6
4 changed files with 30 additions and 40 deletions

View File

@@ -70,7 +70,7 @@ describe("tag lib", () => {
test("updates tag name and revalidates cache", async () => {
vi.mocked(prisma.tag.update).mockResolvedValueOnce(baseTag);
const result = await updateTagName(baseTag.id, "Tag1");
expect(result).toEqual({ data: baseTag, error: null });
expect(result).toEqual(baseTag);
expect(prisma.tag.update).toHaveBeenCalledWith({ where: { id: baseTag.id }, data: { name: "Tag1" } });
});
test("throws error on prisma error", async () => {

View File

@@ -2,7 +2,9 @@ import "server-only";
import { validateInputs } from "@/lib/utils/validate";
import { Prisma } from "@prisma/client";
import { prisma } from "@formbricks/database";
import { PrismaErrorType } from "@formbricks/database/types/error";
import { ZId, ZString } from "@formbricks/types/common";
import { InvalidInputError } from "@formbricks/types/errors";
import { TTag } from "@formbricks/types/tags";
export const deleteTag = async (id: string): Promise<TTag> => {
@@ -21,30 +23,27 @@ export const deleteTag = async (id: string): Promise<TTag> => {
}
};
export const updateTagName = async (
id: string,
name: string
): Promise<{ data: TTag | null; error: { message: string } | null }> => {
export const updateTagName = async (id: string, name: string): Promise<TTag> => {
validateInputs([id, ZId], [name, ZString]);
try {
const tag = await prisma.tag.update({
where: { id },
data: { name },
where: {
id,
},
data: {
name,
},
});
return {
data: tag,
error: null,
};
} catch (error: any) {
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") {
return {
data: null,
error: { message: "already exists" },
};
return tag;
} catch (error) {
if (
error instanceof Prisma.PrismaClientKnownRequestError &&
error.code === PrismaErrorType.UniqueConstraintViolation
) {
throw new InvalidInputError(error.message);
}
throw error;
}
};

View File

@@ -26,6 +26,7 @@ export const ProjectLookSettingsPage = async (props: { params: Promise<{ environ
}
const canRemoveBranding = await getWhiteLabelPermission(organization.billing.plan);
return (
<PageContentWrapper>
<PageHeader pageTitle={t("common.project_configuration")}>

View File

@@ -71,32 +71,22 @@ export const SingleTag: React.FC<SingleTagProps> = ({
)}
defaultValue={tagName}
onBlur={(e) => {
const trimmedName = e.target.value.trim();
updateTagNameAction({ tagId, name: trimmedName }).then((res) => {
const response = res?.data;
const data = response?.data;
const errorMessage = response?.error?.message?.toLowerCase?.() ?? "";
if (data?.id) {
updateTagNameAction({ tagId, name: e.target.value.trim() }).then((updateTagNameResponse) => {
if (updateTagNameResponse?.data) {
setUpdateTagError(false);
toast.success(t("environments.project.tags.tag_updated"));
} else {
const isDuplicate =
errorMessage.includes("already exists") ||
errorMessage.includes("unique constraint failed");
toast.error(
isDuplicate
? t("environments.project.tags.tag_already_exists")
: t("common.something_went_wrong_please_try_again"),
{
const errorMessage = getFormattedErrorMessage(updateTagNameResponse);
if (errorMessage?.includes("Unique constraint failed")) {
toast.error(t("environments.project.tags.tag_already_exists"), {
duration: 2000,
icon: isDuplicate ? (
<AlertCircleIcon className="h-5 w-5 text-orange-500" />
) : undefined,
}
);
icon: <AlertCircleIcon className="h-5 w-5 text-orange-500" />,
});
} else {
toast.error(errorMessage ?? t("common.something_went_wrong_please_try_again"), {
duration: 2000,
});
}
setUpdateTagError(true);
}
});