Fix duplicate code and import from lib server

pull/11421/merge^2
Sean Brydon 2023-09-22 08:55:08 +01:00
parent ff8dc3cb0f
commit c677ce03f6
2 changed files with 2 additions and 15 deletions

View File

@ -2,11 +2,10 @@ import type { NextApiRequest, NextApiResponse } from "next";
import { z } from "zod";
import notEmpty from "@calcom/lib/notEmpty";
import { isPremiumUserName, generateUsernameSuggestion } from "@calcom/lib/server/username";
import slugify from "@calcom/lib/slugify";
import prisma from "@calcom/prisma";
const wordlist = {};
export type RequestWithUsernameStatus = NextApiRequest & {
usernameStatus: {
/**
@ -42,18 +41,6 @@ type CustomNextApiHandler<T = unknown> = (
res: NextApiResponse<T>
) => void | Promise<void>;
export const isPremiumUserName = (username: string): boolean =>
username.length <= 4 || Object.prototype.hasOwnProperty.call(wordlist, username);
const generateUsernameSuggestion = async (users: string[], username: string) => {
const limit = username.length < 2 ? 9999 : 999;
let rand = 1;
while (users.includes(username + String(rand).padStart(4 - rand.toString().length, "0"))) {
rand = Math.ceil(1 + Math.random() * (limit - 1));
}
return username + String(rand).padStart(4 - rand.toString().length, "0");
};
const usernameHandler =
(handler: CustomNextApiHandler) =>
async (req: RequestWithUsernameStatus, res: NextApiResponse): Promise<void> => {

View File

@ -35,7 +35,7 @@ type CustomNextApiHandler<T = unknown> = (
export const isPremiumUserName = (username: string): boolean =>
username.length <= 4 || Object.prototype.hasOwnProperty.call(wordlist, username);
const generateUsernameSuggestion = async (users: string[], username: string) => {
export const generateUsernameSuggestion = async (users: string[], username: string) => {
const limit = username.length < 2 ? 9999 : 999;
let rand = 1;
while (users.includes(username + String(rand).padStart(4 - rand.toString().length, "0"))) {