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

https://edu.duthanhduoc.com/learn/Nest.js-Super?lessonId=726 #107

Open
utterances-bot opened this issue Feb 22, 2025 · 2 comments
Open

https://edu.duthanhduoc.com/learn/Nest.js-Super?lessonId=726 #107

utterances-bot opened this issue Feb 22, 2025 · 2 comments

Comments

@utterances-bot
Copy link

Khu vực học tập | Được Edu

Được chỉ cung cấp các khóa học lập trình online chất lượng, nói không với khóa lởm!

https://edu.duthanhduoc.com/learn/Nest.js-Super?lessonId=726

Copy link

Anh ơi em có 1 câu hỏi là vd bên controller anh khai báo body: RegisterBodyDTO bên services anh sao không khai báo lun là body: RegisterBodyDTO mà sao anh lại phải body: RegisterBodyType

@duthanhduoc
Copy link
Contributor

Anh ơi em có 1 câu hỏi là vd bên controller anh khai báo body: RegisterBodyDTO bên services anh sao không khai báo lun là body: RegisterBodyDTO mà sao anh lại phải body: RegisterBodyType

2 lý do:

  • Dùng RegisterBodyDTO làm dev có cảm giác kiểu dữ liệu của body là instance class RegisterBodyDTO, nhưng thực tế nó chỉ là một object thông thường. Điều này dễ gây hiểu lầm với người mới join project. Tất nhiên nó chỉ là 1 chi tiết nhỏ thôi.

  • Khai báo thêmRegisterBodyType là một thói quen của anh sau khi khai báo schema, nó không tốn thời gian (vì Copilot tự generate ra), nhưng rất tiện nếu anh cần dùng type trong các method khác ngoài file service. Rất dễ dùng các Util như Pick, Omit, &

Đây là ví dụ file auth.repo.ts khi áp dụng Repository Pattern

import { Injectable } from '@nestjs/common'
import { RegisterBodyType, VerificationCodeType } from 'src/routes/auth/auth.model'
import { TypeOfVerificationCodeType } from 'src/shared/constants/auth.constant'
import { UserType } from 'src/shared/models/shared-user.model'
import { PrismaService } from 'src/shared/services/prisma.service'

@Injectable()
export class AuthRepository {
  constructor(private readonly prismaService: PrismaService) {}

  async createUser(
    user: Omit<RegisterBodyType, 'confirmPassword' | 'code'> & Pick<UserType, 'roleId'>,
  ): Promise<Omit<UserType, 'password' | 'totpSecret'>> {
    return this.prismaService.user.create({
      data: user,
      omit: {
        password: true,
        totpSecret: true,
      },
    })
  }

  async createVerificationCode(
    payload: Pick<VerificationCodeType, 'email' | 'type' | 'code' | 'expiresAt'>,
  ): Promise<VerificationCodeType> {
    return this.prismaService.verificationCode.upsert({
      where: {
        email: payload.email,
      },
      create: payload,
      update: {
        code: payload.code,
        expiresAt: payload.expiresAt,
      },
    })
  }

  async findUniqueVerificationCode(
    uniqueValue:
      | { email: string }
      | { id: number }
      | {
          email: string
          code: string
          type: TypeOfVerificationCodeType
        },
  ): Promise<VerificationCodeType | null> {
    return this.prismaService.verificationCode.findUnique({
      where: uniqueValue,
    })
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants