Skip to content

Commit

Permalink
Merge pull request #35 from Honeybrain/change-language
Browse files Browse the repository at this point in the history
✨ change-language + add authorization to headers
  • Loading branch information
valentinbreiz committed Jan 3, 2024
2 parents 32e73e8 + 4df8ae1 commit 0122dd5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion envoy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static_resources:
allow_origin_string_match:
- prefix: "*"
allow_methods: GET, PUT, DELETE, POST, OPTIONS
allow_headers: keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,custom-header-1,access_token,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout
allow_headers: authorization, keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,custom-header-1,access_token,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout
max_age: "1728000"
expose_headers: custom-header-1,access_token,grpc-status,grpc-message
http_filters:
Expand Down
8 changes: 8 additions & 0 deletions src/user/_utils/dto/response/user-language-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';

export class UserLanguageResponseDto {
@ApiProperty()
@IsString()
lan: string;
}
7 changes: 7 additions & 0 deletions src/user/_utils/user.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ message ChangeLanguageRequest {
string language = 1;
}

message UserLanguageResponse {
string lan = 1;
}

message UserRequest {}

// The UserService defines the methods that our service exposes
service User {
rpc SignIn(SignInSignUpRequest) returns (UserResponse);
Expand All @@ -66,4 +72,5 @@ service User {
rpc GetUsers(EmptyRequest) returns (GetUsersResponse);
rpc DeleteUser(EmailRequest) returns (EmptyResponse);
rpc ChangeLanguage(ChangeLanguageRequest) returns (EmptyResponse);
rpc GetUserLanguage(UserRequest) returns (UserLanguageResponse);
}
7 changes: 7 additions & 0 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { GrpcAuthGuard } from './_utils/jwt/grpc-auth.guard';
import { MetadataWithUser } from './_utils/interface/metadata-with-user.interface';
import { GetUsersListDto } from './_utils/dto/response/get-users-list.dto';
import { ActivateResponseDto } from './_utils/dto/response/activate-response.dto';
import { UserRequestDto } from './_utils/dto/request/user-request.dto';
import { UserLanguageResponseDto } from './_utils/dto/response/user-language-response.dto';

@Controller('user')
@ApiTags('User')
Expand Down Expand Up @@ -72,4 +74,9 @@ export class UserController {
changeLanguage(data: ChangeLanguageRequestDto, meta: MetadataWithUser): Promise<GetEmptyDto> {
return this.userService.changeLanguage(data.language, meta.user);
}
@UseGuards(GrpcAuthGuard)
@GrpcMethod('User', 'getUserLanguage')
getUserLanguage(data: UserRequestDto, meta: MetadataWithUser): Promise<UserLanguageResponseDto> {
return this.userService.getUserLanguage(meta.user);
}
}
11 changes: 6 additions & 5 deletions src/user/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ export class UserRepository {
.orFail(new RpcException({ code: status.NOT_FOUND, message: 'USER_NOT_FOUND' }))
.exec();

updateLanguageByUserId = (userId: Types.ObjectId, newLanguage: string): Promise<UserDocument> =>
this.model
.findByIdAndUpdate(userId, {lan: newLanguage}, {new: true})
.orFail(new RpcException('USER_NOT_FOUND'))
.exec();
updateLanguageByUserId = (userId: Types.ObjectId, newLanguage: string): Promise<UserDocument> => {
return this.model
.findByIdAndUpdate(userId, { lan: newLanguage }, {new: true})
.orFail(new RpcException('USER_NOT_FOUND'))
.exec();
}
}
5 changes: 5 additions & 0 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { MailsService } from '../mails/mails.service';
import { EmailRequestDto } from './_utils/dto/request/email-request.dto';
import { ActivateUserRequestDto } from './_utils/dto/request/activate-request.dto';
import { ActivateResponseDto } from './_utils/dto/response/activate-response.dto';
import { UserLanguageResponseDto } from './_utils/dto/response/user-language-response.dto';
import { Status } from '@grpc/grpc-js/build/src/constants';

@Injectable()
Expand Down Expand Up @@ -146,4 +147,8 @@ export class UserService implements OnModuleInit {
this.usersRepository
.updateLanguageByUserId(user._id, newLanguage)
.then(() => ({ message: 'langue modifié avec succès !' }));

async getUserLanguage(user: UserDocument): Promise<UserLanguageResponseDto> {
return { lan: user.lan };
}
}

0 comments on commit 0122dd5

Please sign in to comment.