Skip to content

Commit

Permalink
fix[backend]: Remove Duplicates v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Kholoudxs55kh committed Aug 3, 2024
1 parent 50469b0 commit bd974e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RUN apt-get install -y openssl
WORKDIR /app

COPY package.json ./
COPY package-lock.json ./
# COPY package-lock.json ./

COPY ./prisma ./prisma

Expand Down
46 changes: 13 additions & 33 deletions src/dao/api.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,47 +310,27 @@ export class NodeDao {
OFFSET ${offset}
`;

console.log(
"Feed entries raw query: " +
rawQ.sql +
"\n with values \n" +
rawQ.values
);
const feedEntries = await prisma.$queryRaw<FeedEntry[]>`${rawQ}`;
// remove duplicates
const uniqueEntriesMap: Map<
string,
Map<string | null, Map<number, FeedEntry>>
> = new Map();

feedEntries.forEach((entry: FeedEntry) => {
if (!uniqueEntriesMap.has(entry.name)) {
uniqueEntriesMap.set(entry.name, new Map());
}

const sourceLinkMap = uniqueEntriesMap.get(entry.name)!;

if (!sourceLinkMap.has(entry.source_link)) {
sourceLinkMap.set(entry.source_link, new Map());
}

const claimIdMap = sourceLinkMap.get(entry.source_link)!;
const feedEntries = await prisma.$queryRaw<FeedEntry[]>(rawQ);

const claimIdMap = new Map<number, FeedEntry>();
feedEntries.forEach((entry) => {
if (!claimIdMap.has(entry.claim_id)) {
claimIdMap.set(entry.claim_id, entry);
}
});

const uniqueFeedEntries: FeedEntry[] = [];
uniqueEntriesMap.forEach((sourceLinkMap) => {
sourceLinkMap.forEach((claimIdMap) => {
claimIdMap.forEach((entry) => {
uniqueFeedEntries.push(entry);
});
});
const uniqueEntriesByClaimId = Array.from(claimIdMap.values());

const nameMap = new Map<string, FeedEntry>();
uniqueEntriesByClaimId.forEach((entry) => {
if (!nameMap.has(entry.name)) {
nameMap.set(entry.name, entry);
}
});

return uniqueFeedEntries;
const uniqueEntriesByName = Array.from(nameMap.values());

return uniqueEntriesByName;
} catch (error) {
console.error("Error fetching feed entries:", error);
throw new Error("Failed to fetch feed entries");
Expand Down

0 comments on commit bd974e4

Please sign in to comment.