From c6806bea92f6f7b5885e895c197c0e710fcc1e85 Mon Sep 17 00:00:00 2001 From: Stuart Long Chay Boon Date: Mon, 24 Oct 2022 21:25:25 +0800 Subject: [PATCH] [offers][fix] integrate random name generator to profile seeding --- apps/portal/prisma/readSheet.ts | 7 +++++- .../router/offers/offers-profile-router.ts | 17 +------------ .../src/utils/offers/randomNameGenerator.ts | 24 +++++++++++++++++-- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/apps/portal/prisma/readSheet.ts b/apps/portal/prisma/readSheet.ts index 067c3d3f..5b286816 100644 --- a/apps/portal/prisma/readSheet.ts +++ b/apps/portal/prisma/readSheet.ts @@ -4,6 +4,7 @@ import crypto from 'crypto'; import { baseCurrencyString } from '../src/utils/offers/currency'; import { convert } from '../src/utils/offers/currency/currencyExchange'; import { generateAnalysis } from '../src/utils/offers/analysisGeneration'; +import generateRandomName from '../src/utils/offers/randomNameGenerator'; const prisma = new PrismaClient(); @@ -68,11 +69,15 @@ const seedSalaries = async () => { .createHash('sha256') .update(xlSerialToJsDate(data.Timestamp).toString()) .digest('hex'); + + // Generate random name until unique + let uniqueName: string = await generateRandomName(); + if (data.Type.toUpperCase() === 'INTERNSHIP') { // create profile const dataAdded = await prisma.offersProfile.create({ data: { - profileName: crypto.randomUUID().substring(0, 10), + profileName: uniqueName, createdAt: xlSerialToJsDate(data.Timestamp), editToken: token, background: { diff --git a/apps/portal/src/server/router/offers/offers-profile-router.ts b/apps/portal/src/server/router/offers/offers-profile-router.ts index 506b961b..c29dfde5 100644 --- a/apps/portal/src/server/router/offers/offers-profile-router.ts +++ b/apps/portal/src/server/router/offers/offers-profile-router.ts @@ -268,22 +268,7 @@ export const offersProfileRouter = createRouter() .digest('hex'); // Generate random name until unique - let uniqueName: string = generateRandomName(); - - let sameNameProfiles = await ctx.prisma.offersProfile.findMany({ - where: { - profileName: uniqueName - } - }) - - while (sameNameProfiles.length !== 0) { - uniqueName = generateRandomName(); - sameNameProfiles = await ctx.prisma.offersProfile.findMany({ - where: { - profileName: uniqueName - } - }) - } + const uniqueName: string = await generateRandomName(); const profile = await ctx.prisma.offersProfile.create({ data: { diff --git a/apps/portal/src/utils/offers/randomNameGenerator.ts b/apps/portal/src/utils/offers/randomNameGenerator.ts index ba286197..ff02e503 100644 --- a/apps/portal/src/utils/offers/randomNameGenerator.ts +++ b/apps/portal/src/utils/offers/randomNameGenerator.ts @@ -1,5 +1,8 @@ import type { Config } from 'unique-names-generator'; import { adjectives, animals,colors, uniqueNamesGenerator } from 'unique-names-generator'; +import { PrismaClient } from '@prisma/client'; + +const prisma = new PrismaClient() const customConfig: Config = { dictionaries: [adjectives, colors, animals], @@ -8,6 +11,23 @@ const customConfig: Config = { }; -export default function generateRandomName(): string { - return uniqueNamesGenerator(customConfig) +export default async function generateRandomName(): Promise { + let uniqueName: string = uniqueNamesGenerator(customConfig); + + let sameNameProfiles = await prisma.offersProfile.findMany({ + where: { + profileName: uniqueName + } + }) + + while (sameNameProfiles.length !== 0) { + uniqueName = uniqueNamesGenerator(customConfig); + sameNameProfiles = await prisma.offersProfile.findMany({ + where: { + profileName: uniqueName + } + }) + } + + return uniqueName } \ No newline at end of file