Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adjustment user and auth module to new structure #301

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@
-->

## Jira issue(s)

<!-- https://selleolabs.atlassian.net/browse/LC-121 -->
[LC-]()

## Overview

## Screenshots

## Testing prerequisites
<!-- ## Testing prerequisites

## Testing scenarios

- [ ] A
- [ ] B
- [ ] C

### Notes
### Notes -->
5 changes: 2 additions & 3 deletions apps/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { APP_GUARD } from "@nestjs/core";
import { JwtModule } from "@nestjs/jwt";
import { ScheduleModule } from "@nestjs/schedule";

import { UsersModule } from "src/users/users.module";

import { AuthModule } from "./auth/auth.module";
import { CacheModule } from "./cache/cache.module";
import { CategoryModule } from "./category/category.module";
Expand All @@ -33,6 +31,7 @@ import * as schema from "./storage/schema";
import { StripeModule } from "./stripe/stripe.module";
import { StudentLessonProgressModule } from "./studentLessonProgress/studentLessonProgress.module";
import { TestConfigModule } from "./test-config/test-config.module";
import { UserModule } from "./user/user.module";

@Module({
imports: [
Expand Down Expand Up @@ -68,7 +67,7 @@ import { TestConfigModule } from "./test-config/test-config.module";
}),
AuthModule,
HealthModule,
UsersModule,
UserModule,
EmailModule,
TestConfigModule,
CategoryModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ import { CurrentUser } from "src/common/decorators/user.decorator";
import { RefreshTokenGuard } from "src/common/guards/refresh-token.guard";
import { commonUserSchema } from "src/common/schemas/common-user.schema";
import { UserActivityEvent } from "src/events";
import { USER_ROLES } from "src/users/schemas/user-roles";
import { USER_ROLES } from "src/user/schemas/userRoles";

import { AuthService } from "../auth.service";
import { CreateAccountBody, createAccountSchema } from "../schemas/create-account.schema";
import { type CreatePasswordBody, createPasswordSchema } from "../schemas/create-password.schema";
import { LoginBody, loginSchema } from "../schemas/login.schema";
import { AuthService } from "./auth.service";
import { CreateAccountBody, createAccountSchema } from "./schemas/create-account.schema";
import { type CreatePasswordBody, createPasswordSchema } from "./schemas/create-password.schema";
import { LoginBody, loginSchema } from "./schemas/login.schema";
import {
ForgotPasswordBody,
forgotPasswordSchema,
ResetPasswordBody,
resetPasswordSchema,
} from "../schemas/reset-password.schema";
import { TokenService } from "../token.service";
} from "./schemas/reset-password.schema";
import { TokenService } from "./token.service";

import type { Static } from "@sinclair/typebox";

Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { PassportModule } from "@nestjs/passport";

import { ResendVerificationMailCron } from "src/auth/resend-verification-mail-cron";
import { EmailModule } from "src/common/emails/emails.module";
import { UsersService } from "src/users/users.service";
import { UserService } from "src/user/user.service";

import { AuthController } from "./api/auth.controller";
import { AuthController } from "./auth.controller";
import { AuthService } from "./auth.service";
import { CreatePasswordService } from "./create-password.service";
import { ResetPasswordService } from "./reset-password.service";
Expand All @@ -18,7 +18,7 @@ import { TokenService } from "./token.service";
controllers: [AuthController],
providers: [
AuthService,
UsersService,
UserService,
TokenService,
JwtStrategy,
LocalStrategy,
Expand Down
12 changes: 6 additions & 6 deletions apps/api/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { EmailService } from "src/common/emails/emails.service";
import hashPassword from "src/common/helpers/hashPassword";

import { createTokens, credentials, resetTokens, users } from "../storage/schema";
import { UsersService } from "../users/users.service";
import { UserService } from "../user/user.service";

import { CreatePasswordService } from "./create-password.service";
import { ResetPasswordService } from "./reset-password.service";
Expand All @@ -36,7 +36,7 @@ export class AuthService {
constructor(
@Inject("DB") private readonly db: DatabasePg,
private jwtService: JwtService,
private usersService: UsersService,
private userService: UserService,
private configService: ConfigService,
private emailService: EmailService,
private createPasswordService: CreatePasswordService,
Expand Down Expand Up @@ -97,7 +97,7 @@ export class AuthService {
}

public async currentUser(id: UUIDType) {
const user = await this.usersService.getUserById(id);
const user = await this.userService.getUserById(id);

if (!user) {
throw new UnauthorizedException("User not found");
Expand All @@ -113,7 +113,7 @@ export class AuthService {
ignoreExpiration: false,
});

const user = await this.usersService.getUserById(payload.userId);
const user = await this.userService.getUserById(payload.userId);
if (!user) {
throw new UnauthorizedException("User not found");
}
Expand Down Expand Up @@ -176,7 +176,7 @@ export class AuthService {
}

public async forgotPassword(email: string) {
const user = await this.usersService.getUserByEmail(email);
const user = await this.userService.getUserByEmail(email);

if (!user) throw new BadRequestException("Email not found");

Expand Down Expand Up @@ -226,7 +226,7 @@ export class AuthService {
public async resetPassword(token: string, newPassword: string) {
const resetToken = await this.resetPasswordService.getOneByToken(token);

await this.usersService.resetPassword(resetToken.userId, newPassword);
await this.userService.resetPassword(resetToken.userId, newPassword);
await this.resetPasswordService.deleteToken(token);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import request from "supertest";

import { USER_ROLES } from "src/users/schemas/user-roles";
import { USER_ROLES } from "src/user/schemas/userRoles";

import { createE2ETest } from "../../../test/create-e2e-test";
import { createCategoryFactory } from "../../../test/factory/category.factory";
Expand Down Expand Up @@ -38,7 +38,7 @@ describe("CategoryController (e2e)", () => {
it("returns archived and createdAt equal to null", async () => {
const user = await userFactory
.withCredentials({ password })
.create({ role: USER_ROLES.student });
.create({ role: USER_ROLES.STUDENT });

const response = await request(app.getHttpServer())
.get("/api/category")
Expand All @@ -58,7 +58,7 @@ describe("CategoryController (e2e)", () => {
it("returns all filled category columns", async () => {
const user = await userFactory
.withCredentials({ password })
.create({ role: USER_ROLES.admin });
.create({ role: USER_ROLES.ADMIN });

const response = await request(app.getHttpServer())
.get("/api/category")
Expand All @@ -81,7 +81,7 @@ describe("CategoryController (e2e)", () => {
let page = 1;
const user = await userFactory
.withCredentials({ password })
.create({ role: USER_ROLES.student });
.create({ role: USER_ROLES.STUDENT });

const response = await request(app.getHttpServer())
.get(`/api/category?perPage=${perPage}&page=${page}`)
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/category/__tests__/category.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DEFAULT_PAGE_SIZE } from "src/common/pagination";
import { categories } from "src/storage/schema";
import { USER_ROLES } from "src/users/schemas/user-roles";
import { USER_ROLES } from "src/user/schemas/userRoles";
import { createUnitTest, type TestContext } from "test/create-unit-test";
import { createCategoryFactory } from "test/factory/category.factory";
import { truncateAllTables } from "test/helpers/test-helpers";
Expand All @@ -9,7 +9,7 @@ import { CategoryService } from "../category.service";

import type { CategoryQuery } from "../schemas/category.types";
import type { DatabasePg } from "src/common";
import type { UserRole } from "src/users/schemas/user-roles";
import type { UserRole } from "src/user/schemas/userRoles";

const CATEGORIES_COUNT = 20;

Expand Down Expand Up @@ -37,7 +37,7 @@ describe("CategoryService", () => {
describe("getCategories", () => {
describe("when the request specifies the pagination params", () => {
it("returns correct pagination data", async () => {
userRole = USER_ROLES.student;
userRole = USER_ROLES.STUDENT;
const perPage = 5;
const page = 2,
query = { page, perPage };
Expand Down
10 changes: 5 additions & 5 deletions apps/api/src/category/category.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import { Roles } from "src/common/decorators/roles.decorator";
import { CurrentUser } from "src/common/decorators/user.decorator";
import { RolesGuard } from "src/common/guards/roles.guard";
import { USER_ROLES, UserRole } from "src/users/schemas/user-roles";
import { USER_ROLES, UserRole } from "src/user/schemas/userRoles";

import { CategoryService } from "./category.service";
import {
Expand Down Expand Up @@ -69,7 +69,7 @@ export class CategoryController {
}

@Get(":id")
@Roles(USER_ROLES.admin)
@Roles(USER_ROLES.ADMIN)
@Validate({
response: baseResponse(categorySchema),
request: [{ type: "param", name: "id", schema: UUIDSchema }],
Expand All @@ -81,7 +81,7 @@ export class CategoryController {
}

@Post()
@Roles(USER_ROLES.admin)
@Roles(USER_ROLES.ADMIN)
@Validate({
request: [
{
Expand All @@ -100,7 +100,7 @@ export class CategoryController {
}

@Patch(":id")
@Roles(USER_ROLES.admin)
@Roles(USER_ROLES.ADMIN)
@Validate({
response: baseResponse(categorySchema),
request: [
Expand All @@ -113,7 +113,7 @@ export class CategoryController {
@Body() updateCategoryBody: CategoryUpdateBody,
@CurrentUser("role") currentUserRole: UserRole,
): Promise<BaseResponse<CategorySchema>> {
if (currentUserRole !== USER_ROLES.admin) {
if (currentUserRole !== USER_ROLES.ADMIN) {
throw new UnauthorizedException("You don't have permission to update category");
}

Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/category/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DatabasePg } from "src/common";
import { getSortOptions } from "src/common/helpers/getSortOptions";
import { addPagination, DEFAULT_PAGE_SIZE } from "src/common/pagination";
import { categories } from "src/storage/schema";
import { USER_ROLES, type UserRole } from "src/users/schemas/user-roles";
import { USER_ROLES, type UserRole } from "src/user/schemas/userRoles";

import {
type CategoryFilterSchema,
Expand Down Expand Up @@ -44,7 +44,7 @@ export class CategoryService {

const { sortOrder, sortedField } = getSortOptions(sort);

const isAdmin = userRole === USER_ROLES.admin;
const isAdmin = userRole === USER_ROLES.ADMIN;

const selectedColumns = {
id: categories.id,
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/chapter/adminChapter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class AdminChapterService {
// const { sortOrder, sortedField } = getSortOptions(sort);
// const conditions = this.getFiltersConditions(filters);

// if (query.currentUserRole === USER_ROLES.teacher && query.currentUserId) {
// if (query.currentUserRole === USER_ROLES.TEACHER && query.currentUserId) {
// conditions.push(eq(lessons.authorId, query.currentUserId));
// }

Expand Down
26 changes: 13 additions & 13 deletions apps/api/src/chapter/chapter.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { baseResponse, BaseResponse, UUIDSchema, type UUIDType } from "src/commo
import { Roles } from "src/common/decorators/roles.decorator";
import { CurrentUser } from "src/common/decorators/user.decorator";
import { RolesGuard } from "src/common/guards/roles.guard";
import { USER_ROLES } from "src/users/schemas/user-roles";
import { USER_ROLES } from "src/user/schemas/userRoles";

import { AdminChapterService } from "./adminChapter.service";
import { ChapterService } from "./chapter.service";
Expand Down Expand Up @@ -36,12 +36,12 @@ export class ChapterController {
// @CurrentUser("userId") userId: UUIDType,
// ): Promise<BaseResponse<ShowLessonResponse>> {
// return new BaseResponse(
// await this.lessonsService.getLesson(id, courseId, userId, userRole === USER_ROLES.admin),
// await this.lessonsService.getLesson(id, courseId, userId, userRole === USER_ROLES.ADMIN),
// );
// }

// @Get("lesson/:id")
// @Roles(USER_ROLES.teacher, USER_ROLES.admin)
// @Roles(USER_ROLES.TEACHER, USER_ROLES.ADMIN)
// @Validate({
// response: baseResponse(showLessonSchema),
// })
Expand All @@ -62,13 +62,13 @@ export class ChapterController {
// const data = await this.chapterService.getChapterWithDetails(
// id,
// currentUserId,
// currentUserRole === USER_ROLES.student,
// currentUserRole === USER_ROLES.STUDENT,
// );
// return new BaseResponse(data);
// }

@Post("beta-create-chapter")
@Roles(USER_ROLES.teacher, USER_ROLES.admin)
@Roles(USER_ROLES.TEACHER, USER_ROLES.ADMIN)
@Validate({
request: [
{
Expand All @@ -88,7 +88,7 @@ export class ChapterController {
}

@Patch("chapter-display-order")
@Roles(USER_ROLES.teacher, USER_ROLES.admin)
@Roles(USER_ROLES.TEACHER, USER_ROLES.ADMIN)
@Validate({
request: [
{
Expand Down Expand Up @@ -117,7 +117,7 @@ export class ChapterController {
}

// @Patch("lesson")
// @Roles(USER_ROLES.teacher, USER_ROLES.admin)
// @Roles(USER_ROLES.TEACHER, USER_ROLES.ADMIN)
// @Validate({
// request: [
// {
Expand All @@ -141,7 +141,7 @@ export class ChapterController {
// }

// @Delete(":courseId/:lessonId")
// @Roles(USER_ROLES.teacher, USER_ROLES.admin)
// @Roles(USER_ROLES.TEACHER, USER_ROLES.ADMIN)
// @Validate({
// request: [
// { type: "param", name: "courseId", schema: UUIDSchema },
Expand All @@ -159,7 +159,7 @@ export class ChapterController {
// });
// }
@Delete()
@Roles(USER_ROLES.teacher, USER_ROLES.admin)
@Roles(USER_ROLES.TEACHER, USER_ROLES.ADMIN)
@Validate({
request: [{ type: "query", name: "chapterId", schema: UUIDSchema, required: true }],
response: baseResponse(Type.Object({ message: Type.String() })),
Expand All @@ -174,7 +174,7 @@ export class ChapterController {
}

// @Post("evaluation-quiz")
// @Roles(USER_ROLES.student)
// @Roles(USER_ROLES.STUDENT)
// @Validate({
// request: [
// { type: "query", name: "courseId", schema: UUIDSchema },
Expand All @@ -194,7 +194,7 @@ export class ChapterController {
// }

// @Delete("clear-quiz-progress")
// @Roles(USER_ROLES.student)
// @Roles(USER_ROLES.STUDENT)
// @Validate({
// request: [
// { type: "query", name: "courseId", schema: UUIDSchema, required: true },
Expand All @@ -219,7 +219,7 @@ export class ChapterController {
// }

// @Get("question-options")
// @Roles(USER_ROLES.admin, USER_ROLES.teacher)
// @Roles(USER_ROLES.ADMIN, USER_ROLES.TEACHER)
// @Validate({
// request: [
// {
Expand Down Expand Up @@ -254,7 +254,7 @@ export class ChapterController {
// }

@Patch("freemium-status")
@Roles(USER_ROLES.teacher, USER_ROLES.admin)
@Roles(USER_ROLES.TEACHER, USER_ROLES.ADMIN)
@Validate({
request: [
{
Expand Down
Loading
Loading