Skip to content

Commit 43561bf

Browse files
Merge pull request #77 from kelvinsb/feat/hide-contact-for-unauthorized
Somente exibir campo de Contato quando logado(staff ou user)
2 parents 873554c + ecdf8f4 commit 43561bf

File tree

6 files changed

+55
-19
lines changed

6 files changed

+55
-19
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
2+
3+
export const UserDecorator = createParamDecorator(
4+
(data: unknown, ctx: ExecutionContext) => {
5+
const request = ctx.switchToHttp().getRequest();
6+
return request?.user;
7+
},
8+
);

src/guards/apply-user.guard.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { AuthGuard } from '@nestjs/passport';
3+
4+
@Injectable()
5+
export class ApplyUser extends AuthGuard('jwt') {
6+
handleRequest(err: any, user: any) {
7+
if (user) return user;
8+
return null;
9+
}
10+
}

src/guards/utils.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,34 @@ async function canActivate(context: ExecutionContext, allowed: AccessLevel[]) {
1111
if (request.user) {
1212
const { userId, sessionId } = request.user;
1313

14-
const session = await service.session.findUnique({
15-
where: { id: sessionId, active: true, user: { id: userId } },
16-
include: {
17-
user: true,
18-
},
19-
});
20-
21-
if (
22-
session &&
23-
allowed.some((permission) => permission === session.user.accessLevel)
24-
) {
25-
return true;
26-
}
14+
return isRightSessionRole(allowed, sessionId, userId);
2715
}
2816

2917
return false;
3018
}
3119

20+
async function isRightSessionRole(
21+
allowed: AccessLevel[],
22+
sessionId?: string,
23+
userId?: string,
24+
) {
25+
if (!sessionId) return false;
26+
if (!userId) return false;
27+
28+
const session = await service.session.findUnique({
29+
where: { id: sessionId, active: true, user: { id: userId } },
30+
include: {
31+
user: true,
32+
},
33+
});
34+
35+
if (
36+
session &&
37+
allowed.some((permission) => permission === session.user.accessLevel)
38+
) {
39+
return true;
40+
}
41+
return false;
42+
}
43+
3244
export { canActivate };

src/shelter/shelter.controller.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { ApiTags } from '@nestjs/swagger';
1515
import { ShelterService } from './shelter.service';
1616
import { ServerResponse } from '../utils';
1717
import { StaffGuard } from '@/guards/staff.guard';
18+
import { ApplyUser } from '@/guards/apply-user.guard';
19+
import { UserDecorator } from '@/decorators/UserDecorator/user.decorator';
1820

1921
@ApiTags('Abrigos')
2022
@Controller('shelters')
@@ -35,9 +37,12 @@ export class ShelterController {
3537
}
3638

3739
@Get(':id')
38-
async show(@Param('id') id: string) {
40+
@UseGuards(ApplyUser)
41+
async show(@UserDecorator() user: any, @Param('id') id: string) {
3942
try {
40-
const data = await this.shelterService.show(id);
43+
const isLogged =
44+
Boolean(user) && Boolean(user?.sessionId) && Boolean(user?.userId);
45+
const data = await this.shelterService.show(id, isLogged);
4146
return new ServerResponse(200, 'Successfully get shelter', data);
4247
} catch (err: any) {
4348
this.logger.error(`Failed to get shelter: ${err}`);

src/shelter/shelter.service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class ShelterService {
6060
});
6161
}
6262

63-
async show(id: string) {
63+
async show(id: string, shouldShowContact: boolean) {
6464
const data = await this.prismaService.shelter.findFirst({
6565
where: {
6666
id,
@@ -72,7 +72,7 @@ export class ShelterService {
7272
pix: true,
7373
shelteredPeople: true,
7474
capacity: true,
75-
contact: true,
75+
contact: shouldShowContact,
7676
petFriendly: true,
7777
prioritySum: true,
7878
latitude: true,
@@ -137,7 +137,6 @@ export class ShelterService {
137137
pix: true,
138138
address: true,
139139
capacity: true,
140-
contact: true,
141140
petFriendly: true,
142141
shelteredPeople: true,
143142
prioritySum: true,

src/shelter/types/search.types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export interface IFilterFormProps {
1212
tags: ShelterTagInfo | null;
1313
}
1414

15-
export type SearchShelterTagResponse = Shelter & {
15+
type AllowedShelterFields = Omit<Shelter, 'contact'>;
16+
17+
export type SearchShelterTagResponse = AllowedShelterFields & {
1618
shelterSupplies: (ShelterSupply & { supply: Supply })[];
1719
};
1820

0 commit comments

Comments
 (0)