Create DTO for admin registration

master
nicekid1 2 months ago
parent 8411c2a28b
commit 7240744076
  1. 6
      migrations/20250104094702-create-admin.js
  2. 6
      src/admin/admin.controller.ts
  3. 22
      src/admin/admin.service.ts
  4. 29
      src/admin/dto/create-Admin.dto.ts

@ -3,6 +3,12 @@
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('Admins', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
email: {
type: Sequelize.STRING,
allowNull: false,

@ -1,14 +1,14 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from "@nestjs/common";
import { AdminService } from "./admin.service";
import { Admin } from "./entities/admin.entity";
import { CreateAdminDto } from "./dto/create-Admin.dto";
@Controller("admin")
export class AdminController {
constructor(private readonly adminService: AdminService) {}
@Post("register")
async register(@Body() body: { email: string; password: string }): Promise<Admin> {
const { email, password } = body;
return this.adminService.register(email, password);
async register(@Body()createAdminDto:CreateAdminDto): Promise<Admin> {
return this.adminService.register(createAdminDto);
}
@Post("login")
async login(@Body() body: { email: string; password: string }): Promise<{ token: string }> {

@ -4,6 +4,7 @@ import { Admin } from "./entities/admin.entity";
import * as bcrypt from "bcrypt";
import { JwtService } from "@nestjs/jwt";
import { ConfigService } from "@nestjs/config";
import { CreateAdminDto } from "./dto/create-Admin.dto";
@Injectable()
export class AdminService {
constructor(
@ -11,31 +12,20 @@ export class AdminService {
private readonly jwtService: JwtService,
private readonly configService: ConfigService,
) {}
async register(email: string, password: string): Promise<Admin> {
async register(createAdminDto:CreateAdminDto): Promise<Admin> {
try {
if (!email) {
throw new HttpException("Email should be entered.", HttpStatus.BAD_REQUEST);
}
if (!password) {
throw new HttpException("Password should be entered.", HttpStatus.BAD_REQUEST);
}
const existingAdmin = await this.adminModel.findOne({ where: { email } });
const existingAdmin = await this.adminModel.findOne({ where: { email:createAdminDto.email } });
if (existingAdmin) {
throw new HttpException("Email is already registered.", HttpStatus.CONFLICT);
}
const hashedPassword = await bcrypt.hash(password, 10);
createAdminDto.password = await bcrypt.hash(createAdminDto.password, 10);
const admin = await this.adminModel.create({
email,
password: hashedPassword,
role: "admin",
});
const admin = await this.adminModel.create(createAdminDto);
return admin;
} catch (error) {
console.log(error)
if (error instanceof HttpException) {
throw error;
}

@ -0,0 +1,29 @@
import { IsString, IsEmail, IsEnum, IsNotEmpty, IsOptional, Matches } from "class-validator";
export class CreateAdminDto {
@IsEmail({}, { message: "Invalid email format" })
email: string;
@IsString()
@IsNotEmpty({ message: "Password is required" })
password: string;
@IsString()
@IsNotEmpty({ message: "First name is required" })
firstName: string;
@IsString()
@IsNotEmpty({ message: "Last name is required" })
lastName: string;
@IsString()
@IsNotEmpty({ message: "Username is required" })
username: string;
@IsString()
@Matches(/^[0-9]{11}$/, { message: "Phone number must be 10 digits" })
phoneNumber: string;
@IsEnum(["male", "female"], { message: "Gender must be 'male' or 'female'" })
gender: string;
}
Loading…
Cancel
Save