Files
formbricks/packages/lib/shortUrl/service.ts
T
Dhruwang Jariwala f70cda6e11 refactor: removes old types (#1288)
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
2023-10-20 12:03:16 +00:00

73 lines
1.9 KiB
TypeScript

import { prisma } from "@formbricks/database";
import { DatabaseError } from "@formbricks/types/errors";
import { TShortUrl, ZShortUrlId } from "@formbricks/types/shortUrl";
import { Prisma } from "@prisma/client";
import { customAlphabet } from "nanoid";
import { validateInputs } from "../utils/validate";
import z from "zod";
// Create the short url and return it
export const createShortUrl = async (url: string): Promise<TShortUrl> => {
validateInputs([url, z.string().url()]);
try {
// Check if an entry with the provided fullUrl already exists.
const existingShortUrl = await getShortUrlByUrl(url);
if (existingShortUrl) {
return existingShortUrl;
}
// If an entry with the provided fullUrl does not exist, create a new one.
const id = customAlphabet("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 10)();
return await prisma.shortUrl.create({
data: {
id,
url,
},
});
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseError(error.message);
}
throw error;
}
};
// Get the full url from short url and return it
export const getShortUrl = async (id: string): Promise<TShortUrl | null> => {
validateInputs([id, ZShortUrlId]);
try {
return await prisma.shortUrl.findUnique({
where: {
id,
},
});
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseError(error.message);
}
throw error;
}
};
export const getShortUrlByUrl = async (url: string): Promise<TShortUrl | null> => {
validateInputs([url, z.string().url()]);
try {
return await prisma.shortUrl.findUnique({
where: {
url,
},
});
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseError(error.message);
}
throw error;
}
};