-
Notifications
You must be signed in to change notification settings - Fork 89
/
incoming_payment.ts
197 lines (183 loc) · 5.84 KB
/
incoming_payment.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import {
ResolversTypes,
WalletAddressResolvers,
MutationResolvers,
IncomingPayment as SchemaIncomingPayment,
QueryResolvers
} from '../generated/graphql'
import { IncomingPayment } from '../../open_payments/payment/incoming/model'
import {
isIncomingPaymentError,
errorToCode,
errorToMessage
} from '../../open_payments/payment/incoming/errors'
import { ApolloContext } from '../../app'
import { getPageInfo } from '../../shared/pagination'
import { Pagination, SortOrder } from '../../shared/baseModel'
import { GraphQLError } from 'graphql'
import { GraphQLErrorCode } from '../errors'
export const getIncomingPayment: QueryResolvers<ApolloContext>['incomingPayment'] =
async (parent, args, ctx): Promise<ResolversTypes['IncomingPayment']> => {
const incomingPaymentService = await ctx.container.use(
'incomingPaymentService'
)
const payment = await incomingPaymentService.get({
id: args.id
})
if (!payment) {
throw new GraphQLError('payment does not exist', {
extensions: {
code: GraphQLErrorCode.NotFound
}
})
}
return paymentToGraphql(payment)
}
export const getWalletAddressIncomingPayments: WalletAddressResolvers<ApolloContext>['incomingPayments'] =
async (
parent,
args,
ctx
): Promise<ResolversTypes['IncomingPaymentConnection']> => {
if (!parent.id) {
throw new GraphQLError('missing wallet address id', {
extensions: {
code: GraphQLErrorCode.BadUserInput
}
})
}
const incomingPaymentService = await ctx.container.use(
'incomingPaymentService'
)
const { sortOrder, ...pagination } = args
const order = sortOrder === 'ASC' ? SortOrder.Asc : SortOrder.Desc
const incomingPayments = await incomingPaymentService.getWalletAddressPage({
walletAddressId: parent.id,
pagination,
sortOrder: order
})
const pageInfo = await getPageInfo({
getPage: (pagination: Pagination, sortOrder?: SortOrder) =>
incomingPaymentService.getWalletAddressPage({
walletAddressId: parent.id as string,
pagination,
sortOrder
}),
page: incomingPayments,
sortOrder: order
})
return {
pageInfo,
edges: incomingPayments.map((incomingPayment: IncomingPayment) => {
return {
cursor: incomingPayment.id,
node: paymentToGraphql(incomingPayment)
}
})
}
}
export const createIncomingPayment: MutationResolvers<ApolloContext>['createIncomingPayment'] =
async (
parent,
args,
ctx
): Promise<ResolversTypes['IncomingPaymentResponse']> => {
const incomingPaymentService = await ctx.container.use(
'incomingPaymentService'
)
const incomingPaymentOrError = await incomingPaymentService.create({
walletAddressId: args.input.walletAddressId,
expiresAt: !args.input.expiresAt
? undefined
: new Date(args.input.expiresAt),
incomingAmount: args.input.incomingAmount,
metadata: args.input.metadata,
tenantId: args.input.tenantId
})
if (isIncomingPaymentError(incomingPaymentOrError)) {
throw new GraphQLError(errorToMessage[incomingPaymentOrError], {
extensions: {
code: errorToCode[incomingPaymentOrError]
}
})
} else
return {
payment: paymentToGraphql(incomingPaymentOrError)
}
}
export const approveIncomingPayment: MutationResolvers<ApolloContext>['approveIncomingPayment'] =
async (
parent,
args,
ctx
): Promise<ResolversTypes['ApproveIncomingPaymentResponse']> => {
const incomingPaymentService = await ctx.container.use(
'incomingPaymentService'
)
const incomingPaymentOrError = await incomingPaymentService.approve(
args.input.id
)
if (isIncomingPaymentError(incomingPaymentOrError)) {
throw new GraphQLError(errorToMessage[incomingPaymentOrError], {
extensions: {
code: errorToCode[incomingPaymentOrError]
}
})
}
return {
payment: paymentToGraphql(incomingPaymentOrError)
}
}
export const cancelIncomingPayment: MutationResolvers<ApolloContext>['cancelIncomingPayment'] =
async (
parent,
args,
ctx
): Promise<ResolversTypes['CancelIncomingPaymentResponse']> => {
const incomingPaymentService = await ctx.container.use(
'incomingPaymentService'
)
// ACCESS CONTROL CASE: Update/Deletes. Check existing resource's tenantId before mutating.
// If from operator use tenantId given on input, otherwise use the requestor's tenant's id.
// In other words, dont use the operator's tenantId
const tenantId = ctx.isOperator ? args.input.tenantId : ctx.tenantId
const incomingPayment = await incomingPaymentService.get({
id: args.input.id
})
if (!incomingPayment || tenantId !== incomingPayment.tenantId) {
throw new GraphQLError('Unknown incoming payment', {
extensions: {
code: GraphQLErrorCode.NotFound,
id: args.input.id
}
})
}
const incomingPaymentOrError = await incomingPaymentService.cancel(
args.input.id
)
if (isIncomingPaymentError(incomingPaymentOrError)) {
throw new GraphQLError(errorToMessage[incomingPaymentOrError], {
extensions: {
code: errorToCode[incomingPaymentOrError]
}
})
}
return {
payment: paymentToGraphql(incomingPaymentOrError)
}
}
export function paymentToGraphql(
payment: IncomingPayment
): SchemaIncomingPayment {
return {
id: payment.id,
walletAddressId: payment.walletAddressId,
client: payment.client,
state: payment.state,
expiresAt: payment.expiresAt.toISOString(),
incomingAmount: payment.incomingAmount,
receivedAmount: payment.receivedAmount,
metadata: payment.metadata,
createdAt: new Date(+payment.createdAt).toISOString()
}
}