Fix duplicate code and import from lib server
parent
ff8dc3cb0f
commit
c677ce03f6
|
@ -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> => {
|
||||
|
|
|
@ -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"))) {
|
||||
|
|
Loading…
Reference in New Issue