mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
import { faker } from '@faker-js/faker';
|
|
import { BeerComment, BeerPost, User } from '@prisma/client';
|
|
|
|
import DBClient from '../../DBClient';
|
|
|
|
interface CreateNewBeerCommentsArgs {
|
|
numberOfComments: number;
|
|
joinData: {
|
|
beerPosts: BeerPost[];
|
|
users: User[];
|
|
};
|
|
}
|
|
const createNewBeerComments = async ({
|
|
numberOfComments,
|
|
joinData,
|
|
}: CreateNewBeerCommentsArgs) => {
|
|
const { beerPosts, users } = joinData;
|
|
const prisma = DBClient.instance;
|
|
const beerCommentPromises: Promise<BeerComment>[] = [];
|
|
// eslint-disable-next-line no-plusplus
|
|
for (let i = 0; i < numberOfComments; i++) {
|
|
const content = faker.lorem.lines(5);
|
|
const user = users[Math.floor(Math.random() * users.length)];
|
|
const beerPost = beerPosts[Math.floor(Math.random() * beerPosts.length)];
|
|
const createdAt = faker.date.past(1);
|
|
beerCommentPromises.push(
|
|
prisma.beerComment.create({
|
|
data: {
|
|
content,
|
|
postedBy: { connect: { id: user.id } },
|
|
beerPost: { connect: { id: beerPost.id } },
|
|
rating: Math.floor(Math.random() * 5) + 1,
|
|
createdAt,
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
return Promise.all(beerCommentPromises);
|
|
};
|
|
|
|
export default createNewBeerComments;
|