Skip to content

Commit cd27183

Browse files
authored
Fix/develop bugs (#54)
* feat: update many supplies * feat: created admin user role
1 parent 794c7b8 commit cd27183

File tree

15 files changed

+139
-32
lines changed

15 files changed

+139
-32
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterEnum
2+
ALTER TYPE "AccessLevel" ADD VALUE 'Admin';

prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enum AccessLevel {
1111
User
1212
Staff
1313
DistributionCenter
14+
Admin
1415
}
1516

1617
model User {

src/guards/admin.guard.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ExecutionContext, HttpException, Injectable } from '@nestjs/common';
2+
import { AuthGuard } from '@nestjs/passport';
3+
import { AccessLevel } from '@prisma/client';
4+
5+
import { canActivate } from './utils';
6+
7+
@Injectable()
8+
export class AdminGuard extends AuthGuard('jwt') {
9+
constructor() {
10+
super();
11+
}
12+
13+
async canActivate(context: ExecutionContext): Promise<boolean> {
14+
await super.canActivate(context);
15+
const ok = await canActivate(context, [AccessLevel.Admin]);
16+
if (ok) return true;
17+
18+
throw new HttpException('Acesso não autorizado', 401);
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ExecutionContext, HttpException, Injectable } from '@nestjs/common';
2+
import { AuthGuard } from '@nestjs/passport';
3+
import { AccessLevel } from '@prisma/client';
4+
5+
import { canActivate } from './utils';
6+
7+
@Injectable()
8+
export class DistributionCenterGuard extends AuthGuard('jwt') {
9+
constructor() {
10+
super();
11+
}
12+
13+
async canActivate(context: ExecutionContext): Promise<boolean> {
14+
await super.canActivate(context);
15+
const ok = await canActivate(context, [
16+
AccessLevel.Admin,
17+
AccessLevel.DistributionCenter,
18+
]);
19+
if (ok) return true;
20+
21+
throw new HttpException('Acesso não autorizado', 401);
22+
}
23+
}

src/guards/staff.guard.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ export class StaffGuard extends AuthGuard('jwt') {
1212

1313
async canActivate(context: ExecutionContext): Promise<boolean> {
1414
await super.canActivate(context);
15-
const ok = await canActivate(context, [AccessLevel.Staff]);
15+
const ok = await canActivate(context, [
16+
AccessLevel.Admin,
17+
AccessLevel.Staff,
18+
]);
1619
if (ok) return true;
1720

1821
throw new HttpException('Acesso não autorizado', 401);

src/guards/user.guard.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ export class UserGuard extends AuthGuard('jwt') {
1313
async canActivate(context: ExecutionContext): Promise<boolean> {
1414
await super.canActivate(context);
1515
const ok = await canActivate(context, [
16-
AccessLevel.Staff,
1716
AccessLevel.User,
17+
AccessLevel.Staff,
18+
AccessLevel.DistributionCenter,
19+
AccessLevel.Admin,
1820
]);
1921
if (ok) return true;
2022
throw new HttpException('Acesso não autorizado', 401);

src/shelter-managers/shelter-managers.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ApiTags } from '@nestjs/swagger';
1313

1414
import { ShelterManagersService } from './shelter-managers.service';
1515
import { ServerResponse } from '../utils';
16-
import { StaffGuard } from '@/guards/staff.guard';
16+
import { AdminGuard } from '@/guards/admin.guard';
1717

1818
@ApiTags('Admin de Abrigo')
1919
@Controller('shelter/managers')
@@ -25,7 +25,7 @@ export class ShelterManagersController {
2525
) {}
2626

2727
@Post('')
28-
@UseGuards(StaffGuard)
28+
@UseGuards(AdminGuard)
2929
async store(@Body() body) {
3030
try {
3131
await this.shelterManagerServices.store(body);

src/shelter-supply/shelter-supply.controller.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import {
77
Param,
88
Post,
99
Put,
10+
UseGuards,
1011
} from '@nestjs/common';
1112
import { ApiTags } from '@nestjs/swagger';
1213

1314
import { ShelterSupplyService } from './shelter-supply.service';
1415
import { ServerResponse } from '../utils';
16+
import { DistributionCenterGuard } from '@/guards/distribution-center.guard';
1517

1618
@ApiTags('Suprimento de abrigos')
1719
@Controller('shelter/supplies')
@@ -67,4 +69,23 @@ export class ShelterSupplyController {
6769
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400);
6870
}
6971
}
72+
73+
@Put(':shelterId/supplies/many')
74+
@UseGuards(DistributionCenterGuard)
75+
async updateMany(@Body() body, @Param('shelterId') shelterId: string) {
76+
try {
77+
const data = await this.shelterSupplyService.updateMany({
78+
shelterId,
79+
...body,
80+
});
81+
return new ServerResponse(
82+
200,
83+
'Successfully updated many shelter supplies',
84+
data,
85+
);
86+
} catch (err: any) {
87+
this.logger.error(`Failed to update many shelter supplies: ${err}`);
88+
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400);
89+
}
90+
}
7091
}

src/shelter-supply/shelter-supply.service.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { z } from 'zod';
22
import { Injectable } from '@nestjs/common';
33

44
import { PrismaService } from '../prisma/prisma.service';
5-
import { CreateShelterSupplySchema, UpdateShelterSupplySchema } from './types';
5+
import {
6+
CreateShelterSupplySchema,
7+
UpdateManyShelterSupplySchema,
8+
UpdateShelterSupplySchema,
9+
} from './types';
610
import { SupplyPriority } from '../supply/types';
711

812
@Injectable()
@@ -70,7 +74,24 @@ export class ShelterSupplyService {
7074
data: {
7175
...data,
7276
quantity: priority !== SupplyPriority.UnderControl ? quantity : null,
73-
createdAt: new Date().toISOString(),
77+
updatedAt: new Date().toISOString(),
78+
},
79+
});
80+
}
81+
82+
async updateMany(body: z.infer<typeof UpdateManyShelterSupplySchema>) {
83+
const { ids, shelterId } = UpdateManyShelterSupplySchema.parse(body);
84+
85+
await this.prismaService.shelterSupply.updateMany({
86+
where: {
87+
shelterId: shelterId,
88+
supplyId: {
89+
in: ids,
90+
},
91+
},
92+
data: {
93+
priority: SupplyPriority.UnderControl,
94+
updatedAt: new Date().toISOString(),
7495
},
7596
});
7697
}

src/shelter-supply/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@ const UpdateShelterSupplySchema = z.object({
4444
}),
4545
});
4646

47+
const UpdateManyShelterSupplySchema = z.object({
48+
ids: z.array(z.string()),
49+
shelterId: z.string(),
50+
});
51+
4752
export {
4853
ShelterSupplySchema,
4954
CreateShelterSupplySchema,
5055
UpdateShelterSupplySchema,
56+
UpdateManyShelterSupplySchema,
5157
};

0 commit comments

Comments
 (0)