Create location table for brewery post locations

This commit is contained in:
Aaron William Po
2023-04-26 08:37:59 -04:00
parent 4aeafc0de8
commit c19cddceb7
17 changed files with 209 additions and 55 deletions

View File

@@ -26,17 +26,23 @@ const createNewBeerPosts = async ({
const beerType = beerTypes[Math.floor(Math.random() * beerTypes.length)];
const breweryPost = breweryPosts[Math.floor(Math.random() * breweryPosts.length)];
const createdAt = faker.date.past(1);
const abv = Math.floor(Math.random() * (12 - 4) + 4);
const ibu = Math.floor(Math.random() * (60 - 10) + 10);
const name = faker.commerce.productName();
const description = faker.lorem.lines(20).replace(/(\r\n|\n|\r)/gm, ' ');
beerPostPromises.push(
prisma.beerPost.create({
data: {
abv: Math.floor(Math.random() * (12 - 4) + 4),
ibu: Math.floor(Math.random() * (60 - 10) + 10),
name: faker.commerce.productName(),
description: faker.lorem.lines(12).replace(/(\r\n|\n|\r)/gm, ' '),
abv,
ibu,
name,
description,
createdAt,
brewery: { connect: { id: breweryPost.id } },
postedBy: { connect: { id: user.id } },
type: { connect: { id: beerType.id } },
createdAt,
},
}),
);

View File

@@ -1,13 +1,13 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { faker } from '@faker-js/faker';
import { User } from '@prisma/client';
import { Location, User } from '@prisma/client';
import DBClient from '../../DBClient';
import geocode from '../../../config/mapbox/geocoder';
interface CreateNewBreweryPostsArgs {
numberOfPosts: number;
joinData: {
users: User[];
locations: Location[];
};
}
@@ -15,23 +15,17 @@ const createNewBreweryPosts = async ({
numberOfPosts,
joinData,
}: CreateNewBreweryPostsArgs) => {
const { users } = joinData;
const { users, locations } = joinData;
const prisma = DBClient.instance;
const breweryPromises = [];
// eslint-disable-next-line no-plusplus
for (let i = 0; i < numberOfPosts; i++) {
const name = `${faker.commerce.productName()} Brewing Company`;
const location = faker.address.cityName();
// eslint-disable-next-line no-await-in-loop
const geodata = await geocode(location);
const city = geodata.text;
const stateOrProvince = geodata.context.find((c) => c.id.startsWith('region'))?.text;
const country = geodata.context.find((c) => c.id.startsWith('country'))?.text;
const coordinates = geodata.center;
const address = geodata.place_name;
const description = faker.lorem.lines(5);
const locationIndex = Math.floor(Math.random() * locations.length);
const location = locations[locationIndex];
locations.splice(locationIndex, 1); // Remove the location from the array
const description = faker.lorem.lines(20).replace(/(\r\n|\n|\r)/gm, ' ');
const user = users[Math.floor(Math.random() * users.length)];
const createdAt = faker.date.past(1);
const dateEstablished = faker.date.past(40);
@@ -40,17 +34,11 @@ const createNewBreweryPosts = async ({
prisma.breweryPost.create({
data: {
name,
city,
stateOrProvince,
country,
coordinates,
address,
description,
postedBy: { connect: { id: user.id } },
createdAt,
dateEstablished,
postedBy: { connect: { id: user.id } },
location: { connect: { id: location.id } },
},
}),
);

View File

@@ -0,0 +1,63 @@
/* eslint-disable import/no-extraneous-dependencies */
import { faker } from '@faker-js/faker';
import { User, Location } from '@prisma/client';
import { GeocodeFeature } from '@mapbox/mapbox-sdk/services/geocoding';
import DBClient from '../../DBClient';
import geocode from '../../../config/mapbox/geocoder';
interface CreateNewLocationsArgs {
numberOfLocations: number;
joinData: {
users: User[];
};
}
const createNewLocations = async ({
numberOfLocations,
joinData,
}: CreateNewLocationsArgs) => {
const prisma = DBClient.instance;
const locationNames: string[] = [];
// eslint-disable-next-line no-plusplus
for (let i = 0; i < numberOfLocations; i++) {
locationNames.push(faker.address.cityName());
}
const geocodePromises: Promise<GeocodeFeature>[] = [];
locationNames.forEach((locationName) => {
geocodePromises.push(geocode(locationName));
});
const geocodedLocations = await Promise.all(geocodePromises);
const locationPromises: Promise<Location>[] = [];
geocodedLocations.forEach((geodata) => {
const city = geodata.text;
const user = joinData.users[Math.floor(Math.random() * joinData.users.length)];
const stateOrProvince = geodata.context?.find((c) => c.id.startsWith('region'))?.text;
const country = geodata.context?.find((c) => c.id.startsWith('country'))?.text;
const coordinates = geodata.center;
const address = geodata.place_name;
locationPromises.push(
prisma.location.create({
data: {
city,
stateOrProvince,
country,
coordinates,
address,
postedBy: { connect: { id: user.id } },
},
}),
);
});
return Promise.all(locationPromises);
};
export default createNewLocations;

View File

@@ -16,6 +16,9 @@ const createNewUsers = async ({ numberOfUsers }: CreateNewUsersArgs) => {
Array.from({ length: numberOfUsers }, () => argon2.hash(faker.internet.password())),
);
const takenEmails: string[] = [];
const takenUsernames: string[] = [];
// eslint-disable-next-line no-plusplus
for (let i = 0; i < numberOfUsers; i++) {
const randomValue = crypto.randomBytes(10).toString('hex');
@@ -24,6 +27,18 @@ const createNewUsers = async ({ numberOfUsers }: CreateNewUsersArgs) => {
const username = `${firstName[0]}.${lastName}.${randomValue}`;
const email = faker.internet.email(firstName, randomValue, 'example.com');
const usernameTaken = takenUsernames.includes(username);
const emailTaken = takenEmails.includes(email);
if (usernameTaken || emailTaken) {
i -= 1;
// eslint-disable-next-line no-continue
continue;
}
takenEmails.push(email);
takenUsernames.push(username);
const hash = hashedPasswords[i];
const dateOfBirth = faker.date.birthdate({ mode: 'age', min: 19 });
const createdAt = faker.date.past(1);