42 lines
998 B
TypeScript
42 lines
998 B
TypeScript
|
|
// lib/utils/invite-code.ts
|
||
|
|
import crypto from "crypto"
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Gera um código de convite único
|
||
|
|
* Formato: cmhrtjzk6001jox4z9dzb5al
|
||
|
|
*/
|
||
|
|
export function generateInviteCode(): string {
|
||
|
|
return crypto
|
||
|
|
.randomBytes(16)
|
||
|
|
.toString("base64")
|
||
|
|
.replace(/[^a-z0-9]/gi, "")
|
||
|
|
.toLowerCase()
|
||
|
|
.substring(0, 24)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Valida formato do código de convite
|
||
|
|
*/
|
||
|
|
export function isValidInviteCode(code: string): boolean {
|
||
|
|
return /^[a-z0-9]{24}$/.test(code)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Gera slug único para workspace
|
||
|
|
* Formato: employee-name-manager-name-xyz123
|
||
|
|
*/
|
||
|
|
export function generateWorkspaceSlug(employeeName: string, managerName: string): string {
|
||
|
|
const slugify = (text: string) =>
|
||
|
|
text
|
||
|
|
.toLowerCase()
|
||
|
|
.normalize("NFD")
|
||
|
|
.replace(/[\u0300-\u036f]/g, "")
|
||
|
|
.replace(/[^a-z0-9]+/g, "-")
|
||
|
|
.replace(/^-+|-+$/g, "")
|
||
|
|
.substring(0, 20)
|
||
|
|
|
||
|
|
const random = crypto.randomBytes(3).toString("hex")
|
||
|
|
return `${slugify(employeeName)}-${slugify(managerName)}-${random}`
|
||
|
|
}
|
||
|
|
|