Skip to content

Commit

Permalink
Merge pull request #26 from orcfax/bug/fact-url-mismatch
Browse files Browse the repository at this point in the history
fix url parsing bug and search bug
  • Loading branch information
gchartier authored Dec 9, 2024
2 parents 701c986 + 7691038 commit 0325616
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "explorer.orcfax.io",
"version": "2024-11-21",
"version": "2024-12-09",
"private": true,
"scripts": {
"dev": "vite dev",
Expand Down
13 changes: 6 additions & 7 deletions src/lib/server/db/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import type {
DirectoryNode,
FactSourceMessage,
ArchiveDownload,
FactStatementStub
FactStatementStub,
DBFactStatementWithFeed
} from '$lib/types';
import { getFactByURN, getSources } from '$lib/server/db';
import {
BagInfoSchema,
CEXValidationFileSchema,
DBFactStatementSchema,
DEXValidationFileSchema,
SourceSchema,
type DBFactStatement,
Expand Down Expand Up @@ -199,14 +199,13 @@ export async function getSelectedFact(
network: Network,
factURN: string,
feed: DBFeedWithData
): Promise<DBFactStatement | null> {
): Promise<DBFactStatementWithFeed | null> {
if (feed.latestFact === null) return null;

let selectedFact = feed.latestFact;
let selectedFact: DBFactStatementWithFeed = { ...feed.latestFact, feed };
if (factURN !== feed.latestFact.fact_urn) {
const response = await getFactByURN(network, factURN);
const parsed = DBFactStatementSchema.safeParse(response).data || feed.latestFact;
selectedFact = parsed;
const fact = await getFactByURN(network, factURN, `feed="${feed.id}"`);
selectedFact = fact;
}

return selectedFact;
Expand Down
25 changes: 16 additions & 9 deletions src/lib/server/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,18 @@ export async function doesFactExist(network: Network, factID?: string): Promise<

export async function getFactByURN(
network: Network,
factURN: string
): Promise<DBFactStatementWithFeed | null> {
factURN: string,
filters = ''
): Promise<DBFactStatementWithFeed> {
try {
const record = await db
.collection('facts')
.getFirstListItem(`fact_urn="${factURN}" && network="${network.id}"`, {
expand: 'feed.quote_asset,feed.base_asset'
});
.getFirstListItem(
`fact_urn="${factURN}" && network="${network.id}" ${filters ? `&& ${filters}` : ''}`,
{
expand: 'feed.quote_asset,feed.base_asset'
}
);

const parsed = DBFactStatementWithFeedSchema.safeParse({
...record,
Expand All @@ -283,7 +287,6 @@ export async function getFactByURN(
} catch (e) {
console.error(`Error retrieving fact by URN: ${e}`);
error(500, 'Error retrieving fact by URN');
return null;
}
}

Expand Down Expand Up @@ -316,7 +319,7 @@ export async function getFeedFactsByDateRange(
export async function searchFactStatements(
networkID: string,
query: string
): Promise<DBFactStatement[]> {
): Promise<DBFactStatementWithFeed[]> {
try {
const records = await db.collection('facts').getList(1, 50, {
filter: `network = "${networkID}" && (fact_urn ~ "${query}" || storage_urn ~ "${query}" || transaction_id ~ "${query}" || block_hash ~ "${query}")`,
Expand All @@ -325,10 +328,14 @@ export async function searchFactStatements(
const withExpandedFeeds = records.items.map((record) => {
return {
...record,
feed: record.expand?.feed ?? record.feed
feed: {
...(record.expand?.feed ?? record.feed),
base_asset: record.expand?.base_asset,
quote_asset: record.expand?.quote_asset
}
};
});
const parsedFacts = z.array(DBFactStatementSchema).parse(withExpandedFeeds);
const parsedFacts = z.array(DBFactStatementWithFeedSchema).parse(withExpandedFeeds);

return parsedFacts;
} catch (error) {
Expand Down
4 changes: 3 additions & 1 deletion src/routes/feeds/[...feed_id]/facts/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { redirect, type ServerLoad } from '@sveltejs/kit';

export const load: ServerLoad = async ({ parent, params }) => {
const layout = await parent();

const latestFactID = layout.feed.latestFact
? layout.feed.latestFact.fact_urn.slice(11)
: undefined;
const feedID = params.feed_id?.replace(/\/facts\/undefined$/, '');
throw redirect(307, `/feeds/${feedID}/facts/${latestFactID}`);
const factID = params.fact_id || latestFactID;
throw redirect(307, `/feeds/${feedID}/facts/${factID}`);
};

0 comments on commit 0325616

Please sign in to comment.