forked from Yara724/api
New module for expert field
This commit is contained in:
32
src/expert-initiated/dtos/login.dto.ts
Normal file
32
src/expert-initiated/dtos/login.dto.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import { ApiProperty } from "@nestjs/swagger";
|
||||||
|
import { IsEmail, IsNotEmpty, IsString } from "class-validator";
|
||||||
|
|
||||||
|
export class FieldExpertLoginDto {
|
||||||
|
@ApiProperty()
|
||||||
|
@IsEmail({ allow_underscores: true })
|
||||||
|
@IsString({
|
||||||
|
message: "email is not string",
|
||||||
|
context: {
|
||||||
|
errorCode: 4000,
|
||||||
|
note: "The validated email type must be string",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
readonly email: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@IsNotEmpty({
|
||||||
|
message: "password is empty",
|
||||||
|
context: {
|
||||||
|
errorCode: 4001,
|
||||||
|
note: "The validated password must not be empty",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
@IsString({
|
||||||
|
message: "password is not string",
|
||||||
|
context: {
|
||||||
|
errorCode: 4002,
|
||||||
|
note: "The validated password type must be string",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
readonly password: string;
|
||||||
|
}
|
||||||
16
src/expert-initiated/expert-initiated.controller.ts
Normal file
16
src/expert-initiated/expert-initiated.controller.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { Body, Controller, Post } from "@nestjs/common";
|
||||||
|
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
|
||||||
|
import { FieldExpertLoginDto } from "./dtos/login.dto";
|
||||||
|
import { FieldExpertService } from "./expert-initiated.service";
|
||||||
|
|
||||||
|
@Controller("expert-initiated")
|
||||||
|
@ApiTags("Expert Initiated Flow (Field Expert)")
|
||||||
|
@ApiBearerAuth()
|
||||||
|
export class FieldExpertController {
|
||||||
|
constructor(private readonly fieldExpertService: FieldExpertService) {}
|
||||||
|
|
||||||
|
@Post("login")
|
||||||
|
async login(@Body() fieldExpertLoginDto: FieldExpertLoginDto) {
|
||||||
|
return await this.fieldExpertService.login(fieldExpertLoginDto);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/expert-initiated/expert-initiated.module.ts
Normal file
25
src/expert-initiated/expert-initiated.module.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { Module } from "@nestjs/common";
|
||||||
|
import { MongooseModule } from "@nestjs/mongoose";
|
||||||
|
import { FieldExpert, FieldExpertSchema } from "./schemas/field-expert.schema";
|
||||||
|
import { FieldExpertController } from "./expert-initiated.controller";
|
||||||
|
import { FieldExpertService } from "./expert-initiated.service";
|
||||||
|
import { FieldExpertRepository } from "./repositories/field-expert.repository";
|
||||||
|
import { AuthModule } from "src/auth/auth.module";
|
||||||
|
import { JwtModule } from "@nestjs/jwt";
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [
|
||||||
|
MongooseModule.forFeature([
|
||||||
|
{
|
||||||
|
name: FieldExpert.name,
|
||||||
|
schema: FieldExpertSchema,
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
AuthModule,
|
||||||
|
JwtModule,
|
||||||
|
],
|
||||||
|
controllers: [FieldExpertController],
|
||||||
|
providers: [FieldExpertRepository, FieldExpertService],
|
||||||
|
exports: [],
|
||||||
|
})
|
||||||
|
export class ExpertInitiatedModule {}
|
||||||
32
src/expert-initiated/expert-initiated.service.ts
Normal file
32
src/expert-initiated/expert-initiated.service.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
Injectable,
|
||||||
|
NotFoundException,
|
||||||
|
} from "@nestjs/common";
|
||||||
|
import { FieldExpertRepository } from "./repositories/field-expert.repository";
|
||||||
|
import { FieldExpertLoginDto } from "./dtos/login.dto";
|
||||||
|
import { UserAuthService } from "src/auth/auth-services/user.auth.service";
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class FieldExpertService {
|
||||||
|
constructor(
|
||||||
|
private readonly fieldExpertRepository: FieldExpertRepository,
|
||||||
|
private readonly authService: UserAuthService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async login(fieldExpertLoginDto: FieldExpertLoginDto) {
|
||||||
|
const user = await this.fieldExpertRepository.retrieveByEmail(
|
||||||
|
fieldExpertLoginDto.email,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!user) throw new NotFoundException("User not found");
|
||||||
|
|
||||||
|
const validate = await this.authService.validateUser(
|
||||||
|
user.email,
|
||||||
|
user.password,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!validate)
|
||||||
|
throw new BadRequestException("Username/Password does not match");
|
||||||
|
}
|
||||||
|
}
|
||||||
22
src/expert-initiated/repositories/field-expert.repository.ts
Normal file
22
src/expert-initiated/repositories/field-expert.repository.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { Injectable } from "@nestjs/common";
|
||||||
|
import { InjectModel } from "@nestjs/mongoose";
|
||||||
|
import { Model } from "mongoose";
|
||||||
|
import { FieldExpert } from "../schemas/field-expert.schema";
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class FieldExpertRepository {
|
||||||
|
constructor(
|
||||||
|
@InjectModel(FieldExpert.name)
|
||||||
|
private readonly fieldExpertModel: Model<FieldExpert>,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async retrieveById(id: string): Promise<FieldExpert> {
|
||||||
|
return await this.fieldExpertModel.findById(id).exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
async retrieveByEmail(email: string): Promise<FieldExpert> {
|
||||||
|
return await this.fieldExpertModel.findOne({
|
||||||
|
email,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
27
src/expert-initiated/schemas/field-expert.schema.ts
Normal file
27
src/expert-initiated/schemas/field-expert.schema.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
|
||||||
|
import { HydratedDocument, Types } from "mongoose";
|
||||||
|
|
||||||
|
export type FieldExpertDocument = HydratedDocument<FieldExpert>;
|
||||||
|
|
||||||
|
@Schema({
|
||||||
|
id: true,
|
||||||
|
timestamps: true,
|
||||||
|
})
|
||||||
|
export class FieldExpert {
|
||||||
|
@Prop({ unique: true })
|
||||||
|
email: string;
|
||||||
|
|
||||||
|
@Prop({ required: true })
|
||||||
|
password: string;
|
||||||
|
|
||||||
|
@Prop({ required: true })
|
||||||
|
firstName: string;
|
||||||
|
|
||||||
|
@Prop({ required: true })
|
||||||
|
lastName: string;
|
||||||
|
|
||||||
|
@Prop({ required: true })
|
||||||
|
cellphone: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const FieldExpertSchema = SchemaFactory.createForClass(FieldExpert);
|
||||||
Reference in New Issue
Block a user