remove trailing slashes + small improvement

This commit is contained in:
daniel31x13 2024-04-15 00:37:18 -04:00
parent cac90524ed
commit 4a71af8a67

View File

@ -116,24 +116,24 @@ export default async function postLink(
}); });
if (user?.preventDuplicateLinks) { if (user?.preventDuplicateLinks) {
const trimmedUrl = link.url?.trim(); const url = link.url?.trim().replace(/\/+$/, ""); // trim and remove trailing slashes from the URL
const wwwPrefix = 'www.'; const hasWwwPrefix = url?.includes(`://www.`);
const hasWwwPrefix = trimmedUrl?.includes(`://${wwwPrefix}`); const urlWithoutWww = hasWwwPrefix ? url?.replace(`://www.`, "://") : url;
const urlWithoutWww = hasWwwPrefix ? trimmedUrl?.replace(`://${wwwPrefix}`, '://') : trimmedUrl; const urlWithWww = hasWwwPrefix ? url : url?.replace("://", `://www.`);
const urlWithWww = hasWwwPrefix ? trimmedUrl : trimmedUrl?.replace('://', `://${wwwPrefix}`);
console.log(url, urlWithoutWww, urlWithWww);
const existingLink = await prisma.link.findFirst({ const existingLink = await prisma.link.findFirst({
where: { where: {
OR: [ OR: [{ url: urlWithWww }, { url: urlWithoutWww }],
{ url: trimmedUrl },
{ url: hasWwwPrefix ? urlWithoutWww : urlWithWww }, // Toggling "www."
],
collection: { collection: {
ownerId: userId, ownerId: userId,
}, },
}, },
}); });
console.log(url, urlWithoutWww, urlWithWww, "DONE!");
if (existingLink) if (existingLink)
return { return {
response: "Link already exists", response: "Link already exists",
@ -180,7 +180,7 @@ export default async function postLink(
const newLink = await prisma.link.create({ const newLink = await prisma.link.create({
data: { data: {
url: link.url?.trim(), url: link.url?.trim().replace(/\/+$/, ""),
name: link.name, name: link.name,
description, description,
type: linkType, type: linkType,