[ADD] services

This commit is contained in:
Ankur Datta
2023-07-13 17:53:48 +00:00
parent cddbbc2be2
commit 4410b14d0c
2 changed files with 95 additions and 3 deletions
+37 -2
View File
@@ -4,8 +4,8 @@ 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 } from "@formbricks/types/v1/product";
import { ZProduct, ZProductWithEnvironments } from "@formbricks/types/v1/product";
import type { TProduct, TProductWithEnvironments } from "@formbricks/types/v1/product";
import { cache } from "react";
export const getProductByEnvironmentId = cache(async (environmentId: string): Promise<TProduct> => {
@@ -41,3 +41,38 @@ export const getProductByEnvironmentId = cache(async (environmentId: string): Pr
throw new ValidationError("Data validation of product failed");
}
});
export const getProductWithEnvironments = cache(
async (productId: string): Promise<TProductWithEnvironments> => {
let productPrisma;
try {
productPrisma = await prisma.product.findFirst({
where: {
id: productId,
},
include:{
environments:true
}
});
if (!productPrisma) {
throw new ResourceNotFoundError("Product", productId);
}
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseError("Database operation failed");
}
throw error;
}
try {
const product = ZProductWithEnvironments.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 with environments failed");
}
}
);
+58 -1
View File
@@ -2,7 +2,14 @@ import { prisma } from "@formbricks/database";
import { z } from "zod";
import { ValidationError } from "@formbricks/errors";
import { DatabaseError, ResourceNotFoundError } from "@formbricks/errors";
import { TSurvey, TSurveyWithAnalytics, ZSurvey, ZSurveyWithAnalytics } from "@formbricks/types/v1/surveys";
import {
TSurvey,
TSurveyWithAnalytics,
ZSurvey,
ZSurveyWithAnalytics,
TSurveyWithResponseCount,
ZSurveyWithResponseCount,
} from "@formbricks/types/v1/surveys";
import { Prisma } from "@prisma/client";
import "server-only";
import { cache } from "react";
@@ -146,3 +153,53 @@ export const getSurveys = cache(async (environmentId: string): Promise<TSurvey[]
throw new ValidationError("Data validation of survey failed");
}
});
export const getSurveysWithResponseCount = cache(
async (environmentId: string): Promise<TSurveyWithResponseCount[]> => {
let surveysPrisma;
try {
surveysPrisma = await prisma.survey.findMany({
where: {
environment: {
id: environmentId,
},
},
include: {
_count: {
select: {
responses: true,
},
},
},
});
} catch (error) {
console.log(error);
if (error instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseError("Database operation failed");
}
throw error;
}
const surveys: TSurveyWithResponseCount[] = [];
for (const surveyPrisma of surveysPrisma) {
const transformedSurvey = {
...surveyPrisma,
attributeFilters: [],
triggers: [],
responses: surveyPrisma._count.responses,
};
const survey = ZSurveyWithResponseCount.parse(transformedSurvey);
surveys.push(survey);
}
try {
return surveys;
} catch (error) {
if (error instanceof z.ZodError) {
console.error(JSON.stringify(error.errors, null, 2)); // log the detailed error information
}
throw new ValidationError("Data validation of survey failed");
}
}
);