well deserved clean up (#1)

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-03-18 21:53:07 +01:00
parent 3a8404f704
commit b7513f24d5
106 changed files with 453 additions and 1275 deletions

View File

@@ -1,9 +1,3 @@
export function getDaysOldDate(days: number) {
const date = new Date();
date.setDate(date.getDate() - days);
return date;
}
export function dateDifferanceInDays(date1: Date, date2: Date) {
const diffTime = Math.abs(date2.getTime() - date1.getTime());
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));

View File

@@ -0,0 +1,35 @@
import { db } from '@openpanel/db';
import { slug } from './slug';
export async function getId(tableName: 'project' | 'dashboard', name: string) {
const newId = slug(name);
if (!db[tableName]) {
throw new Error('Table does not exists');
}
if (!('findUnique' in db[tableName])) {
throw new Error('findUnique does not exists');
}
// @ts-expect-error
const existingProject = await db[tableName].findUnique({
where: {
id: newId,
},
});
function random(str: string) {
const numbers = Math.floor(1000 + Math.random() * 9000);
if (str.match(/-\d{4}$/g)) {
return str.replace(/-\d{4}$/g, `-${numbers}`);
}
return `${str}-${numbers}`;
}
if (existingProject) {
return getId(tableName, random(name));
}
return newId;
}

View File

@@ -1,6 +0,0 @@
import type { IServiceProfile } from '@openpanel/db';
export function getProfileName(profile: IServiceProfile | undefined | null) {
if (!profile) return 'No profile';
return [profile.first_name, profile.last_name].filter(Boolean).join(' ');
}