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

Implementação de Flag de Autorização para Exibição de Contato e Testes TDD no ShelterService #194

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions prisma/migrations/20240813012053_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "shelters" ADD COLUMN "authorizedContact" BOOLEAN DEFAULT false;
43 changes: 22 additions & 21 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,28 @@ model Supply {
}

model Shelter {
id String @id @default(uuid())
name String @unique
pix String? @unique
address String
street String?
neighbourhood String?
city String?
streetNumber String? @map("street_number")
zipCode String? @map("zip_code")
petFriendly Boolean? @map("pet_friendly")
shelteredPeople Int? @map("sheltered_people")
capacity Int?
contact String?
prioritySum Int @default(value: 0) @map("priority_sum")
latitude Float?
longitude Float?
verified Boolean @default(value: false)
category ShelterCategory @default(value: Shelter)
actived Boolean @default(value: true)
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)
id String @id @default(uuid())
name String @unique
pix String? @unique
address String
street String?
neighbourhood String?
city String?
streetNumber String? @map("street_number")
zipCode String? @map("zip_code")
petFriendly Boolean? @map("pet_friendly")
shelteredPeople Int? @map("sheltered_people")
authorizedContact Boolean? @default(value: false)
capacity Int?
contact String?
prioritySum Int @default(value: 0) @map("priority_sum")
latitude Float?
longitude Float?
verified Boolean @default(value: false)
category ShelterCategory @default(value: Shelter)
actived Boolean @default(value: true)
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)

shelterManagers ShelterManagers[]
shelterSupplies ShelterSupply[]
Expand Down
3 changes: 3 additions & 0 deletions src/shelter/ShelterSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function parseTagResponse(
};

const parsed = results.map((result) => {
if (!result.authorizedContact) {
result.contact = 'Não autorizado informar';
}
return {
...result,
shelterSupplies: result.shelterSupplies.reduce((prev, shelterSupply) => {
Expand Down
258 changes: 257 additions & 1 deletion src/shelter/shelter.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,285 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ShelterService } from './shelter.service';
import { PrismaService } from 'src/prisma/prisma.service';
import { ShelterCategory } from '@prisma/client';

describe('ShelterService', () => {
let service: ShelterService;
let prismaService: PrismaService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ShelterService],
providers: [ShelterService, PrismaService],
})
.useMocker((token) => {
if (token === PrismaService) {
return {
supplyCategory: {
findMany: jest.fn().mockResolvedValue([]),
},
shelter: {
findFirst: jest.fn(),
findMany: jest.fn(),
count: jest.fn(),
},
};
}
})
.compile();

service = module.get<ShelterService>(ShelterService);
prismaService = module.get<PrismaService>(PrismaService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('ShelterService - show method', () => {
it('should return data with contact hidden when authorizedContact is false and shouldShowContact is false', async () => {
const mockShelterData = {
id: '1',
name: 'Test Shelter',
pix: '12345678901234567890',
address: 'Test Address',
street: 'Test Street',
neighbourhood: 'Test Neighbourhood',
city: 'Test City',
streetNumber: '123',
zipCode: '12345-678',
petFriendly: true,
shelteredPeople: 50,
authorizedContact: false,
capacity: 100,
contact: '123456789',
prioritySum: 5,
latitude: -23.55052,
longitude: -46.633308,
verified: true,
category: ShelterCategory.Shelter,
actived: true,
createdAt: '2024-08-12T12:00:00Z',
updatedAt: '2024-08-13T12:00:00Z',
shelterManagers: [],
shelterSupplies: [],
supplyHistories: [],
donationOrders: [],
shelterUsers: [],
};

jest
.spyOn(prismaService.shelter, 'findFirst')
.mockResolvedValueOnce(mockShelterData);

const result = await service.show('1', false);
expect(result).not.toBeNull();
expect(result!.contact).toBe('Não autorizado informar');
});

it('should return data with contact visible when authorizedContact is true or shouldShowContact is true', async () => {
const mockShelterData = {
id: '1',
name: 'Test Shelter',
pix: '12345678901234567890',
address: 'Test Address',
street: 'Test Street',
neighbourhood: 'Test Neighbourhood',
city: 'Test City',
streetNumber: '123',
zipCode: '12345-678',
petFriendly: true,
shelteredPeople: 50,
authorizedContact: true,
capacity: 100,
contact: '123456789',
prioritySum: 5,
latitude: -23.55052,
longitude: -46.633308,
verified: true,
category: ShelterCategory.Shelter,
actived: true,
createdAt: '2024-08-12T12:00:00Z',
updatedAt: '2024-08-13T12:00:00Z',
shelterManagers: [],
shelterSupplies: [],
supplyHistories: [],
donationOrders: [],
shelterUsers: [],
};

jest
.spyOn(prismaService.shelter, 'findFirst')
.mockResolvedValueOnce(mockShelterData);

const result = await service.show('1', true);
expect(result).not.toBeNull();
expect(result!.contact).toBe('123456789');
});

it('should return data with contact visible when shouldShowContact is true and authorizedContact is false', async () => {
const mockShelterData = {
id: '1',
name: 'Test Shelter',
pix: '12345678901234567890',
address: 'Test Address',
street: 'Test Street',
neighbourhood: 'Test Neighbourhood',
city: 'Test City',
streetNumber: '123',
zipCode: '12345-678',
petFriendly: true,
shelteredPeople: 50,
authorizedContact: false,
capacity: 100,
contact: '123456789',
prioritySum: 5,
latitude: -23.55052,
longitude: -46.633308,
verified: true,
category: ShelterCategory.Shelter,
actived: true,
createdAt: '2024-08-12T12:00:00Z',
updatedAt: '2024-08-13T12:00:00Z',
shelterManagers: [],
shelterSupplies: [],
supplyHistories: [],
donationOrders: [],
shelterUsers: [],
};

jest
.spyOn(prismaService.shelter, 'findFirst')
.mockResolvedValueOnce(mockShelterData);

const result = await service.show('1', true);
expect(result).not.toBeNull();
expect(result!.contact).toBe('123456789');
});
});

describe('ShelterService - index method', () => {
it('should return shelters with contacts hidden when authorizedContact is false', async () => {
const mockShelterData = [
{
id: '1',
name: 'Test Shelter',
pix: '12345678901234567890',
address: 'Test Address',
street: 'Test Street',
neighbourhood: 'Test Neighbourhood',
city: 'Test City',
streetNumber: '123',
zipCode: '12345-678',
petFriendly: true,
shelteredPeople: 50,
authorizedContact: false,
capacity: 100,
contact: '123456789',
prioritySum: 5,
latitude: -23.55052,
longitude: -46.633308,
verified: true,
category: ShelterCategory.Shelter,
actived: true,
createdAt: '2024-08-12T12:00:00Z',
updatedAt: '2024-08-13T12:00:00Z',
shelterManagers: [],
shelterSupplies: [],
supplyHistories: [],
donationOrders: [],
shelterUsers: [],
},
];

jest
.spyOn(prismaService.shelter, 'findMany')
.mockResolvedValueOnce(mockShelterData);
jest.spyOn(prismaService.shelter, 'count').mockResolvedValueOnce(1);

const query = {
order: 'asc',
orderBy: 'name',
page: 1,
perPage: 10,
search: '',
};

const result = await service.index(query);

result.results.forEach((shelter) => {
expect(shelter.contact).toBe('Não autorizado informar');
});
});

it('should return shelters with contacts visible when authorizedContact is true', async () => {
const mockShelterData = [
{
id: '1',
name: 'Test Shelter',
pix: '12345678901234567890',
address: 'Test Address',
street: 'Test Street',
neighbourhood: 'Test Neighbourhood',
city: 'Test City',
streetNumber: '123',
zipCode: '12345-678',
petFriendly: true,
shelteredPeople: 50,
authorizedContact: true,
capacity: 100,
contact: '123456789',
prioritySum: 5,
latitude: -23.55052,
longitude: -46.633308,
verified: true,
category: ShelterCategory.Shelter,
actived: true,
createdAt: '2024-08-12T12:00:00Z',
updatedAt: '2024-08-13T12:00:00Z',
shelterManagers: [],
shelterSupplies: [],
supplyHistories: [],
donationOrders: [],
shelterUsers: [],
},
];

jest
.spyOn(prismaService.shelter, 'findMany')
.mockResolvedValueOnce(mockShelterData);
jest.spyOn(prismaService.shelter, 'count').mockResolvedValueOnce(1);

const query = {
order: 'asc',
orderBy: 'name',
page: 1,
perPage: 10,
search: '',
};

const result = await service.index(query);

result.results.forEach((shelter) => {
expect(shelter.contact).toBe('123456789');
});
});

it('should return an empty list when no shelters match the query', async () => {
jest.spyOn(prismaService.shelter, 'findMany').mockResolvedValueOnce([]);
jest.spyOn(prismaService.shelter, 'count').mockResolvedValueOnce(0);

const query = {
order: 'asc',
orderBy: 'name',
page: 1,
perPage: 10,
search: '',
};

const result = await service.index(query);

expect(result.results).toEqual([]);
expect(result.count).toBe(0);
});
});
});
16 changes: 13 additions & 3 deletions src/shelter/shelter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ export class ShelterService implements OnModuleInit {
pix: true,
shelteredPeople: true,
capacity: true,
contact: shouldShowContact,
contact: true,
petFriendly: true,
prioritySum: true,
latitude: true,
longitude: true,
verified: true,
actived: true,
authorizedContact: true,
category: true,
shelterSupplies: {
select: {
Expand Down Expand Up @@ -122,7 +123,15 @@ export class ShelterService implements OnModuleInit {
},
});

if (data) this.decayShelterSupply(data.shelterSupplies);
if (data) {
this.decayShelterSupply(data.shelterSupplies);

if (!shouldShowContact && !data.authorizedContact) {
data.contact = 'Não autorizado informar';
} else if (shouldShowContact && !data.authorizedContact) {
return data;
}
}

return data;
}
Expand Down Expand Up @@ -150,7 +159,6 @@ export class ShelterService implements OnModuleInit {
orderBy: { [orderBy]: order },
where,
};

const results = await this.prismaService.shelter.findMany({
...whereData,
select: {
Expand All @@ -174,6 +182,8 @@ export class ShelterService implements OnModuleInit {
category: true,
createdAt: true,
updatedAt: true,
contact: true,
authorizedContact: true,
shelterSupplies: {
where: {
priority: {
Expand Down
Loading