Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): implement generateUsername query #99

Merged
merged 11 commits into from
Aug 22, 2023
17 changes: 16 additions & 1 deletion packages/server/src/routes/auth.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { loginSchema, singupSchema, validateSchema } from '@root/shared/validato
import { TRPCError } from '@trpc/server';
import { z } from 'zod';
import { publicProcedure, router } from '../trpc';
import { generateFromEmail } from '../utils/usernameGenerator';
import { generateFromEmail, generateUsername } from '../utils/usernameGenerator';

export const authRouter = router({
login: publicProcedure.input(loginSchema).mutation(async ({ input, ctx }) => {
Expand Down Expand Up @@ -50,4 +50,19 @@ export const authRouter = router({
const user = ctx.prisma.user.findUnique({ where: { username: input.username } });
return !!user;
}),

generateUsername: publicProcedure
.input(usernameGeneratorInputSchema)
.output(usernameGeneratorOutputSchema)
.query(async ({ input: { name, email }, ctx }) => {
const recursiveGenerator = async () => {
const res = generateUsername(name, email, 2, 3);
res.forEach(async (username) => {
if (await ctx.prisma.user.findUnique({ where: { username } }))
return recursiveGenerator();
});
return res;
};
return recursiveGenerator();
}),
});
7 changes: 7 additions & 0 deletions packages/shared/src/validators/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ export const loginSchema = z.object({

export const validateSchema = z.object({ username: z.string() });

export const usernameGeneratorInputSchema = z.object({
name: z.string(),
email: z.string().email(),
});

export const usernameGeneratorOutputSchema = z.array(z.string());

export const updateSchema = z.object({
id: z.string(),
name: z.string().optional(),
Expand Down
Loading