Refactor: update types for brewery comments

This commit is contained in:
Aaron William Po
2023-10-07 15:21:57 -04:00
parent 2ee12d351f
commit 3b626e2f95
13 changed files with 3388 additions and 3517 deletions

6802
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -14,8 +14,8 @@
"dependencies": { "dependencies": {
"@hapi/iron": "^7.0.1", "@hapi/iron": "^7.0.1",
"@headlessui/react": "^1.7.15", "@headlessui/react": "^1.7.15",
"@headlessui/tailwindcss": "^0.1.3", "@headlessui/tailwindcss": "^0.2.0",
"@hookform/resolvers": "^3.1.1", "@hookform/resolvers": "^3.3.1",
"@mapbox/mapbox-sdk": "^0.15.2", "@mapbox/mapbox-sdk": "^0.15.2",
"@mapbox/search-js-core": "^1.0.0-beta.17", "@mapbox/search-js-core": "^1.0.0-beta.17",
"@mapbox/search-js-react": "^1.0.0-beta.17", "@mapbox/search-js-react": "^1.0.0-beta.17",
@@ -24,9 +24,9 @@
"@react-email/components": "^0.0.7", "@react-email/components": "^0.0.7",
"@react-email/render": "^0.0.7", "@react-email/render": "^0.0.7",
"@react-email/tailwind": "^0.0.8", "@react-email/tailwind": "^0.0.8",
"@vercel/analytics": "^1.0.1", "@vercel/analytics": "^1.1.0",
"argon2": "^0.30.3", "argon2": "^0.31.1",
"cloudinary": "^1.37.3", "cloudinary": "^1.41.0",
"cookie": "^0.5.0", "cookie": "^0.5.0",
"date-fns": "^2.30.0", "date-fns": "^2.30.0",
"dotenv": "^16.3.1", "dotenv": "^16.3.1",
@@ -35,7 +35,7 @@
"mapbox-gl": "^2.15.0", "mapbox-gl": "^2.15.0",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",
"multer-storage-cloudinary": "^4.0.0", "multer-storage-cloudinary": "^4.0.0",
"next": "^13.4.10", "next": "^13.5.4",
"next-connect": "^1.0.0-next.3", "next-connect": "^1.0.0-next.3",
"passport": "^0.6.0", "passport": "^0.6.0",
"passport-local": "^1.0.0", "passport-local": "^1.0.0",
@@ -70,14 +70,14 @@
"@types/sparkpost": "^2.1.5", "@types/sparkpost": "^2.1.5",
"@vercel/fetch": "^7.0.0", "@vercel/fetch": "^7.0.0",
"autoprefixer": "^10.4.14", "autoprefixer": "^10.4.14",
"daisyui": "^3.2.1", "daisyui": "^3.9.2",
"dotenv-cli": "^7.2.1", "dotenv-cli": "^7.2.1",
"eslint": "^8.45.0", "eslint": "^8.51.0",
"eslint-config-airbnb-base": "15.0.0", "eslint-config-airbnb-base": "15.0.0",
"eslint-config-airbnb-typescript": "17.1.0", "eslint-config-airbnb-typescript": "17.1.0",
"eslint-config-next": "^13.4.10", "eslint-config-next": "^13.5.4",
"eslint-config-prettier": "^8.8.0", "eslint-config-prettier": "^9.0.0",
"eslint-plugin-react": "^7.32.2", "eslint-plugin-react": "^7.33.2",
"onchange": "^7.1.0", "onchange": "^7.1.0",
"postcss": "^8.4.26", "postcss": "^8.4.26",
"prettier": "^3.0.0", "prettier": "^3.0.0",
@@ -87,7 +87,7 @@
"tailwindcss": "^3.3.3", "tailwindcss": "^3.3.3",
"tailwindcss-animate": "^1.0.6", "tailwindcss-animate": "^1.0.6",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"typescript": "^5.1.6" "typescript": "^5.2.2"
}, },
"prisma": { "prisma": {
"schema": "./src/prisma/schema.prisma", "schema": "./src/prisma/schema.prisma",

View File

@@ -61,10 +61,11 @@ const getAll = async (
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
const { page_size, page_num } = req.query; const { page_size, page_num } = req.query;
const comments = await getAllBreweryComments( const comments = await getAllBreweryComments({
{ id: breweryPostId }, id: breweryPostId,
{ pageSize: parseInt(page_size, 10), pageNum: parseInt(page_num, 10) }, pageNum: parseInt(page_num, 10),
); pageSize: parseInt(page_size, 10),
});
const pageCount = await DBClient.instance.breweryComment.count({ const pageCount = await DBClient.instance.breweryComment.count({
where: { breweryPostId }, where: { breweryPostId },

View File

@@ -4,6 +4,7 @@ import validateRequest from '@/config/nextConnect/middleware/validateRequest';
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions'; import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
import ServerError from '@/config/util/ServerError'; import ServerError from '@/config/util/ServerError';
import DBClient from '@/prisma/DBClient'; import DBClient from '@/prisma/DBClient';
import getBreweryCommentById from '@/services/BreweryComment/getBreweryCommentById';
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema'; import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema'; import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
@@ -27,9 +28,7 @@ const checkIfCommentOwner = async (
) => { ) => {
const { id } = req.query; const { id } = req.query;
const user = req.user!; const user = req.user!;
const comment = await DBClient.instance.breweryComment.findUnique({ const comment = await getBreweryCommentById(id);
where: { id },
});
if (!comment) { if (!comment) {
throw new ServerError('Comment not found', 404); throw new ServerError('Comment not found', 404);
@@ -87,7 +86,9 @@ const router = createRouter<
router router
.delete( .delete(
validateRequest({ querySchema: z.object({ id: z.string().cuid() }) }), validateRequest({
querySchema: z.object({ id: z.string().cuid() }),
}),
getCurrentUser, getCurrentUser,
checkIfCommentOwner, checkIfCommentOwner,
deleteComment, deleteComment,

View File

@@ -1,7 +1,7 @@
import DBClient from '@/prisma/DBClient'; import DBClient from '@/prisma/DBClient';
import { z } from 'zod'; import { z } from 'zod';
import CreateCommentValidationSchema from '../schema/CommentSchema/CreateCommentValidationSchema'; import CreateCommentValidationSchema from '../schema/CommentSchema/CreateCommentValidationSchema';
import BeerCommentQueryResult from './schema/BeerCommentQueryResult'; import CommentQueryResult from '../schema/CommentSchema/CommentQueryResult';
const CreateNewBeerCommentServiceSchema = CreateCommentValidationSchema.extend({ const CreateNewBeerCommentServiceSchema = CreateCommentValidationSchema.extend({
userId: z.string().cuid(), userId: z.string().cuid(),
@@ -15,7 +15,7 @@ const createNewBeerComment = async ({
rating, rating,
beerPostId, beerPostId,
userId, userId,
}: CreateNewBeerCommentArgs): Promise<z.infer<typeof BeerCommentQueryResult>> => { }: CreateNewBeerCommentArgs): Promise<z.infer<typeof CommentQueryResult>> => {
return DBClient.instance.beerComment.create({ return DBClient.instance.beerComment.create({
data: { data: {
content, content,

View File

@@ -1,6 +1,6 @@
import DBClient from '@/prisma/DBClient'; import DBClient from '@/prisma/DBClient';
import { z } from 'zod'; import { z } from 'zod';
import BeerCommentQueryResult from './schema/BeerCommentQueryResult'; import CommentQueryResult from '../schema/CommentSchema/CommentQueryResult';
interface EditBeerCommentByIdArgs { interface EditBeerCommentByIdArgs {
id: string; id: string;
@@ -12,7 +12,7 @@ const editBeerCommentById = async ({
id, id,
content, content,
rating, rating,
}: EditBeerCommentByIdArgs): Promise<z.infer<typeof BeerCommentQueryResult>> => { }: EditBeerCommentByIdArgs): Promise<z.infer<typeof CommentQueryResult>> => {
return DBClient.instance.beerComment.update({ return DBClient.instance.beerComment.update({
where: { id }, where: { id },
data: { content, rating, updatedAt: new Date() }, data: { content, rating, updatedAt: new Date() },

View File

@@ -1,6 +1,6 @@
import DBClient from '@/prisma/DBClient'; import DBClient from '@/prisma/DBClient';
import { z } from 'zod'; import { z } from 'zod';
import BeerCommentQueryResult from './schema/BeerCommentQueryResult'; import CommentQueryResult from '../schema/CommentSchema/CommentQueryResult';
interface FindBeerCommentArgs { interface FindBeerCommentArgs {
beerCommentId: string; beerCommentId: string;
@@ -8,7 +8,7 @@ interface FindBeerCommentArgs {
const findBeerCommentById = async ({ const findBeerCommentById = async ({
beerCommentId, beerCommentId,
}: FindBeerCommentArgs): Promise<z.infer<typeof BeerCommentQueryResult> | null> => { }: FindBeerCommentArgs): Promise<z.infer<typeof CommentQueryResult> | null> => {
return DBClient.instance.beerComment.findUnique({ return DBClient.instance.beerComment.findUnique({
where: { id: beerCommentId }, where: { id: beerCommentId },
select: { select: {

View File

@@ -23,6 +23,7 @@ const getAllBeerComments = async ({
content: true, content: true,
rating: true, rating: true,
createdAt: true, createdAt: true,
updatedAt: true,
postedBy: { select: { id: true, username: true, createdAt: true } }, postedBy: { select: { id: true, username: true, createdAt: true } },
}, },
}); });

View File

@@ -1,15 +0,0 @@
import { z } from 'zod';
const BeerCommentQueryResult = z.object({
id: z.string().cuid(),
content: z.string(),
rating: z.number(),
postedBy: z.object({
id: z.string().cuid(),
username: z.string(),
}),
createdAt: z.coerce.date(),
updatedAt: z.coerce.date().nullable(),
});
export default BeerCommentQueryResult;

View File

@@ -1,6 +1,7 @@
import DBClient from '@/prisma/DBClient'; import DBClient from '@/prisma/DBClient';
import { z } from 'zod'; import { z } from 'zod';
import CreateCommentValidationSchema from '../schema/CommentSchema/CreateCommentValidationSchema'; import CreateCommentValidationSchema from '../schema/CommentSchema/CreateCommentValidationSchema';
import CommentQueryResult from '../schema/CommentSchema/CommentQueryResult';
const CreateNewBreweryCommentServiceSchema = CreateCommentValidationSchema.extend({ const CreateNewBreweryCommentServiceSchema = CreateCommentValidationSchema.extend({
userId: z.string().cuid(), userId: z.string().cuid(),
@@ -12,7 +13,9 @@ const createNewBreweryComment = async ({
rating, rating,
breweryPostId, breweryPostId,
userId, userId,
}: z.infer<typeof CreateNewBreweryCommentServiceSchema>) => { }: z.infer<typeof CreateNewBreweryCommentServiceSchema>): Promise<
z.infer<typeof CommentQueryResult>
> => {
return DBClient.instance.breweryComment.create({ return DBClient.instance.breweryComment.create({
data: { data: {
content, content,
@@ -26,6 +29,7 @@ const createNewBreweryComment = async ({
rating: true, rating: true,
postedBy: { select: { id: true, username: true } }, postedBy: { select: { id: true, username: true } },
createdAt: true, createdAt: true,
updatedAt: true,
}, },
}); });
}; };

View File

@@ -1,12 +1,16 @@
import DBClient from '@/prisma/DBClient'; import DBClient from '@/prisma/DBClient';
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
import { z } from 'zod'; import { z } from 'zod';
import CommentQueryResult from '../schema/CommentSchema/CommentQueryResult'; import CommentQueryResult from '../schema/CommentSchema/CommentQueryResult';
const getAllBreweryComments = async ( const getAllBreweryComments = async ({
{ id }: Pick<z.infer<typeof BeerPostQueryResult>, 'id'>, id,
{ pageSize, pageNum = 0 }: { pageSize: number; pageNum?: number }, pageNum,
) => { pageSize,
}: {
id: string;
pageNum: number;
pageSize: number;
}) => {
const skip = (pageNum - 1) * pageSize; const skip = (pageNum - 1) * pageSize;
const breweryComments: z.infer<typeof CommentQueryResult>[] = const breweryComments: z.infer<typeof CommentQueryResult>[] =
await DBClient.instance.breweryComment.findMany({ await DBClient.instance.breweryComment.findMany({
@@ -18,6 +22,7 @@ const getAllBreweryComments = async (
content: true, content: true,
rating: true, rating: true,
createdAt: true, createdAt: true,
updatedAt: true,
postedBy: { select: { id: true, username: true, createdAt: true } }, postedBy: { select: { id: true, username: true, createdAt: true } },
}, },
orderBy: { createdAt: 'desc' }, orderBy: { createdAt: 'desc' },

View File

@@ -0,0 +1,9 @@
import DBClient from '@/prisma/DBClient';
const getBreweryCommentById = async (id: string) => {
return DBClient.instance.breweryComment.findUnique({
where: { id },
});
};
export default getBreweryCommentById;

View File

@@ -9,6 +9,7 @@ const CommentQueryResult = z.object({
id: z.string().cuid(), id: z.string().cuid(),
username: z.string().min(1).max(50), username: z.string().min(1).max(50),
}), }),
updatedAt: z.coerce.date().nullable(),
}); });
export default CommentQueryResult; export default CommentQueryResult;