[Enhancement] Accounting for "www." prefix for duplicates

This commit is contained in:
GoodM4ven 2024-03-08 14:34:56 +03:00
parent 9fce74971f
commit cac90524ed

View File

@ -116,9 +116,18 @@ export default async function postLink(
}); });
if (user?.preventDuplicateLinks) { if (user?.preventDuplicateLinks) {
const trimmedUrl = link.url?.trim();
const wwwPrefix = 'www.';
const hasWwwPrefix = trimmedUrl?.includes(`://${wwwPrefix}`);
const urlWithoutWww = hasWwwPrefix ? trimmedUrl?.replace(`://${wwwPrefix}`, '://') : trimmedUrl;
const urlWithWww = hasWwwPrefix ? trimmedUrl : trimmedUrl?.replace('://', `://${wwwPrefix}`);
const existingLink = await prisma.link.findFirst({ const existingLink = await prisma.link.findFirst({
where: { where: {
url: link.url?.trim(), OR: [
{ url: trimmedUrl },
{ url: hasWwwPrefix ? urlWithoutWww : urlWithWww }, // Toggling "www."
],
collection: { collection: {
ownerId: userId, ownerId: userId,
}, },