Skip to content

Commit

Permalink
feat(server): implement generateUsername query (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
dotmanki authored Aug 22, 2023
1 parent a1f09a9 commit 42628dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
25 changes: 23 additions & 2 deletions packages/server/src/routes/auth.route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { passwordResetSchema } from '@root/shared/validators/password.model';
import { loginSchema, singupSchema, validateSchema } from '@root/shared/validators/user.model';
import {
loginSchema,
singupSchema,
usernameGeneratorInputSchema,
usernameGeneratorOutputSchema,
validateSchema,
} from '@root/shared/validators/user.model';
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 +56,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

1 comment on commit 42628dc

@vercel
Copy link

@vercel vercel bot commented on 42628dc Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.