Skip to content

Commit

Permalink
chore: Add transfers by domain route
Browse files Browse the repository at this point in the history
  • Loading branch information
mj52951 committed Sep 10, 2024
1 parent 5ef7c52 commit 233a204
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/controllers/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,16 @@ export const transfersByResourceBetweenDomainsSchema = {
}

export const transfersByDomainSchema = {
summary: "Get all transfers with a specific domain as source or destination",
summary:
"Get all transfers with a specific domain as source or destination. Possible to get transfers by domain only as source and only as destination.",
querystring: {
type: "object",
properties: {
...paginationSchema,
domain: {
type: "string",
default: "source",
default: undefined,
nullable: true,
enum: ["source", "destination"],
},
},
Expand Down
34 changes: 22 additions & 12 deletions src/services/transfers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
The Licensed Work is (c) 2023 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import { PrismaClient, Transfer, TransferStatus } from "@prisma/client"
import { Prisma, PrismaClient, Transfer, TransferStatus } from "@prisma/client"
import { NotFound, getTransferQueryParams } from "../utils/helpers"

export type Pagination = {
Expand Down Expand Up @@ -34,7 +34,7 @@ class TransfersService {
}
}

public async findTransfers(where: Partial<Transfer>, paginationParams: Pagination): Promise<Transfer[]> {
public async findTransfers(where: Prisma.TransferWhereInput, paginationParams: Pagination): Promise<Transfer[]> {
const { skip, take } = this.calculatePaginationParams(paginationParams)
const transfers = await this.transfers.findMany({
where,
Expand All @@ -54,7 +54,7 @@ class TransfersService {
}

public async findAllTransfers(status: TransferStatus | undefined, paginationParams: Pagination): Promise<Transfer[]> {
const where: Partial<Transfer> = {
const where: Prisma.TransferWhereInput = {
status: status,
}

Expand All @@ -76,7 +76,7 @@ class TransfersService {
}

public async findTransfersByTxHash(txHash: string, domainID: number): Promise<Transfer[]> {
let where: Partial<any>
let where: Prisma.DepositWhereInput
domainID == undefined
? (where = { txHash: txHash })
: (where = {
Expand All @@ -97,7 +97,7 @@ class TransfersService {
}

public async findTransfersByAccountAddress(sender: string, status: TransferStatus | undefined, paginationParams: Pagination): Promise<Transfer[]> {
const where: Partial<Transfer> = {
const where: Prisma.TransferWhereInput = {
accountId: sender,
status: status,
}
Expand All @@ -108,7 +108,7 @@ class TransfersService {
}

public async findTransfersByResourceID(resourceID: string, status: TransferStatus | undefined, paginationParams: Pagination): Promise<Transfer[]> {
const where: Partial<Transfer> = {
const where: Prisma.TransferWhereInput = {
resourceID: resourceID,
status: status,
}
Expand All @@ -123,7 +123,7 @@ class TransfersService {
destinationDomainID: number,
paginationParams: Pagination,
): Promise<Transfer[]> {
const where: Partial<Transfer> = {
const where: Prisma.TransferWhereInput = {
fromDomainId: sourceDomainID,
toDomainId: destinationDomainID,
}
Expand All @@ -139,7 +139,7 @@ class TransfersService {
destinationDomainID: number,
paginationParams: Pagination,
): Promise<Transfer[]> {
const where: Partial<Transfer> = {
const where: Prisma.TransferWhereInput = {
resourceID: resourceID,
fromDomainId: sourceDomainID,
toDomainId: destinationDomainID,
Expand All @@ -156,10 +156,20 @@ class TransfersService {
status: TransferStatus | undefined,
paginationParams: Pagination,
): Promise<Transfer[]> {
let where: Partial<Transfer>

domain == DomainType.Source ? (where = { fromDomainId: domainID, status: status }) : (where = { toDomainId: domainID, status: status })

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
Expand Down

0 comments on commit 233a204

Please sign in to comment.