EP teacher

This commit is contained in:
IO
2024-06-16 16:51:45 -06:00
parent ed5f114e7a
commit a0b40e9677
8 changed files with 168 additions and 84 deletions
+1 -10
View File
@@ -1,4 +1,4 @@
import { IsBoolean, IsDate, IsEmail, IsEmpty, IsNotEmpty, IsNumber, IsString, Length,} from "class-validator";
import { IsBoolean, IsEmpty, IsNotEmpty, IsNumber, IsString, Length,} from "class-validator";
export class profesorDto{
@@ -89,13 +89,4 @@ export class profesorDto{
@IsBoolean()
@IsNotEmpty()
mostrar: boolean = true;
@IsDate()
@IsNotEmpty()
fecha_alta: Date;
@IsDate()
@IsNotEmpty()
fecha_actualizacion: Date;
}
+39
View File
@@ -0,0 +1,39 @@
import { IsOptional, IsString, IsInt, IsBoolean } from 'class-validator';
export class ProfesorFilterDto {
@IsOptional()
@IsString()
nombre?: string;
@IsOptional()
@IsInt()
id_categoria?: number;
/*
@IsOptional()
@IsString()
num_trabajador?: string;
*/
@IsOptional()
@IsString()
extension?: string;
@IsOptional()
@IsInt()
id_adscripcion?: number;
@IsOptional()
@IsInt()
id_edificio?: number;
@IsOptional()
@IsBoolean()
activo?: boolean;
/*
@IsOptional()
@IsBoolean()
mostrar?: boolean;
*/
}
+44 -45
View File
@@ -1,65 +1,64 @@
import { Column, Entity, PrimaryColumn, Timestamp } from "typeorm";
import { Column, Entity, PrimaryColumn } from "typeorm";
@Entity()
export class Profesor{
@PrimaryColumn({type:'int', length: 11, nullable:false})
id_profesor: number;
export class Profesor {
@PrimaryColumn({ type: 'int', nullable: false })
id_profesor: number;
@Column({ type: 'int', nullable: false })
id_usuario: number;
@Column({ type: 'int', nullable: false })
id_usuario: number;
@Column({ type: 'varchar', length: 10, nullable: true })
num_trabajador: string;
@Column({ type: 'varchar', length: 10, nullable: true })
num_trabajador: string;
@Column({ type: 'varchar', length: 80, nullable: false })
nombre: string;
@Column({ type: 'varchar', length: 80, nullable: false })
nombre: string;
@Column({ type: 'varchar', length: 10, nullable: true })
rfc: string;
@Column({ type: 'varchar', length: 10, nullable: true })
rfc: string;
@Column({ type: 'varchar', length: 3, nullable: true })
homoclave: string;
@Column({ type: 'varchar', length: 3, nullable: true })
homoclave: string;
@Column({ type: 'varchar', length: 13, nullable: true })
tel_oficina: string;
@Column({ type: 'varchar', length: 13, nullable: true })
tel_oficina: string;
@Column({ type: 'varchar', length: 10, nullable: true })
extension: string;
@Column({ type: 'varchar', length: 10, nullable: true })
extension: string;
@Column({ type: 'varchar', length: 13, nullable: true })
tel_personal: string;
@Column({ type: 'varchar', length: 13, nullable: true })
tel_personal: string;
@Column({ type: 'varchar', length: 65, nullable: true })
correo_pcp: string;
@Column({ type: 'varchar', length: 65, nullable: true })
correo_pcp: string;
@Column({ type: 'varchar', length: 65, nullable: true })
correo_per: string;
@Column({ type: 'varchar', length: 65, nullable: true })
correo_per: string;
@Column({ type: 'int', nullable: true })
id_adscripcion: number;
@Column({ type: 'int', nullable: true })
id_adscripcion: number;
@Column({ type: 'int', nullable: true })
id_categoria: number;
@Column({ type: 'int', nullable: true })
id_categoria: number;
@Column({ type: 'int', nullable: true })
id_edificio: number;
@Column({ type: 'int', nullable: true })
id_edificio: number;
@Column({ type: 'varchar', length: 256, nullable: true })
ubicacion: string;
@Column({ type: 'varchar', length: 256, nullable: true })
ubicacion: string;
@Column({ type: 'varchar', length: 150, nullable: true })
fotografia: string;
@Column({ type: 'varchar', length: 150, nullable: true })
fotografia: string;
@Column({ type: 'boolean', nullable: false, default: false })
activo: boolean;
@Column({ type: 'boolean', nullable: false, default: false })
activo: boolean;
@Column({ type: 'boolean', nullable: false, default: true })
mostrar: boolean;
@Column({ type: 'boolean', nullable: false, default: true })
mostrar: boolean;
@Column({ type: 'datetime', nullable: false, default: () => 'CURRENT_DATETIME' })
fecha_alta: Date;
@Column({ type: 'timestamp', nullable: false, default: () => 'CURRENT_TIMESTAMP' })
fecha_actualizacion: Date;
}
@Column({ type: 'timestamp', nullable: false, default: () => 'CURRENT_TIMESTAMP' })
fecha_alta: Date;
@Column({ type: 'timestamp', nullable: false, default: () => 'CURRENT_TIMESTAMP', onUpdate: 'CURRENT_TIMESTAMP' })
fecha_actualizacion: Date;
}
+4 -4
View File
@@ -1,15 +1,15 @@
import { Test, TestingModule } from '@nestjs/testing';
import { profesorController } from './profesor.controller';
import { ProfesorController } from './profesor.controller';
describe('ProfesorController', () => {
let controller: profesorController;
let controller: ProfesorController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [profesorController],
controllers: [ProfesorController],
}).compile();
controller = module.get<profesorController>(profesorController);
controller = module.get<ProfesorController>(ProfesorController);
});
it('should be defined', () => {
+13 -7
View File
@@ -1,33 +1,39 @@
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put } from '@nestjs/common';
import { ProfesorService } from './profesor.service';
import { profesorDto } from './dto/profesorDto.dto';
import { ProfesorFilterDto } from './dto/profesorFilterDto.dto';
@Controller('profesor')
export class ProfesorController {
constructor(private profesorService: ProfesorService) {}
@Post('alta')
@Post()
async postRegister(@Body() data: profesorDto) {
return this.profesorService.register(data);
}
@Put('modificacion/:id_profesor')
@Get()
findAll() {
return this.profesorService.findAll();
}
@Put(':id_profesor')
async postModify(@Param('id_profesor', ParseIntPipe) id_profesor: number, @Body() data: profesorDto) {
return this.profesorService.modify(id_profesor, data);
}
@Delete('borrado/:id_profesor')
@Delete(':id_profesor')
async remove(@Param('id_profesor', ParseIntPipe) id_profesor: number) {
return this.profesorService.remove(id_profesor);
}
@Get('profile/:id_profesor')
@Get(':id_profesor')
async profile(@Param('id_profesor', ParseIntPipe) id_profesor: number) {
return this.profesorService.profile(id_profesor);
}
@Post('profile/filter')
async filter(@Body() data: profesorDto) {
return this.profesorService.filter(data);
@Post('filter')
async filter(@Body() filterDto: ProfesorFilterDto) {
return this.profesorService.filter(filterDto);
}
}
+54 -7
View File
@@ -3,6 +3,7 @@ import { Profesor } from './entities/profesor.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { profesorDto } from './dto/profesorDto.dto';
import { ProfesorFilterDto } from './dto/profesorFilterDto.dto';
@Injectable()
export class ProfesorService {
@@ -10,18 +11,30 @@ export class ProfesorService {
@InjectRepository(Profesor) private profesorRepository: Repository<Profesor>
) {}
//register teacher
async register(data: profesorDto) {
const { nombre } = data;
const {num_trabajador,rfc } = data;
const existingProfesor = await this.profesorRepository.findOne({ where: { nombre } });
if (existingProfesor) {
throw new HttpException('User already exist', 403);
const existrfc = await this.profesorRepository.findOne({where:{rfc}});
if(existrfc){
throw new HttpException('rfc already exist', 403);
}
const existnumber = await this.profesorRepository.findOne({where:{num_trabajador}});
if(existnumber){
throw new HttpException('number of worker already exist', 403);
}
const newProfesor = this.profesorRepository.create(data);
return this.profesorRepository.save(newProfesor);
}
//brings all teachers
async findAll(): Promise<Profesor[]> {
return this.profesorRepository.find();
}
//modify teacher
async modify(id_profesor: number, data: profesorDto) {
const profesor = await this.profesorRepository.findOne({ where: { id_profesor } });
if (!profesor) {
@@ -32,6 +45,7 @@ export class ProfesorService {
return this.profesorRepository.save(profesor);
}
//remove teacher
async remove(id_profesor: number) {
const profesor = await this.profesorRepository.findOne({ where: { id_profesor } });
if (!profesor) {
@@ -42,6 +56,7 @@ export class ProfesorService {
return { message: 'Profesor removed successfully' };
}
//brings teachers by id
async profile(id_profesor: number) {
const profesor = await this.profesorRepository.findOne({ where: { id_profesor } });
if (!profesor) {
@@ -51,13 +66,45 @@ export class ProfesorService {
return profesor;
}
async filter(data: profesorDto) {
//brings teachers by filter
async filter(filterDto: ProfesorFilterDto): Promise<Profesor[]> {
const query = this.profesorRepository.createQueryBuilder('profesor');
if (data.nombre) {
query.andWhere('profesor.nombre = :nombre', { nombre: data.nombre });
if (filterDto.nombre) {
query.andWhere('profesor.nombre like :nombre', { nombre: `%${filterDto.nombre}%` });
}
if (filterDto.id_categoria !== undefined) {
query.andWhere('profesor.id_categoria = :id_categoria', { id_categoria: filterDto.id_categoria });
}
/*
if (filterDto.num_trabajador) {
query.andWhere('profesor.num_trabajador like :num_trabajador', { num_trabajador: `%${filterDto.num_trabajador}%` });
}
*/
if (filterDto.extension) {
query.andWhere('profesor.extension like :extension', { extension: `%${filterDto.extension}%` });
}
if (filterDto.id_adscripcion !== undefined) {
query.andWhere('profesor.id_adscripcion = :id_adscripcion', { id_adscripcion: filterDto.id_adscripcion });
}
if (filterDto.id_edificio !== undefined) {
query.andWhere('profesor.id_edificio = :id_edificio', { id_edificio: filterDto.id_edificio });
}
if (filterDto.activo !== undefined) {
query.andWhere('profesor.activo = :activo', { activo: filterDto.activo });
}
/*
if (filterDto.mostrar !== undefined) {
query.andWhere('profesor.mostrar = :mostrar', { mostrar: filterDto.mostrar });
}
*/
return query.getMany();
}
+11 -9
View File
@@ -19,36 +19,37 @@ export class AuthService {
@InjectRepository(User) private userRepository: Repository<User>
) {}
//register users
async register(data: registerDto){
const{username,password,} = data;
const{usuario,contraseña,} = data;
if(await this.usersService.findOne(username)){
if(await this.usersService.findOne(usuario)){
throw new HttpException("User already exist",403);
}
const hashedpassword = await hash(password, 10);
const hashedpassword = await hash(contraseña, 10);
data = {...data, password:hashedpassword}
data = {...data, contraseña:hashedpassword}
return this.userRepository.save(
this.userRepository.create(data)
)
}
//singin
async signIn(data: registerDto) {
const{username,password} = data;
const user = await this.usersService.findOne(username);
const{usuario,contraseña} = data;
const user = await this.usersService.findOne(usuario);
if (!user) {
throw new UnauthorizedException('Invalid name');
}
const checkPassword = await compare(password, (await user).password)
const checkPassword = await compare(contraseña, (await user).contraseña)
if (!checkPassword) {
throw new UnauthorizedException('Invalid password');
}
const payload = {
idUser: user.userId,
username: user.username
username: user.usuario
};
const token = this.jwtService.sign(payload);
@@ -74,6 +75,7 @@ export class AuthService {
}
//validate tojen of users
async validateToken(token: string): Promise<User> {
try {
const decoded = this.jwtService.verify(token, {
+2 -2
View File
@@ -4,11 +4,11 @@ export class registerDto{
@IsString()
@IsNotEmpty()
username: string;
usuario: string;
@IsString()
@IsNotEmpty()
password: string;
contraseña: string;
}