Merge pull request #507 from GoodM4ven/missing-duplicate-checks

[Enhancement] Accounting for "www." prefix for duplicates
This commit is contained in:
Daniel 2024-04-15 08:09:10 +03:30 committed by GitHub
commit a89274fc03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

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