From dc5d409a831f283c464d216badcdd6a376f14008 Mon Sep 17 00:00:00 2001 From: Veirt Date: Sat, 30 Nov 2024 11:05:04 +0800 Subject: [PATCH] feat(seeder): assign tutor profile pictures randomly --- src/module/tutor/tutor.seeder.ts | 62 ++++++++++++++++++++++++++++---- src/seed.ts | 6 +++- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/module/tutor/tutor.seeder.ts b/src/module/tutor/tutor.seeder.ts index 6eeaa40..9562efd 100644 --- a/src/module/tutor/tutor.seeder.ts +++ b/src/module/tutor/tutor.seeder.ts @@ -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; @@ -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, }); @@ -45,3 +44,54 @@ export const seedTutors = async () => { }); } }; + +const fetchRandomPersonImage = async (gender: string): Promise => { + 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 => { + 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); + } +}; diff --git a/src/seed.ts b/src/seed.ts index 9377f0f..d3dc96f 100644 --- a/src/seed.ts +++ b/src/seed.ts @@ -1,7 +1,10 @@ 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 () => { @@ -9,6 +12,7 @@ export const runSeeder = async () => { // await seedLearners(); // await seedTutors(); // await seedServices({ randomTeachingMethodology: false }); + // await assignTutorProfilePictures(); }; runSeeder();