-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
autosync Fidelity balances and autosync all on tab change
- Loading branch information
Showing
14 changed files
with
274 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
gitleaks detect --source . -v | ||
if ! [ -x "$(command -v docker)" ]; then | ||
if ! [ -x "$(command -v podman)" ]; then | ||
echo 'docker or podman could not be found.' >&2 | ||
exit 1 | ||
fi | ||
podman run --rm -v $PWD:/app ghcr.io/zricethezav/gitleaks:latest detect --source /app -v | ||
fi | ||
|
||
if [ -x "$(command -v docker)" ]; then | ||
docker run --rm -v $PWD:/app ghcr.io/zricethezav/gitleaks:latest detect --source /app -v | ||
fi | ||
|
||
npx pretty-quick --staged |
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 |
---|---|---|
@@ -1,37 +1,98 @@ | ||
import { execute } from "./execute"; | ||
import { apiSync } from "./autorun"; | ||
import { downloadTcbNoSync } from "./sync/downloadTcbNoSync"; | ||
import { apiSync } from "./sync/sync"; | ||
import { getCurrentTab } from "./util/chrome"; | ||
import { fidelitySync } from "./sync/sync"; | ||
|
||
let tcb: string; | ||
chrome.storage.sync.get(["tcbUrl", "actualUrl"], (items) => { | ||
let fidelity: string; | ||
|
||
chrome.storage.sync.get(["tcbUrl", "fidelityUrl"], (items) => { | ||
tcb = items.tcbUrl; | ||
fidelity = items.fidelityUrl; | ||
}); | ||
|
||
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { | ||
if (message.action != "execute") return true; | ||
|
||
getCurrentTab().then((tab: any) => { | ||
if (!tab.url?.startsWith(tcb)) { | ||
sendResponse({ text: "Wrong tab!" }); | ||
return true; | ||
} | ||
execute(message.body.minDate, message.body.maxDate).then(() => {}); | ||
function runFidelitySync() { | ||
return getCurrentTab().then((tab) => { | ||
if (!tab.url?.startsWith(fidelity)) throw new Error("Wrong tab!"); | ||
fidelitySync().then(() => {}); | ||
}); | ||
} | ||
|
||
return true; | ||
}); | ||
function runTcbSync() { | ||
return getCurrentTab().then((tab) => { | ||
if (!tab.url?.startsWith(tcb)) throw new Error("Wrong tab!"); | ||
apiSync().then(() => {}); | ||
}); | ||
} | ||
|
||
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { | ||
if (message.action != "auto-run") return true; | ||
const minDate: string = message.minDate || ""; | ||
switch (message.action) { | ||
case "execute": { | ||
getCurrentTab().then((tab: any) => { | ||
if (!tab.url?.startsWith(tcb)) { | ||
sendResponse({ text: "Wrong tab!" }); | ||
return true; | ||
} | ||
downloadTcbNoSync(message.body.minDate, message.body.maxDate).then( | ||
() => {} | ||
); | ||
}); | ||
break; | ||
} | ||
case "auto-run": { | ||
const minDate: string = message.minDate; | ||
runTcbSync().then(() => | ||
chrome.storage.local.set({ lastTcbSync: new Date().toISOString() }) | ||
); | ||
break; | ||
} | ||
case "fidelity": { | ||
runFidelitySync().then(() => | ||
chrome.storage.local.set({ lastFidelitySync: new Date().toISOString() }) | ||
); | ||
break; | ||
} | ||
} | ||
}); | ||
|
||
getCurrentTab().then((tab) => { | ||
if (!tab.url?.startsWith(tcb)) { | ||
sendResponse({ text: "Wrong tab!" }); | ||
return true; | ||
// sync Fidelity automatically on tab change | ||
chrome.tabs.onActivated.addListener((activeInfo) => { | ||
chrome.tabs.get(activeInfo.tabId).then((tab) => { | ||
if (tab.url?.startsWith(fidelity)) { | ||
chrome.storage.local.get(["lastFidelitySync"], (items) => { | ||
const lastFidelitySync = items.lastFidelitySync; | ||
if ( | ||
lastFidelitySync && | ||
new Date().getTime() - new Date(lastFidelitySync).getTime() < | ||
4 * 60 * 60 * 1000 | ||
) | ||
return; | ||
runFidelitySync().then(() => | ||
chrome.storage.local.set({ | ||
lastFidelitySync: new Date().toISOString(), | ||
}) | ||
); | ||
}); | ||
} | ||
apiSync(minDate).then(() => {}); | ||
}); | ||
}); | ||
|
||
return true; | ||
//sync TCB automatically on tab change | ||
chrome.tabs.onActivated.addListener((activeInfo) => { | ||
chrome.tabs.get(activeInfo.tabId).then((tab) => { | ||
if (tab.url?.startsWith(tcb)) { | ||
chrome.storage.local.get(["lastTcbSync"], (items) => { | ||
const lastTcbSync = items.lastTcbSync; | ||
if ( | ||
lastTcbSync && | ||
new Date().getTime() - new Date(lastTcbSync).getTime() < | ||
4 * 60 * 60 * 1000 | ||
) | ||
return; | ||
runTcbSync().then(() => | ||
chrome.storage.local.set({ lastTcbSync: new Date().toISOString() }) | ||
); | ||
}); | ||
} | ||
}); | ||
}); |
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,47 @@ | ||
import { getCurrentTab } from "../util/chrome"; | ||
|
||
let fidelity: string; | ||
chrome.storage.sync.get(["fidelityUrl", "actualUrl"], (items) => { | ||
fidelity = items.fidelityUrl; | ||
}); | ||
|
||
async function getAccessToken() { | ||
const activeTab = await getCurrentTab(); | ||
const tabId = activeTab?.id as number; | ||
|
||
function _() { | ||
try { | ||
return sessionStorage?.access_token as string; | ||
} catch (err) { | ||
console.error("Error occured in getting sessionStorage", err); | ||
} | ||
} | ||
|
||
const injectionResults = await chrome.scripting.executeScript({ | ||
target: { tabId: tabId }, | ||
func: _, | ||
}); | ||
return injectionResults[0]?.result; | ||
} | ||
|
||
export async function getBalances(): Promise<Balances> { | ||
const myHeaders = new Headers(); | ||
myHeaders.append("accept", "*/*"); | ||
myHeaders.append("accept-language", "en-US,en"); | ||
myHeaders.append("content-type", "application/json"); | ||
|
||
const graphql = JSON.stringify({ | ||
query: | ||
"query GetContext {\n getContext {\n person {\n assets {\n acctNum\n preferenceDetail {\n name\n }\n gainLossBalanceDetail {\n totalMarketVal\n }\n }\n }\n }\n}\n", | ||
variables: {}, | ||
}); | ||
const requestOptions = { | ||
method: "POST", | ||
headers: myHeaders, | ||
body: graphql, | ||
}; | ||
|
||
const url = `${fidelity}/ftgw/digital/portfolio/api/graphql?ref_at=portsum`; | ||
const r = await fetch(url, requestOptions); | ||
return await r.json(); | ||
} |
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,24 @@ | ||
import { getBalances } from "./index"; | ||
import { getMappings } from "../util/mapping"; | ||
import { getExchangeRate } from "../util/currency"; | ||
|
||
export async function processFidelity() { | ||
const balances = await getBalances(); | ||
const { bm: balanceMapping } = await getMappings(); | ||
let accounts = {} as { [key: string]: number }; | ||
const exchangeRate = await getExchangeRate("USD"); | ||
|
||
balances.data.getContext.person.assets.forEach((asset) => { | ||
if (balanceMapping[asset.acctNum]) { | ||
const assetValue = asset.gainLossBalanceDetail.totalMarketVal; | ||
const accountId = balanceMapping[asset.acctNum]; | ||
// convert asset value to VND | ||
const assetValueVnd = assetValue * exchangeRate; | ||
accounts[accountId] = assetValueVnd; | ||
} | ||
}); | ||
|
||
console.log(accounts); | ||
|
||
return accounts; | ||
} |
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
Oops, something went wrong.