Skip to content

Commit

Permalink
Merge branch 'development' into task/th-13-create-business-schema
Browse files Browse the repository at this point in the history
# Conflicts:
#	backend/src/libs/packages/database/generated-schema/0000_condemned_lilith.sql
#	backend/src/libs/packages/database/schema/schema.ts
#	backend/src/libs/packages/database/schema/tables-schema.ts
#	shared/src/libs/packages/http/http.ts
#	shared/src/libs/packages/http/libs/enums/enum.ts
#	shared/src/libs/packages/http/libs/enums/http-code.enum.ts
#	shared/src/libs/packages/http/libs/enums/http-error-message.enum.ts
  • Loading branch information
mV22110702 committed Aug 23, 2023
2 parents 10757b9 + b6cbb98 commit a5cef4a
Show file tree
Hide file tree
Showing 55 changed files with 849 additions and 104 deletions.
2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"build": "tsc && tsc-alias"
},
"devDependencies": {
"@types/bcryptjs": "2.4.2",
"@types/convict": "6.1.3",
"@types/swagger-jsdoc": "6.0.1",
"nodemon": "3.0.1",
Expand All @@ -25,6 +26,7 @@
"dependencies": {
"@fastify/swagger": "8.9.0",
"@fastify/swagger-ui": "1.9.3",
"bcryptjs": "2.4.3",
"convict": "6.2.4",
"dotenv": "16.3.1",
"drizzle-kit": "0.19.12",
Expand Down
2 changes: 2 additions & 0 deletions backend/src/libs/enums/enums.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export {
ApiPath,
AppEnvironment,
HttpCode,
HttpMessage,
ServerErrorType,
} from 'shared/build/index.js';
2 changes: 1 addition & 1 deletion backend/src/libs/exceptions/exceptions.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { DatabaseConnectionError } from './database/database.js';
export { ValidationError } from 'shared/build/index.js';
export { HttpError, ValidationError } from 'shared/build/index.js';
8 changes: 8 additions & 0 deletions backend/src/libs/interfaces/encrypt.inreface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { type HashedPass } from '~/packages/auth/libs/types/hashed-pass.type';

interface IEncryptService {
encrypt(password: string): Promise<HashedPass>;
compare(password: string, hash: string): Promise<boolean>;
}

export { type IEncryptService };
8 changes: 8 additions & 0 deletions backend/src/libs/interfaces/encrypt.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { type HashedPass } from '~/packages/auth/libs/types/hashed-pass.type';

interface IEncryptService {
encrypt(password: string): Promise<HashedPass>;
compare(password: string, hash: string): Promise<boolean>;
}

export { type IEncryptService };
1 change: 1 addition & 0 deletions backend/src/libs/interfaces/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { type IEncryptService } from './encrypt.interface.js';
export { type IEntity } from './entity.interface.js';
export { type IRepository } from './repository.interface.js';
export { type IService } from './service.interface.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
CREATE TABLE IF NOT EXISTS
"groups" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar NOT NULL,
"key" varchar NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);

INSERT INTO
"groups" (name, key)
VALUES
('Customer', 'customer'),
('Buisness', 'business'),
('Driver', 'driver');

--> statement-breakpoint
ALTER TABLE "users"
ADD COLUMN "email" varchar NOT NULL;

--> statement-breakpoint
ALTER TABLE "users"
ADD COLUMN "first_name" varchar NOT NULL;

--> statement-breakpoint
ALTER TABLE "users"
ADD COLUMN "last_name" varchar NOT NULL;

--> statement-breakpoint
ALTER TABLE "users"
ADD COLUMN "group_id" integer NOT NULL;

--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "users" ADD CONSTRAINT "users_group_id_groups_id_fk" FOREIGN KEY ("group_id") REFERENCES "groups"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{
"version": "5",
"dialect": "pg",
"id": "7773db4d-0369-4f74-8c23-78b76371e4db",
"prevId": "b732274c-2d68-457d-8d5b-73a978a05cb4",
"tables": {
"groups": {
"name": "groups",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"key": {
"name": "key",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"users": {
"name": "users",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"phone": {
"name": "phone",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"email": {
"name": "email",
"type": "varchar",
"primaryKey": false,
"notNull": false
},
"first_name": {
"name": "first_name",
"type": "varchar",
"primaryKey": false,
"notNull": false
},
"last_name": {
"name": "last_name",
"type": "varchar",
"primaryKey": false,
"notNull": false
},
"password_hash": {
"name": "password_hash",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"password_salt": {
"name": "password_salt",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"group_id": {
"name": "group_id",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"users_phone_unique_idx": {
"name": "users_phone_unique_idx",
"columns": ["phone"],
"isUnique": true
}
},
"foreignKeys": {
"users_group_id_groups_id_fk": {
"name": "users_group_id_groups_id_fk",
"tableFrom": "users",
"tableTo": "groups",
"columnsFrom": ["group_id"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"when": 1692711854142,
"tag": "0000_pale_romulus",
"breakpoints": true
},
{
"idx": 1,
"version": "5",
"when": 1692626206950,
"tag": "0001_powerful_sphinx",
"breakpoints": true
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const DatabaseTableName = {
USERS: 'users',
GROUPS: 'groups',
} as const;

export { DatabaseTableName };
27 changes: 27 additions & 0 deletions backend/src/libs/packages/encrypt/encrypt.package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import bcrypt from 'bcryptjs';

import { type IEncryptService } from '~/libs/interfaces/encrypt.interface';
import { type HashedPass } from '~/packages/auth/libs/types/hashed-pass.type';

const { compare: comparePass, genSalt, hash } = bcrypt;

class EncryptService implements IEncryptService {
private generateSalt(rounds: number): Promise<string> {
return genSalt(rounds);
}

public async encrypt(password: string): Promise<HashedPass> {
const passwordSalt = await this.generateSalt(10);
const passwordHash = await hash(password, passwordSalt);

return { passwordHash, passwordSalt };
}

public compare(password: string, hash: string): Promise<boolean> {
return comparePass(password, hash);
}
}

const encryptService = new EncryptService();

export { encryptService };
1 change: 1 addition & 0 deletions backend/src/libs/packages/packages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { encryptService } from './encrypt/encrypt.package.js';
14 changes: 14 additions & 0 deletions backend/src/packages/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,21 @@ class AuthController extends Controller {
* application/json:
* schema:
* $ref: '#/components/schemas/User'
* 409:
* description: User already exists
* content:
* application/json:
* schema:
* type: object
* properties:
* errorType:
* type: string
* example: COMMON
* message:
* type: string
* example: User already exists
*/

private async signUp(
options: ApiHandlerOptions<{
body: UserSignUpRequestDto;
Expand Down
26 changes: 24 additions & 2 deletions backend/src/packages/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
import { HttpCode, HttpMessage } from '~/libs/enums/enums.js';
import { HttpError } from '~/libs/exceptions/exceptions.js';
import {
type UserSignUpRequestDto,
type UserSignUpResponseDto,
} from '~/packages/users/libs/types/types.js';
import { type UserService } from '~/packages/users/user.service.js';

import { type UserEntity } from '../users/user.entity.js';

class AuthService {
private userService: UserService;

public constructor(userService: UserService) {
this.userService = userService;
}

public signUp(
private async checkExistingUser({
phone,
}: UserSignUpRequestDto): Promise<boolean> {
const existingUser: UserEntity | null =
await this.userService.findByPhone(phone);

return Boolean(existingUser);
}

public async signUp(
userRequestDto: UserSignUpRequestDto,
): Promise<UserSignUpResponseDto> {
return this.userService.create(userRequestDto);
const isUserExist = await this.checkExistingUser(userRequestDto);

if (isUserExist) {
throw new HttpError({
message: HttpMessage.USER_EXISTS,
status: HttpCode.CONFLICT,
});
}

return await this.userService.create(userRequestDto);
}
}

Expand Down
5 changes: 5 additions & 0 deletions backend/src/packages/auth/libs/types/hashed-pass.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type HashedPass = {
passwordSalt: string;
passwordHash: string;
};
export { type HashedPass };
1 change: 1 addition & 0 deletions backend/src/packages/auth/libs/types/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { type HashedPass } from './hashed-pass.type.js';
Loading

0 comments on commit a5cef4a

Please sign in to comment.