carrito listo
This commit is contained in:
@@ -11,8 +11,8 @@ import { InstitucionProgramaModule } from '../institucion-programa/institucion-p
|
||||
imports: [
|
||||
InstitucionModule,
|
||||
InstitucionCarreraModule,
|
||||
TypeOrmModule.forFeature([CarreraPrograma]),
|
||||
InstitucionProgramaModule,
|
||||
TypeOrmModule.forFeature([CarreraPrograma]),
|
||||
],
|
||||
controllers: [CarreraProgramaController],
|
||||
providers: [CarreraProgramaService],
|
||||
|
||||
@@ -42,7 +42,7 @@ export class CarreraProgramaService {
|
||||
}),
|
||||
);
|
||||
})
|
||||
.then(() => ({ message: 'Se creó correctamente la carrera programa.' }));
|
||||
.then((_) => ({ message: 'Se creó correctamente la carrera programa.' }));
|
||||
}
|
||||
|
||||
findById(id_carrera_programa: number) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common';
|
||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { ApiBody, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
|
||||
import { CarritoService } from './carrito.service';
|
||||
import { CarritoCreateDto } from './dto/carrito-create.dto';
|
||||
import { CarritoUpdateDto } from './dto/carrito-update.dto';
|
||||
@@ -7,52 +7,63 @@ import { CarritoCarritosDto } from './dto/carrito-carritos-dto';
|
||||
import { CarritoDto } from './dto/carrito.dto';
|
||||
|
||||
@Controller('carrito')
|
||||
// @ApiTags('carrito')
|
||||
@ApiTags('carrito')
|
||||
export class CarritoController {
|
||||
constructor(private carritoService: CarritoService) {}
|
||||
|
||||
@Post()
|
||||
// @ApiOperation({
|
||||
// description:
|
||||
// 'Creamos un carrito, pasandole los parametros id_tipo_carrito y id_modulo',
|
||||
// })
|
||||
create(@Body() body: CarritoCreateDto) {
|
||||
return this.carritoService.create(body.id_tipo_carrito, body.id_modulo);
|
||||
}
|
||||
|
||||
@Get()
|
||||
// @ApiOperation({
|
||||
// description: 'Nos trae todos los carritos',
|
||||
// })
|
||||
get() {
|
||||
return this.carritoService.findAll();
|
||||
}
|
||||
|
||||
@Get('carrito')
|
||||
// @ApiOperation({
|
||||
// description:
|
||||
// 'Nos trae un carrito en especifico, pasandole el parámetro id_carrito',
|
||||
// })
|
||||
@ApiOperation({
|
||||
description: 'Endpoint que retorna la información de un carrito.',
|
||||
})
|
||||
@ApiQuery({
|
||||
description: 'Id del carrito.',
|
||||
name: 'id_carrito',
|
||||
type: 'string',
|
||||
})
|
||||
carrito(@Query() query: CarritoDto) {
|
||||
return this.carritoService.findById(parseInt(query.id_carrito));
|
||||
}
|
||||
|
||||
@Get('carritos')
|
||||
// @ApiOperation({
|
||||
// description:
|
||||
// 'Nos trae todos los carritos de un módulo, con el parámetro del id_modulo',
|
||||
// })
|
||||
@ApiOperation({
|
||||
description:
|
||||
'Endpoint que retorna 25 carritos de un módulo dependiendo de la página en la que se encuentra el usuario y sus filtros.',
|
||||
})
|
||||
carritos(@Query() query: CarritoCarritosDto) {
|
||||
return this.carritoService.findByIdModulo(query);
|
||||
}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({
|
||||
description: 'Endpoint que crea un carrito nuevo en un módulo.',
|
||||
})
|
||||
@ApiBody({
|
||||
description: 'Todas las variables son obligatorias.',
|
||||
examples: {
|
||||
ejemplo: {
|
||||
value: { id_tipo_carrito: 1, id_modulo: 1, marca: '', modelo: '' },
|
||||
},
|
||||
},
|
||||
})
|
||||
create(@Body() body: CarritoCreateDto) {
|
||||
return this.carritoService.create(
|
||||
body.id_tipo_carrito,
|
||||
body.id_modulo,
|
||||
body.marca,
|
||||
body.modelo,
|
||||
);
|
||||
}
|
||||
|
||||
@Get('carritoInstitucion')
|
||||
@ApiOperation({
|
||||
description: 'Endpoint .',
|
||||
})
|
||||
carritosInstitucion() {}
|
||||
|
||||
@Put()
|
||||
// @ApiOperation({
|
||||
// description: 'Actualizamos información de un carrito',
|
||||
// })
|
||||
@ApiOperation({
|
||||
description: 'Endpoint .',
|
||||
})
|
||||
update(@Body() body: CarritoUpdateDto) {
|
||||
return this.carritoService.update(body);
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ import { InstitucionTipoCarritoModule } from '../institucion-tipo-carrito/instit
|
||||
import { ModuloModule } from '../modulo/modulo.module';
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([Carrito]),
|
||||
InstitucionTipoCarritoModule,
|
||||
ModuloModule,
|
||||
TypeOrmModule.forFeature([Carrito]),
|
||||
],
|
||||
controllers: [CarritoController],
|
||||
providers: [CarritoService],
|
||||
|
||||
@@ -6,11 +6,10 @@ import {
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { FindOperator, Like, Repository } from 'typeorm';
|
||||
import { Carrito } from './entity/carrito.entity';
|
||||
import { InstitucionTipoCarrito } from '../institucion-tipo-carrito/entity/institucion-tipo-carrito.entity';
|
||||
import { Modulo } from '../modulo/entity/modulo.entity';
|
||||
import { TipoCarrito } from '../institucion-tipo-carrito/entity/tipo-carrito.entity';
|
||||
import { InstitucionTipoCarritoService } from '../institucion-tipo-carrito/institucion-tipo-carrito.service';
|
||||
import { ModuloService } from '../modulo/modulo.service';
|
||||
import { TipoCarrito } from '../institucion-tipo-carrito/entity/tipo-carrito.entity';
|
||||
|
||||
@Injectable()
|
||||
export class CarritoService {
|
||||
@@ -20,40 +19,36 @@ export class CarritoService {
|
||||
private moduloService: ModuloService,
|
||||
) {}
|
||||
|
||||
async create(id_tipo_carrito: number, id_modulo: number) {
|
||||
async create(
|
||||
id_tipo_carrito: number,
|
||||
id_modulo: number,
|
||||
marca: string,
|
||||
modelo: string,
|
||||
) {
|
||||
const modulo = await this.moduloService.findById(id_modulo);
|
||||
const tipoCarrito =
|
||||
await this.institucionTipoCarritoService.findByIdTipoCarito(
|
||||
await this.institucionTipoCarritoService.findTipoCaritoById(
|
||||
id_tipo_carrito,
|
||||
);
|
||||
let carrito = '';
|
||||
|
||||
return this.repository
|
||||
.count({ modulo, tipoCarrito })
|
||||
.then((carritos) => {
|
||||
carrito = `C${carritos > 9 ? '' : '0'}${carritos + 1}`;
|
||||
return this.repository.findOne({ modulo, tipoCarrito, carrito });
|
||||
})
|
||||
.then((existeCarrito) => {
|
||||
if (existeCarrito)
|
||||
throw new ConflictException('Este carrito ya existe en este módulo.');
|
||||
return this.repository.save(
|
||||
.then((carritos) =>
|
||||
this.repository.save(
|
||||
this.repository.create({
|
||||
tipoCarrito,
|
||||
carrito: `C${carritos > 9 ? '' : '0'}${carritos + 1}`,
|
||||
marca,
|
||||
modelo,
|
||||
modulo,
|
||||
carrito,
|
||||
tipoCarrito,
|
||||
}),
|
||||
);
|
||||
})
|
||||
),
|
||||
)
|
||||
.then((_) => ({
|
||||
message: 'Se creó correctamente el carrito en el módulo.',
|
||||
}));
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return this.repository.find();
|
||||
}
|
||||
|
||||
findById(id_carrito: number) {
|
||||
return this.repository.findOne({ id_carrito }).then((carrito) => {
|
||||
if (!carrito) throw new NotFoundException('No existe este carrito.');
|
||||
@@ -63,34 +58,34 @@ export class CarritoService {
|
||||
|
||||
async findByIdModulo(filtros: {
|
||||
pagina: string;
|
||||
id_modulo?: string;
|
||||
id_modulo: string;
|
||||
activo?: string;
|
||||
carrito?: string;
|
||||
id_tipo_carrito?: string;
|
||||
}) {
|
||||
const busqueda: {
|
||||
where: {
|
||||
modulo?: Modulo;
|
||||
tipoCarrito?: TipoCarrito;
|
||||
};
|
||||
skip: number;
|
||||
take: number;
|
||||
modulo: Modulo;
|
||||
activo?: boolean;
|
||||
carrito?: FindOperator<string>;
|
||||
tipoCarrito?: TipoCarrito;
|
||||
} = {
|
||||
where: {},
|
||||
skip: (parseInt(filtros.pagina) - 1) * 25,
|
||||
take: 25,
|
||||
modulo: await this.moduloService.findById(parseInt(filtros.id_modulo)),
|
||||
};
|
||||
const modulo = filtros.id_modulo
|
||||
? await this.moduloService.findById(parseInt(filtros.id_modulo))
|
||||
: null;
|
||||
const tipoCarrito = filtros.id_tipo_carrito
|
||||
? await this.institucionTipoCarritoService.findTipoCarritoById(
|
||||
parseInt(filtros.id_tipo_carrito),
|
||||
)
|
||||
: null;
|
||||
|
||||
if (modulo) busqueda.where.modulo = modulo;
|
||||
if (tipoCarrito) busqueda.where.tipoCarrito = tipoCarrito;
|
||||
|
||||
return this.repository.find(busqueda);
|
||||
if (filtros.activo === 'true') busqueda.activo = true;
|
||||
if (filtros.activo === 'false') busqueda.activo = false;
|
||||
if (filtros.carrito) busqueda.carrito = Like(`%${filtros.carrito}%`);
|
||||
if (tipoCarrito) busqueda.tipoCarrito = tipoCarrito;
|
||||
return this.repository.findAndCount({
|
||||
where: busqueda,
|
||||
skip: (parseInt(filtros.pagina) - 1) * 25,
|
||||
take: 25,
|
||||
});
|
||||
}
|
||||
|
||||
async existeCarrito(id_modulo: number | Modulo, carrito: string) {
|
||||
@@ -109,16 +104,37 @@ export class CarritoService {
|
||||
});
|
||||
}
|
||||
|
||||
async update(attrs: Partial<Carrito>) {
|
||||
async update(attrs: {
|
||||
id_carrito: number;
|
||||
activo?: boolean;
|
||||
carrito?: string;
|
||||
id_modulo?: number;
|
||||
id_tipo_carrito?: number;
|
||||
marca?: string;
|
||||
modelo?: string;
|
||||
}) {
|
||||
return this.findById(attrs.id_carrito)
|
||||
.then(async (carrito) => {
|
||||
const modulo = attrs.id_modulo
|
||||
? await this.moduloService.findById(attrs.id_modulo)
|
||||
: null;
|
||||
const tipoCarrito = attrs.id_modulo
|
||||
? await this.institucionTipoCarritoService.findTipoCaritoById(
|
||||
attrs.id_tipo_carrito,
|
||||
)
|
||||
: null;
|
||||
|
||||
if (attrs.carrito)
|
||||
await this.existeCarrito(carrito.modulo, attrs.carrito);
|
||||
delete attrs.id_tipo_carrito;
|
||||
delete attrs.id_modulo;
|
||||
Object.assign(carrito, attrs);
|
||||
if (modulo) carrito.modulo = modulo;
|
||||
if (tipoCarrito) carrito.tipoCarrito = tipoCarrito;
|
||||
return this.repository.save(carrito);
|
||||
})
|
||||
.then((_) => ({
|
||||
message: 'Se actualizo correctamente la información del carrito.',
|
||||
message: 'Se actualizó correctamente la información del carrito.',
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,17 +4,18 @@ export class CarritoCarritosDto {
|
||||
@IsNumberString()
|
||||
pagina: string;
|
||||
|
||||
@IsNumberString()
|
||||
@IsOptional()
|
||||
id_carrito?: string;
|
||||
|
||||
@IsNumberString()
|
||||
id_tipo_carrito: string;
|
||||
|
||||
@IsNumberString()
|
||||
id_modulo: string;
|
||||
|
||||
@IsBooleanString()
|
||||
@IsOptional()
|
||||
activo?: boolean;
|
||||
activo?: string;
|
||||
|
||||
@IsNumberString()
|
||||
@IsOptional()
|
||||
id_tipo_carrito?: string;
|
||||
|
||||
@IsNumberString()
|
||||
@IsOptional()
|
||||
carrito?: string;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
import { IsNumber } from 'class-validator';
|
||||
import { IsNumber, IsString } from 'class-validator';
|
||||
|
||||
export class CarritoCreateDto {
|
||||
@IsNumber()
|
||||
id_tipo_carrito: number;
|
||||
id_modulo: number;
|
||||
|
||||
@IsNumber()
|
||||
id_modulo: number;
|
||||
id_tipo_carrito: number;
|
||||
|
||||
@IsString()
|
||||
marca: string;
|
||||
|
||||
@IsString()
|
||||
modelo: string;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,30 @@
|
||||
import { IsBoolean, IsNumber, IsOptional, IsString } from 'class-validator';
|
||||
import { IsBoolean, IsInt, IsOptional, IsString } from 'class-validator';
|
||||
|
||||
export class CarritoUpdateDto {
|
||||
// Cambiar por IsInt
|
||||
@IsNumber()
|
||||
@IsInt()
|
||||
id_carrito: number;
|
||||
|
||||
// Solo dejar cambiar el modulo y activo, es decir quita carrito y agrega id_modulo
|
||||
@IsNumber()
|
||||
@IsOptional()
|
||||
carrito?: string;
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
activo?: boolean;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
carrito?: string;
|
||||
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
id_modulo?: number;
|
||||
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
id_tipo_carrito?: number;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
marca?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
modelo?: string;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IsNumberString, } from 'class-validator';
|
||||
import { IsNumberString } from 'class-validator';
|
||||
|
||||
export class CarritoDto {
|
||||
@IsNumberString()
|
||||
|
||||
@@ -31,7 +31,9 @@ export class Carrito {
|
||||
@JoinColumn({ name: 'id_modulo' })
|
||||
modulo: Modulo;
|
||||
|
||||
@ManyToOne(() => TipoCarrito, (tipoCarrito) => tipoCarrito.carritos)
|
||||
@ManyToOne(() => TipoCarrito, (tipoCarrito) => tipoCarrito.carritos, {
|
||||
eager: true,
|
||||
})
|
||||
@JoinColumn({ name: 'id_tipo_carrito' })
|
||||
tipoCarrito: TipoCarrito;
|
||||
|
||||
|
||||
@@ -100,7 +100,7 @@ export class InstitucionTipoCarritoService {
|
||||
});
|
||||
}
|
||||
|
||||
findByIdTipoCarito(id_tipo_carrito: number) {
|
||||
findTipoCaritoById(id_tipo_carrito: number) {
|
||||
return this.repositoryTipoCarrito
|
||||
.findOne({ id_tipo_carrito })
|
||||
.then((tipoCarrito) => {
|
||||
|
||||
@@ -102,7 +102,7 @@ export class OperadorService {
|
||||
query.andWhere('tu.id_tipo_usuario = :id_tipo_usuario', {
|
||||
id_tipo_usuario: tipoUsuario.id_tipo_usuario,
|
||||
});
|
||||
return query.getMany();
|
||||
return query.getManyAndCount();
|
||||
}
|
||||
|
||||
findById(id_operador: number) {
|
||||
|
||||
@@ -127,7 +127,7 @@ export class UsuarioService {
|
||||
if (carrera) busqueda.institucionCarrera.carrera = carrera;
|
||||
if (institucion) busqueda.institucionCarrera.institucion = institucion;
|
||||
if (tipoUsuario) busqueda.tipoUsuario = tipoUsuario;
|
||||
return this.repository.find({
|
||||
return this.repository.findAndCount({
|
||||
join: {
|
||||
alias: 'u',
|
||||
innerJoinAndSelect: {
|
||||
|
||||
Reference in New Issue
Block a user