Skip to content

Commit

Permalink
Add prettier (#157)
Browse files Browse the repository at this point in the history
* Add prettier

* prettier --check --write .

* Fail test if prettier fails
  • Loading branch information
daniel-hauser authored Aug 5, 2023
1 parent 05bc788 commit 5290075
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 40 deletions.
8 changes: 1 addition & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
{
"cSpell.words": [
"Kusto",
"moneyman",
"MULTIJSON",
"nektos",
"txns"
],
"cSpell.words": ["Kusto", "moneyman", "MULTIJSON", "nektos", "txns"],
"yaml.schemas": {
"https://json.schemastore.org/github-workflow.json": "file:///workspaces/money/.github/workflows/build.yml"
}
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,22 @@ If you want to see them, use the `DEBUG` environment variable with the value `mo
Use the following env vars to setup the data fetching:

#### ACCOUNTS_JSON

A json array of accounts following [this](https://github.com/eshaham/israeli-bank-scrapers#specific-definitions-per-scraper) schema with an additional `companyId` field with a [companyType](https://github.com/eshaham/israeli-bank-scrapers/blob/master/src/definitions.ts#L5:L23) as the value.

Example:

```json
[
{ "companyId": "hapoalim", "userCode": "AB1234", "password": "p@ssword" },
{ "companyId": "visaCal", "username": "Ploni Almoni", "password": "p@ssword" }
]
```

#### Other configurations

| env variable name | default | description |
| -------------------- | -------------------| ----------------------------------------------------------------------------------------------------- |
| -------------------- | ------------------ | ----------------------------------------------------------------------------------------------------- |
| `ACCOUNTS_TO_SCRAPE` | `[]` | A comma separated list of providers to take from `ACCOUNTS_JSON`. if empty, all accounts will be used |
| `DAYS_BACK` | `10` | The amount of days back to scrape |
| `TZ` | `'Asia/Jerusalem'` | A timezone for the process - used for the formatting of the timestamp |
Expand Down
6 changes: 3 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
preset: 'ts-jest',
testEnvironment: 'node',
};
preset: "ts-jest",
testEnvironment: "node",
};
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"start:container": "docker compose up",
"postinstall": "patch-package",
"build": "tsc",
"test": "jest"
"lint": "prettier --check .",
"test": "npm run lint && jest --bail"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -41,6 +42,7 @@
"@types/jest": "^29.5.3",
"jest": "^29.6.2",
"patch-package": "^8.0.0",
"prettier": "^3.0.1",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
}
Expand Down
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export const scrapeStartDate = subDays(Date.now(), Number(daysBackToScrape));

export const accounts = parseAccounts(ACCOUNTS_JSON).filter(
(account) =>
accountsToScrape.length == 0 || accountsToScrape.includes(account.companyId)
accountsToScrape.length == 0 ||
accountsToScrape.includes(account.companyId),
);

export const FileHeaders = [
Expand Down
10 changes: 5 additions & 5 deletions src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const logger = createLogger("data");
export async function scrapeAccounts(
accounts: Array<AccountConfig>,
startDate: Date,
statusMessageId?: number
statusMessageId?: number,
) {
const start = performance.now();

Expand Down Expand Up @@ -38,15 +38,15 @@ export async function scrapeAccounts(
logger(`scraping ended`);
const stats = getStats(results);
logger(
`Got ${stats.transactions} transactions from ${stats.accounts} accounts`
`Got ${stats.transactions} transactions from ${stats.accounts} accounts`,
);

const duration = (performance.now() - start) / 1000;
logger(`total duration: ${duration}s`);

await editMessage(
statusMessageId,
`${status.join("\n")}\n\ntotal time: ${duration.toFixed(1)}s`
`${status.join("\n")}\n\ntotal time: ${duration.toFixed(1)}s`,
);

return results;
Expand All @@ -55,12 +55,12 @@ export async function scrapeAccounts(
export async function scrapeAccount(
account: AccountConfig,
startDate: Date,
setStatusMessage: (message: string) => Promise<void>
setStatusMessage: (message: string) => Promise<void>,
) {
let message = "";
const start = performance.now();
const result = await getAccountTransactions(account, startDate, (cid, step) =>
setStatusMessage((message = `[${cid}] ${step}`))
setStatusMessage((message = `[${cid}] ${step}`)),
);

const duration = (performance.now() - start) / 1000;
Expand Down
2 changes: 1 addition & 1 deletion src/data/scrape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const logger = createLogger("scrape");
export async function getAccountTransactions(
account: AccountConfig,
startDate: Date,
onProgress: (companyId: string, status: string) => void
onProgress: (companyId: string, status: string) => void,
): Promise<ScraperScrapingResult> {
logger(`started`);
try {
Expand Down
14 changes: 7 additions & 7 deletions src/notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const bot =
logToPublicLog(
bot
? "Telegram logger initialized, status and errors will be sent"
: "No Telegram bot info, status and errors will not be sent"
: "No Telegram bot info, status and errors will not be sent",
);

export async function send(message: string) {
Expand All @@ -33,15 +33,15 @@ export async function deleteMessage(message: Message.TextMessage) {

export async function editMessage(
message: number | undefined,
newText: string
newText: string,
) {
if (message !== undefined) {
try {
await bot?.telegram.editMessageText(
TELEGRAM_CHAT_ID,
message,
undefined,
newText
newText,
);
} catch (e) {
if (canIgnoreTelegramError(e)) {
Expand All @@ -65,14 +65,14 @@ export function sendError(message: any, caller: string = "") {
`${caller}\n❌ ${String(
message instanceof Error
? `${message.message}\n${message.stack}`
: message
)}`.trim()
: message,
)}`.trim(),
);
}

export function getSummaryMessage(
results: Array<AccountScrapeResult>,
stats: Array<SaveStats>
stats: Array<SaveStats>,
) {
const accountsSummary = results.flatMap(({ result, companyId }) => {
if (!result.success) {
Expand All @@ -82,7 +82,7 @@ export function getSummaryMessage(
}
return result.accounts?.map(
(account) =>
`\t✔️ [${companyId}] ${account.accountNumber}: ${account.txns.length}`
`\t✔️ [${companyId}] ${account.accountNumber}: ${account.txns.length}`,
);
});

Expand Down
12 changes: 6 additions & 6 deletions src/storage/azure-data-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class AzureDataExplorerStorage implements TransactionStorage {
ADE_INGEST_URI!,
AZURE_APP_ID!,
AZURE_APP_KEY!,
AZURE_TENANT_ID
AZURE_TENANT_ID,
);

const ingestionProps = new IngestionProperties({
Expand All @@ -64,15 +64,15 @@ export class AzureDataExplorerStorage implements TransactionStorage {
ADE_DATABASE_NAME &&
ADE_TABLE_NAME &&
ADE_INGESTION_MAPPING &&
ADE_INGEST_URI
ADE_INGEST_URI,
);
}

async saveTransactions(txns: Array<TransactionRow>) {
logger(`Saving ${txns.length} transactions`);

const pending = txns.filter(
(tx) => tx.status === TransactionStatuses.Pending
(tx) => tx.status === TransactionStatuses.Pending,
).length;

const stats: SaveStats = {
Expand All @@ -88,18 +88,18 @@ export class AzureDataExplorerStorage implements TransactionStorage {
if (!this.ingestClient) {
await sendError(
"Called without initializing",
"AzureDataExplorerStorage.saveTransactions"
"AzureDataExplorerStorage.saveTransactions",
);
} else if (txns.length) {
const stream = Readable.from(
JSON.stringify(txns.map(this.transactionRow))
JSON.stringify(txns.map(this.transactionRow)),
);
const res = await this.ingestClient.ingestFromStream(stream);

if (res.errorCode) {
await sendError(
`returned ${res.errorCode}`,
"AzureDataExplorer.ingestFromStream "
"AzureDataExplorer.ingestFromStream ",
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function saveResults(results: Array<AccountScrapeResult>) {

if (txns.length) {
const res = await Promise.all(
storages.map((s) => s.saveTransactions(txns))
storages.map((s) => s.saveTransactions(txns)),
);

return {
Expand All @@ -39,7 +39,7 @@ export async function saveResults(results: Array<AccountScrapeResult>) {
}

function resultsToTransactions(
results: Array<AccountScrapeResult>
results: Array<AccountScrapeResult>,
): Array<TransactionRow> {
const txns: Array<TransactionRow> = [];

Expand Down
2 changes: 1 addition & 1 deletion src/storage/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class LocalJsonStorage implements TransactionStorage {

const fileName = path.join(
LocalJsonStorage.folder,
`${new Date().toISOString()}.json`
`${new Date().toISOString()}.json`,
);

await fs.appendFile(fileName, JSON.stringify(txns), { encoding: "utf8" });
Expand Down
2 changes: 1 addition & 1 deletion src/storage/sheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class GoogleSheetsStorage implements TransactionStorage {
const { GOOGLE_SERVICE_ACCOUNT_EMAIL, GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY } =
process.env;
return Boolean(
GOOGLE_SERVICE_ACCOUNT_EMAIL && GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY
GOOGLE_SERVICE_ACCOUNT_EMAIL && GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY,
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/storage/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ describe("transactionHash", () => {
const hash1 = transactionHash(
transaction1,
CompanyTypes.leumi,
accountNumber
accountNumber,
);

const hash2 = transactionHash(
transaction1,
CompanyTypes.isracard,
accountNumber
accountNumber,
);

expect(hash1).not.toBe(hash2);
Expand Down Expand Up @@ -93,7 +93,7 @@ describe("transactionHash", () => {
const hash = transactionHash(
transaction as any,
CompanyTypes.leumi,
"123"
"123",
);

expect(hash).not.toContain("null");
Expand Down
2 changes: 1 addition & 1 deletion src/storage/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Transaction } from "israeli-bank-scrapers/lib/transactions";
export function transactionHash(
tx: Transaction,
companyId: CompanyTypes,
accountNumber: string
accountNumber: string,
) {
const date = roundToNearestMinutes(parseISO(tx.date)).toISOString();
const parts = [
Expand Down

0 comments on commit 5290075

Please sign in to comment.