Skip to content

Commit

Permalink
Dev - allow users outside the organization (#140)
Browse files Browse the repository at this point in the history
* feat/#138 (#139)

* feat: allow users outside the organization

---------

Co-authored-by: Loc Xuan Dao <102164071+locxuandao@users.noreply.github.com>
  • Loading branch information
nc-minh and locxuandao authored Mar 19, 2023
1 parent b9bdd7b commit cd3615e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/apis/v1/auth/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
}

Expand Down
4 changes: 2 additions & 2 deletions src/apis/v1/exam/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
2 changes: 1 addition & 1 deletion src/apis/v1/user/dto/UserDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IsString, IsDefined, IsBoolean, IsOptional } from 'class-validator';

export class UserDto {
@IsString()
@IsDefined()
@IsOptional()
fullname: string;

@IsString()
Expand Down
10 changes: 10 additions & 0 deletions src/apis/v1/user/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit cd3615e

Please sign in to comment.