mirror of
https://github.com/formbricks/formbricks.git
synced 2026-02-12 03:09:41 -06:00
* feat: migrate look and feel to serverside component with loading screen * fix: use existing product type instead of creating a custom type * fix: make improvements as Matti suggested * change attributes order in updateProduct function * run pnpm format --------- Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
91 lines
2.3 KiB
TypeScript
91 lines
2.3 KiB
TypeScript
import "server-only";
|
|
import { prisma } from "@formbricks/database";
|
|
import { z } from "zod";
|
|
import { Prisma } from "@prisma/client";
|
|
import { DatabaseError, ResourceNotFoundError, ValidationError } from "@formbricks/errors";
|
|
import { ZProduct } from "@formbricks/types/v1/product";
|
|
import type { TProduct, TProductUpdateInput } from "@formbricks/types/v1/product";
|
|
import { cache } from "react";
|
|
|
|
const selectProduct = {
|
|
id: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
name: true,
|
|
teamId: true,
|
|
brandColor: true,
|
|
highlightBorderColor: true,
|
|
recontactDays: true,
|
|
formbricksSignature: true,
|
|
placement: true,
|
|
clickOutsideClose: true,
|
|
darkOverlay: true,
|
|
};
|
|
|
|
export const getProductByEnvironmentId = cache(async (environmentId: string): Promise<TProduct> => {
|
|
let productPrisma;
|
|
try {
|
|
productPrisma = await prisma.product.findFirst({
|
|
where: {
|
|
environments: {
|
|
some: {
|
|
id: environmentId,
|
|
},
|
|
},
|
|
},
|
|
select: selectProduct,
|
|
});
|
|
|
|
if (!productPrisma) {
|
|
throw new ResourceNotFoundError("Product for Environment", environmentId);
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
|
throw new DatabaseError("Database operation failed");
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
try {
|
|
const product = ZProduct.parse(productPrisma);
|
|
return product;
|
|
} catch (error) {
|
|
if (error instanceof z.ZodError) {
|
|
console.error(JSON.stringify(error.errors, null, 2));
|
|
}
|
|
throw new ValidationError("Data validation of product failed");
|
|
}
|
|
});
|
|
|
|
export const updateProduct = async (
|
|
productId: string,
|
|
inputProduct: Partial<TProductUpdateInput>
|
|
): Promise<TProduct> => {
|
|
let updatedProduct;
|
|
try {
|
|
updatedProduct = await prisma.product.update({
|
|
where: {
|
|
id: productId,
|
|
},
|
|
data: {
|
|
...inputProduct,
|
|
},
|
|
select: selectProduct,
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
|
throw new DatabaseError("Database operation failed");
|
|
}
|
|
throw error;
|
|
}
|
|
try {
|
|
const product = ZProduct.parse(updatedProduct);
|
|
return product;
|
|
} catch (error) {
|
|
if (error instanceof z.ZodError) {
|
|
console.error(JSON.stringify(error.errors, null, 2));
|
|
}
|
|
throw new ValidationError("Data validation of product failed");
|
|
}
|
|
};
|