-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtransfers.service.ts
More file actions
178 lines (148 loc) · 4.68 KB
/
Copy pathtransfers.service.ts
File metadata and controls
178 lines (148 loc) · 4.68 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
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
/*
The Licensed Work is (c) 2023 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import { Prisma, PrismaClient, Transfer, TransferStatus } from "@prisma/client"
import { NotFound, getTransferQueryParams } from "../utils/helpers"
export type Pagination = {
page: number
limit: number
}
export enum DomainType {
Source = "source",
Destination = "destination",
}
class TransfersService {
public transfers = new PrismaClient().transfer
public deposit = new PrismaClient().deposit
private calculatePaginationParams(paginationParams: Pagination): {
skip: number
take: number
} {
const pageSize = paginationParams.limit
const pageIndex = paginationParams.page
const skip = (pageIndex - 1) * pageSize
const take = pageSize
return {
skip,
take,
}
}
public async findTransfers(where: Prisma.TransferWhereInput, paginationParams: Pagination): Promise<Transfer[]> {
const { skip, take } = this.calculatePaginationParams(paginationParams)
const transfers = await this.transfers.findMany({
where,
take,
skip,
orderBy: {
deposit: {
timestamp: "desc",
},
},
include: {
...getTransferQueryParams().include,
},
})
return transfers
}
public async findAllTransfers(status: TransferStatus | undefined, paginationParams: Pagination): Promise<Transfer[]> {
const where: Prisma.TransferWhereInput = {
status: status,
}
const transfers = this.findTransfers(where, paginationParams)
return transfers
}
public async findTransferById(id: string): Promise<Transfer> {
const transfer = await this.transfers.findUnique({
where: { id },
include: {
...getTransferQueryParams().include,
},
})
if (!transfer) throw new NotFound("Transfer not found")
return transfer as Transfer
}
public async findTransfersByTxHash(txHash: string, domainID: number): Promise<Transfer[]> {
let where: Prisma.DepositWhereInput
domainID == undefined
? (where = { txHash: txHash })
: (where = {
txHash: txHash,
transfer: {
fromDomainId: domainID,
},
})
const deposit = await this.deposit.findMany({
where,
include: { transfer: { include: { ...getTransferQueryParams().include } } },
})
if (!deposit) throw new NotFound("Transfer not found")
const transfers = deposit.map(dep => dep.transfer)
return transfers
}
public async findTransfersByAccountAddress(sender: string, status: TransferStatus | undefined, paginationParams: Pagination): Promise<Transfer[]> {
const where: Prisma.TransferWhereInput = {
accountId: sender,
status: status,
}
const transfers = this.findTransfers(where, paginationParams)
return transfers
}
public async findTransfersByResourceID(resourceID: string, status: TransferStatus | undefined, paginationParams: Pagination): Promise<Transfer[]> {
const where: Prisma.TransferWhereInput = {
resourceID: resourceID,
status: status,
}
const transfers = this.findTransfers(where, paginationParams)
return transfers
}
public async findTransfersBySourceDomainToDestinationDomain(
sourceDomainID: number,
destinationDomainID: number,
paginationParams: Pagination,
): Promise<Transfer[]> {
const where: Prisma.TransferWhereInput = {
fromDomainId: sourceDomainID,
toDomainId: destinationDomainID,
}
const transfers = this.findTransfers(where, paginationParams)
return transfers
}
public async findTransfersByResourceBetweenDomains(
resourceID: string,
sourceDomainID: number,
destinationDomainID: number,
paginationParams: Pagination,
): Promise<Transfer[]> {
const where: Prisma.TransferWhereInput = {
resourceID: resourceID,
fromDomainId: sourceDomainID,
toDomainId: destinationDomainID,
}
const transfers = this.findTransfers(where, paginationParams)
return transfers
}
public async findTransfersByDomain(
domainID: number,
domain: DomainType,
status: TransferStatus | undefined,
paginationParams: Pagination,
): Promise<Transfer[]> {
let where: Prisma.TransferWhereInput
if (domain == DomainType.Source) {
where = { fromDomainId: domainID, status: status }
} else if (domain == DomainType.Destination) {
where = { toDomainId: domainID, status: status }
} else {
where = {
OR: [
{ fromDomainId: domainID, status: status },
{ toDomainId: domainID, status: status },
],
}
}
const transfers = this.findTransfers(where, paginationParams)
return transfers
}
}
export default TransfersService