diff --git a/src/balance.d.ts b/src/balance.d.ts new file mode 100644 index 0000000..a13f861 --- /dev/null +++ b/src/balance.d.ts @@ -0,0 +1,17 @@ +import { FG_Plugin } from '@flaschengeist/types'; + +export interface SendFromNotification { + receiver_id: string; + author_id: string; +} + +export interface SendToNotification { + sender_id: string; +} + +export interface BalanceNotification extends FG_Plugin.Notification { + data: { + type: number; + amount: number; + } & (SendFromNotification | SendToNotification); +} diff --git a/src/index.ts b/src/index.ts index 13ad746..af53601 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,41 @@ import { FG_Plugin } from '@flaschengeist/types'; import { defineAsyncComponent } from 'vue'; import routes from './routes'; +import { BalanceNotification, SendFromNotification, SendToNotification } from 'app/balance'; +import { useUserStore } from '@flaschengeist/api'; + +const BalanceTypes = { + send_to: 0x01, + send_from: 0x02, +}; + +function transpile(msg: FG_Plugin.Notification) { + console.log('notification:', msg); + const message = msg as BalanceNotification; + message.icon = 'mdi-cash'; + const store = useUserStore(); + void store.getUsers(); + if (message.data.type === BalanceTypes.send_from) { + const receiver = store.findUser((message.data).receiver_id); + const author = store.findUser((message.data).author_id); + message.text = `${author.display_name} hat ${message.data.amount.toFixed(2)}€ von dir zu ${ + receiver.display_name + } überwiesen.`; + } else { + const sender = store.findUser((message.data).sender_id); + console.log(sender); + message.text = `${sender.display_name} hat dir ${message.data.amount.toFixed(2)}€ überwiesen.`; + } + return message; +} const plugin: FG_Plugin.Plugin = { - id: 'balance', + id: 'dev.flaschengeist.balance', name: 'Balance', innerRoutes: routes, requiredModules: [['balance']], version: '0.0.2', + notification: transpile, widgets: [ { priority: 0, diff --git a/src/store.ts b/src/store.ts index 3180165..6f1dde1 100644 --- a/src/store.ts +++ b/src/store.ts @@ -77,12 +77,12 @@ export const useBalanceStore = defineStore({ return data; }, - async getBalances(filter?: {limit?: number, offset?: number, sortBy?: string, descending?: boolean}) { - const { data } = await api.get<{balances: BalancesResponse[], count: number}>('/balance', { - params: filter + async getBalances(filter?: { limit?: number; offset?: number; sortBy?: string; descending?: boolean }) { + const { data } = await api.get<{ balances: BalancesResponse[]; count: number }>('/balance', { + params: filter, }); this.balances = data.balances; - return {balances: this.balances, count: data.count}; + return { balances: this.balances, count: data.count }; }, async changeBalance(amount: number, user: FG.User, sender: FG.User | undefined = undefined) { @@ -158,9 +158,9 @@ export const useBalanceStore = defineStore({ this._balances_dirty = 0; }, - async getLimits(filter?: {userids: undefined|string}) { + async getLimits(filter?: { userids: undefined | string }) { const { data } = await api.get>('users/balance/limit', { - params: filter + params: filter, }); this.user_limits = data; },