diff --git a/migrations/20250104094702-create-admin.js b/migrations/20250104094702-create-admin.js index 4ecc42e..77c56a4 100644 --- a/migrations/20250104094702-create-admin.js +++ b/migrations/20250104094702-create-admin.js @@ -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, diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts index 36e1b43..26d8da1 100644 --- a/src/admin/admin.controller.ts +++ b/src/admin/admin.controller.ts @@ -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 { - const { email, password } = body; - return this.adminService.register(email, password); + async register(@Body()createAdminDto:CreateAdminDto): Promise { + return this.adminService.register(createAdminDto); } @Post("login") async login(@Body() body: { email: string; password: string }): Promise<{ token: string }> { diff --git a/src/admin/admin.service.ts b/src/admin/admin.service.ts index 58d427c..464127b 100644 --- a/src/admin/admin.service.ts +++ b/src/admin/admin.service.ts @@ -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 { + async register(createAdminDto:CreateAdminDto): Promise { 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; } diff --git a/src/admin/dto/create-Admin.dto.ts b/src/admin/dto/create-Admin.dto.ts new file mode 100644 index 0000000..38e4120 --- /dev/null +++ b/src/admin/dto/create-Admin.dto.ts @@ -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; +}