-
Notifications
You must be signed in to change notification settings - Fork 2
Add get-results command (read back live results/customs as JSON) #258
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| const SF = require("./api/sfApi"); | ||
| const Utils = require("./utils/liquidTestUtils"); | ||
| const { consola } = require("consola"); | ||
|
|
||
| /** | ||
| * Fetch the computed results and custom data of a reconciliation or account | ||
| * in a LIVE company file, identified by its Silverfin URL. | ||
| * | ||
| * Returns a plain object (no I/O) so it is easy to test and to serialise. | ||
| * Reuses the same getters and URL parsing as `create-test`. | ||
| * | ||
| * @param {String} url Full Silverfin URL of the reconciliation/account in the company file | ||
| * @returns {Promise<Object|null>} { templateType, firmId, companyId, periodId, ... , results, custom } or null on failure | ||
| */ | ||
| async function fetchResults(url) { | ||
| const parameters = Utils.extractURL(url); | ||
|
|
||
| switch (parameters.templateType) { | ||
| case "reconciliationText": { | ||
| const customResponse = await SF.getReconciliationCustom("firm", parameters.firmId, parameters.companyId, parameters.ledgerId, parameters.reconciliationId); | ||
| const resultsResponse = await SF.getReconciliationResults("firm", parameters.firmId, parameters.companyId, parameters.ledgerId, parameters.reconciliationId); | ||
| return { | ||
| templateType: parameters.templateType, | ||
| firmId: parameters.firmId, | ||
| companyId: parameters.companyId, | ||
| periodId: parameters.ledgerId, | ||
| reconciliationId: parameters.reconciliationId, | ||
| results: resultsResponse?.data ?? null, | ||
| custom: customResponse?.data ?? null, | ||
| }; | ||
| } | ||
| case "accountTemplate": { | ||
| const account = await SF.findAccountByNumber(parameters.firmId, parameters.companyId, parameters.ledgerId, parameters.accountId); | ||
| const accountId = account?.account?.id; | ||
| if (!accountId) { | ||
| consola.error(`Account "${parameters.accountId}" could not be resolved in this company file.`); | ||
| return null; | ||
| } | ||
| const customResponse = await SF.getAccountTemplateCustom("firm", parameters.firmId, parameters.companyId, parameters.ledgerId, accountId); | ||
| const resultsResponse = await SF.getAccountTemplateResults("firm", parameters.firmId, parameters.companyId, parameters.ledgerId, accountId); | ||
| return { | ||
| templateType: parameters.templateType, | ||
| firmId: parameters.firmId, | ||
| companyId: parameters.companyId, | ||
| periodId: parameters.ledgerId, | ||
| accountNumber: account.account.number, | ||
| accountId, | ||
| results: resultsResponse?.data ?? null, | ||
| custom: customResponse?.data ?? null, | ||
| }; | ||
| } | ||
| default: | ||
| consola.error(`Unsupported template type for URL: ${url}`); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| module.exports = { fetchResults }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| jest.mock("../../lib/api/sfApi"); | ||
| jest.mock("../../lib/utils/liquidTestUtils"); | ||
| jest.mock("consola"); | ||
|
|
||
| const SF = require("../../lib/api/sfApi"); | ||
| const Utils = require("../../lib/utils/liquidTestUtils"); | ||
| const { consola } = require("consola"); | ||
| const { fetchResults } = require("../../lib/resultsReader"); | ||
|
|
||
| describe("resultsReader.fetchResults", () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("fetches results + customs for a reconciliation URL", async () => { | ||
| Utils.extractURL.mockReturnValue({ | ||
| templateType: "reconciliationText", | ||
| firmId: "96", | ||
| companyId: "100", | ||
| ledgerId: "200", | ||
| reconciliationId: "300", | ||
| }); | ||
| SF.getReconciliationCustom.mockResolvedValue({ data: [{ namespace: "ns", key: "k", value: "v" }] }); | ||
| SF.getReconciliationResults.mockResolvedValue({ data: { vol_1022_man: "5000.0" } }); | ||
|
|
||
| const result = await fetchResults("https://live.getsilverfin.com/f/96/100/..."); | ||
|
|
||
| expect(SF.getReconciliationResults).toHaveBeenCalledWith("firm", "96", "100", "200", "300"); | ||
| expect(result).toEqual({ | ||
| templateType: "reconciliationText", | ||
| firmId: "96", | ||
| companyId: "100", | ||
| periodId: "200", | ||
| reconciliationId: "300", | ||
| results: { vol_1022_man: "5000.0" }, | ||
| custom: [{ namespace: "ns", key: "k", value: "v" }], | ||
| }); | ||
| }); | ||
|
|
||
| it("resolves the account and fetches results + customs for an account URL", async () => { | ||
| Utils.extractURL.mockReturnValue({ | ||
| templateType: "accountTemplate", | ||
| firmId: "96", | ||
| companyId: "100", | ||
| ledgerId: "200", | ||
| accountId: "610000", | ||
| }); | ||
| SF.findAccountByNumber.mockResolvedValue({ account: { id: 555, number: "610000", name: "Costs" } }); | ||
| SF.getAccountTemplateCustom.mockResolvedValue({ data: [{ namespace: "a", key: "b", value: 1 }] }); | ||
| SF.getAccountTemplateResults.mockResolvedValue({ data: { unreconciled_amount: "0.0" } }); | ||
|
|
||
| const result = await fetchResults("https://live.getsilverfin.com/f/96/100/..."); | ||
|
|
||
| expect(SF.getAccountTemplateResults).toHaveBeenCalledWith("firm", "96", "100", "200", 555); | ||
| expect(result).toMatchObject({ | ||
| templateType: "accountTemplate", | ||
| accountNumber: "610000", | ||
| accountId: 555, | ||
| results: { unreconciled_amount: "0.0" }, | ||
| custom: [{ namespace: "a", key: "b", value: 1 }], | ||
| }); | ||
| }); | ||
|
|
||
| it("returns null and logs an error when the account cannot be resolved", async () => { | ||
| Utils.extractURL.mockReturnValue({ | ||
| templateType: "accountTemplate", | ||
| firmId: "96", | ||
| companyId: "100", | ||
| ledgerId: "200", | ||
| accountId: "999999", | ||
| }); | ||
| SF.findAccountByNumber.mockResolvedValue(null); | ||
|
|
||
| const result = await fetchResults("https://live.getsilverfin.com/f/96/100/..."); | ||
|
|
||
| expect(result).toBeNull(); | ||
| expect(consola.error).toHaveBeenCalled(); | ||
| expect(SF.getAccountTemplateResults).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Handle fetch/write failures inside the command action.
Line 538 and Line 546 can both throw. Right now those failures bypass the
!datapath and abort the command with an uncaught error instead of a controlled non-zero exit plus a concise message.🛠️ Proposed fix
.option("-o, --output <file>", "Write the JSON to a file instead of stdout (optional)") .action(async (options) => { - const data = await resultsReader.fetchResults(options.url); - if (!data) { - process.exitCode = 1; - return; - } - const json = JSON.stringify(data, null, 2); - if (options.output) { - const fs = require("fs"); - fs.writeFileSync(options.output, json); - consola.success(`Wrote results to ${options.output}`); - } else { - console.log(json); - } + try { + const data = await resultsReader.fetchResults(options.url); + if (!data) { + process.exitCode = 1; + return; + } + const json = JSON.stringify(data, null, 2); + if (options.output) { + const fs = require("fs"); + fs.writeFileSync(options.output, json); + consola.success(`Wrote results to ${options.output}`); + } else { + console.log(json); + } + } catch (error) { + consola.error(error?.message ?? error); + process.exitCode = 1; + } });📝 Committable suggestion
🧰 Tools
🪛 ast-grep (0.44.0)
[warning] 545-545: Filesystem path is not a string literal; a request-/variable-derived path can enable path traversal. Validate and normalize the path before use.
Context: fs.writeFileSync(options.output, json)
Note: [CWE-22] Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal').
(detect-non-literal-fs-filename)
🤖 Prompt for AI Agents