dbl-coins-invest
Version:
Sharade DBL package over other coins invest services
71 lines (57 loc) • 1.79 kB
JavaScript
import Rx from 'rxjs';
import { Account } from './models';
import DBL from './dbl';
import * as Sequelize from "sequelize";
const Op = Sequelize.Op;
export default class AccountService extends DBL {
constructor(userSub) {
if (!userSub)
throw new Error('userSub have to be provided!!!!');
super();
this.userSub = userSub;
this.model = Account;
}
getOrCreate(account, user) {
return this.getUserAccount()
.flatMap((existsAccount) => {
if (!!existsAccount)
return Rx.Observable.of(existsAccount);
return this.create(account, user);
});
}
create(account) {
return Rx.Observable.fromPromise(this.model.create(account));
}
assignPortfolio(account, portfolio) {
return Rx.Observable.fromPromise(account.addPortfolios(portfolio))
}
updateAccount(user) {
return this.getUserAccount()
.flatMap((accountModel) => {
accountModel.firstName = user.firstName;
accountModel.lastName = user.lastName;
return Rx.Observable.fromPromise(accountModel.save())
.flatMap((resp) => {
return this.getUserAccount();
})
})
}
getUserAccount() {
return Rx.Observable.fromPromise(this.model.findOne({
where: {
userSub: {
[Op.eq]: this.userSub
}
}
}));
}
getBySub(sub) {
return Rx.Observable.fromPromise(this.model.findOne({
where: {
userSub: {
[Op.eq]: this.userSub
}
}
}));
}
}