import * as _ from 'lodash'; import { Transaction } from './transaction'; export class Account { public balance: number = 0; private numTransactions: number; public static fromJson (obj: any) { const items = _.orderBy(_.chain(obj.transactions).values().map(Transaction.fromJson).value(), ['created']); return new Account(Number(obj.numTransactions), obj.id, obj.code, obj.userId, obj.type, obj.status, obj.name, obj.currency , items, obj.created); } // _.orderBy(activity, ['date'], ['desc']); public toJson() { const accountJson: any = { // __v: '', id: this.id, code: this.code, userId: this.userId, type: this.type, status: this.status, name: this.name, currency: this.currency, // tenantId: '', // created: '', transactions: [] , // carItems to json numTransactions: this.getItemsCount(), balance: this.getbalance(), created: this.created // storeId: this.storeId, }; return accountJson; } // { // “id”: “590daf9118ce43c57ea18c24”, # // “userId”: “57ad4788840868525b18f489”, # // “created”: “2017-05-06T11:12:17.605Z”, ! // “status”: “active”, ! // “transactions”: [], # // “numTransactions”: 0, # // “balance”: 0, # // “type”: “wallet” # // } constructor( numTransactions: number // , public storeId?: string // TODO should be a Store instance when we have that // , public tenantId?: string , public id: string , public code: string , public userId: string , public type: string , public status: string , public name: string , public currency: string , public transactions: Transaction[] // , public status: string , public created: string ) { this.numTransactions = (_.isUndefined(numTransactions) || numTransactions == null? this.transactions.length : numTransactions ); this.calculatebalance(); if (this.id === undefined) { this.id = ''; } } public calculatebalance() { this.balance = 0; this.transactions.forEach( (transaction: Transaction) => { this.balance += transaction.getCost(); // this.balance += transaction.getCost(); }); } setItemsCount() { this.numTransactions = this.transactions.length; } getItemsCount() { return this.numTransactions; } getType() { return this.type; } getTransactions() { return this.transactions; } getbalance() { return this.balance; } }