diff --git a/src/pages/api/beers/[id]/index.ts b/src/pages/api/beers/[id]/index.ts index 8d89326..5517867 100644 --- a/src/pages/api/beers/[id]/index.ts +++ b/src/pages/api/beers/[id]/index.ts @@ -45,12 +45,7 @@ const editBeerPost = async ( req: EditBeerPostRequest, res: NextApiResponse>, ) => { - const { - body, - query: { id }, - } = req; - - await editBeerPostById(id, body); + await editBeerPostById({ id: req.query.id, data: req.body }); res.status(200).json({ message: 'Beer post updated successfully', @@ -62,8 +57,7 @@ const editBeerPost = async ( const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => { const { id } = req.query; - const deleted = deleteBeerPostById(id); - + const deleted = deleteBeerPostById({ beerPostId: id }); if (!deleted) { throw new ServerError('Beer post not found', 404); } @@ -74,26 +68,28 @@ const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => { statusCode: 200, }); }; + const router = createRouter< EditBeerPostRequest, NextApiResponse> >(); -router.put( - validateRequest({ - bodySchema: EditBeerPostValidationSchema, - querySchema: z.object({ id: z.string() }), - }), - getCurrentUser, - checkIfBeerPostOwner, - editBeerPost, -); -router.delete( - validateRequest({ querySchema: z.object({ id: z.string() }) }), - getCurrentUser, - checkIfBeerPostOwner, - deleteBeerPost, -); +router + .put( + validateRequest({ + bodySchema: EditBeerPostValidationSchema, + querySchema: z.object({ id: z.string() }), + }), + getCurrentUser, + checkIfBeerPostOwner, + editBeerPost, + ) + .delete( + validateRequest({ querySchema: z.object({ id: z.string() }) }), + getCurrentUser, + checkIfBeerPostOwner, + deleteBeerPost, + ); const handler = router.handler(NextConnectOptions); diff --git a/src/pages/api/beers/index.ts b/src/pages/api/beers/index.ts index e0f65cb..8e6f919 100644 --- a/src/pages/api/beers/index.ts +++ b/src/pages/api/beers/index.ts @@ -19,7 +19,7 @@ const getBeerPosts = async ( const pageNum = parseInt(req.query.page_num, 10); const pageSize = parseInt(req.query.page_size, 10); - const beerPosts = await getAllBeerPosts(pageNum, pageSize); + const beerPosts = await getAllBeerPosts({ pageNum, pageSize }); const beerPostCount = await DBClient.instance.beerPost.count(); res.setHeader('X-Total-Count', beerPostCount); diff --git a/src/pages/api/beers/search.ts b/src/pages/api/beers/search.ts index 58bf513..80ea329 100644 --- a/src/pages/api/beers/search.ts +++ b/src/pages/api/beers/search.ts @@ -26,6 +26,7 @@ const search = async (req: SearchAPIRequest, res: NextApiResponse) => { ibu: true, abv: true, createdAt: true, + updatedAt: true, description: true, postedBy: { select: { username: true, id: true } }, brewery: { select: { name: true, id: true } }, diff --git a/src/pages/api/breweries/[id]/beers/index.ts b/src/pages/api/breweries/[id]/beers/index.ts index a5175d5..7b213c5 100644 --- a/src/pages/api/breweries/[id]/beers/index.ts +++ b/src/pages/api/breweries/[id]/beers/index.ts @@ -29,6 +29,7 @@ const getAllBeersByBrewery = async ( ibu: true, abv: true, createdAt: true, + updatedAt: true, description: true, postedBy: { select: { username: true, id: true } }, brewery: { select: { name: true, id: true } }, diff --git a/src/pages/api/breweries/index.ts b/src/pages/api/breweries/index.ts index 5320d25..d8814fa 100644 --- a/src/pages/api/breweries/index.ts +++ b/src/pages/api/breweries/index.ts @@ -19,7 +19,7 @@ const getBreweryPosts = async ( const pageNum = parseInt(req.query.page_num, 10); const pageSize = parseInt(req.query.page_size, 10); - const breweryPosts = await getAllBreweryPosts(pageNum, pageSize); + const breweryPosts = await getAllBreweryPosts({ pageNum, pageSize }); const breweryPostCount = await DBClient.instance.breweryPost.count(); res.setHeader('X-Total-Count', breweryPostCount); diff --git a/src/services/BeerComment/schema/CreateNewBeerCommentSchema.ts b/src/services/BeerComment/schema/CreateNewBeerCommentSchema.ts deleted file mode 100644 index e9b9c27..0000000 --- a/src/services/BeerComment/schema/CreateNewBeerCommentSchema.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { z } from 'zod'; - -const CreateCommentValidationSchema = z.object({ - userId: z.string().uuid(), - beerPostId: z.string().uuid(), -}); - -export default CreateCommentValidationSchema; diff --git a/src/services/BeerPost/createNewBeerPost.ts b/src/services/BeerPost/createNewBeerPost.ts index 538363e..97475e2 100644 --- a/src/services/BeerPost/createNewBeerPost.ts +++ b/src/services/BeerPost/createNewBeerPost.ts @@ -7,7 +7,7 @@ const CreateBeerPostWithUserSchema = CreateBeerPostValidationSchema.extend({ userId: z.string().cuid(), }); -const createNewBeerPost = async ({ +const createNewBeerPost = ({ name, description, abv, @@ -15,32 +15,33 @@ const createNewBeerPost = async ({ styleId, breweryId, userId, -}: z.infer) => { - const newBeerPost: z.infer = - await DBClient.instance.beerPost.create({ - data: { - name, - description, - abv, - ibu, - style: { connect: { id: styleId } }, - postedBy: { connect: { id: userId } }, - brewery: { connect: { id: breweryId } }, - }, - select: { - id: true, - name: true, - description: true, - abv: true, - ibu: true, - createdAt: true, - beerImages: { select: { id: true, path: true, caption: true, alt: true } }, - brewery: { select: { id: true, name: true } }, - style: { select: { id: true, name: true, description: true } }, - postedBy: { select: { id: true, username: true } }, - }, - }); - return newBeerPost; +}: z.infer): Promise< + z.infer +> => { + return DBClient.instance.beerPost.create({ + data: { + name, + description, + abv, + ibu, + style: { connect: { id: styleId } }, + postedBy: { connect: { id: userId } }, + brewery: { connect: { id: breweryId } }, + }, + select: { + id: true, + name: true, + description: true, + abv: true, + ibu: true, + createdAt: true, + updatedAt: true, + beerImages: { select: { id: true, path: true, caption: true, alt: true } }, + brewery: { select: { id: true, name: true } }, + style: { select: { id: true, name: true, description: true } }, + postedBy: { select: { id: true, username: true } }, + }, + }); }; export default createNewBeerPost; diff --git a/src/services/BeerPost/deleteBeerPostById.ts b/src/services/BeerPost/deleteBeerPostById.ts index 4eb6c5a..2344f9b 100644 --- a/src/services/BeerPost/deleteBeerPostById.ts +++ b/src/services/BeerPost/deleteBeerPostById.ts @@ -2,27 +2,29 @@ import DBClient from '@/prisma/DBClient'; import { z } from 'zod'; import BeerPostQueryResult from './schema/BeerPostQueryResult'; -const deleteBeerPostById = async ( - id: string, -): Promise | null> => { - const deleted = await DBClient.instance.beerPost.delete({ - where: { id }, +interface DeleteBeerPostByIdArgs { + beerPostId: string; +} + +const deleteBeerPostById = ({ + beerPostId, +}: DeleteBeerPostByIdArgs): Promise | null> => { + return DBClient.instance.beerPost.delete({ + where: { id: beerPostId }, select: { + abv: true, + createdAt: true, + description: true, + ibu: true, id: true, name: true, - brewery: { select: { id: true, name: true } }, - description: true, + updatedAt: true, beerImages: { select: { id: true, path: true, caption: true, alt: true } }, - ibu: true, - abv: true, style: { select: { id: true, name: true, description: true } }, postedBy: { select: { id: true, username: true } }, - createdAt: true, - updatedAt: true, + brewery: { select: { id: true, name: true } }, }, }); - - return deleted; }; export default deleteBeerPostById; diff --git a/src/services/BeerPost/editBeerPostById.ts b/src/services/BeerPost/editBeerPostById.ts index 213e6b1..bed2629 100644 --- a/src/services/BeerPost/editBeerPostById.ts +++ b/src/services/BeerPost/editBeerPostById.ts @@ -1,10 +1,36 @@ import DBClient from '@/prisma/DBClient'; import { z } from 'zod'; import EditBeerPostValidationSchema from './schema/EditBeerPostValidationSchema'; +import BeerPostQueryResult from './schema/BeerPostQueryResult'; -const schema = EditBeerPostValidationSchema.omit({ id: true }); +const schema = EditBeerPostValidationSchema.omit({ id: true, styleId: true }); -export default async function editBeerPostById(id: string, data: z.infer) { - const beerPost = await DBClient.instance.beerPost.update({ where: { id }, data }); - return beerPost; +interface EditBeerPostByIdArgs { + id: string; + data: z.infer; } + +const editBeerPostById = ({ + id, + data: { abv, ibu, name, description }, +}: EditBeerPostByIdArgs): Promise> => { + return DBClient.instance.beerPost.update({ + where: { id }, + data: { abv, ibu, name, description }, + select: { + id: true, + name: true, + description: true, + abv: true, + ibu: true, + createdAt: true, + updatedAt: true, + beerImages: { select: { id: true, path: true, caption: true, alt: true } }, + brewery: { select: { id: true, name: true } }, + style: { select: { id: true, name: true, description: true } }, + postedBy: { select: { id: true, username: true } }, + }, + }); +}; + +export default editBeerPostById; diff --git a/src/services/BeerPost/getAllBeerPosts.ts b/src/services/BeerPost/getAllBeerPosts.ts index ec2187f..c04ee9e 100644 --- a/src/services/BeerPost/getAllBeerPosts.ts +++ b/src/services/BeerPost/getAllBeerPosts.ts @@ -4,30 +4,33 @@ import { z } from 'zod'; const prisma = DBClient.instance; -const getAllBeerPosts = async (pageNum: number, pageSize: number) => { - const skip = (pageNum - 1) * pageSize; +interface GetAllBeerPostsArgs { + pageNum: number; + pageSize: number; +} - const beerPosts: z.infer[] = await prisma.beerPost.findMany( - { - select: { - id: true, - name: true, - ibu: true, - abv: true, - description: true, - createdAt: true, - style: { select: { name: true, id: true, description: true } }, - brewery: { select: { name: true, id: true } }, - postedBy: { select: { id: true, username: true } }, - beerImages: { select: { path: true, caption: true, id: true, alt: true } }, - }, - take: pageSize, - skip, - orderBy: { createdAt: 'desc' }, +const getAllBeerPosts = ({ + pageNum, + pageSize, +}: GetAllBeerPostsArgs): Promise[]> => { + return prisma.beerPost.findMany({ + select: { + id: true, + name: true, + ibu: true, + abv: true, + description: true, + createdAt: true, + updatedAt: true, + style: { select: { name: true, id: true, description: true } }, + brewery: { select: { name: true, id: true } }, + postedBy: { select: { id: true, username: true } }, + beerImages: { select: { path: true, caption: true, id: true, alt: true } }, }, - ); - - return beerPosts; + take: pageSize, + skip: (pageNum - 1) * pageSize, + orderBy: { createdAt: 'desc' }, + }); }; export default getAllBeerPosts; diff --git a/src/services/BeerPost/getBeerPostById.ts b/src/services/BeerPost/getBeerPostById.ts index 2867fba..57b3759 100644 --- a/src/services/BeerPost/getBeerPostById.ts +++ b/src/services/BeerPost/getBeerPostById.ts @@ -4,25 +4,25 @@ import { z } from 'zod'; const prisma = DBClient.instance; -const getBeerPostById = async (id: string) => { - const beerPost: z.infer | null = - await prisma.beerPost.findFirst({ - select: { - id: true, - name: true, - ibu: true, - abv: true, - createdAt: true, - description: true, - postedBy: { select: { username: true, id: true } }, - brewery: { select: { name: true, id: true } }, - style: { select: { name: true, id: true, description: true } }, - beerImages: { select: { alt: true, path: true, caption: true, id: true } }, - }, - where: { id }, - }); - - return beerPost; +const getBeerPostById = async ( + id: string, +): Promise | null> => { + return prisma.beerPost.findFirst({ + select: { + id: true, + name: true, + ibu: true, + abv: true, + createdAt: true, + updatedAt: true, + description: true, + postedBy: { select: { username: true, id: true } }, + brewery: { select: { name: true, id: true } }, + style: { select: { name: true, id: true, description: true } }, + beerImages: { select: { alt: true, path: true, caption: true, id: true } }, + }, + where: { id }, + }); }; export default getBeerPostById; diff --git a/src/services/BeerPost/getBeerRecommendations.ts b/src/services/BeerPost/getBeerRecommendations.ts index 4f5f45b..e855548 100644 --- a/src/services/BeerPost/getBeerRecommendations.ts +++ b/src/services/BeerPost/getBeerRecommendations.ts @@ -13,7 +13,10 @@ const getBeerRecommendations = async ({ beerPost, pageNum, pageSize, -}: GetBeerRecommendationsArgs) => { +}: GetBeerRecommendationsArgs): Promise<{ + beerRecommendations: z.infer[]; + count: number; +}> => { const skip = (pageNum - 1) * pageSize; const take = pageSize; @@ -30,6 +33,7 @@ const getBeerRecommendations = async ({ abv: true, description: true, createdAt: true, + updatedAt: true, style: { select: { name: true, id: true, description: true } }, brewery: { select: { name: true, id: true } }, postedBy: { select: { id: true, username: true } }, diff --git a/src/services/BeerPost/schema/BeerPostQueryResult.ts b/src/services/BeerPost/schema/BeerPostQueryResult.ts index d523f0e..9fca4cd 100644 --- a/src/services/BeerPost/schema/BeerPostQueryResult.ts +++ b/src/services/BeerPost/schema/BeerPostQueryResult.ts @@ -13,7 +13,7 @@ const BeerPostQueryResult = z.object({ style: z.object({ id: z.string(), name: z.string(), description: z.string() }), postedBy: z.object({ id: z.string(), username: z.string() }), createdAt: z.coerce.date(), - updatedAt: z.coerce.date().optional(), + updatedAt: z.coerce.date().nullable(), }); export default BeerPostQueryResult; diff --git a/src/services/BeerStyles/deleteBeerStyleById.ts b/src/services/BeerStyles/deleteBeerStyleById.ts index a8a7551..5d169ae 100644 --- a/src/services/BeerStyles/deleteBeerStyleById.ts +++ b/src/services/BeerStyles/deleteBeerStyleById.ts @@ -2,9 +2,15 @@ import { z } from 'zod'; import DBClient from '@/prisma/DBClient'; import BeerStyleQueryResult from './schema/BeerStyleQueryResult'; -const deleteBeerStyleById = async (id: string) => { +interface DeleteBeerStyleByIdArgs { + beerStyleId: string; +} + +const deleteBeerStyleById = async ({ + beerStyleId, +}: DeleteBeerStyleByIdArgs): Promise | null> => { const deleted = await DBClient.instance.beerStyle.delete({ - where: { id }, + where: { id: beerStyleId }, select: { id: true, name: true, @@ -18,7 +24,11 @@ const deleteBeerStyleById = async (id: string) => { }, }); - return deleted as z.infer | null; + /** + * Prisma does not support tuples, so we have to typecast the ibuRange and abvRange + * fields to [number, number] in order to satisfy the zod schema. + */ + return deleted as Awaited>; }; export default deleteBeerStyleById; diff --git a/src/services/BeerStyles/editBeerStyleById.ts b/src/services/BeerStyles/editBeerStyleById.ts new file mode 100644 index 0000000..07adb3f --- /dev/null +++ b/src/services/BeerStyles/editBeerStyleById.ts @@ -0,0 +1,30 @@ +import DBClient from '@/prisma/DBClient'; +import { z } from 'zod'; +import BeerStyleQueryResult from './schema/BeerStyleQueryResult'; + +const editBeerStyleById = async ( + id: string, +): Promise | null> => { + const beerStyle = await DBClient.instance.beerStyle.findUnique({ + where: { id }, + select: { + id: true, + name: true, + postedBy: { select: { id: true, username: true } }, + createdAt: true, + updatedAt: true, + abvRange: true, + ibuRange: true, + description: true, + glassware: { select: { id: true, name: true } }, + }, + }); + + /** + * Prisma does not support tuples, so we have to typecast the ibuRange and abvRange + * fields to [number, number] in order to satisfy the zod schema. + */ + return beerStyle as Awaited>; +}; + +export default editBeerStyleById; diff --git a/src/services/BeerStyles/getAllBeerStyles.ts b/src/services/BeerStyles/getAllBeerStyles.ts index a6e0f20..65df57b 100644 --- a/src/services/BeerStyles/getAllBeerStyles.ts +++ b/src/services/BeerStyles/getAllBeerStyles.ts @@ -2,14 +2,16 @@ import DBClient from '@/prisma/DBClient'; import { z } from 'zod'; import BeerStyleQueryResult from './schema/BeerStyleQueryResult'; +interface GetAllBeerStylesArgs { + pageNum: number; + pageSize: number; +} + const getAllBeerStyles = async ({ pageNum, pageSize, -}: { - pageNum: number; - pageSize: number; -}): Promise[]> => - DBClient.instance.beerStyle.findMany({ +}: GetAllBeerStylesArgs): Promise[]> => { + const beerStyles = await DBClient.instance.beerStyle.findMany({ take: pageSize, skip: (pageNum - 1) * pageSize, select: { @@ -23,6 +25,13 @@ const getAllBeerStyles = async ({ description: true, glassware: { select: { id: true, name: true } }, }, - }) as ReturnType; + }); + + /** + * Prisma does not support tuples, so we have to typecast the ibuRange and abvRange + * fields to [number, number] in order to satisfy the zod schema. + */ + return beerStyles as Awaited>; +}; export default getAllBeerStyles; diff --git a/src/services/BeerStyles/getBeerStyleById.ts b/src/services/BeerStyles/getBeerStyleById.ts index a81c66f..688fcda 100644 --- a/src/services/BeerStyles/getBeerStyleById.ts +++ b/src/services/BeerStyles/getBeerStyleById.ts @@ -2,8 +2,10 @@ import DBClient from '@/prisma/DBClient'; import { z } from 'zod'; import BeerStyleQueryResult from './schema/BeerStyleQueryResult'; -const getBeerStyleById = async (id: string) => { - const beerStyle = (await DBClient.instance.beerStyle.findUnique({ +const getBeerStyleById = async ( + id: string, +): Promise | null> => { + const beerStyle = await DBClient.instance.beerStyle.findUnique({ where: { id }, select: { id: true, @@ -16,9 +18,13 @@ const getBeerStyleById = async (id: string) => { description: true, glassware: { select: { id: true, name: true } }, }, - })) as z.infer | null; + }); - return beerStyle; + /** + * Prisma does not support tuples, so we have to typecast the ibuRange and abvRange + * fields to [number, number] in order to satisfy the zod schema. + */ + return beerStyle as Awaited>; }; export default getBeerStyleById; diff --git a/src/services/BeerStyles/schema/CreateBeerStyleValidationSchema.ts b/src/services/BeerStyles/schema/CreateBeerStyleValidationSchema.ts new file mode 100644 index 0000000..325829d --- /dev/null +++ b/src/services/BeerStyles/schema/CreateBeerStyleValidationSchema.ts @@ -0,0 +1,46 @@ +import { z } from 'zod'; + +const CreateBeerStyleValidationSchema = z.object({ + glasswareId: z + .string() + .cuid({ + message: 'Glassware ID must be a valid CUID.', + }) + .min(1, { message: 'Glassware ID is required.' }), + description: z + .string() + .min(1, { message: 'Description is required.' }) + .max(500, { message: 'Description must be less than or equal to 500 characters.' }), + ibuRange: z + .tuple([ + z + .number() + .min(0, { message: 'IBU range minimum must be greater than or equal to 0.' }) + .max(100, { message: 'IBU range minimum must be less than or equal to 100.' }), + z + .number() + .min(0, { message: 'IBU range maximum must be greater than or equal to 0.' }) + .max(100, { message: 'IBU range maximum must be less than or equal to 100.' }), + ]) + .refine((ibuRange) => ibuRange[0] <= ibuRange[1], { + message: 'IBU range minimum must be less than or equal to maximum.', + }), + abvRange: z + .tuple([ + z + .number() + .min(0, { message: 'ABV range minimum must be greater than or equal to 0.' }), + z + .number() + .min(0, { message: 'ABV range maximum must be greater than or equal to 0.' }), + ]) + .refine((abvRange) => abvRange[0] <= abvRange[1], { + message: 'ABV range minimum must be less than or equal to maximum.', + }), + name: z + .string() + .min(1, { message: 'Name is required.' }) + .max(100, { message: 'Name must be less than or equal to 100 characters.' }), +}); + +export default CreateBeerStyleValidationSchema; diff --git a/src/services/BreweryPost/createNewBreweryPost.ts b/src/services/BreweryPost/createNewBreweryPost.ts index 5ccc938..2d60a3b 100644 --- a/src/services/BreweryPost/createNewBreweryPost.ts +++ b/src/services/BreweryPost/createNewBreweryPost.ts @@ -19,37 +19,37 @@ const createNewBreweryPost = async ({ locationId, name, userId, -}: z.infer) => { - const breweryPost: z.infer = - (await DBClient.instance.breweryPost.create({ - data: { - name, - description, - dateEstablished, - location: { connect: { id: locationId } }, - postedBy: { connect: { id: userId } }, - }, - select: { - id: true, - name: true, - description: true, - createdAt: true, - dateEstablished: true, - postedBy: { select: { id: true, username: true } }, - breweryImages: { select: { path: true, caption: true, id: true, alt: true } }, - location: { - select: { - city: true, - address: true, - coordinates: true, - country: true, - stateOrProvince: true, - }, +}: z.infer): Promise< + z.infer +> => { + const post = (await DBClient.instance.breweryPost.create({ + data: { + name, + description, + dateEstablished, + location: { connect: { id: locationId } }, + postedBy: { connect: { id: userId } }, + }, + select: { + id: true, + name: true, + description: true, + createdAt: true, + dateEstablished: true, + postedBy: { select: { id: true, username: true } }, + breweryImages: { select: { path: true, caption: true, id: true, alt: true } }, + location: { + select: { + city: true, + address: true, + coordinates: true, + country: true, + stateOrProvince: true, }, }, - })) as z.infer; + }, + })) as Awaited>; - return breweryPost; + return post; }; - export default createNewBreweryPost; diff --git a/src/services/BreweryPost/getAllBreweryPosts.ts b/src/services/BreweryPost/getAllBreweryPosts.ts index 04a2f47..d913aa9 100644 --- a/src/services/BreweryPost/getAllBreweryPosts.ts +++ b/src/services/BreweryPost/getAllBreweryPosts.ts @@ -5,36 +5,42 @@ import { z } from 'zod'; const prisma = DBClient.instance; -const getAllBreweryPosts = async (pageNum?: number, pageSize?: number) => { - const skip = pageNum && pageSize ? (pageNum - 1) * pageSize : undefined; - const take = pageNum && pageSize ? pageSize : undefined; - - const breweryPosts: z.infer[] = - (await prisma.breweryPost.findMany({ - skip, - take, - select: { - id: true, - location: { - select: { - city: true, - address: true, - coordinates: true, - country: true, - stateOrProvince: true, - }, +const getAllBreweryPosts = async ({ + pageNum, + pageSize, +}: { + pageNum: number; + pageSize: number; +}): Promise[]> => { + const breweryPosts = await prisma.breweryPost.findMany({ + take: pageSize, + skip: (pageNum - 1) * pageSize, + select: { + id: true, + location: { + select: { + city: true, + address: true, + coordinates: true, + country: true, + stateOrProvince: true, }, - description: true, - name: true, - postedBy: { select: { username: true, id: true } }, - breweryImages: { select: { path: true, caption: true, id: true, alt: true } }, - createdAt: true, - dateEstablished: true, }, - orderBy: { createdAt: 'desc' }, - })) as z.infer[]; + description: true, + name: true, + postedBy: { select: { username: true, id: true } }, + breweryImages: { select: { path: true, caption: true, id: true, alt: true } }, + createdAt: true, + dateEstablished: true, + }, + orderBy: { createdAt: 'desc' }, + }); - return breweryPosts; + /** + * Prisma does not support tuples, so we have to typecast the coordinates field to + * [number, number] in order to satisfy the zod schema. + */ + return breweryPosts as Awaited>; }; export default getAllBreweryPosts; diff --git a/src/services/BreweryPost/getBreweryPostById.ts b/src/services/BreweryPost/getBreweryPostById.ts index bb18301..55509a4 100644 --- a/src/services/BreweryPost/getBreweryPostById.ts +++ b/src/services/BreweryPost/getBreweryPostById.ts @@ -5,30 +5,33 @@ import { z } from 'zod'; const prisma = DBClient.instance; const getBreweryPostById = async (id: string) => { - const breweryPost: z.infer | null = - (await prisma.breweryPost.findFirst({ - select: { - id: true, - location: { - select: { - city: true, - address: true, - coordinates: true, - country: true, - stateOrProvince: true, - }, + const breweryPost = await prisma.breweryPost.findFirst({ + select: { + id: true, + location: { + select: { + city: true, + address: true, + coordinates: true, + country: true, + stateOrProvince: true, }, - description: true, - name: true, - breweryImages: { select: { path: true, caption: true, id: true, alt: true } }, - postedBy: { select: { username: true, id: true } }, - createdAt: true, - dateEstablished: true, }, - where: { id }, - })) as z.infer | null; + description: true, + name: true, + breweryImages: { select: { path: true, caption: true, id: true, alt: true } }, + postedBy: { select: { username: true, id: true } }, + createdAt: true, + dateEstablished: true, + }, + where: { id }, + }); - return breweryPost; + /** + * Prisma does not support tuples, so we have to typecast the coordinates field to + * [number, number] in order to satisfy the zod schema. + */ + return breweryPost as z.infer | null; }; export default getBreweryPostById;