-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathWalletService.js
More file actions
82 lines (69 loc) · 1.98 KB
/
WalletService.js
File metadata and controls
82 lines (69 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const { validate: uuidValidate } = require('uuid');
const Wallet = require('../models/Wallet');
const Session = require('../infra/database/Session');
const Token = require('../models/Token');
class WalletService {
constructor() {
this._session = new Session();
this._wallet = new Wallet(this._session);
}
async getSubWallets(id) {
return this._wallet.getSubWallets(id);
}
async getById(id) {
return this._wallet.getById(id);
}
async getByName(name) {
return this._wallet.getByName(name);
}
async getWallet(walletId) {
return this._wallet.getWallet(walletId);
}
async getByIdOrName(idOrName) {
let wallet;
if (uuidValidate(idOrName)) {
wallet = await this.getById(idOrName);
} else {
wallet = await this.getByName(idOrName);
}
return wallet;
}
async createWallet(loggedInWalletId, wallet) {
try {
await this._session.beginTransaction();
const addedWallet = await this._wallet.createWallet(
loggedInWalletId,
wallet,
);
await this._session.commitTransaction();
return { wallet: addedWallet.name, id: addedWallet.id };
} catch (e) {
if (this._session.isTransactionInProgress()) {
await this._session.rollbackTransaction();
}
throw e;
}
}
async getAllWallets(id, limitOptions, sortOptions, getTokenCount = true) {
if (getTokenCount) {
const token = new Token(this._session);
const wallets = await this._wallet.getAllWallets(
id,
limitOptions,
sortOptions,
);
return Promise.all(
wallets.map(async (wallet) => {
const json = { ...wallet };
json.tokens_in_wallet = await token.countTokenByWallet(wallet.id);
return json;
}),
);
}
return this._wallet.getAllWallets(id, limitOptions, sortOptions);
}
async hasControlOver(parentId, childId) {
return this._wallet.hasControlOver(parentId, childId);
}
}
module.exports = WalletService;