-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathpeer.ts
133 lines (127 loc) · 3.65 KB
/
peer.ts
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { assetToGraphql } from './asset'
import {
QueryResolvers,
ResolversTypes,
Peer as SchemaPeer,
MutationResolvers
} from '../generated/graphql'
import { Peer } from '../../payment-method/ilp/peer/model'
import {
isPeerError,
errorToCode,
errorToMessage,
PeerError
} from '../../payment-method/ilp/peer/errors'
import { ApolloContext } from '../../app'
import { getPageInfo } from '../../shared/pagination'
import { Pagination, SortOrder } from '../../shared/baseModel'
import { GraphQLError } from 'graphql'
export const getPeers: QueryResolvers<ApolloContext>['peers'] = async (
parent,
args,
ctx
): Promise<ResolversTypes['PeersConnection']> => {
const peerService = await ctx.container.use('peerService')
const { sortOrder, ...pagination } = args
const order = sortOrder === 'ASC' ? SortOrder.Asc : SortOrder.Desc
const peers = await peerService.getPage(pagination, order)
const pageInfo = await getPageInfo({
getPage: (pagination: Pagination, sortOrder?: SortOrder) =>
peerService.getPage(pagination, sortOrder),
page: peers,
sortOrder: order
})
return {
pageInfo,
edges: peers.map((peer: Peer) => ({
cursor: peer.id,
node: peerToGraphql(peer)
}))
}
}
export const getPeer: QueryResolvers<ApolloContext>['peer'] = async (
parent,
args,
ctx
): Promise<ResolversTypes['Peer']> => {
const peerService = await ctx.container.use('peerService')
const peer = await peerService.get(args.id)
if (!peer) {
throw new GraphQLError(errorToMessage[PeerError.UnknownPeer], {
extensions: {
code: errorToCode[PeerError.UnknownPeer]
}
})
}
return peerToGraphql(peer)
}
export const createPeer: MutationResolvers<ApolloContext>['createPeer'] =
async (
parent,
args,
ctx
): Promise<ResolversTypes['CreatePeerMutationResponse']> => {
const peerService = await ctx.container.use('peerService')
const peerOrError = await peerService.create(args.input)
if (isPeerError(peerOrError)) {
throw new GraphQLError(errorToMessage[peerOrError], {
extensions: {
code: errorToCode[peerOrError]
}
})
}
return {
peer: peerToGraphql(peerOrError)
}
}
export const updatePeer: MutationResolvers<ApolloContext>['updatePeer'] =
async (
parent,
args,
ctx
): Promise<ResolversTypes['UpdatePeerMutationResponse']> => {
const peerService = await ctx.container.use('peerService')
const peerOrError = await peerService.update(args.input)
if (isPeerError(peerOrError)) {
throw new GraphQLError(errorToMessage[peerOrError], {
extensions: {
code: errorToCode[peerOrError]
}
})
}
return {
peer: peerToGraphql(peerOrError)
}
}
export const deletePeer: MutationResolvers<ApolloContext>['deletePeer'] =
async (
_,
args,
ctx
): Promise<ResolversTypes['DeletePeerMutationResponse']> => {
const peerService = await ctx.container.use('peerService')
const peer = await peerService.delete(args.input.id)
if (!peer) {
throw new GraphQLError(errorToMessage[PeerError.UnknownPeer], {
extensions: {
code: errorToCode[PeerError.UnknownPeer]
}
})
}
return {
success: true
}
}
export const peerToGraphql = (peer: Peer): SchemaPeer => ({
id: peer.id,
maxPacketAmount: peer.maxPacketAmount,
http: peer.http,
incomingTokens: peer.incomingTokens?.map(
(incomingToken) => incomingToken.token
),
asset: assetToGraphql(peer.asset),
staticIlpAddress: peer.staticIlpAddress,
name: peer.name,
liquidityThreshold: peer.liquidityThreshold,
createdAt: new Date(+peer.createdAt).toISOString()
})