-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathWalletRepository.js
More file actions
109 lines (100 loc) · 3 KB
/
Copy pathWalletRepository.js
File metadata and controls
109 lines (100 loc) · 3 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* The model for: entity, wallet, entity table and so on
*/
const expect = require('expect-runtime');
const HttpError = require('../utils/HttpError');
const TrustRelationshipEnums = require('../utils/trust-enums');
const BaseRepository = require('./BaseRepository');
class WalletRepository extends BaseRepository {
constructor(session) {
super('wallet', session);
this._tableName = 'wallet';
this._session = session;
}
async getByName(wallet) {
expect(
wallet,
() => new HttpError(400, `invalid wallet name:${wallet}`),
).match(/^\S+$/);
const list = await this._session
.getDB()
.select()
.table(this._tableName)
.where('name', wallet);
expect(
list,
() =>
new HttpError(404, `Could not find entity by wallet name: ${wallet}`),
)
.defined()
.lengthOf(1);
return list[0];
}
async getById(id) {
const object = await this._session
.getDB()
.select()
.table(this._tableName)
.where('id', id)
.first();
if (!object) {
throw new HttpError(404, `Could not find wallet by id: ${id}`);
}
return object;
}
// Get a wallet itself including its sub wallets
async getAllWallets(id, limitOptions, sortOptions) {
let promise = this._session
.getDB()
.select('id', 'name', 'logo_url', 'created_at')
.table('wallet')
.where('id', id)
.union(
this._session
.getDB()
.select(
'wallet.id',
'wallet.name',
'wallet.logo_url',
'wallet.created_at',
)
.table('wallet_trust')
.join('wallet', 'wallet_trust.target_wallet_id', '=', 'wallet.id')
.where({
'wallet_trust.actor_wallet_id': id,
'wallet_trust.request_type':
TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.manage,
'wallet_trust.state':
TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted,
}),
this._session
.getDB()
.select(
'wallet.id',
'wallet.name',
'wallet.logo_url',
'wallet.created_at',
)
.table('wallet_trust')
.join('wallet', 'wallet_trust.actor_wallet_id', '=', 'wallet.id')
.where({
'wallet_trust.target_wallet_id': id,
'wallet_trust.request_type':
TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.yield,
'wallet_trust.state':
TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted,
}),
);
if (limitOptions && limitOptions.limit) {
promise = promise.limit(limitOptions.limit);
}
if (limitOptions && limitOptions.offset) {
promise = promise.offset(limitOptions.offset);
}
if (sortOptions && sortOptions.sortField && sortOptions.sortOrder) {
promise = promise.orderBy(sortOptions.sortField, sortOptions.sortOrder);
}
return promise;
}
}
module.exports = WalletRepository;