feat: implement user follow system

This commit is contained in:
Aaron William Po
2023-11-12 17:32:43 -05:00
parent d48d9a8886
commit 24e245fc6f
5 changed files with 88 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { faker } from '@faker-js/faker';
import type { User } from '@prisma/client';
import DBClient from '../../DBClient';
interface CreateNewUserFollowsArgs {
joinData: { users: User[] };
}
interface UserFollowData {
followerId: string;
followingId: string;
followedAt: Date;
}
const createNewUserFollows = async ({
joinData: { users },
}: CreateNewUserFollowsArgs) => {
const userFollows: UserFollowData[] = [];
users.forEach((user) => {
// Get 20 random users to follow.
const randomUsers = users
.filter((randomUser) => randomUser.id !== user.id)
.sort(() => Math.random() - Math.random())
.slice(0, 20);
// Get the user to follow the random users, and the random users to follow the user.
const data = randomUsers.flatMap((randomUser) => [
{
followerId: user.id,
followingId: randomUser.id,
followedAt: faker.date.between({ from: user.createdAt, to: new Date() }),
},
{
followerId: randomUser.id,
followingId: user.id,
followedAt: faker.date.between({ from: randomUser.createdAt, to: new Date() }),
},
]);
userFollows.push(...data);
});
await DBClient.instance.userFollow.createMany({
data: userFollows,
skipDuplicates: true,
});
return DBClient.instance.userFollow.findMany();
};
export default createNewUserFollows;