[offers][chore] integrate random name generator to create profile

pull/422/head
Stuart Long Chay Boon 2 years ago
parent ef5892a0d6
commit e77bb03639

@ -1,4 +1,4 @@
import crypto, { randomUUID } from 'crypto'; import crypto from 'crypto';
import { z } from 'zod'; import { z } from 'zod';
import { JobType } from '@prisma/client'; import { JobType } from '@prisma/client';
import * as trpc from '@trpc/server'; import * as trpc from '@trpc/server';
@ -10,6 +10,7 @@ import {
} from '~/mappers/offers-mappers'; } from '~/mappers/offers-mappers';
import { baseCurrencyString } from '~/utils/offers/currency'; import { baseCurrencyString } from '~/utils/offers/currency';
import { convert } from '~/utils/offers/currency/currencyExchange'; import { convert } from '~/utils/offers/currency/currencyExchange';
import generateRandomName from '~/utils/offers/randomNameGenerator';
import { createValidationRegex } from '~/utils/offers/zodRegex'; import { createValidationRegex } from '~/utils/offers/zodRegex';
import { createRouter } from '../context'; import { createRouter } from '../context';
@ -266,6 +267,24 @@ export const offersProfileRouter = createRouter()
.update(Date.now().toString()) .update(Date.now().toString())
.digest('hex'); .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 profile = await ctx.prisma.offersProfile.create({ const profile = await ctx.prisma.offersProfile.create({
data: { data: {
background: { background: {
@ -538,7 +557,7 @@ export const offersProfileRouter = createRouter()
}), }),
), ),
}, },
profileName: randomUUID().substring(0, 10), profileName: uniqueName,
}, },
}); });

Loading…
Cancel
Save