-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a generic web storage (#317)
* Add a generic web storage
- Loading branch information
1 parent
72f25b9
commit 9b550b5
Showing
5 changed files
with
118 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,63 @@ | ||
import { createLogger } from "../utils/logger.js"; | ||
import type { | ||
TransactionRow, | ||
TransactionStorage, | ||
SaveStats, | ||
} from "../types.js"; | ||
import { WEB_POST_URL } from "../config.js"; | ||
import { TransactionStatuses } from "israeli-bank-scrapers/lib/transactions.js"; | ||
import { transactionRow } from "./sheets.js"; | ||
|
||
const logger = createLogger("WebPostStorage"); | ||
|
||
export class WebPostStorage implements TransactionStorage { | ||
private url = WEB_POST_URL; | ||
|
||
async init() { | ||
logger("init"); | ||
} | ||
|
||
canSave() { | ||
return Boolean(this.url) && URL.canParse(this.url); | ||
} | ||
|
||
async saveTransactions(txns: Array<TransactionRow>) { | ||
logger("saveTransactions"); | ||
await this.init(); | ||
|
||
const nonPendingTxns = txns.filter( | ||
(txn) => txn.status !== TransactionStatuses.Pending, | ||
); | ||
|
||
logger(`Posting ${nonPendingTxns.length} transactions to ${this.url}`); | ||
|
||
const response = await fetch(this.url, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(nonPendingTxns.map((tx) => transactionRow(tx))), | ||
}); | ||
|
||
if (!response.ok) { | ||
logger(`Failed to post transactions: ${response.statusText}`); | ||
throw new Error(`Failed to post transactions: ${response.statusText}`); | ||
} | ||
|
||
const { added = nonPendingTxns.length, skipped = NaN } = | ||
await response.json(); | ||
|
||
const pending = txns.length - nonPendingTxns.length; | ||
const stats: SaveStats = { | ||
name: "WebPostStorage", | ||
table: "web-post", | ||
total: txns.length, | ||
added, | ||
pending, | ||
skipped: skipped + pending, | ||
existing: skipped, | ||
}; | ||
|
||
return stats; | ||
} | ||
} |