Skip to content

Commit 6a4d00d

Browse files
committed
handlers refactoring
1 parent 33a05bb commit 6a4d00d

File tree

6 files changed

+63
-81
lines changed

6 files changed

+63
-81
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Handler {
2+
handleRoutes(): void
3+
}

source/api/v1/api/handlers/Handler.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

source/api/v1/api/handlers/auth/AuthHandlers.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,22 @@ import { isException } from "../../../shared/utils/guards/ExceptionGuard";
88
import { Handler } from "../Handler";
99
import { AuthorizationPreHandler } from "../../prehandlers/AuthPreHandler";
1010

11-
export class AuthHandler extends Handler<IAuthService> {
11+
export class AuthHandler implements Handler {
1212
constructor(
13-
server: FastifyInstance,
14-
authorizationPreHandler: AuthorizationPreHandler,
15-
authService: IAuthService
16-
) {
17-
super(server, authorizationPreHandler, authService)
18-
}
13+
private server: FastifyInstance,
14+
private authorizationPreHandler: AuthorizationPreHandler,
15+
private authService: IAuthService
16+
) {}
1917

20-
public override handleRoutes(): void {
18+
public handleRoutes(): void {
2119
this.server.post<{
2220
Body: UserCredentials,
2321
}>("/auth", {
2422
schema: AuthUserSchema
2523
}, async (request, reply) => {
2624
const credentials: UserCredentials = request.body
2725

28-
const result = await this.service.authenticateAndGenerateToken(
26+
const result = await this.authService.authenticateAndGenerateToken(
2927
credentials.email,
3028
credentials.password
3129
)
@@ -42,14 +40,14 @@ export class AuthHandler extends Handler<IAuthService> {
4240
Body: { oldPassword: string, newPassword: string },
4341
}>("/auth/password", {
4442
schema: ChangePasswordSchema,
45-
preHandler: this.authentificationPreHandler
43+
preHandler: this.authorizationPreHandler
4644
}, async (request, reply) => {
4745
const passwords = request.body
4846
const { login } = extractJwtPayload(
4947
extractToken(request)
5048
)
5149

52-
const state = await this.service.changePassword(
50+
const state = await this.authService.changePassword(
5351
login,
5452
passwords.oldPassword,
5553
passwords.newPassword

source/api/v1/api/handlers/common/CommonHandler.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import Healthcheck, { SystemReport } from "../../../shared/utils/common/Healthch
33
import { Handler } from "../Handler";
44
import { isException } from "../../../shared/utils/guards/ExceptionGuard";
55

6-
export class CommonHandler extends Handler<Healthcheck> {
7-
constructor(server: FastifyInstance, healthcheck: Healthcheck) {
8-
super(server, undefined, healthcheck)
9-
}
6+
export class CommonHandler implements Handler {
7+
constructor(
8+
private server: FastifyInstance,
9+
private healthcheck: Healthcheck
10+
) {}
1011

11-
public override handleRoutes(): void {
12+
public handleRoutes(): void {
1213

1314
this.server.get("/ping", async (_, reply) => {
1415
reply.code(200).send("pong")
@@ -20,7 +21,7 @@ export class CommonHandler extends Handler<Healthcheck> {
2021

2122
this.server.get("/healthcheck", async (_, reply) => {
2223

23-
const result = await this.service.getFullSystemReport()
24+
const result = await this.healthcheck.getFullSystemReport()
2425
if (isException(result)) {
2526
// @ts-ignore
2627
reply.code(result.statusCode).send(result)

source/api/v1/api/handlers/notes/NotesHandlers.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,19 @@ import { isException } from "../../../shared/utils/guards/ExceptionGuard";
88
import { Handler } from "../Handler";
99
import { AuthorizationPreHandler } from "../../prehandlers/AuthPreHandler";
1010

11-
export class NotesHandler extends Handler<INotesService> {
11+
export class NotesHandler implements Handler {
1212
constructor(
13-
server: FastifyInstance,
14-
authorizationPreHandler: AuthorizationPreHandler,
15-
notesService: INotesService
16-
) {
17-
super(server, authorizationPreHandler, notesService)
18-
}
13+
private server: FastifyInstance,
14+
private authorizationPreHandler: AuthorizationPreHandler,
15+
private notesService: INotesService
16+
) {}
1917

20-
public override handleRoutes(): void {
18+
public handleRoutes(): void {
2119
this.server.post<{
2220
Body: Omit<NoteWithoutMetadata, "author">,
2321
}>("/notes", {
2422
schema: CreateNoteSchema,
25-
preHandler: this.authentificationPreHandler
23+
preHandler: this.authorizationPreHandler
2624
}, async (request, reply) => {
2725
const { login } = extractJwtPayload(
2826
extractToken(request)
@@ -32,7 +30,7 @@ export class NotesHandler extends Handler<INotesService> {
3230
...request.body,
3331
author: login
3432
}
35-
const createdNote = await this.service.createNote(insertData)
33+
const createdNote = await this.notesService.createNote(insertData)
3634
if (isException(createdNote)) {
3735
reply.code(createdNote.statusCode).send(createdNote)
3836
return
@@ -52,7 +50,7 @@ export class NotesHandler extends Handler<INotesService> {
5250
}>("/notes/my",
5351
{
5452
schema: GetNotesSchema,
55-
preHandler: this.authentificationPreHandler
53+
preHandler: this.authorizationPreHandler
5654
},
5755
async (request, reply) => {
5856
const { login } = extractJwtPayload(
@@ -64,7 +62,7 @@ export class NotesHandler extends Handler<INotesService> {
6462
const sort = request.query.sort
6563
const tags = request.query.tags
6664

67-
const notes = await this.service.getMyNotes(login, {tags, limit, skip, sort})
65+
const notes = await this.notesService.getMyNotes(login, {tags, limit, skip, sort})
6866
if (isException(notes)) {
6967
reply.code(notes.statusCode).send(notes)
7068
return
@@ -83,7 +81,7 @@ export class NotesHandler extends Handler<INotesService> {
8381
},
8482
}>("/notes/collaborated", {
8583
schema: GetNotesSchema,
86-
preHandler: this.authentificationPreHandler
84+
preHandler: this.authorizationPreHandler
8785
}, async (request, reply) => {
8886
const { login } = extractJwtPayload(
8987
extractToken(request)
@@ -94,7 +92,7 @@ export class NotesHandler extends Handler<INotesService> {
9492
const sort = request.query.sort
9593
const tags = request.query.tags
9694

97-
const notes = await this.service.getCollaboratedNotes(login, {tags, limit, skip, sort})
95+
const notes = await this.notesService.getCollaboratedNotes(login, {tags, limit, skip, sort})
9896
if (isException(notes)) {
9997
reply.code(notes.statusCode).send(notes)
10098
return
@@ -107,7 +105,7 @@ export class NotesHandler extends Handler<INotesService> {
107105
}>("/notes/:id",
108106
{
109107
schema: GetNoteSchema,
110-
preHandler: this.authentificationPreHandler
108+
preHandler: this.authorizationPreHandler
111109
},
112110
async (request, reply) => {
113111
const { login } = extractJwtPayload(
@@ -116,7 +114,7 @@ export class NotesHandler extends Handler<INotesService> {
116114

117115
const id = request.params.id
118116

119-
const foundNote = await this.service.getNote(id, login)
117+
const foundNote = await this.notesService.getNote(id, login)
120118
if (isException(foundNote)) {
121119
reply.code(foundNote.statusCode).send(foundNote)
122120
return
@@ -130,7 +128,7 @@ export class NotesHandler extends Handler<INotesService> {
130128
}>("/notes/:id",
131129
{
132130
schema: DeleteNoteSchema,
133-
preHandler: this.authentificationPreHandler
131+
preHandler: this.authorizationPreHandler
134132
},
135133
async (request, reply) => {
136134
const { login } = extractJwtPayload(
@@ -139,7 +137,7 @@ export class NotesHandler extends Handler<INotesService> {
139137

140138
const id = request.params.id
141139

142-
const state = await this.service.deleteNote(id, login)
140+
const state = await this.notesService.deleteNote(id, login)
143141
if (isException(state)) {
144142
reply.code(state.statusCode).send(state)
145143
return
@@ -153,7 +151,7 @@ export class NotesHandler extends Handler<INotesService> {
153151
Body: NoteUpdate
154152
}>("/notes/:id", {
155153
schema: UpdateNoteSchema,
156-
preHandler: this.authentificationPreHandler
154+
preHandler: this.authorizationPreHandler
157155
}, async (request, reply) => {
158156
const { login } = extractJwtPayload(
159157
extractToken(request)
@@ -162,7 +160,7 @@ export class NotesHandler extends Handler<INotesService> {
162160
const id = request.params.id
163161
const updateData = request.body
164162

165-
const updatedNote = await this.service.updateNote(id, login, updateData)
163+
const updatedNote = await this.notesService.updateNote(id, login, updateData)
166164
if (isException(updatedNote)) {
167165
reply.code(updatedNote.statusCode).send(updatedNote)
168166
return
@@ -176,15 +174,15 @@ export class NotesHandler extends Handler<INotesService> {
176174
Params: { id: string },
177175
}>("/notes/:id/collaborators", {
178176
schema: GetNoteCollaboratorsSchema,
179-
preHandler: this.authentificationPreHandler
177+
preHandler: this.authorizationPreHandler
180178
}, async (request, reply) => {
181179
const { login } = extractJwtPayload(
182180
extractToken(request)
183181
)
184182

185183
const id = request.params.id
186184

187-
const collaborators = await this.service.getCollaborators(id, login)
185+
const collaborators = await this.notesService.getCollaborators(id, login)
188186
if (isException(collaborators)) {
189187
reply.code(collaborators.statusCode).send(collaborators)
190188
return
@@ -200,7 +198,7 @@ export class NotesHandler extends Handler<INotesService> {
200198
},
201199
}>("/notes/:id/collaborators", {
202200
schema: AddCollaboratorSchema,
203-
preHandler: this.authentificationPreHandler
201+
preHandler: this.authorizationPreHandler
204202
}, async (request, reply) => {
205203
const { login } = extractJwtPayload(
206204
extractToken(request)
@@ -209,7 +207,7 @@ export class NotesHandler extends Handler<INotesService> {
209207
const id = request.params.id
210208
const collaboratorLogin = request.body.collaboratorLogin
211209

212-
const state = await this.service.addCollaborator(
210+
const state = await this.notesService.addCollaborator(
213211
id,
214212
login,
215213
collaboratorLogin
@@ -229,7 +227,7 @@ export class NotesHandler extends Handler<INotesService> {
229227
},
230228
}>("/notes/:id/collaborators", {
231229
schema: RemoveCollaboratorSchema,
232-
preHandler: this.authentificationPreHandler
230+
preHandler: this.authorizationPreHandler
233231
}, async (request, reply) => {
234232
const { login } = extractJwtPayload(
235233
extractToken(request)
@@ -238,7 +236,7 @@ export class NotesHandler extends Handler<INotesService> {
238236
const id = request.params.id
239237
const collaboratorLogin = request.body.collaboratorLogin
240238

241-
const state = await this.service.removeCollaborator(
239+
const state = await this.notesService.removeCollaborator(
242240
id,
243241
login,
244242
collaboratorLogin

source/api/v1/api/handlers/users/UsersHandlers.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,94 @@
11
import { FastifyInstance } from "fastify";
2-
import { IUsersService } from "../../../services/users/UsersServiceInterface";
32
import { UserUpdate, UserWithoutMetadata, UserWithoutSensetives } from "../../../shared/dto/UserDto";
43
import { CreateUserSchema, GetMyProfileSchema, GetUserSchema, UpdateUserSchema } from "../../validation/schemas/UserSchemas";
5-
import { UsersService } from "../../../services/users/UsersService";
64
import { extractJwtPayload } from "../../../shared/utils/jwt/PayloadExtractor";
75
import { extractToken } from "../../../shared/utils/common/TokenExtractor";
86
import { isException } from "../../../shared/utils/guards/ExceptionGuard";
97
import { Handler } from "../Handler";
108
import { AuthorizationPreHandler } from "../../prehandlers/AuthPreHandler";
9+
import { IUsersService } from "../../../services/users/UsersServiceInterface";
10+
import { UsersService } from "../../../services/users/UsersService";
1111

1212

13-
export class UsersHandler extends Handler<IUsersService> {
14-
constructor(
15-
server: FastifyInstance,
16-
authorizationPreHandler: AuthorizationPreHandler,
17-
usersService: IUsersService
18-
) {
19-
super(server, authorizationPreHandler, usersService)
20-
}
13+
export class UsersHandler implements Handler {
14+
constructor (
15+
private server: FastifyInstance,
16+
private authorizationPreHandler: AuthorizationPreHandler,
17+
private usersService: IUsersService
18+
) {}
2119

22-
public override handleRoutes(): void {
20+
public handleRoutes(): void {
2321
this.server.post<{
2422
Body: UserWithoutMetadata,
2523
}>("/users", { schema: CreateUserSchema }, async (request, reply) => {
2624

2725
const insertData: UserWithoutMetadata = request.body
28-
let createdUser = await this.service.createUser(insertData)
26+
let createdUser = await this.usersService.createUser(insertData)
2927

3028
if (isException(createdUser)) {
3129
reply.code(createdUser.statusCode).send(createdUser)
3230
return
3331
}
3432

3533
UsersService.omitSensetiveData(createdUser)
36-
37-
reply.code(201).send(createdUser)
38-
34+
reply.code(201).send(createdUser)
3935
})
4036

4137
this.server.get("/users/me", {
4238
schema: GetMyProfileSchema,
43-
preHandler: this.authentificationPreHandler
39+
preHandler: this.authorizationPreHandler
4440
}, async (request, reply) => {
4541
const { login } = extractJwtPayload(
4642
extractToken(request)
4743
)
4844

49-
let user = await this.service.getUser("login", login)
45+
let user = await this.usersService.getUser("login", login)
5046
if (isException(user)) {
5147
reply.code(user.statusCode).send(user)
5248
return
5349
}
50+
5451
UsersService.omitSensetiveData(user)
55-
5652
reply.code(200).send(user)
57-
5853
})
5954

6055
this.server.patch<{
6156
Body: Omit<UserUpdate, "password" | "validToken">,
6257
}>("/users/me", {
6358
schema: UpdateUserSchema,
64-
preHandler: this.authentificationPreHandler
59+
preHandler: this.authorizationPreHandler
6560
}, async (request, reply) => {
6661
const { login } = extractJwtPayload(
6762
extractToken(request)
6863
)
6964

7065
const updateData = request.body
7166

72-
let updatedUser = await this.service.updateUserByLogin(login, updateData)
67+
let updatedUser = await this.usersService.updateUserByLogin(login, updateData)
7368
if (isException(updatedUser)) {
7469
reply.code(updatedUser.statusCode).send(updatedUser)
7570
return
7671
}
72+
7773
UsersService.omitSensetiveData(updatedUser)
78-
7974
reply.code(200).send(updatedUser)
80-
8175
})
8276

8377
this.server.get<{
8478
Params: { login: string },
8579
}>("/users/:login", {
8680
schema: GetUserSchema,
87-
preHandler: this.authentificationPreHandler
81+
preHandler: this.authorizationPreHandler
8882
}, async (request, reply) => {
8983
const login: string = request.params.login
9084

91-
let user = await this.service.getUser("login", login)
85+
let user = await this.usersService.getUser("login", login)
9286
if (isException(user)) {
9387
reply.code(user.statusCode).send(user)
9488
return
9589
}
90+
9691
UsersService.omitSensetiveData(user)
97-
9892
reply.code(200).send(user)
9993

10094
})

0 commit comments

Comments
 (0)