mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-30 14:39:29 +02:00
Check unique org niceId
This commit is contained in:
parent
c47d7fb1ff
commit
1f42409826
1 changed files with 20 additions and 1 deletions
|
@ -1,6 +1,9 @@
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
import { dirname, join } from 'path';
|
import { dirname, join } from 'path';
|
||||||
import { readFileSync } from 'fs';
|
import { readFileSync } from 'fs';
|
||||||
|
import { db } from '@server/db';
|
||||||
|
import { sites } from './schema';
|
||||||
|
import { eq, and } from 'drizzle-orm';
|
||||||
|
|
||||||
// Get the directory name of the current module
|
// Get the directory name of the current module
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
@ -10,7 +13,23 @@ const __dirname = dirname(__filename);
|
||||||
const file = join(__dirname, 'names.json');
|
const file = join(__dirname, 'names.json');
|
||||||
export const names = JSON.parse(readFileSync(file, 'utf-8'));
|
export const names = JSON.parse(readFileSync(file, 'utf-8'));
|
||||||
|
|
||||||
export function getUniqueName(): string {
|
export async function getUniqueName(orgId: string): Promise<string> {
|
||||||
|
let loops = 0;
|
||||||
|
while (true) {
|
||||||
|
if (loops > 100) {
|
||||||
|
throw new Error('Could not generate a unique name');
|
||||||
|
}
|
||||||
|
|
||||||
|
const name = generateName();
|
||||||
|
const count = await db.select({niceId: sites.niceId, orgId: sites.orgId}).from(sites).where(and(eq(sites.niceId, name), eq(sites.orgId, orgId)));
|
||||||
|
if (count.length === 0) {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
loops++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateName(): string {
|
||||||
return (
|
return (
|
||||||
names.descriptors[Math.floor(Math.random() * names.descriptors.length)] + "-" +
|
names.descriptors[Math.floor(Math.random() * names.descriptors.length)] + "-" +
|
||||||
names.animals[Math.floor(Math.random() * names.animals.length)]
|
names.animals[Math.floor(Math.random() * names.animals.length)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue