From cd3615e9aee6fbc83d6addcf9fa8322e03e94f81 Mon Sep 17 00:00:00 2001 From: Nguyen Cong Minh <59333368+nc-minh@users.noreply.github.com> Date: Sun, 19 Mar 2023 09:58:48 +0700 Subject: [PATCH] Dev - allow users outside the organization (#140) * feat/#138 (#139) * feat: allow users outside the organization --------- Co-authored-by: Loc Xuan Dao <102164071+locxuandao@users.noreply.github.com> --- src/apis/v1/auth/service.ts | 6 ++++-- src/apis/v1/exam/service.ts | 4 ++-- src/apis/v1/user/dto/UserDto.ts | 2 +- src/apis/v1/user/service.ts | 10 ++++++++++ 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/apis/v1/auth/service.ts b/src/apis/v1/auth/service.ts index f1888c9..736cafe 100644 --- a/src/apis/v1/auth/service.ts +++ b/src/apis/v1/auth/service.ts @@ -7,7 +7,7 @@ import { signAccessToken, signRefreshToken, verifyRefreshToken } from 'helpers/j import { logger } from 'utils/logger'; import JWTPayload from 'utils/types'; import { LoginDto } from './dto/LoginDto'; -import { createUser, getUserById } from '../user/service'; +import { createUser, getUserByEmail, getUserById } from '../user/service'; import { HOU_ENDPOINT, ROLES } from 'utils/constants'; import { UserDto } from '../user/dto/UserDto'; import { RefreshTokenDto } from './dto/RefreshTokenDto'; @@ -52,7 +52,9 @@ export const verifyGoogleAccessToken = async (accessToken: string) => { const userInfo: UserinfoByGoogleApiResponse = res.data; - if (!organizationValidation(userInfo.email)) { + const userExisted = await getUserByEmail(userInfo.email); + + if (!organizationValidation(userInfo.email) && !userExisted) { throw new HttpException(403, 'Does not belong to our organization', 'NOT_BELONG_TO_ORGANIZATION'); } diff --git a/src/apis/v1/exam/service.ts b/src/apis/v1/exam/service.ts index 0caaf0c..d8bcf79 100644 --- a/src/apis/v1/exam/service.ts +++ b/src/apis/v1/exam/service.ts @@ -181,8 +181,8 @@ export const deleteExam = async (id: string) => { export const getExamsBySubjectId = async (subjectId: string) => { try { const results = ExamModel.find({ is_approved: true, subject: subjectId }) - .populate('author', '-is_blocked -roles -created_at -updated_at -__v') - .populate('question', '-is_blocked -roles -created_at -updated_at -__v') + .populate('author', '-is_blocked -roles -created_at -updated_at -__v') + .populate('questions', '-is_blocked -roles -created_at -updated_at -__v') .populate('subject', '-is_deleted -created_at -updated_at -__v'); const subject = SubjectModel.findOne({ _id: subjectId }); diff --git a/src/apis/v1/user/dto/UserDto.ts b/src/apis/v1/user/dto/UserDto.ts index 2f3c487..332357b 100644 --- a/src/apis/v1/user/dto/UserDto.ts +++ b/src/apis/v1/user/dto/UserDto.ts @@ -2,7 +2,7 @@ import { IsString, IsDefined, IsBoolean, IsOptional } from 'class-validator'; export class UserDto { @IsString() - @IsDefined() + @IsOptional() fullname: string; @IsString() diff --git a/src/apis/v1/user/service.ts b/src/apis/v1/user/service.ts index f6e807f..325cce9 100644 --- a/src/apis/v1/user/service.ts +++ b/src/apis/v1/user/service.ts @@ -72,6 +72,16 @@ export const getUserById = async function (id: ObjectId) { } }; +export const getUserByEmail = async function (email: string) { + try { + const user = await UserModel.findOne({ email }); + return user; + } catch (error) { + logger.error(`Error while get user by email: ${error}`); + throw new HttpException(400, ErrorCodes.BAD_REQUEST.MESSAGE, ErrorCodes.BAD_REQUEST.CODE); + } +}; + export const updateUser = async function (input: UpdateUserDto, id: string) { try { const users = await UserModel.findOneAndUpdate(