Skip to content

Commit 8937614

Browse files
authored
Fix Template import adding duplicate accounts (#13)
Fix bug on adding duplicate accounts
1 parent 071f720 commit 8937614

3 files changed

Lines changed: 38 additions & 16 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mongo-restart: mongo-stop mongo-start
1010

1111
mongo-reset: mongo-stop
1212
docker rm -f tradenote_db
13+
docker volume rm -f $$(docker volume ls)
1314
$(MAKE) mongo-start
1415

1516
node-env-prep:

src/utils/addTrades.js

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ export async function useGetExistingTradesArray(param99, param0) {
8181

8282
export async function useImportTrades(param1, param2, param3, param0) {
8383
return new Promise(async (resolve, reject) => {
84+
// reset accounts list for this import to avoid duplicates from previous files
85+
tradeAccounts.length = 0
86+
8487
//console.log("param1 " + param1)
8588
//console.log("param2 " + param2)
8689
//console.log("param3 " + param3)
@@ -394,9 +397,13 @@ async function createTempExecutions() {
394397
for (const key of keys) {
395398
try {
396399
let temp2 = {};
397-
temp2.account = tradesData[key].Account
400+
// normalise account and drop blanks
401+
let acctVal = tradesData[key].Account ? tradesData[key].Account.toString().trim() : ""
402+
temp2.account = acctVal
398403
temp2.broker = selectedBroker.value
399-
if (!tradeAccounts.includes(tradesData[key].Account)) tradeAccounts.push(tradesData[key].Account)
404+
if (acctVal && !tradeAccounts.includes(acctVal)) {
405+
tradeAccounts.push(acctVal)
406+
}
400407
/*usDate = dayjs.tz("07/22/2021 00:00:00", 'MM/DD/YYYY 00:00:00', "UTC")
401408
//frDate = usDate.tz("Europe/Paris")
402409
console.log("date "+usDate+" and fr ")*/
@@ -2101,8 +2108,14 @@ export async function useUploadTrades(param99, param0) {
21012108
const results = await query.first(param99 === "api" ? { useMasterKey: true } : undefined);
21022109
//console.log(" results "+JSON.stringify(results))
21032110
if (results) {
2104-
results.set("accounts", param)
2105-
//console.log("param 2" + JSON.stringify(param2))
2111+
// remove any empty account objects before saving
2112+
const cleanAccounts = Array.isArray(param)
2113+
? param.filter(a => a && a.value && a.value.toString().trim())
2114+
: param
2115+
results.set("accounts", cleanAccounts)
2116+
// update local user copy so subsequent imports see the change
2117+
currentUser.value.accounts = cleanAccounts
2118+
//console.log("param 2" + JSON.stringify(param2))
21062119
if (param99 === "api") {
21072120
await results.save(null, { useMasterKey: true }) //very important to have await or else too quick to update
21082121
} else {
@@ -2123,8 +2136,10 @@ export async function useUploadTrades(param99, param0) {
21232136
} else {
21242137
selectedItemsArray = []
21252138
}
2126-
//console.log(" selected items value " + JSON.stringify(selectedItemsArray))
2127-
selectedItemsArray.push(param2)
2139+
// only add the entry if it isn't already present and not empty
2140+
if (param2 && !selectedItemsArray.includes(param2)) {
2141+
selectedItemsArray.push(param2)
2142+
}
21282143
localStorage.setItem(selectedItems, selectedItemsArray)
21292144
//console.log(" -> Updated selectedItems / localstorage " + selectedItemsArray)
21302145
}
@@ -2135,25 +2150,28 @@ export async function useUploadTrades(param99, param0) {
21352150

21362151
if (currentUser.value.accounts) {
21372152
tradeAccounts.forEach(element => {
2138-
let check = currentUser.value.accounts.find(x => x.value == element)
2139-
//console.log("check "+JSON.stringify(check))
2153+
const acct = (typeof element === 'string' ? element.trim() : element)
2154+
if (!acct) return // skip blank
2155+
let check = currentUser.value.accounts.find(x => x.value == acct)
21402156
if (!check) {
2141-
let tempArray = currentUser.value.accounts
2157+
let tempArray = currentUser.value.accounts.slice() // copy existing array
21422158
let temp = {}
2143-
temp.value = tradeAccounts[0]
2144-
temp.label = tradeAccounts[0]
2159+
temp.value = acct
2160+
temp.label = acct
21452161
tempArray.push(temp)
2146-
updateTradeAccounts(tempArray, temp.value)
2162+
updateTradeAccounts(tempArray, acct)
21472163
}
21482164
});
21492165
} else {
21502166
let tempArray = []
21512167
tradeAccounts.forEach(element => {
2168+
const acct = (typeof element === 'string' ? element.trim() : element)
2169+
if (!acct) return
21522170
let temp = {}
2153-
temp.value = element
2154-
temp.label = element
2171+
temp.value = acct
2172+
temp.label = acct
21552173
tempArray.push(temp)
2156-
updateTradeAccounts(tempArray, temp.value)
2174+
updateTradeAccounts(tempArray, acct)
21572175
})
21582176
}
21592177
})

src/utils/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,10 @@ export async function useSetValues() {
996996

997997
if (Object.is(localStorage.getItem('selectedAccounts'), null) && currentUser.value && currentUser.value.hasOwnProperty("accounts") && currentUser.value.accounts.length > 0) {
998998
currentUser.value.accounts.forEach(element => {
999-
selectedAccounts.value.push(element.value)
999+
if (element && element.value) {
1000+
const acct = element.value.toString().trim()
1001+
if (acct) selectedAccounts.value.push(acct)
1002+
}
10001003
});
10011004
//console.log("selected accounts " + JSON.stringify(selectedAccounts))
10021005
localStorage.setItem('selectedAccounts', selectedAccounts.value)

0 commit comments

Comments
 (0)