From 3d06d149b4fc727d8942445c3576cad107545dec Mon Sep 17 00:00:00 2001 From: Huey Date: Sat, 6 Jul 2024 11:15:09 +0800 Subject: [PATCH] fix: OpenLaw citation search --- package.json | 2 +- src/utils/scraper/SG/OpenLaw.ts | 56 +++++++++---------- .../__snapshots__/OpenLaw.test.ts.snap | 39 ++++--------- 3 files changed, 39 insertions(+), 58 deletions(-) diff --git a/package.json b/package.json index fb23086..de03d43 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "clerkent", - "version": "3.7.2", + "version": "3.7.3", "private": true, "description": "quick search legal search", "repository": "https://github.com/lacuna-technologies/clerkent.git", diff --git a/src/utils/scraper/SG/OpenLaw.ts b/src/utils/scraper/SG/OpenLaw.ts index 944bf9a..014fb0c 100644 --- a/src/utils/scraper/SG/OpenLaw.ts +++ b/src/utils/scraper/SG/OpenLaw.ts @@ -6,7 +6,6 @@ import Helpers from 'utils/Helpers' import { CacheRequestConfig } from 'axios-cache-interceptor' const SEARCH_API_URL = `https://api.lawnet.sg/lawnet/search-service/api/lawnetcore/search/supreme-court` -const CITATION_API_URL = `https://api.lawnet.sg/lawnet/search-service/api/lawnetcore/document/citation` const PDF_DOWNLOAD_URL = `https://api.lawnet.sg/lawnet/search-service/api/lawnetcore/download/v1` const defaultUserDevice = { isBot: false, @@ -56,40 +55,41 @@ const defaultRequestOptions = { const getCaseByCitation = async (citation: string): Promise => { try { const requestBody = { - ...defaultCitationConfig, + ...defaultSearchConfig, data: { - ...defaultCitationConfig.data, - citation, + ...defaultSearchConfig.data, + searchQuery: `"${citation}"`, }, } const { data } = await Request.post( - CITATION_API_URL, + SEARCH_API_URL, requestBody, defaultRequestOptions as unknown as CacheRequestConfig, ) - const encodedCitation = citation.replaceAll(` `, `+`) - const judgmentLink: Law.Link = { - doctype: `Judgment`, - filetype: `HTML`, - url: `https://www.lawnet.com/openlaw/cases/citation/${encodedCitation}`, - } - const summaryLink: Law.Link = { - doctype: `Summary`, - filetype: `HTML`, - url: `https://www.lawnet.com/openlaw/cases/citation/${encodedCitation}`, - } - const result: Law.Case = { - citation: data.data.metadata[`NeutralCitation`][`NCit`], - database: Constants.DATABASES.SG_openlaw, - jurisdiction: Constants.JURISDICTIONS.SG.id, - links: [ - summaryLink, - judgmentLink, - ], - name: data.data.metadata[`CaseTitle`], - } - return [result] - + const results: Law.Case[] = data.data.results.map((result): Law.Case => { + const encodedCitation = result.ncitation.replaceAll(` `, `+`) + const summaryLink: Law.Link = { + doctype: `Summary`, + filetype: `HTML`, + url: `https://www.lawnet.com/openlaw/cases/citation/${encodedCitation}`, + } + const judgmentLink: Law.Link = { + doctype: `Judgment`, + filetype: `HTML`, + url: `https://www.lawnet.com/openlaw/cases/citation/${encodedCitation}`, + } + return { + citation: result.ncitation, + database: Constants.DATABASES.SG_openlaw, + jurisdiction: Constants.JURISDICTIONS.SG.id, + links: [ + summaryLink, + judgmentLink, + ], + name: result.titles[0], + } + }) + return results } catch (error) { Logger.error(error) return [] diff --git a/src/utils/scraper/SG/__tests__/__snapshots__/OpenLaw.test.ts.snap b/src/utils/scraper/SG/__tests__/__snapshots__/OpenLaw.test.ts.snap index 2282a24..037afea 100644 --- a/src/utils/scraper/SG/__tests__/__snapshots__/OpenLaw.test.ts.snap +++ b/src/utils/scraper/SG/__tests__/__snapshots__/OpenLaw.test.ts.snap @@ -1,40 +1,21 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`SG OpenLaw should parse getCaseByCitation result correctly 1`] = ` -[ - { - "citation": "[1999] SGHC 283", - "database": { - "icon": "", - "id": "SG_openlaw", - "name": "OpenLaw", - "url": "https://www.lawnet.com/openlaw", - }, - "jurisdiction": "SG", - "links": [ - { - "doctype": "Summary", - "filetype": "HTML", - "url": "https://www.lawnet.com/openlaw/cases/citation/[1999]+SGHC+283", - }, - { - "doctype": "Judgment", - "filetype": "HTML", - "url": "https://www.lawnet.com/openlaw/cases/citation/[1999]+SGHC+283", - }, - ], - "name": "NV Sumatra Tobacco Co v Nanyang Brothers Tobacco Co Ltd", - }, -] -`; +exports[`SG OpenLaw should parse getCaseByCitation result correctly 1`] = `[]`; exports[`SG OpenLaw should send valid request for getCaseByCitation 1`] = ` [ [ - "https://api.lawnet.sg/lawnet/search-service/api/lawnetcore/document/citation", + "https://api.lawnet.sg/lawnet/search-service/api/lawnetcore/search/supreme-court", { "data": { - "citation": "[1999] SGHC 283", + "filters": null, + "includeDraft": "true", + "orderBy": "relevancy-desc", + "pageLength": 10, + "searchDatabase": "allOfLawnet", + "searchQuery": ""[1999] SGHC 283"", + "searchSource": null, + "start": 1, }, "info": { "appId": "APP_LAWNET_ONE",