Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add routes endpoint #39

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ type Fee @entity {
type Route @entity @index(fields: ["fromDomainID", "toDomainID", "resourceID"], unique: true) {
id: ID!
fromDomainID: String
fromDomain: Domain
fromDomain: Domain!

toDomainID: String
toDomain: Domain
toDomain: Domain!

resourceID: String
resource: Resource
resource: Resource!
transfers: [Transfer!] @derivedFrom(field: "route")
}
30 changes: 30 additions & 0 deletions src/api/controllers/RoutesController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
The Licensed Work is (c) 2024 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import type { FastifyReply, FastifyRequest } from "fastify";
import type { DataSource } from "typeorm";

import { logger } from "../../utils/logger";
import { RoutesService } from "../services/dataAccess/routes.service";

export class RoutesController {
private routesService: RoutesService;

constructor(dataSource: DataSource) {
this.routesService = new RoutesService(dataSource);
}

public async getRoutes(
request: FastifyRequest,
reply: FastifyReply,
): Promise<void> {
try {
const routesResult = await this.routesService.findRoutes({});
await reply.status(200).send(routesResult);
} catch (error) {
logger.error("Error occurred when fetching transfers", error);
await reply.status(500).send({ error: "Internal server error" });
}
}
}
2 changes: 2 additions & 0 deletions src/api/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ SPDX-License-Identifier: LGPL-3.0-only
*/
import type { FastifyInstance } from "fastify";

import { routeEntityRoutes } from "./routeEntity.routes";
import { transferRoutes } from "./transfers.routes";

export async function registerRoutes(server: FastifyInstance): Promise<void> {
await server.register(transferRoutes, { prefix: "/api" });
await server.register(routeEntityRoutes, { prefix: "/api" });
}
20 changes: 20 additions & 0 deletions src/api/routes/routeEntity.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
The Licensed Work is (c) 2024 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import type { FastifyInstance } from "fastify";

import { RoutesController } from "../controllers/RoutesController";
import { routesSchema } from "../schemas/routes.schema";

export async function routeEntityRoutes(
server: FastifyInstance,
): Promise<void> {
const routesController = new RoutesController(server.db);
server.get(
"/routes",
{ schema: routesSchema },
routesController.getRoutes.bind(routesController),
);
return Promise.resolve();
}
24 changes: 24 additions & 0 deletions src/api/schemas/routes.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
The Licensed Work is (c) 2024 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import { routeSchema } from ".";

export const routesSchema = {
summary: "Get routes",
response: {
200: {
description: "List of routes",
content: {
"application/json": {
schema: {
type: "array",
items: {
...routeSchema,
},
},
},
},
},
},
};
32 changes: 32 additions & 0 deletions src/api/services/dataAccess/routes.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
The Licensed Work is (c) 2024 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import type { DataSource, FindOptionsWhere, Repository } from "typeorm";

import { Route } from "../../../model";

export class RoutesService {
private routeRepository: Repository<Route>;
constructor(dataSource: DataSource) {
this.routeRepository = dataSource.getRepository(Route);
}

public async findRoutes(where: FindOptionsWhere<Route>): Promise<Route[]> {
const routes = await this.routeRepository.find({
where,
order: {
fromDomainID: "ASC",
toDomainID: "ASC",
resourceID: "ASC",
},
relations: {
fromDomain: true,
toDomain: true,
resource: true,
},
});

return routes;
}
}
6 changes: 3 additions & 3 deletions src/model/generated/route.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ export class Route {

@Index_()
@ManyToOne_(() => Domain, {nullable: true})
fromDomain!: Domain | undefined | null
fromDomain!: Domain

@StringColumn_({nullable: true})
toDomainID!: string | undefined | null

@Index_()
@ManyToOne_(() => Domain, {nullable: true})
toDomain!: Domain | undefined | null
toDomain!: Domain

@StringColumn_({nullable: true})
resourceID!: string | undefined | null

@Index_()
@ManyToOne_(() => Resource, {nullable: true})
resource!: Resource | undefined | null
resource!: Resource

@OneToMany_(() => Transfer, e => e.route)
transfers!: Transfer[]
Expand Down
Loading