Skip to content

Commit cd3bfcf

Browse files
committed
[FUSE-74] - Convert TCP Microservices to gRPC Microservices.
1 parent 17fb499 commit cd3bfcf

File tree

132 files changed

+5002
-3451
lines changed

Some content is hidden

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

132 files changed

+5002
-3451
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ testem.log
3838
.DS_Store
3939
Thumbs.db
4040

41-
#env files
41+
#.env files
4242
apps/weight-loss-consultant-authentication/src/assets/.env
4343
apps/weight-loss-consultant-users-mgnt-api/src/.env
4444
/apps/weight-loss-consultant-authentication/src/assets/.env
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const GRPC_AUTHENTICATION_SERVICE = 'AuthenticationService';
2+
export const EMAIL_PASSWORD_AUTHENTICATE_USER = 'Login';

apps/common/grpc-services.route.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {ClientOptions, Transport} from "@nestjs/microservices";
2+
import {resolveGRPCProtosPath} from "./utils";
3+
4+
export const GRPC_SERVICES_HOST = 'localhost';
5+
6+
export const AUTHENTICATION_GRPC_SERVICE_NAME = 'AUTHENTICATION_SERVICE';
7+
export const AUTHENTICATION_GRPC_SERVICE_PORT = '50001';
8+
export const AUTHENTICATION_GRPC_SERVICE_URL = `${GRPC_SERVICES_HOST}:${AUTHENTICATION_GRPC_SERVICE_PORT}`;
9+
export const AUTHENTICATION_GRPC_SERVICE_PACKAGE = 'authentication';
10+
export const AUTHENTICATION_GRPC_SERVICE_PROTO_PATH = resolveGRPCProtosPath('protos/authentication.proto');
11+
12+
export const USERS_MANAGEMENT_GRPC_SERVICE_NAME = 'USERS_MANAGEMENT_SERVICE';
13+
export const USERS_MANAGEMENT_GRPC_SERVICE_PORT = '50002';
14+
export const USERS_MANAGEMENT_GRPC_SERVICE_URL = `${GRPC_SERVICES_HOST}:${USERS_MANAGEMENT_GRPC_SERVICE_PORT}`;
15+
export const USERS_MANAGEMENT_GRPC_SERVICE_PACKAGE = 'users';
16+
export const USERS_MANAGEMENT_GRPC_SERVICE_PROTO_PATH = resolveGRPCProtosPath('protos/users-mgnt.proto');
17+
18+
export const AUTHENTICATION_GRPC_SERVICE: ClientOptions = {
19+
transport: Transport.GRPC,
20+
options: {
21+
url: AUTHENTICATION_GRPC_SERVICE_URL,
22+
package: AUTHENTICATION_GRPC_SERVICE_PACKAGE,
23+
protoPath: AUTHENTICATION_GRPC_SERVICE_PROTO_PATH,
24+
}
25+
};
26+
27+
export const USERS_MANAGEMENT_GRPC_SERVICE: ClientOptions = {
28+
transport: Transport.GRPC,
29+
options: {
30+
url: USERS_MANAGEMENT_GRPC_SERVICE_URL,
31+
package: USERS_MANAGEMENT_GRPC_SERVICE_PACKAGE,
32+
protoPath: USERS_MANAGEMENT_GRPC_SERVICE_PROTO_PATH,
33+
}
34+
};
35+
36+
export const GRPC_ADMIN_SERVICE = 'AdminService';
37+
export const ADMIN_SERVICE_FIND_ALL = 'FindAll';
38+
export const ADMIN_SERVICE_VIEW_DETAIL = 'ViewDetail';
39+
40+
export const GRPC_CUSTOMER_SERVICE = 'CustomerService';
41+
export const CUSTOMER_SERVICE_FIND_ALL = 'FindAll';
42+
export const CUSTOMER_SERVICE_VIEW_DETAIL = 'ViewDetail';
43+
44+
export const GRPC_TRAINER_SERVICE = 'CustomerService';
45+
export const TRAINER_SERVICE_FIND_ALL = 'FindAll';
46+
export const TRAINER_SERVICE_VIEW_DETAIL = 'ViewDetail';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {Observable} from "rxjs";
2+
3+
export type LoginRequestModel = {
4+
email: string;
5+
password: string;
6+
}
7+
8+
export type LoginResponseModel = {
9+
accessToken: string;
10+
email: string;
11+
fullname: string;
12+
profileImage: string;
13+
role: string;
14+
}
15+
16+
export type LoginResponse = {
17+
data: LoginResponseModel;
18+
}
19+
20+
export interface AuthenticationService {
21+
login(data: LoginRequestModel): Observable<LoginResponse>;
22+
};
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import {Observable} from "rxjs";
2+
import {Column, ManyToOne, PrimaryGeneratedColumn} from "typeorm";
3+
4+
export class AdminEntity {
5+
email?: string;
6+
password?: string;
7+
fullname?: string;
8+
address?: string;
9+
phone?: string;
10+
gender?: string;
11+
status?: number;
12+
profileImage?: string;
13+
dob?: number;
14+
}
15+
16+
export interface AdminService {
17+
findAll(data): Observable<AdminEntitiesResponse>;
18+
viewDetail(data: AdminEntityViewDetailRequest): Observable<AdminEntityResponse>;
19+
}
20+
21+
export interface TrainerService {
22+
findAll(data): Observable<TrainerEntitiesResponse>;
23+
viewDetail(data: TrainerEntityViewDetailRequest): Observable<TrainerEntityResponse>;
24+
}
25+
export interface CustomerService {
26+
findAll(data): Observable<CustomerEntitiesResponse>;
27+
viewDetail(data: CustomerEntityViewDetailRequest): Observable<CustomerEntityResponse>;
28+
}
29+
30+
export type AdminEntityViewDetailRequest = {
31+
email: string;
32+
}
33+
34+
export type TrainerEntityViewDetailRequest = {
35+
email: string;
36+
}
37+
38+
export type CustomerEntityViewDetailRequest = {
39+
email: string;
40+
}
41+
42+
export type AdminEntityResponse = {
43+
data: AdminEntity;
44+
}
45+
46+
export type AdminEntitiesResponse = {
47+
data: AdminEntity[];
48+
}
49+
50+
export type TrainerEntityResponse = {
51+
data: TrainerEntity;
52+
}
53+
54+
export type TrainerEntitiesResponse = {
55+
data: TrainerEntity[];
56+
}
57+
58+
59+
export type CustomerEntityResponse = {
60+
data: CustomerEntity;
61+
}
62+
63+
export type CustomerEntitiesResponse = {
64+
data: CustomerEntity[];
65+
}
66+
67+
export type CustomerEntity = {
68+
email: string;
69+
password: string;
70+
fullname: string;
71+
address: string;
72+
phone: string;
73+
gender: string;
74+
status: number;
75+
profileImage: string;
76+
dob: number;
77+
campaigns: CampaignEntity[];
78+
}
79+
80+
export type TrainerEntity = {
81+
email: string;
82+
password: string;
83+
fullname: string;
84+
address: string;
85+
phone: string;
86+
gender: string;
87+
status: number;
88+
profileImage: string;
89+
dob: number;
90+
yearOfExp: number;
91+
rating: number;
92+
packages: PackageEntity[];
93+
}
94+
95+
export type CampaignEntity = {
96+
id: number;
97+
description: string;
98+
status: number;
99+
startDate: number;
100+
endDate: number;
101+
feedback: string;
102+
customer : CustomerEntity;
103+
}
104+
105+
export type PackageEntity = {
106+
id: number;
107+
exercisePlan: string;
108+
schedule: string;
109+
price: number;
110+
status: number;
111+
dietPlan: string;
112+
trainer : TrainerEntity;
113+
}

apps/common/redis-routes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const ADMIN_SERVICE_FIND_ALL_KEY = 'FindAllAdminEntities_';
2+
export const ADMIN_SERVICE_VIEW_DETAIL_KEY = 'ViewDetailAdminEntity_';

apps/common/utils.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {firstValueFrom, from, lastValueFrom, Observable} from "rxjs";
2+
import {map} from "rxjs/operators";
3+
import {join} from "path";
4+
5+
type gRPCResponseType<T> = {
6+
data: T;
7+
};
8+
export const resolveGRPCProtosPath = (protoPath) => join(process.cwd(), protoPath);
9+
10+
export const constructGRPCResponse = (data: Promise<any> | Observable<any>): Observable<gRPCResponseType<any>> => {
11+
if (data instanceof Observable) {
12+
return data.pipe(map((resp) => {
13+
return {
14+
data: resp,
15+
}
16+
}));
17+
}
18+
return from(data)
19+
.pipe(map((resp) => {
20+
return {
21+
data: resp,
22+
}
23+
}));
24+
};
25+
26+
export const unwrapGRPCResponse = (resp: Observable<gRPCResponseType<any>>): Promise<any> => {
27+
return lastValueFrom(resp).then((res) => res.data);
28+
}
29+
30+
export const unwrapGRPCResponse$ = (resp: Observable<gRPCResponseType<any>>): Observable<any> => {
31+
return resp.pipe(map((res) => res.data));
32+
}

apps/weight-loss-consultant-authentication/src/app/controllers/__test__/app.controller.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { AppController } from '../app.controller';
2-
import { CustomerService } from '../../services/customer.service';
3-
import { AuthenticationService } from '../../services/authentication.service';
4-
import { TrainerService } from '../../services/trainer.service';
5-
import { MailService } from '../../services/mail.service';
6-
import { ResetPasswordTokenService } from '../../services/reset-password-token.service';
7-
import { AccountService } from '../../services/account.service';
8-
import { LoginResponse } from '../../models/login.res';
1+
import {AppController} from '../app.controller';
2+
import {CustomerService} from '../../services/customer.service';
3+
import {AuthenticationService} from '../../services/authentication.service';
4+
import {TrainerService} from '../../services/trainer.service';
5+
import {MailService} from '../../services/mail.service';
6+
import {ResetPasswordTokenService} from '../../services/reset-password-token.service';
7+
import {AccountService} from '../../services/account.service';
8+
import {LoginResponse} from '../../models/login.res';
99

1010
describe('The AuthenticationController', () => {
1111

apps/weight-loss-consultant-authentication/src/app/controllers/app.controller.ts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
1-
import { Controller, HttpStatus, UseFilters } from '@nestjs/common';
2-
import { AuthenticationService } from '../services/authentication.service';
3-
import { CustomerService } from '../services/customer.service';
4-
import { MailService } from '../services/mail.service';
5-
import { ResetPasswordRequestModel } from '../models/reset-password-request-model';
6-
import { generateOtp } from '../utils/otp-generator';
7-
import { ResetPasswordTokenService } from '../services/reset-password-token.service';
8-
import { ResetPasswordTokenEntity } from '../entities/reset-password-token.entity';
9-
import { v4 as uuidv4 } from 'uuid';
10-
import { ResetPasswordConfirmRequestModel } from '../models/reset-password-confirm-request-model';
11-
import { AccountDTO } from '../dtos/acount.dto';
12-
import { AccountService } from '../services/account.service';
13-
import { RESET_PASSWORD_TOKEN_EXPIRED_TIME, Status } from '../../constant';
14-
import { TrainerService } from '../services/trainer.service';
15-
import { MessagePattern, Payload, RpcException } from '@nestjs/microservices';
1+
import {ClassSerializerInterceptor, Controller, HttpStatus, UseFilters, UseInterceptors} from '@nestjs/common';
2+
import {AuthenticationService} from '../services/authentication.service';
3+
import {CustomerService} from '../services/customer.service';
4+
import {MailService} from '../services/mail.service';
5+
import {ResetPasswordRequestModel} from '../models/reset-password-request-model';
6+
import {generateOtp} from '../utils/otp-generator';
7+
import {ResetPasswordTokenService} from '../services/reset-password-token.service';
8+
import {ResetPasswordTokenEntity} from '../entities/reset-password-token.entity';
9+
import {v4 as uuidv4} from 'uuid';
10+
import {ResetPasswordConfirmRequestModel} from '../models/reset-password-confirm-request-model';
11+
import {AccountDTO} from '../dtos/acount.dto';
12+
import {AccountService} from '../services/account.service';
13+
import {RESET_PASSWORD_TOKEN_EXPIRED_TIME, Status} from '../../constant';
14+
import {TrainerService} from '../services/trainer.service';
15+
import {GrpcMethod, MessagePattern, Payload, RpcException} from '@nestjs/microservices';
1616
import {
1717
CONFIRM_CHANGE_PASSWORD,
18-
EMAIL_PASSWORD_AUTHENTICATE_USER,
1918
GOOGLE_FIREBASE_AUTHENTICATE_USER,
2019
RESET_PASSWORD
2120
} from '../../../../common/routes/authentication-routes';
22-
import { ExceptionFilter } from '../filters/rpc-exception.filter';
23-
import { LoginRequest } from '../models/login.req';
24-
import { LoginResponse } from '../models/login.res';
25-
import { RpcExceptionModel } from '../filters/rpc-exception.model';
21+
import {ExceptionFilter} from '../filters/rpc-exception.filter';
22+
import {LoginRequest} from '../models/login.req';
23+
import {RpcExceptionModel} from '../filters/rpc-exception.model';
24+
import {
25+
EMAIL_PASSWORD_AUTHENTICATE_USER,
26+
GRPC_AUTHENTICATION_SERVICE
27+
} from "../../../../common/authentication-route.grpc";
28+
import {constructGRPCResponse} from "../../../../common/utils";
2629

2730
@Controller()
31+
@UseInterceptors(ClassSerializerInterceptor)
2832
export class AppController {
2933

3034
constructor(private customerService: CustomerService,
@@ -35,10 +39,10 @@ export class AppController {
3539
private readonly accountService: AccountService) {
3640
}
3741

38-
@MessagePattern({cmd: EMAIL_PASSWORD_AUTHENTICATE_USER})
42+
@GrpcMethod(GRPC_AUTHENTICATION_SERVICE)
3943
@UseFilters(new ExceptionFilter())
40-
async login(@Payload() credential: LoginRequest): Promise<LoginResponse> {
41-
return this.authenticationService.login(credential);
44+
login(credential: LoginRequest) {
45+
return constructGRPCResponse(this.authenticationService.login(credential));
4246
}
4347

4448
@MessagePattern({cmd: GOOGLE_FIREBASE_AUTHENTICATE_USER})

apps/weight-loss-consultant-authentication/src/app/dtos/acount.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Role, Status } from '../../constant';
1+
import {Role, Status} from '../../constant';
22

33
export class AccountDTO{
44
email: string;

0 commit comments

Comments
 (0)