convert project from microservices to rest apis

This commit is contained in:
faris Aljohari
2024-03-10 12:49:51 +03:00
parent b3179a5c1f
commit c5537b3230
72 changed files with 155 additions and 384 deletions

View File

@ -22,7 +22,7 @@ import { UserRepository } from '../modules/user/repositories';
}),
HelperModule,
],
providers: [JwtStrategy, UserSessionRepository,AuthService,UserRepository],
providers: [JwtStrategy, UserSessionRepository, AuthService, UserRepository],
exports: [AuthService],
})
export class AuthModule {}

View File

@ -1,9 +1,9 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { HelperHashService } from '../../helper/services';
import { UserRepository } from '@app/common/modules/user/repositories';
import { UserSessionRepository } from '@app/common/modules/session/repositories/session.repository';
import { UserSessionEntity } from '@app/common/modules/session/entities';
import { UserRepository } from '../../../../common/src/modules/user/repositories';
import { UserSessionRepository } from '../../../../common/src/modules/session/repositories/session.repository';
import { UserSessionEntity } from '../../../../common/src/modules/session/entities';
@Injectable()
export class AuthService {

View File

@ -2,7 +2,7 @@ import { ConfigService } from '@nestjs/config';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { BadRequestException, Injectable } from '@nestjs/common';
import { UserSessionRepository } from '@app/common/modules/session/repositories/session.repository';
import { UserSessionRepository } from '../../../src/modules/session/repositories/session.repository';
import { AuthInterface } from '../interfaces/auth.interface';
@Injectable()

View File

@ -8,8 +8,8 @@ import config from './config';
import { EmailService } from './util/email.service';
@Module({
providers: [CommonService,EmailService],
exports: [CommonService, HelperModule, AuthModule,EmailService],
providers: [CommonService, EmailService],
exports: [CommonService, HelperModule, AuthModule, EmailService],
imports: [
ConfigModule.forRoot({
load: config,

View File

@ -5,7 +5,7 @@ export default registerAs(
(): Record<string, any> => ({
SMTP_HOST: process.env.SMTP_HOST,
SMTP_PORT: parseInt(process.env.SMTP_PORT),
SMTP_SECURE: process.env.SMTP_SECURE === 'true',
SMTP_SECURE: process.env.SMTP_SECURE === 'true',
SMTP_USER: process.env.SMTP_USER,
SMTP_PASSWORD: process.env.SMTP_PASSWORD,
}),

View File

@ -19,7 +19,7 @@ import { UserOtpEntity } from '../modules/user-otp/entities';
username: configService.get('DB_USER'),
password: configService.get('DB_PASSWORD'),
database: configService.get('DB_NAME'),
entities: [UserEntity, UserSessionEntity,UserOtpEntity],
entities: [UserEntity, UserSessionEntity, UserOtpEntity],
namingStrategy: new SnakeNamingStrategy(),
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
logging: true,
@ -32,7 +32,6 @@ import { UserOtpEntity } from '../modules/user-otp/entities';
},
continuationLocalStorage: true,
ssl: Boolean(JSON.parse(configService.get('DB_SSL'))),
}),
}),
],

View File

@ -32,7 +32,6 @@ export class SnakeNamingStrategy
firstTableName: string,
secondTableName: string,
firstPropertyName: any,
_secondPropertyName: string,
): string {
return snakeCase(
firstTableName +

View File

@ -2,7 +2,7 @@ import { UnauthorizedException } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
export class JwtAuthGuard extends AuthGuard('jwt') {
handleRequest(err, user, info) {
handleRequest(err, user) {
if (err || !user) {
throw err || new UnauthorizedException();
}

View File

@ -1 +1 @@
export * from './helper.hash.service'
export * from './helper.hash.service';

View File

@ -1 +1 @@
export * from './abstract.dto'
export * from './abstract.dto';

View File

@ -8,11 +8,11 @@ import {
} from 'typeorm';
import { AbstractDto } from '../dtos';
import { Constructor } from '@app/common/util/types';
import { Constructor } from '../../../../../common/src/util/types';
export abstract class AbstractEntity<
T extends AbstractDto = AbstractDto,
O = never
O = never,
> {
@PrimaryGeneratedColumn('increment')
@Exclude()
@ -38,7 +38,7 @@ export abstract class AbstractEntity<
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!dtoClass) {
throw new Error(
`You need to use @UseDto on class (${this.constructor.name}) be able to call toDto function`
`You need to use @UseDto on class (${this.constructor.name}) be able to call toDto function`,
);
}

View File

@ -22,5 +22,4 @@ export class SessionDto {
@IsBoolean()
@IsNotEmpty()
public isLoggedOut: boolean;
}

View File

@ -1 +1 @@
export * from './session.entity'
export * from './session.entity';

View File

@ -5,7 +5,7 @@ import { SessionDto } from '../dtos/session.dto';
@Entity({ name: 'userSession' })
export class UserSessionEntity extends AbstractEntity<SessionDto> {
@Column({
type: 'uuid',
type: 'uuid',
default: () => 'gen_random_uuid()',
nullable: false,
})

View File

@ -1 +1 @@
export * from './user-otp.dto'
export * from './user-otp.dto';

View File

@ -4,7 +4,7 @@ export class UserOtpDto {
@IsString()
@IsNotEmpty()
public uuid: string;
@IsString()
@IsNotEmpty()
public email: string;

View File

@ -1 +1 @@
export * from './user-otp.entity'
export * from './user-otp.entity';

View File

@ -1,13 +1,13 @@
import { Column, Entity } from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { UserOtpDto } from '../dtos';
import { OtpType } from '@app/common/constants/otp-type.enum';
import { OtpType } from '../../../../src/constants/otp-type.enum';
@Entity({ name: 'user-otp' })
export class UserOtpEntity extends AbstractEntity<UserOtpDto> {
@Column({
type: 'uuid',
default: () => 'gen_random_uuid()',
type: 'uuid',
default: () => 'gen_random_uuid()',
nullable: false,
})
public uuid: string;

View File

@ -5,7 +5,7 @@ import { AbstractEntity } from '../../abstract/entities/abstract.entity';
@Entity({ name: 'user' })
export class UserEntity extends AbstractEntity<UserDto> {
@Column({
type: 'uuid',
type: 'uuid',
default: () => 'gen_random_uuid()', // Use gen_random_uuid() for default value
nullable: false,
})

View File

@ -1,4 +1,4 @@
import { SetMetadata } from '@nestjs/common';
export const ResponseMessage = (message: string) =>
SetMetadata('response_message', message);
SetMetadata('response_message', message);

View File

@ -1,5 +1,5 @@
export interface Response<T> {
statusCode: number;
message: string;
data?: T;
}
statusCode: number;
message: string;
data?: T;
}

View File

@ -1,46 +1,45 @@
export type Constructor<T, Arguments extends unknown[] = undefined[]> = new (
...arguments_: Arguments
) => T;
export type Plain<T> = T;
export type Optional<T> = T | undefined;
export type Nullable<T> = T | null;
export type PathImpl<T, Key extends keyof T> = Key extends string
? T[Key] extends Record<string, any>
?
| `${Key}.${PathImpl<T[Key], Exclude<keyof T[Key], keyof any[]>> &
string}`
| `${Key}.${Exclude<keyof T[Key], keyof any[]> & string}`
...arguments_: Arguments
) => T;
export type Plain<T> = T;
export type Optional<T> = T | undefined;
export type Nullable<T> = T | null;
export type PathImpl<T, Key extends keyof T> = Key extends string
? T[Key] extends Record<string, any>
?
| `${Key}.${PathImpl<T[Key], Exclude<keyof T[Key], keyof any[]>> &
string}`
| `${Key}.${Exclude<keyof T[Key], keyof any[]> & string}`
: never
: never;
export type PathImpl2<T> = PathImpl<T, keyof T> | keyof T;
export type Path<T> = keyof T extends string
? PathImpl2<T> extends string | keyof T
? PathImpl2<T>
: keyof T
: never;
export type PathValue<
T,
P extends Path<T>,
> = P extends `${infer Key}.${infer Rest}`
? Key extends keyof T
? Rest extends Path<T[Key]>
? PathValue<T[Key], Rest>
: never
: never;
export type PathImpl2<T> = PathImpl<T, keyof T> | keyof T;
export type Path<T> = keyof T extends string
? PathImpl2<T> extends string | keyof T
? PathImpl2<T>
: keyof T
: never;
export type PathValue<
T,
P extends Path<T>
> = P extends `${infer Key}.${infer Rest}`
? Key extends keyof T
? Rest extends Path<T[Key]>
? PathValue<T[Key], Rest>
: never
: never
: P extends keyof T
: never
: P extends keyof T
? T[P]
: never;
export type KeyOfType<Entity, U> = {
[P in keyof Required<Entity>]: Required<Entity>[P] extends U
? P
: Required<Entity>[P] extends U[]
export type KeyOfType<Entity, U> = {
[P in keyof Required<Entity>]: Required<Entity>[P] extends U
? P
: Required<Entity>[P] extends U[]
? P
: never;
}[keyof Entity];
}[keyof Entity];

View File

@ -3,7 +3,7 @@ import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
export function setupSwaggerAuthentication(app: INestApplication): void {
const options = new DocumentBuilder()
.setTitle('Authentication-Service')
.setTitle('APIs Documentation')
.addBearerAuth({
type: 'http',
scheme: 'bearer',
@ -13,7 +13,7 @@ export function setupSwaggerAuthentication(app: INestApplication): void {
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api/authentication/documentation', app, document, {
SwaggerModule.setup('api', app, document, {
swaggerOptions: {
persistAuthorization: true,
},