Skip to content

Commit

Permalink
feat(seeder): assign tutor profile pictures randomly
Browse files Browse the repository at this point in the history
  • Loading branch information
Veirt committed Nov 30, 2024
1 parent b0a90ee commit dc5d409
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
62 changes: 56 additions & 6 deletions src/module/tutor/tutor.seeder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Tutor } from "@/types";
import { faker } from "@faker-js/faker";
import { container } from "@/container";
import { bucket } from "@/config";
import axios from "axios";

const authRepository = container.authRepository;
const tutorRepository = container.tutorRepository;
Expand All @@ -16,15 +18,12 @@ export const seedTutors = async () => {
const city = faker.helpers.arrayElement(["Surabaya", "Samarinda"]);
const district = faker.helpers.arrayElement(cityDistricts[city]);

const sexType = faker.person.sexType();
tutors.push({
name: faker.person.fullName(),
name: faker.person.fullName({ sex: sexType }),
email: faker.internet.email(),
password: faker.internet.password(),
gender: faker.helpers.arrayElement([
"male",
"female",
"prefer not to say",
]),
gender: sexType,
city,
district,
});
Expand All @@ -45,3 +44,54 @@ export const seedTutors = async () => {
});
}
};

const fetchRandomPersonImage = async (gender: string): Promise<string> => {
const baseURL = "https://this-person-does-not-exist.com";
try {
const qs = new URLSearchParams({
time: Date.now().toString(),
gender,
age: "19-25",
etnic: "all",
});

const response = await axios.get("/new", { params: qs, baseURL });
return `${baseURL}${response.data.src}`;
} catch (error) {
console.error("Failed to fetch random person image:", error);
throw error;
}
};

export const assignTutorProfilePictures = async (): Promise<void> => {
try {
const tutors = await tutorRepository.getAllTutors();

await Promise.all(
tutors.map(async (tutor) => {
try {
const name = `profile-pictures/${tutor.id}.jpg`;
const gender = ["male", "female"].includes(tutor.gender!)
? tutor.gender!
: "any";

const imageBuffer = (
await axios.get(await fetchRandomPersonImage(gender), {
responseType: "arraybuffer",
})
).data;

const bucketFile = bucket.file(name);
await bucketFile.save(imageBuffer, { public: true });
} catch (error) {
console.error(
`Failed to assign picture for tutor ID ${tutor.id}:`,
error,
);
}
}),
);
} catch (error) {
console.error("Failed to assign tutor profile pictures:", error);
}
};
6 changes: 5 additions & 1 deletion src/seed.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import "dotenv/config";
import { seedLearners } from "@/module/learner/learner.seeder";
import { seedSubjects } from "@/module/subject/subject.seeder";
import { seedTutors } from "@/module/tutor/tutor.seeder";
import {
assignTutorProfilePictures,
seedTutors,
} from "@/module/tutor/tutor.seeder";
import { seedTutories } from "@/module/tutories/tutories.seeder";

export const runSeeder = async () => {
await seedSubjects();
// await seedLearners();
// await seedTutors();
// await seedServices({ randomTeachingMethodology: false });
// await assignTutorProfilePictures();
};

runSeeder();

0 comments on commit dc5d409

Please sign in to comment.