Skip to content

Commit

Permalink
reloading the portfolio now also reload the activities
Browse files Browse the repository at this point in the history
  • Loading branch information
BinarSkugga committed Jun 11, 2023
1 parent 364aba8 commit 29b82c1
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions backend/interface/wealthsimple_api_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from backend.models.stock import Stock
from backend.models.ws_token_set import WSTokenSet
from backend.models.ws_user import WSUser
from models.activity import Activity


class FailedWSLogin(BaseException):
Expand Down Expand Up @@ -33,6 +34,10 @@ def accounts(self) -> List[WSAccount]:
def positions(self) -> List[WSPosition]:
pass

@abstractmethod
def activities(self) -> List[Activity]:
pass

@abstractmethod
def watchlist(self) -> List[Stock]:
pass
12 changes: 11 additions & 1 deletion backend/routes/activity_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
from backend.auth_utils import auth
from backend.models.activity import Activity
from backend.repository import Repository
from interface.wealthsimple_api_interface import IWealthSimpleAPI


def load(fastapi: FastAPI):
def load(fastapi: FastAPI, ws_api: IWealthSimpleAPI):
activities = Repository('activity', Activity)

@fastapi.get('/api/v1/activities', dependencies=[auth('default')])
def activities_list():
return activities.list()

@fastapi.get('/api/v1/activities/update', dependencies=[auth('default')])
def activities_set():
ws_activities = ws_api.activities()
activities.truncate()
for activity in ws_activities:
activities.upsert(activity)

return activities.list()
2 changes: 1 addition & 1 deletion backend/wealthsimple_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def watchlist(self):
self.refresh(self.key_ring.refresh)
return ws_watchlist(self.key_ring.access, self.account.id)

def activity(self):
def activities(self):
self.refresh(self.key_ring.refresh)
return ws_activity(self.key_ring.access, self.account.id)

Expand Down
4 changes: 4 additions & 0 deletions frontend/src/api/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ const host = import.meta.env.VITE_API_URL
export function api_activities(token: string) {
return axios.get(host + '/api/v1/activities', {headers: {'Authorization': 'Bearer ' + token}})
}

export function api_update_activities(token: string) {
return axios.get(host + '/api/v1/activities/update', {headers: {'Authorization': 'Bearer ' + token}})
}
9 changes: 8 additions & 1 deletion frontend/src/stores/ActivitiesStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {defineStore, storeToRefs} from "pinia";
import {useUsersStore} from "@/stores/UsersStore";
import {api_activities} from "@/api/activity";
import {api_activities, api_update_activities} from "@/api/activity";
import {api_update_positions} from "@/api/position";


export const useActivityStore = defineStore({
Expand All @@ -17,6 +18,12 @@ export const useActivityStore = defineStore({
return api_activities(token.value!).then(response => {
this.activities = response.data
}).catch(error => {})
},
updateActivities() {
const {token} = storeToRefs(useUsersStore())
return api_update_activities(token.value!).then(response => {
this.activities = response.data
}).catch(error => {})
}
}
})
4 changes: 2 additions & 2 deletions frontend/src/views/Portfolio.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export default {
methods: {
...mapActions(usePositionStore, ['getPositions', "fetchPositions", "updatePositions"]),
...mapActions(useStocksStore, ['getStocks', "fetchStocks"]),
...mapActions(useActivityStore, ['getActivities', "fetchActivities"]),
...mapActions(useActivityStore, ['getActivities', "fetchActivities", "updateActivities"]),
updatePositionsWithLoading() {
this.updatingPositions = true
this.updatePositions().finally(_ => {
Promise.all([this.updatePositions(), this.updateActivities()]).finally(_ => {
this.updatingPositions = false
})
},
Expand Down
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
current_activities = list(activities.list())

if len(current_activities) == 0:
ws_activities = ws.activity()
ws_activities = ws.activities()
for activity in ws_activities:
activities.upsert(activity)

Expand All @@ -87,7 +87,7 @@
auth_routes.load(fastapi)
stock_routes.load(fastapi, ws)
position_routes.load(fastapi, ws)
activity_routes.load(fastapi)
activity_routes.load(fastapi, ws)

create_dist_folder('frontend/dist')
fastapi.mount("/", StaticFiles(directory="frontend/dist", html=True), name="frontend")

0 comments on commit 29b82c1

Please sign in to comment.