diff --git a/package.json b/package.json index d9801d5..0d8ec72 100644 --- a/package.json +++ b/package.json @@ -93,7 +93,8 @@ ".presenter.ts", ".profile.ts", ".exception.ts", - "main.ts" + "main.ts", + "prisma-service.ts" ], "rootDir": "src", "testRegex": ".*\\.spec\\.ts$", @@ -110,7 +111,8 @@ ".presenter.ts", ".profile.ts", ".exception.ts", - "main.ts" + "main.ts", + "prisma-service.ts" ], "coverageDirectory": "../coverage", "testEnvironment": "node" diff --git a/src/modules/database/src/adapters/secondaries/prisma-repository.abstract.ts b/src/modules/database/src/adapters/secondaries/prisma-repository.abstract.ts index 2827de9..fa2ba59 100644 --- a/src/modules/database/src/adapters/secondaries/prisma-repository.abstract.ts +++ b/src/modules/database/src/adapters/secondaries/prisma-repository.abstract.ts @@ -1,5 +1,5 @@ import { Injectable } from '@nestjs/common'; -import { PrismaClientKnownRequestError } from '@prisma/client/runtime'; +import { Prisma } from '@prisma/client'; import { DatabaseException } from '../../exceptions/database.exception'; import { ICollection } from '../../interfaces/collection.interface'; import { IRepository } from '../../interfaces/repository.interface'; @@ -45,9 +45,9 @@ export abstract class PrismaRepository implements IRepository { return entity; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -66,8 +66,11 @@ export abstract class PrismaRepository implements IRepository { return entity; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { - throw new DatabaseException(PrismaClientKnownRequestError.name, e.code); + if (e instanceof Prisma.PrismaClientKnownRequestError) { + throw new DatabaseException( + Prisma.PrismaClientKnownRequestError.name, + e.code, + ); } else { throw new DatabaseException(); } @@ -85,9 +88,9 @@ export abstract class PrismaRepository implements IRepository { return res; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -105,9 +108,9 @@ export abstract class PrismaRepository implements IRepository { }); return updatedEntity; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -131,9 +134,9 @@ export abstract class PrismaRepository implements IRepository { return updatedEntity; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -151,9 +154,9 @@ export abstract class PrismaRepository implements IRepository { return entity; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -171,9 +174,9 @@ export abstract class PrismaRepository implements IRepository { return entity; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -204,9 +207,9 @@ export abstract class PrismaRepository implements IRepository { )}) VALUES (${Object.values(fields).join(',')})`; return await this._prisma.$executeRawUnsafe(command); } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -225,9 +228,9 @@ export abstract class PrismaRepository implements IRepository { )} WHERE uuid = '${uuid}'`; return await this._prisma.$executeRawUnsafe(command); } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -242,9 +245,9 @@ export abstract class PrismaRepository implements IRepository { await this._prisma.$queryRaw`SELECT 1`; return true; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); diff --git a/src/modules/database/tests/unit/prisma-repository.spec.ts b/src/modules/database/tests/unit/prisma-repository.spec.ts index bc194c4..1b0e1f7 100644 --- a/src/modules/database/tests/unit/prisma-repository.spec.ts +++ b/src/modules/database/tests/unit/prisma-repository.spec.ts @@ -3,7 +3,7 @@ import { Test, TestingModule } from '@nestjs/testing'; import { PrismaService } from '../../src/adapters/secondaries/prisma-service'; import { PrismaRepository } from '../../src/adapters/secondaries/prisma-repository.abstract'; import { DatabaseException } from '../../src/exceptions/database.exception'; -import { PrismaClientKnownRequestError } from '@prisma/client/runtime'; +import { Prisma } from '@prisma/client'; class FakeEntity { uuid?: string; @@ -66,7 +66,7 @@ const mockPrismaService = { .mockResolvedValueOnce(fakeEntityCreated) // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((fields: object) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -78,7 +78,7 @@ const mockPrismaService = { .mockResolvedValueOnce(fakeEntityCreated) // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((fields: object) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -90,7 +90,7 @@ const mockPrismaService = { $queryRaw: jest .fn() .mockImplementationOnce(() => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -99,7 +99,7 @@ const mockPrismaService = { return true; }) .mockImplementation(() => { - throw new PrismaClientKnownRequestError('Database unavailable', { + throw new Prisma.PrismaClientKnownRequestError('Database unavailable', { code: 'code', clientVersion: 'version', }); @@ -110,7 +110,7 @@ const mockPrismaService = { .mockResolvedValueOnce(fakeEntityCreated) // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((params?: any) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -139,7 +139,7 @@ const mockPrismaService = { } if (!entity && params?.where?.uuid == 'unknown') { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -161,7 +161,7 @@ const mockPrismaService = { }) // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((params?: any) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -175,14 +175,14 @@ const mockPrismaService = { .fn() // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((params?: any) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); }) // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((params?: any) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -212,7 +212,7 @@ const mockPrismaService = { .fn() // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((params?: any) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -236,7 +236,7 @@ const mockPrismaService = { .fn() // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((params?: any) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); diff --git a/src/modules/matcher/adapters/secondaries/georouter-creator.ts b/src/modules/matcher/adapters/secondaries/georouter-creator.ts new file mode 100644 index 0000000..b82b938 --- /dev/null +++ b/src/modules/matcher/adapters/secondaries/georouter-creator.ts @@ -0,0 +1,13 @@ +import { Georouter } from '../../domain/interfaces/georouter.interface'; +import { GraphhopperGeorouter } from './graphhopper-georouter'; + +export class GeorouterCreator { + create(type: string, url: string): Georouter { + switch (type) { + case 'graphhopper': + return new GraphhopperGeorouter(url); + default: + throw new Error('Unknown geocoder'); + } + } +} diff --git a/src/modules/matcher/adapters/secondaries/graphhopper-georouter.ts b/src/modules/matcher/adapters/secondaries/graphhopper-georouter.ts new file mode 100644 index 0000000..06df223 --- /dev/null +++ b/src/modules/matcher/adapters/secondaries/graphhopper-georouter.ts @@ -0,0 +1,19 @@ +import { Route } from '../../domain/entities/route'; +import { Georouter } from '../../domain/interfaces/georouter.interface'; + +export class GraphhopperGeorouter implements Georouter { + _url: string; + + constructor(url: string) { + this._url = url + '/route?'; + } + + route( + routesRequested: [], + withPoints: boolean, + withTime: boolean, + withDistance: boolean, + ): Route[] { + throw new Error('Method not implemented.'); + } +} diff --git a/src/modules/matcher/domain/interfaces/georouter.interface.ts b/src/modules/matcher/domain/interfaces/georouter.interface.ts index e28617d..c3046e3 100644 --- a/src/modules/matcher/domain/interfaces/georouter.interface.ts +++ b/src/modules/matcher/domain/interfaces/georouter.interface.ts @@ -1,3 +1,10 @@ +import { Route } from '../entities/route'; + export interface Georouter { - type: string; + route( + routesRequested: [], + withPoints: boolean, + withTime: boolean, + withDistance: boolean, + ): Array; } diff --git a/src/modules/matcher/tests/unit/georouter-creator.spec.ts b/src/modules/matcher/tests/unit/georouter-creator.spec.ts new file mode 100644 index 0000000..cea0732 --- /dev/null +++ b/src/modules/matcher/tests/unit/georouter-creator.spec.ts @@ -0,0 +1,23 @@ +import { GeorouterCreator } from '../../adapters/secondaries/georouter-creator'; +import { GraphhopperGeorouter } from '../../adapters/secondaries/graphhopper-georouter'; + +describe('Georouter creator', () => { + it('should be defined', () => { + const georouterCreator: GeorouterCreator = new GeorouterCreator(); + expect(georouterCreator).toBeDefined(); + }); + it('should create a graphhopper georouter', () => { + const georouterCreator: GeorouterCreator = new GeorouterCreator(); + const georouter = georouterCreator.create( + 'graphhopper', + 'http://localhost', + ); + expect(georouter).toBeInstanceOf(GraphhopperGeorouter); + }); + it('should throw an exception if georouter type is unknown', () => { + const georouterCreator: GeorouterCreator = new GeorouterCreator(); + expect(() => + georouterCreator.create('unknown', 'http://localhost'), + ).toThrow(); + }); +}); diff --git a/src/modules/matcher/tests/unit/graphhopper-georouter.spec.ts b/src/modules/matcher/tests/unit/graphhopper-georouter.spec.ts new file mode 100644 index 0000000..40e3ec6 --- /dev/null +++ b/src/modules/matcher/tests/unit/graphhopper-georouter.spec.ts @@ -0,0 +1,16 @@ +import { GraphhopperGeorouter } from '../../adapters/secondaries/graphhopper-georouter'; + +describe('Graphhopper Georouter', () => { + it('should be defined', () => { + const graphhopperGeorouter: GraphhopperGeorouter = new GraphhopperGeorouter( + 'http://localhost', + ); + expect(graphhopperGeorouter).toBeDefined(); + }); + it('should throw an exception when calling route', () => { + const graphhopperGeorouter: GraphhopperGeorouter = new GraphhopperGeorouter( + 'http://localhost', + ); + expect(() => graphhopperGeorouter.route([], false, false, false)).toThrow(); + }); +});