Skip to content

Commit e22a75d

Browse files
feat(DBAddOccurrence): call GeolocationService
1 parent 5db6475 commit e22a75d

File tree

24 files changed

+139
-12
lines changed

24 files changed

+139
-12
lines changed

src/data/.gitkeep

Whitespace-only changes.

src/data/implementations/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './occurrence'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { AddOccurrenceUseCase } from '@/domain/usecases';
2+
import { GeolocationService } from '@/infra/protocols';
3+
4+
export class DBAddOccurrence implements AddOccurrenceUseCase {
5+
constructor(private readonly geolocationService: GeolocationService) { }
6+
7+
async add(newOccurrence: AddOccurrenceUseCase.DTO): Promise<AddOccurrenceUseCase.Response> {
8+
const { latitude, longitude } = newOccurrence;
9+
this.geolocationService.getLocation({ latitude, longitude })
10+
return null;
11+
}
12+
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './DBAddOccurrence';

src/domain/usecases/AddOccurrenceUseCase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export interface AddOccurrenceUseCase {
88

99
export namespace AddOccurrenceUseCase {
1010
export type DTO = {
11-
latitude: string
12-
longitude: string
11+
latitude: number
12+
longitude: number
1313
denunciante: {
1414
nome: string,
1515
cpf: string,

src/infra/.gitkeep

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { AddressNotFundError } from '@/presentation/errors';
2+
import { Either } from '@/shared';
3+
4+
export interface GeolocationService {
5+
getLocation(
6+
coordinates: GeolocationService.DTO
7+
): Promise<GeolocationService.Response>;
8+
}
9+
10+
export namespace GeolocationService {
11+
export type DTO = {
12+
latitude: number;
13+
longitude: number;
14+
};
15+
16+
export type Response = Either<
17+
AddressNotFundError,
18+
{
19+
logradouro: string;
20+
bairro: string;
21+
cidade: string;
22+
estado: string;
23+
pais: string;
24+
cep: string;
25+
}
26+
>;
27+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Occurrence } from '@/domain/models'
2+
import { InternalServerError } from '@/presentation/errors'
3+
import { Either } from '@/shared'
4+
5+
export interface OccurrenceRepository {
6+
add(newOccurrence: OccurrenceRepository.DTO): Promise<OccurrenceRepository.Response>;
7+
}
8+
9+
export namespace OccurrenceRepository {
10+
export type DTO = Omit<Occurrence, 'id'>
11+
12+
export type Response = Either<InternalServerError, Occurrence>
13+
}

src/infra/protocols/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './OccurrenceRepository';
2+
export * from './GeolocationService';

src/presentation/.gitkeep

Whitespace-only changes.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { AddOccurrenceUseCase } from '@/domain/usecases';
2+
import { GeolocationService } from '@/infra/protocols';
3+
import { makeOccurrenceMock } from '@tests/domain/models/mocks';
4+
import { makeGeolocationServiceSpy } from '@tests/infra/geolocation/mocks';
5+
import { DBAddOccurrence } from '@/data/implementations';
6+
7+
type MakeSutType = {
8+
sut: DBAddOccurrence
9+
geolocationServiceSpy: GeolocationService
10+
}
11+
12+
const makeSut = (): MakeSutType => {
13+
const geolocationServiceSpy = makeGeolocationServiceSpy();
14+
const sut = new DBAddOccurrence(geolocationServiceSpy);
15+
16+
return { sut, geolocationServiceSpy };
17+
}
18+
19+
const makeSutDTO = (): AddOccurrenceUseCase.DTO => {
20+
const { denuncia, denunciante, latitude, longitude } = makeOccurrenceMock();
21+
22+
return {
23+
denuncia, denunciante, latitude, longitude,
24+
}
25+
}
26+
27+
28+
describe('Unit Test: DBAddOccurrence', () => {
29+
it('should call the GeolocationService service with the correct values', async () => {
30+
const { sut, geolocationServiceSpy } = makeSut();
31+
const spy = jest.spyOn(geolocationServiceSpy, 'getLocation');
32+
const addOccurrenceFake = makeSutDTO();
33+
34+
await sut.add(addOccurrenceFake);
35+
36+
expect(spy).toBeCalledWith({
37+
latitude: addOccurrenceFake.latitude,
38+
longitude: addOccurrenceFake.longitude,
39+
});
40+
});
41+
})

tests/data/implementations/mock/DBAddOccurrenceSpy.ts renamed to tests/data/implementations/mocks/DBAddOccurrenceSpy.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { Occurrence } from '@/domain/models';
1+
import { OccurrenceRepository } from '@/infra/protocols';
2+
23
import { AddOccurrenceUseCase } from '@/domain/usecases';
3-
import { AddressNotFundError } from '@/presentation/errors/AddressNotFundError';
4-
import { Either, right } from '@/shared';
5-
import { makeOccurrenceMock } from '@tests/domain/models/mock';
4+
import { right } from '@/shared';
5+
import { makeOccurrenceMock } from '@tests/domain/models/mocks';
66

77
class DBAddOccurrenceSpy implements AddOccurrenceUseCase {
88
async add(
99
occurrence: AddOccurrenceUseCase.DTO
10-
): Promise<Either<AddressNotFundError, Occurrence>> {
10+
): Promise<AddOccurrenceUseCase.Response> {
1111
return right(makeOccurrenceMock());
1212
}
1313
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './DBAddOccurrenceSpy'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Occurrence } from '@/domain/models';
2+
import { OccurrenceRepository } from '@/infra/protocols';
3+
import { right } from '@/shared';
4+
5+
class OccurrenceRepositorySpy implements OccurrenceRepository {
6+
value: Occurrence;
7+
8+
async add(newOccurrence: OccurrenceRepository.DTO): Promise<OccurrenceRepository.Response> {
9+
this.value = { id: 1, ...newOccurrence };
10+
return right(this.value);
11+
}
12+
13+
}
14+
15+
export const makeOccurrenceRepositorySpy = (): OccurrenceRepository => new OccurrenceRepositorySpy();

tests/infra/db/mongodb/mocks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './OccurrenceRepositorySpy'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { GeolocationService } from '@/infra/protocols'
2+
import { right } from '@/shared';
3+
import { makeOccurrenceMock } from '@tests/domain/models/mocks';
4+
5+
class GeolocationServiceSpy implements GeolocationService {
6+
async getLocation(coordinates: GeolocationService.DTO): Promise<GeolocationService.Response> {
7+
return right(makeOccurrenceMock().endereco);
8+
}
9+
}
10+
11+
export const makeGeolocationServiceSpy = (): GeolocationService => new GeolocationServiceSpy();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './GeolocationServiceSpy';

tests/presentation/controllers/occurrence/AddOccurrenceController.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { AddOccurrenceController } from '@/presentation/controllers/occurrence';
33
import { AddressNotFundError } from '@/presentation/errors/AddressNotFundError';
44
import { left } from '@/shared';
55
import { badRequest, ok, serverError } from '@/utils/http';
6-
import { makeDBAddOccurrenceSpy } from '@tests/data/implementations/mock/DBAddOccurrenceSpy';
7-
import { makeOccurrenceMock } from '@tests/domain/models/mock';
8-
import { makeHttpRequestMock } from './mock/HttpRequestMock';
6+
import { makeDBAddOccurrenceSpy } from '@tests/data/implementations/mocks';
7+
import { makeOccurrenceMock } from '@tests/domain/models/mocks';
8+
import { makeHttpRequestMock } from './mocks';
99

1010
type MakeSutType = {
1111
sut: AddOccurrenceController;

tests/presentation/controllers/occurrence/mock/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/presentation/controllers/occurrence/mock/HttpRequestMock.ts renamed to tests/presentation/controllers/occurrence/mocks/HttpRequestMock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { HttpRequest } from '@/presentation/protocols';
2-
import { makeOccurrenceMock } from '@tests/domain/models/mock';
2+
import { makeOccurrenceMock } from '@tests/domain/models/mocks';
33

44
export const makeHttpRequestMock = (): HttpRequest => {
55
const { latitude, longitude, denunciante, denuncia } = makeOccurrenceMock();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './HttpRequestMock';

0 commit comments

Comments
 (0)