Skip to content

Commit f1414c7

Browse files
committed
made insignificant refactoring in services
1 parent 9f00b7a commit f1414c7

File tree

9 files changed

+45
-43
lines changed

9 files changed

+45
-43
lines changed

source/api/v1/handlers/AuthHandlers.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ export const handleAuthRoutes = (
3838
return
3939
}
4040

41-
reply.code(200).send({
42-
token: result[0],
43-
expiresIn: result[1]
44-
})
41+
reply.code(200).send(result)
4542

4643
})
4744

source/api/v1/handlers/NotesHandlers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const handleNoteRoutes = (
7373
const sort = request.query.sort
7474
const tags = request.query.tags
7575

76-
const notes = await notesService.getMyNotes(login, tags, limit, skip, sort)
76+
const notes = await notesService.getMyNotes(login, {tags, limit, skip, sort})
7777
if (isException(notes)) {
7878
reply.code(notes.statusCode).send(notes)
7979
return
@@ -107,7 +107,7 @@ export const handleNoteRoutes = (
107107
const sort = request.query.sort
108108
const tags = request.query.tags
109109

110-
const notes = await notesService.getCollaboratedNotes(login, tags, limit, skip, sort)
110+
const notes = await notesService.getCollaboratedNotes(login, {tags, limit, skip, sort})
111111
if (isException(notes)) {
112112
reply.code(notes.statusCode).send(notes)
113113
return

source/api/v1/services/AuthService.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export class AuthService implements IAuthService {
1717
private redis: RedisClientType
1818
) {}
1919

20-
// TODO: change return format from tuple to object
2120
@withExceptionCatch
2221
public async authorizeAndGenerateToken(email: string, password: string) {
2322

@@ -38,10 +37,10 @@ export class AuthService implements IAuthService {
3837
await this.redis.SET(foundUser.login, token)
3938

4039

41-
return [
40+
return {
4241
token,
43-
CONFIG.jwtExpiration
44-
] as [string, string]
42+
expiresIn: CONFIG.jwtExpiration
43+
}
4544
}
4645

4746
// checks if token exists in Redis token storage
@@ -59,14 +58,14 @@ export class AuthService implements IAuthService {
5958

6059
@withExceptionCatch
6160
public async changePassword(login: string, oldPassword: string, newPassword: string) {
62-
6361
const foundUser = await this.usersService.getUser("login", login)
6462
if (isException(foundUser)) {
6563
if (foundUser.statusCode === 404) {
6664
return AUTH_EXCEPTIONS.WrongCredentials
6765
}
6866
return foundUser
6967
}
68+
7069
const passwordIsValid = await bcrypt.compare(oldPassword, foundUser.password)
7170
if (!passwordIsValid) {
7271
return AUTH_EXCEPTIONS.WrongCredentials

source/api/v1/services/NotesService.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
UserWithoutSensetives
1313
} from "../database/entities/User";
1414
import {IUsersService} from "./interfaces/UsersServiceInterface";
15-
import {INotesService} from "./interfaces/NotesServiceInterface";
15+
import {INotesService, NotesSearchOptions} from "./interfaces/NotesServiceInterface";
1616
import {NOTE_EXCEPTIONS} from "../exceptions/NoteExceptions";
1717
import {Repository} from "typeorm";
1818
import {transformNoteCollaborators} from "../utils/common/TransformNoteCollaborators";
@@ -161,14 +161,10 @@ export class NotesService implements INotesService {
161161
return transformNoteCollaborators(updatedNote) as unknown as Note;
162162
}
163163

164-
// TODO: pack all arguments after authorLogin in one object
165164
@withExceptionCatch
166165
public async getMyNotes(
167166
authorLogin: string,
168-
tags: string[],
169-
limit: number,
170-
skip: number,
171-
sort: "ASC" | "DESC"
167+
options: NotesSearchOptions
172168
) {
173169
const query = this.noteRepository
174170
.createQueryBuilder("note")
@@ -180,10 +176,11 @@ export class NotesService implements INotesService {
180176
"note.updatedAt"
181177
])
182178
.where("note.author = :login", {login: authorLogin})
183-
.limit(limit)
184-
.skip(skip)
185-
.orderBy("note.updatedAt", sort);
179+
.limit(options?.limit)
180+
.skip(options?.skip)
181+
.orderBy("note.updatedAt", options?.sort);
186182

183+
const tags = options?.tags
187184
if (tags && tags.length) {
188185
query.where("note.tags && :tags", {tags});
189186
}
@@ -192,14 +189,10 @@ export class NotesService implements INotesService {
192189
return foundNotes as unknown as NotePreview[];
193190
}
194191

195-
// TODO: pack all arguments after authorLogin in one object
196192
@withExceptionCatch
197193
public async getCollaboratedNotes(
198194
login: string,
199-
tags: string[],
200-
limit: number,
201-
skip: number,
202-
sort: "ASC" | "DESC"
195+
options: NotesSearchOptions
203196
) {
204197
const query = this.noteRepository
205198
.createQueryBuilder("note")
@@ -216,10 +209,11 @@ export class NotesService implements INotesService {
216209
"note.tags",
217210
"note.updatedAt"
218211
])
219-
.limit(limit)
220-
.skip(skip)
221-
.orderBy("note.updatedAt", sort);
222-
212+
.limit(options?.limit)
213+
.skip(options?.skip)
214+
.orderBy("note.updatedAt", options?.sort);
215+
216+
const tags = options?.tags
223217
if (tags && tags.length) {
224218
query.where("note.tags && :tags", {tags});
225219
}

source/api/v1/services/UsersService.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,32 @@ export class UsersService implements IUsersService {
1515
try {
1616
user.password = undefined
1717
return user
18-
} catch(error) {
18+
} catch(_) {
1919
return user
2020
}
2121
}
2222

2323
constructor(private userRepository: Repository<User>) {}
2424

25-
@withExceptionCatch
26-
public async createUser(user: UserWithoutMetadata) {
27-
25+
private async isUserExist(email: string, login: string): Promise<boolean> {
2826
const foundUserWithEmail = await this.userRepository.findOneBy({
29-
email: user.email
27+
email
3028
})
3129
const foundUserWithLogin = await this.userRepository.findOneBy({
32-
login: user.login
30+
login
3331
})
34-
if (foundUserWithEmail || foundUserWithLogin) {
32+
33+
return Boolean(foundUserWithEmail) || Boolean(foundUserWithLogin)
34+
}
35+
36+
@withExceptionCatch
37+
public async createUser(user: UserWithoutMetadata) {
38+
39+
if (await this.isUserExist(user.email, user.password)) {
3540
return USER_EXCEPTIONS.AlreadyExists
3641
}
3742

38-
let creationData: UserWithoutMetadata & {validToken?: string} = {
43+
let creationData: UserWithoutMetadata = {
3944
...user,
4045
}
4146
creationData.password = await bcrypt.hash(creationData.password, 4)
@@ -62,7 +67,7 @@ export class UsersService implements IUsersService {
6267
public async updateUserByLogin(login: string, updateData: UserUpdate) {
6368

6469
const state = await this.userRepository.update({ login }, updateData)
65-
// if user not found - raise error
70+
// found user check
6671
if (!state.affected) {
6772
return USER_EXCEPTIONS.NotFound
6873
}

source/api/v1/services/interfaces/AuthServiceInterface.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { USER_EXCEPTIONS } from "../../exceptions/UserExceptions";
33

44
export declare interface IAuthService {
55
authorizeAndGenerateToken(email: string, password: string): Promise<
6-
| [string, string]
6+
| { token: string, expiresIn: string }
77
| typeof AUTH_EXCEPTIONS.WrongCredentials
88
| typeof AUTH_EXCEPTIONS.ServiceUnavailable
99
| typeof USER_EXCEPTIONS.ServiceUnavailable

source/api/v1/services/interfaces/NotesServiceInterface.d.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
import { DeepOptional } from "typing-assets";
12
import { Note, NoteCollaborators, NotePreview, NoteUpdate, NoteWithoutMetadata } from "../../database/entities/Note";
23
import { NOTE_EXCEPTIONS } from "../../exceptions/NoteExceptions";
34
import { USER_EXCEPTIONS } from "../../exceptions/UserExceptions";
45

6+
export type NotesSearchOptions = DeepOptional<{
7+
tags: string[]
8+
limit: number
9+
skip: number
10+
sort: "ASC" | "DESC"
11+
}>
12+
513
export interface INotesService {
614
createNote(
715
note: NoteWithoutMetadata
@@ -40,12 +48,12 @@ export interface INotesService {
4048
| typeof NOTE_EXCEPTIONS.ServiceUnavailable
4149
>
4250

43-
getMyNotes(authorLogin: string, filters: string[], limit: number, skip: number, sort: "ASC" | "DESC"): Promise<
51+
getMyNotes(authorLogin: string, options: NotesSearchOptions): Promise<
4452
NotePreview[]
4553
| typeof NOTE_EXCEPTIONS.ServiceUnavailable
4654
>
4755

48-
getCollaboratedNotes(login: string, filters: string[], limit: number, skip: number, sort: "ASC" | "DESC"): Promise<
56+
getCollaboratedNotes(login: string, options: NotesSearchOptions): Promise<
4957
NotePreview[]
5058
| typeof NOTE_EXCEPTIONS.ServiceUnavailable
5159
>

source/api/v1/utils/guards/ExceptionGuard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Exception } from "../typing/Exception";
33

44
// before generateGuard function was used to create guard
55
// but now guard generated by library function has got bug (https://github.com/LCcodder/typing-assets/issues/2)
6-
// so for now it is manually created type guard
6+
// so for now on it is manually created type guard
77
export const isException = (target: unknown): target is Exception => {
88
return target && typeof (target as Exception)["message"] === 'string'
99
}

source/main.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const main = async () => {
3232

3333
await initSwaggerViewer(server)
3434

35-
// logging hooks
3635
server.addHook('onRequest', logRequestMetadata)
3736
server.addHook('onResponse', logResponseMetadata)
3837

0 commit comments

Comments
 (0)