Skip to content

Commit 4de65c6

Browse files
committed
huge refactoring and file structure change
1 parent 77e8266 commit 4de65c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+503
-532
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { FastifyInstance, FastifyReply, FastifyRequest, HookHandlerDoneFunction } from "fastify";
2-
import { AuthentificationPreHandler } from "../auth/AuthPreHandler";
2+
import { AuthorizationPreHandler } from "../prehandlers/AuthPreHandler";
33

44
export abstract class Handler<TService> {
55
constructor(
66
protected server: FastifyInstance,
7-
protected authentificationPreHandler: AuthentificationPreHandler,
7+
protected authentificationPreHandler: AuthorizationPreHandler,
88
protected service: TService
99
) {}
1010

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

+11-23
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,31 @@
11
import { FastifyInstance } from "fastify";
2-
import { IAuthService } from "../services/auth/AuthServiceInterface";
3-
import { UserCredentials } from "../database/entities/User";
4-
import { AUTH_EXCEPTIONS } from "../shared/exceptions/AuthExceptions";
5-
import { AuthUserSchema, ChangePasswordSchema } from "../validation/schemas/AuthSchemas";
6-
import { extractJwtPayload } from "../auth/jwt/PayloadExtractor";
7-
import { extractToken } from "../shared/utils/common/TokenExtractor";
8-
import { isException } from "../shared/utils/guards/ExceptionGuard";
9-
import { USER_EXCEPTIONS } from "../shared/exceptions/UserExceptions";
10-
import { Handler } from "./Handler";
11-
import { AuthentificationPreHandler } from "../auth/AuthPreHandler";
2+
import { IAuthService } from "../../../services/auth/AuthServiceInterface";
3+
import { UserCredentials } from "../../../shared/dto/UserDto";
4+
import { AuthUserSchema, ChangePasswordSchema } from "../../validation/schemas/AuthSchemas";
5+
import { extractJwtPayload } from "../../../shared/utils/jwt/PayloadExtractor";
6+
import { extractToken } from "../../../shared/utils/common/TokenExtractor";
7+
import { isException } from "../../../shared/utils/guards/ExceptionGuard";
8+
import { Handler } from "../Handler";
9+
import { AuthorizationPreHandler } from "../../prehandlers/AuthPreHandler";
1210

1311
export class AuthHandler extends Handler<IAuthService> {
1412
constructor(
1513
server: FastifyInstance,
16-
authentificationPreHandler: AuthentificationPreHandler,
14+
authorizationPreHandler: AuthorizationPreHandler,
1715
authService: IAuthService
1816
) {
19-
super(server, authentificationPreHandler, authService)
17+
super(server, authorizationPreHandler, authService)
2018
}
2119

2220
public override handleRoutes(): void {
2321
this.server.post<{
2422
Body: UserCredentials,
25-
Reply: {
26-
200: { token: string, expiresIn: string },
27-
400: typeof AUTH_EXCEPTIONS.WrongCredentials,
28-
503: typeof AUTH_EXCEPTIONS.ServiceUnavailable | typeof USER_EXCEPTIONS.ServiceUnavailable
29-
}
3023
}>("/auth", {
3124
schema: AuthUserSchema
3225
}, async (request, reply) => {
3326
const credentials: UserCredentials = request.body
3427

35-
const result = await this.service.authorizeAndGenerateToken(
28+
const result = await this.service.authenticateAndGenerateToken(
3629
credentials.email,
3730
credentials.password
3831
)
@@ -47,11 +40,6 @@ export class AuthHandler extends Handler<IAuthService> {
4740

4841
this.server.patch<{
4942
Body: { oldPassword: string, newPassword: string },
50-
Reply: {
51-
200: { success: true },
52-
400: typeof AUTH_EXCEPTIONS.WrongCredentials | typeof AUTH_EXCEPTIONS.NewPasswordIsSame,
53-
503: typeof AUTH_EXCEPTIONS.ServiceUnavailable | typeof USER_EXCEPTIONS.ServiceUnavailable
54-
}
5543
}>("/auth/password", {
5644
schema: ChangePasswordSchema,
5745
preHandler: this.authentificationPreHandler

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FastifyInstance } from "fastify";
2-
import Healthcheck, { SystemReport } from "../shared/utils/common/Healthcheck";
3-
import { Handler } from "./Handler";
4-
import { isException } from "../shared/utils/guards/ExceptionGuard";
2+
import Healthcheck, { SystemReport } from "../../../shared/utils/common/Healthcheck";
3+
import { Handler } from "../Handler";
4+
import { isException } from "../../../shared/utils/guards/ExceptionGuard";
55

66
export class CommonHandler extends Handler<Healthcheck> {
77
constructor(server: FastifyInstance, healthcheck: Healthcheck) {
@@ -18,11 +18,7 @@ export class CommonHandler extends Handler<Healthcheck> {
1818
reply.code(200).send()
1919
})
2020

21-
this.server.get<{
22-
Reply: {
23-
200: SystemReport
24-
}
25-
}>("/healthcheck", async (_, reply) => {
21+
this.server.get("/healthcheck", async (_, reply) => {
2622

2723
const result = await this.service.getFullSystemReport()
2824
if (isException(result)) {

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

+10-58
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
11
import { FastifyInstance } from "fastify";
2-
import { INotesService } from "../services/notes/NotesServiceInterface";
3-
import { Note, NoteCollaborators, NotePreview, NoteUpdate, NoteWithoutMetadata } from "../database/entities/Note";
4-
import { NOTE_EXCEPTIONS } from "../shared/exceptions/NoteExceptions";
5-
import { extractJwtPayload } from "../auth/jwt/PayloadExtractor";
6-
import { extractToken } from "../shared/utils/common/TokenExtractor";
7-
import { AddCollaboratorSchema, CreateNoteSchema, DeleteNoteSchema, GetNoteCollaboratorsSchema, GetNoteSchema, GetNotesSchema, RemoveCollaboratorSchema, UpdateNoteSchema } from "../validation/schemas/NoteSchemas";
8-
import { isException } from "../shared/utils/guards/ExceptionGuard";
9-
import { USER_EXCEPTIONS } from "../shared/exceptions/UserExceptions";
10-
import { Handler } from "./Handler";
11-
import { AuthentificationPreHandler } from "../auth/AuthPreHandler";
2+
import { INotesService } from "../../../services/notes/NotesServiceInterface";
3+
import { Note, NoteCollaborators, NotePreview, NoteUpdate, NoteWithoutMetadata } from "../../../shared/dto/NoteDto";
4+
import { extractJwtPayload } from "../../../shared/utils/jwt/PayloadExtractor";
5+
import { extractToken } from "../../../shared/utils/common/TokenExtractor";
6+
import { AddCollaboratorSchema, CreateNoteSchema, DeleteNoteSchema, GetNoteCollaboratorsSchema, GetNoteSchema, GetNotesSchema, RemoveCollaboratorSchema, UpdateNoteSchema } from "../../validation/schemas/NoteSchemas";
7+
import { isException } from "../../../shared/utils/guards/ExceptionGuard";
8+
import { Handler } from "../Handler";
9+
import { AuthorizationPreHandler } from "../../prehandlers/AuthPreHandler";
1210

1311
export class NotesHandler extends Handler<INotesService> {
1412
constructor(
1513
server: FastifyInstance,
16-
authentificationPreHandler: AuthentificationPreHandler,
14+
authorizationPreHandler: AuthorizationPreHandler,
1715
notesService: INotesService
1816
) {
19-
super(server, authentificationPreHandler, notesService)
17+
super(server, authorizationPreHandler, notesService)
2018
}
2119

2220
public override handleRoutes(): void {
2321
this.server.post<{
2422
Body: Omit<NoteWithoutMetadata, "author">,
25-
Reply: {
26-
201: Note,
27-
503: typeof NOTE_EXCEPTIONS.ServiceUnavailable | typeof USER_EXCEPTIONS.ServiceUnavailable
28-
404: typeof NOTE_EXCEPTIONS.CollaboratorNotFound
29-
}
3023
}>("/notes", {
3124
schema: CreateNoteSchema,
3225
preHandler: this.authentificationPreHandler
@@ -56,10 +49,6 @@ export class NotesHandler extends Handler<INotesService> {
5649
sort: "ASC" | "DESC",
5750
tags: string[]
5851
},
59-
Reply: {
60-
200: NotePreview[],
61-
503: typeof NOTE_EXCEPTIONS.ServiceUnavailable
62-
}
6352
}>("/notes/my",
6453
{
6554
schema: GetNotesSchema,
@@ -92,10 +81,6 @@ export class NotesHandler extends Handler<INotesService> {
9281
sort: "ASC" | "DESC",
9382
tags: string[]
9483
},
95-
Reply: {
96-
200: NotePreview[],
97-
503: typeof NOTE_EXCEPTIONS.ServiceUnavailable
98-
}
9984
}>("/notes/collaborated", {
10085
schema: GetNotesSchema,
10186
preHandler: this.authentificationPreHandler
@@ -119,11 +104,6 @@ export class NotesHandler extends Handler<INotesService> {
119104

120105
this.server.get<{
121106
Params: { id: string },
122-
Reply: {
123-
200: Note,
124-
404: typeof NOTE_EXCEPTIONS.NoteNotFound,
125-
503: typeof NOTE_EXCEPTIONS.ServiceUnavailable
126-
}
127107
}>("/notes/:id",
128108
{
129109
schema: GetNoteSchema,
@@ -147,11 +127,6 @@ export class NotesHandler extends Handler<INotesService> {
147127

148128
this.server.delete<{
149129
Params: { id: string },
150-
Reply: {
151-
200: { success: true },
152-
404: typeof NOTE_EXCEPTIONS.NoteNotFound,
153-
503: typeof NOTE_EXCEPTIONS.ServiceUnavailable
154-
}
155130
}>("/notes/:id",
156131
{
157132
schema: DeleteNoteSchema,
@@ -175,11 +150,6 @@ export class NotesHandler extends Handler<INotesService> {
175150

176151
this.server.patch<{
177152
Params: { id: string },
178-
Reply: {
179-
200: Note,
180-
404: typeof NOTE_EXCEPTIONS.NoteNotFound,
181-
503: typeof NOTE_EXCEPTIONS.ServiceUnavailable
182-
},
183153
Body: NoteUpdate
184154
}>("/notes/:id", {
185155
schema: UpdateNoteSchema,
@@ -204,11 +174,6 @@ export class NotesHandler extends Handler<INotesService> {
204174

205175
this.server.get<{
206176
Params: { id: string },
207-
Reply: {
208-
200: NoteCollaborators
209-
404: typeof NOTE_EXCEPTIONS.NoteNotFound
210-
503: typeof NOTE_EXCEPTIONS.ServiceUnavailable
211-
}
212177
}>("/notes/:id/collaborators", {
213178
schema: GetNoteCollaboratorsSchema,
214179
preHandler: this.authentificationPreHandler
@@ -233,13 +198,6 @@ export class NotesHandler extends Handler<INotesService> {
233198
Body: {
234199
collaboratorLogin: string
235200
},
236-
Reply: {
237-
201: {success: true}
238-
503: typeof NOTE_EXCEPTIONS.ServiceUnavailable,
239-
404: typeof NOTE_EXCEPTIONS.CollaboratorNotFound | typeof NOTE_EXCEPTIONS.NoteNotFound
240-
400: typeof NOTE_EXCEPTIONS.CollaboratorAlreadyInNote
241-
403: typeof NOTE_EXCEPTIONS.AcessRestricted
242-
}
243201
}>("/notes/:id/collaborators", {
244202
schema: AddCollaboratorSchema,
245203
preHandler: this.authentificationPreHandler
@@ -269,12 +227,6 @@ export class NotesHandler extends Handler<INotesService> {
269227
Body: {
270228
collaboratorLogin: string
271229
},
272-
Reply: {
273-
200: {success: true}
274-
503: typeof NOTE_EXCEPTIONS.ServiceUnavailable
275-
404: typeof NOTE_EXCEPTIONS.CollaboratorNotFound | typeof NOTE_EXCEPTIONS.NoteNotFound
276-
403: typeof NOTE_EXCEPTIONS.AcessRestricted
277-
}
278230
}>("/notes/:id/collaborators", {
279231
schema: RemoveCollaboratorSchema,
280232
preHandler: this.authentificationPreHandler

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

+12-34
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
import { FastifyInstance } from "fastify";
2-
import { IUsersService } from "../services/users/UsersServiceInterface";
3-
import { UserUpdate, UserWithoutMetadata, UserWithoutSensetives } from "../database/entities/User";
4-
import { USER_EXCEPTIONS } from "../shared/exceptions/UserExceptions";
5-
import { CreateUserSchema, GetMyProfileSchema, GetUserSchema, UpdateUserSchema } from "../validation/schemas/UserSchemas";
6-
import { UsersService } from "../services/users/UsersService";
7-
import { extractJwtPayload } from "../auth/jwt/PayloadExtractor";
8-
import { extractToken } from "../shared/utils/common/TokenExtractor";
9-
import { isException } from "../shared/utils/guards/ExceptionGuard";
10-
import { Handler } from "./Handler";
11-
import { AuthentificationPreHandler } from "../auth/AuthPreHandler";
2+
import { IUsersService } from "../../../services/users/UsersServiceInterface";
3+
import { UserUpdate, UserWithoutMetadata, UserWithoutSensetives } from "../../../shared/dto/UserDto";
4+
import { CreateUserSchema, GetMyProfileSchema, GetUserSchema, UpdateUserSchema } from "../../validation/schemas/UserSchemas";
5+
import { UsersService } from "../../../services/users/UsersService";
6+
import { extractJwtPayload } from "../../../shared/utils/jwt/PayloadExtractor";
7+
import { extractToken } from "../../../shared/utils/common/TokenExtractor";
8+
import { isException } from "../../../shared/utils/guards/ExceptionGuard";
9+
import { Handler } from "../Handler";
10+
import { AuthorizationPreHandler } from "../../prehandlers/AuthPreHandler";
1211

1312

1413
export class UsersHandler extends Handler<IUsersService> {
1514
constructor(
1615
server: FastifyInstance,
17-
authentificationPreHandler: AuthentificationPreHandler,
16+
authorizationPreHandler: AuthorizationPreHandler,
1817
usersService: IUsersService
1918
) {
20-
super(server, authentificationPreHandler, usersService)
19+
super(server, authorizationPreHandler, usersService)
2120
}
2221

2322
public override handleRoutes(): void {
2423
this.server.post<{
2524
Body: UserWithoutMetadata,
26-
Reply: {
27-
201: UserWithoutSensetives,
28-
400: typeof USER_EXCEPTIONS.AlreadyExists,
29-
503: typeof USER_EXCEPTIONS.ServiceUnavailable
30-
}
3125
}>("/users", { schema: CreateUserSchema }, async (request, reply) => {
3226

3327
const insertData: UserWithoutMetadata = request.body
@@ -44,13 +38,7 @@ export class UsersHandler extends Handler<IUsersService> {
4438

4539
})
4640

47-
this.server.get<{
48-
Reply: {
49-
200: UserWithoutSensetives,
50-
404: typeof USER_EXCEPTIONS.NotFound,
51-
503: typeof USER_EXCEPTIONS.ServiceUnavailable,
52-
}
53-
}>("/users/me", {
41+
this.server.get("/users/me", {
5442
schema: GetMyProfileSchema,
5543
preHandler: this.authentificationPreHandler
5644
}, async (request, reply) => {
@@ -71,11 +59,6 @@ export class UsersHandler extends Handler<IUsersService> {
7159

7260
this.server.patch<{
7361
Body: Omit<UserUpdate, "password" | "validToken">,
74-
Reply: {
75-
200: UserWithoutSensetives,
76-
404: typeof USER_EXCEPTIONS.NotFound,
77-
503: typeof USER_EXCEPTIONS.ServiceUnavailable
78-
}
7962
}>("/users/me", {
8063
schema: UpdateUserSchema,
8164
preHandler: this.authentificationPreHandler
@@ -99,11 +82,6 @@ export class UsersHandler extends Handler<IUsersService> {
9982

10083
this.server.get<{
10184
Params: { login: string },
102-
Reply: {
103-
200: UserWithoutSensetives,
104-
404: typeof USER_EXCEPTIONS.NotFound,
105-
503: typeof USER_EXCEPTIONS.ServiceUnavailable
106-
}
10785
}>("/users/:login", {
10886
schema: GetUserSchema,
10987
preHandler: this.authentificationPreHandler

source/api/v1/openapi/responses/AuthResponses.ts renamed to source/api/v1/api/openapi/responses/AuthResponses.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { AUTH_EXCEPTIONS } from "../../shared/exceptions/AuthExceptions";
2-
import { USER_EXCEPTIONS } from "../../shared/exceptions/UserExceptions";
1+
import { NEW_PASSWORD_IS_SAME, WRONG_CREDENTIALS } from "../../../shared/exceptions/AuthExceptions";
2+
import { SERVICE_UNAVAILABLE } from "../../../shared/exceptions/CommonException";
3+
import { USER_NOT_AUTHORIZED } from "../../../shared/exceptions/UserExceptions";
4+
35

46
export const AUTH_RESPONSES = {
57
Authorize: {
@@ -14,14 +16,14 @@ export const AUTH_RESPONSES = {
1416
type: 'object',
1517
properties: {
1618
statusCode: { enum: [400] },
17-
message: {enum: [AUTH_EXCEPTIONS.WrongCredentials.message]}
19+
message: {enum: [WRONG_CREDENTIALS.message]}
1820
}
1921
},
2022
503: {
2123
type: 'object',
2224
properties: {
2325
statusCode: { enum: [503] },
24-
message: {enum: [AUTH_EXCEPTIONS.ServiceUnavailable.message]}
26+
message: {enum: [SERVICE_UNAVAILABLE.message]}
2527
}
2628
},
2729
},
@@ -36,21 +38,21 @@ export const AUTH_RESPONSES = {
3638
type: 'object',
3739
properties: {
3840
statusCode: { enum: [400] },
39-
message: {enum: [AUTH_EXCEPTIONS.NewPasswordIsSame.message, AUTH_EXCEPTIONS.WrongCredentials.message, "body must have required property 'PROPERTY NAME'"]}
41+
message: {enum: [NEW_PASSWORD_IS_SAME.message, WRONG_CREDENTIALS.message, "body must have required property 'PROPERTY NAME'"]}
4042
}
4143
},
4244
401: {
4345
type: 'object',
4446
properties: {
4547
statusCode: { enum: [401] },
46-
message: { enum: [USER_EXCEPTIONS.NotAuthorized.message] }
48+
message: { enum: [USER_NOT_AUTHORIZED.message] }
4749
}
4850
},
4951
503: {
5052
type: 'object',
5153
properties: {
5254
statusCode: { enum: [503] },
53-
message: {enum: [AUTH_EXCEPTIONS.ServiceUnavailable.message]}
55+
message: {enum: [SERVICE_UNAVAILABLE.message]}
5456
}
5557
},
5658
}

0 commit comments

Comments
 (0)