Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix[backend]: fix claimsfeed2 #121

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions __tests__/Intergration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe("Integration tests", () => {

it("should get all claims if no query params provided", async () => {
// const response = await request(app).get("/api/claim"); // not working
const response = await request(app).get("/api/claims-all");
const response = await request(app).get("/api/claimsfeed2");

expect(response.status).toBe(201);
expect(response.body.claimsData.length).toBeGreaterThan(0);
Expand Down Expand Up @@ -127,7 +127,7 @@ describe("Integration tests", () => {
const response = await request(app).get(
`/api/claim/search?search=${searchQuery}`
); // Corrected route
expect(response.status).toBe(201);
expect(response.status).toBe(200);
expect(response.body.claims.length).toBeGreaterThan(0);
expect(response.body.count).toBeGreaterThan(0);
expect(response.body.claims[0].images).toBeDefined();
Expand Down
152 changes: 88 additions & 64 deletions src/dao/api.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,70 +247,94 @@ export class NodeDao {
};

getFeedEntries = async (offset: number, limit: number, search: string) => {

const baseWhereClause = `WHERE NOT (n1."entType" = 'CLAIM' AND e.label = 'source')`;

const searchWhereClause = `
AND (
c.statement ILIKE ${Prisma.sql`%${search}%`} OR
c."sourceURI" ILIKE ${Prisma.sql`%${search}%`} OR
c."subject" ILIKE ${Prisma.sql`%${search}%`} OR
n1.name ILIKE ${Prisma.sql`%${search}%`} OR
n3.name ILIKE ${Prisma.sql`%${search}%`} OR
n3."descrip" ILIKE ${Prisma.sql`%${search}%`} OR
cd.name ILIKE ${Prisma.sql`%${search}%`} OR
i.url ILIKE ${Prisma.sql`%${search}%`} OR
i.metadata ILIKE ${Prisma.sql`%${search}%`} OR
i.owner ILIKE ${Prisma.sql`%${search}%`}
)
`;

const whereClause = search
? Prisma.sql`${Prisma.raw(baseWhereClause + searchWhereClause)}`
: Prisma.sql`${Prisma.raw(baseWhereClause)}`;

const rawQ = Prisma.sql`
SELECT
n1.name as name,
n1.thumbnail as thumbnail,
n1."nodeUri" as link,
n1."descrip" as description,
c.id as claim_id,
c.statement as statement,
c.stars as stars,
c.score as score,
c.amt as amt,
c."effectiveDate" as effective_date,
c."howKnown" as how_known,
c.aspect as aspect,
c.confidence as confidence,
e.label as claim,
e2.label as basis,
n3.name as source_name,
n3.thumbnail as source_thumbnail,
n3."nodeUri" as source_link,
n3."descrip" as source_description,
cd.name as claim_name,
i.url as image_url,
i."digestMultibase" as image_digest,
i.metadata as image_metadata
FROM "Claim" c
INNER JOIN "Edge" AS e ON c.id = e."claimId"
INNER JOIN "Node" AS n1 ON e."startNodeId" = n1.id
INNER JOIN "Node" AS n2 ON e."endNodeId" = n2.id
LEFT JOIN "Edge" as e2 ON n2.id = e2."startNodeId"
LEFT JOIN "Node" as n3 ON e2."endNodeId" = n3.id
LEFT JOIN "ClaimData" as cd ON c.id = cd."claimId"
LEFT JOIN "Image" as i ON c.id = i."claimId"
${whereClause}
ORDER BY c."effectiveDate" DESC
LIMIT ${limit}
OFFSET ${offset}
`;

console.log("Feed entries raw query: " + rawQ.sql + "\n with values \n" + rawQ.values)
const feedEntries = await prisma.$queryRaw<FeedEntry[]>`${rawQ}`;
return feedEntries;
try {
if (typeof search === "string") {
search = decodeURIComponent(search);
}

const baseWhereClause = `
WHERE n1."entType" != 'CLAIM'
AND e.label != 'source'
AND c."effectiveDate" IS NOT NULL
AND n1.name IS NOT NULL
AND n1.name != ''
`;

const searchWhereClause = search
? `
AND (
c.statement ILIKE '%${search}%' OR
c."sourceURI" ILIKE '%${search}%' OR
c."subject" ILIKE '%${search}%' OR
n1.name ILIKE '%${search}%' OR
n3.name ILIKE '%${search}%' OR
n3."descrip" ILIKE '%${search}%'
)
`
: "";

const whereClause = baseWhereClause + searchWhereClause;

const rawQ = Prisma.sql`
SELECT
n1.name as name,
n1.thumbnail as thumbnail,
n1."nodeUri" as link,
n1."descrip" as description,
c.id as claim_id,
c.statement as statement,
c.stars as stars,
c.score as score,
c.amt as amt,
c."effectiveDate" as effective_date,
c."howKnown" as how_known,
c.aspect as aspect,
c.confidence as confidence,
e.label as claim,
e2.label as basis,
n3.name as source_name,
n3.thumbnail as source_thumbnail,
n3."nodeUri" as source_link,
n3."descrip" as source_description
FROM "Claim" c
INNER JOIN "Edge" AS e ON c.id = e."claimId"
INNER JOIN "Node" AS n1 ON e."startNodeId" = n1.id
INNER JOIN "Node" AS n2 ON e."endNodeId" = n2.id
LEFT JOIN "Edge" as e2 ON n2.id = e2."startNodeId"
LEFT JOIN "Node" as n3 ON e2."endNodeId" = n3.id
LEFT JOIN "Image" as i ON c.id = i."claimId"
LEFT JOIN "ClaimData" as cd ON c.id = cd."claimId"
${Prisma.raw(whereClause)}
ORDER BY c."effectiveDate" DESC
LIMIT ${limit}
OFFSET ${offset}
`;

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 uniqueEntriesByClaimId = Array.from(claimIdMap.values());

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

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");
}
};

getNodeById = async (nodeId: number) => {
Expand Down