carrito a medias
This commit is contained in:
parent
589bf2c010
commit
36dda74a58
@ -1,10 +1,10 @@
|
||||
import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common';
|
||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { CarritoService } from './carrito.service';
|
||||
import { CarritoCreateDto } from './dto/carrito-create.dto';
|
||||
import { CarritoUpdateDto } from './dto/carrito-update.dto';
|
||||
import { CarritoGetDto } from './dto/carrito-get-dto';
|
||||
import { CarritoDto } from './dto/carrito.dto'
|
||||
import {ApiOperation, ApiTags} from '@nestjs/swagger'
|
||||
import { CarritoCarritosDto } from './dto/carrito-carritos-dto';
|
||||
import { CarritoDto } from './dto/carrito.dto';
|
||||
|
||||
@Controller('carrito')
|
||||
@ApiTags('carrito')
|
||||
@ -13,18 +13,16 @@ export class CarritoController {
|
||||
|
||||
@Post()
|
||||
@ApiOperation({
|
||||
description: 'Creamos un carrito, pasandole los parametros id_tipo_carrito y id_modulo'
|
||||
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,
|
||||
);
|
||||
return this.carritoService.create(body.id_tipo_carrito, body.id_modulo);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({
|
||||
description: 'Nos trae todos los carritos'
|
||||
description: 'Nos trae todos los carritos',
|
||||
})
|
||||
get() {
|
||||
return this.carritoService.findAll();
|
||||
@ -32,7 +30,8 @@ export class CarritoController {
|
||||
|
||||
@Get('carrito')
|
||||
@ApiOperation({
|
||||
description: 'Nos trae un carrito en especifico, pasandole el parámetro id_carrito'
|
||||
description:
|
||||
'Nos trae un carrito en especifico, pasandole el parámetro id_carrito',
|
||||
})
|
||||
carrito(@Query() query: CarritoDto) {
|
||||
return this.carritoService.findById(parseInt(query.id_carrito));
|
||||
@ -40,9 +39,10 @@ export class CarritoController {
|
||||
|
||||
@Get('carritos')
|
||||
@ApiOperation({
|
||||
description: 'Nos trae todos los carritos de un módulo, con el parámetro del id_modulo'
|
||||
description:
|
||||
'Nos trae todos los carritos de un módulo, con el parámetro del id_modulo',
|
||||
})
|
||||
carritos(@Query() query: CarritoGetDto) {
|
||||
carritos(@Query() query: CarritoCarritosDto) {
|
||||
return this.carritoService.findByIdModulo(query);
|
||||
}
|
||||
|
||||
@ -51,7 +51,7 @@ export class CarritoController {
|
||||
|
||||
@Put()
|
||||
@ApiOperation({
|
||||
description: 'Actualizamos información de un carrito'
|
||||
description: 'Actualizamos información de un carrito',
|
||||
})
|
||||
update(@Body() body: CarritoUpdateDto) {
|
||||
return this.carritoService.update(body);
|
||||
|
@ -6,17 +6,18 @@ import {
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { FindOperator, Like, Repository } from 'typeorm';
|
||||
import { Carrito } from './entity/carrito.entity';
|
||||
import { Modulo } from '../modulo/entity/modulo.entity';
|
||||
import { InstitucionTipoCarrito } from '../institucion-tipo-carrito/entity/institucion-tipo-carrito.entity';
|
||||
import { ModuloService } from '../modulo/modulo.service';
|
||||
import { Modulo } from '../modulo/entity/modulo.entity';
|
||||
import { InstitucionTipoCarritoService } from '../institucion-tipo-carrito/institucion-tipo-carrito.service';
|
||||
import { ModuloService } from '../modulo/modulo.service';
|
||||
import { TipoCarrito } from 'src/institucion-tipo-carrito/entity/tipo-carrito.entity';
|
||||
|
||||
@Injectable()
|
||||
export class CarritoService {
|
||||
constructor(
|
||||
@InjectRepository(Carrito) private repository: Repository<Carrito>,
|
||||
private moduloService: ModuloService,
|
||||
private institucionTipoCarritoService: InstitucionTipoCarritoService,
|
||||
private moduloService: ModuloService,
|
||||
) {}
|
||||
|
||||
async create(id_tipo_carrito: number, id_modulo: number) {
|
||||
@ -25,28 +26,27 @@ export class CarritoService {
|
||||
await this.institucionTipoCarritoService.findByIdTipoCarito(
|
||||
id_tipo_carrito,
|
||||
);
|
||||
|
||||
let carrito = ""
|
||||
await this.repository.findAndCount({ modulo, tipoCarrito })
|
||||
.then((carritos) => {
|
||||
carrito = `C0${carritos[1] + 1}`
|
||||
})
|
||||
|
||||
const nuevoCarrito = this.repository.create({
|
||||
tipoCarrito,
|
||||
modulo,
|
||||
carrito,
|
||||
});
|
||||
let carrito = '';
|
||||
|
||||
return this.repository
|
||||
.findOne({ modulo, tipoCarrito, carrito })
|
||||
.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 modulo');
|
||||
return this.repository.save(nuevoCarrito);
|
||||
throw new ConflictException('Este carrito ya existe en este módulo.');
|
||||
return this.repository.save(
|
||||
this.repository.create({
|
||||
tipoCarrito,
|
||||
modulo,
|
||||
carrito,
|
||||
}),
|
||||
);
|
||||
})
|
||||
.then(() => ({
|
||||
message: 'Se creo correctamente el carrito en el modulo.',
|
||||
.then((_) => ({
|
||||
message: 'Se creó correctamente el carrito en el módulo.',
|
||||
}));
|
||||
}
|
||||
|
||||
@ -56,62 +56,69 @@ export class CarritoService {
|
||||
|
||||
findById(id_carrito: number) {
|
||||
return this.repository.findOne({ id_carrito }).then((carrito) => {
|
||||
if (!carrito) throw new NotFoundException('No existe este carrito');
|
||||
if (!carrito) throw new NotFoundException('No existe este carrito.');
|
||||
return carrito;
|
||||
});
|
||||
}
|
||||
|
||||
async findByIdModulo(filtros: {
|
||||
pagina: string,
|
||||
id_modulo?: string,
|
||||
id_tipo_carrito?: string,
|
||||
activo?: boolean,
|
||||
pagina: string;
|
||||
id_modulo?: string;
|
||||
id_tipo_carrito?: string;
|
||||
}) {
|
||||
const busqueda: {
|
||||
where: {
|
||||
modulo?: Modulo;
|
||||
institucionTipoCarrito?: InstitucionTipoCarrito;
|
||||
activo?: FindOperator<string>;
|
||||
}
|
||||
skip
|
||||
take
|
||||
tipoCarrito?: TipoCarrito;
|
||||
};
|
||||
skip: number;
|
||||
take: number;
|
||||
} = {
|
||||
where: {}, skip: (parseInt(filtros.pagina) - 1) * 25, take: 25
|
||||
where: {},
|
||||
skip: (parseInt(filtros.pagina) - 1) * 25,
|
||||
take: 25,
|
||||
};
|
||||
|
||||
const modulo = filtros.id_modulo
|
||||
? await this.moduloService.findById(parseInt(filtros.id_modulo))
|
||||
: null;
|
||||
const institucionTipoCarrito = filtros.id_tipo_carrito
|
||||
? await this.institucionTipoCarritoService.findById(parseInt(filtros.id_tipo_carrito))
|
||||
: null
|
||||
|
||||
if (filtros.activo) busqueda.where.activo = Like(`${filtros.activo}`);
|
||||
if (modulo) busqueda.where.modulo = modulo;
|
||||
if (institucionTipoCarrito) busqueda.where.institucionTipoCarrito = institucionTipoCarrito;
|
||||
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;
|
||||
|
||||
return this.repository.find(busqueda);
|
||||
if (modulo) busqueda.where.modulo = modulo;
|
||||
if (tipoCarrito) busqueda.where.tipoCarrito = tipoCarrito;
|
||||
|
||||
return this.repository.find(busqueda);
|
||||
}
|
||||
|
||||
async existeCarrito(id_modulo: number | Modulo, carrito: string) {
|
||||
const modulo = typeof id_modulo === 'number' ? await this.moduloService.findById(id_modulo) : id_modulo
|
||||
|
||||
return this.repository.findOne({ modulo, carrito })
|
||||
const modulo =
|
||||
typeof id_modulo === 'number'
|
||||
? await this.moduloService.findById(id_modulo)
|
||||
: id_modulo;
|
||||
|
||||
return this.repository
|
||||
.findOne({ modulo, carrito })
|
||||
.then((existeCarrito) => {
|
||||
if(existeCarrito) throw new ConflictException('Ya existe un carrito con este nombre, intenta con otro.')
|
||||
})
|
||||
if (existeCarrito)
|
||||
throw new ConflictException(
|
||||
'Ya existe un carrito con este nombre, intenta con otro.',
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
async update(attrs: Partial<Carrito>) {
|
||||
return this.findById(attrs.id_carrito)
|
||||
.then(async (carrito) => {
|
||||
if (attrs.carrito)
|
||||
await this.existeCarrito(carrito.modulo, attrs.carrito)
|
||||
await this.existeCarrito(carrito.modulo, attrs.carrito);
|
||||
Object.assign(carrito, attrs);
|
||||
return this.repository.save(carrito)
|
||||
return this.repository.save(carrito);
|
||||
})
|
||||
.then((_) => ({
|
||||
message: 'Se actualizo correctamente la información del carrito.'
|
||||
}))
|
||||
message: 'Se actualizo correctamente la información del carrito.',
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { IsBooleanString, IsNumberString, IsOptional } from 'class-validator';
|
||||
|
||||
export class CarritoGetDto {
|
||||
export class CarritoCarritosDto {
|
||||
@IsNumberString()
|
||||
pagina: string;
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { IsNumber, IsString } from 'class-validator';
|
||||
import { IsNumber } from 'class-validator';
|
||||
|
||||
export class CarritoCreateDto {
|
||||
@IsNumber()
|
||||
@ -6,8 +6,4 @@ export class CarritoCreateDto {
|
||||
|
||||
@IsNumber()
|
||||
id_modulo: number;
|
||||
|
||||
// Cambiar por IsString
|
||||
@IsString()
|
||||
carrito: string;
|
||||
}
|
||||
|
@ -72,7 +72,21 @@ export class InstitucionTipoCarritoService {
|
||||
return this.findAllByIdInstitucion(id_institucion, true);
|
||||
}
|
||||
|
||||
findById(id_institucion_tipo_carrito) {
|
||||
findTipoCarritoById(id_tipo_carrito) {
|
||||
return this.repositoryTipoCarrito
|
||||
.findOne({
|
||||
id_tipo_carrito,
|
||||
})
|
||||
.then((institucionTipoCarrito) => {
|
||||
if (!institucionTipoCarrito)
|
||||
throw new NotFoundException(
|
||||
'No existe esta institucion tipo carrito.',
|
||||
);
|
||||
return institucionTipoCarrito;
|
||||
});
|
||||
}
|
||||
|
||||
findInstitucionTipoCarritoById(id_institucion_tipo_carrito) {
|
||||
return this.repositoryInstitucionTipoCarrito
|
||||
.findOne({
|
||||
id_institucion_tipo_carrito,
|
||||
@ -97,7 +111,9 @@ export class InstitucionTipoCarritoService {
|
||||
}
|
||||
|
||||
update(attrs: Partial<InstitucionTipoCarrito>) {
|
||||
return this.findById(attrs.id_institucion_tipo_carrito)
|
||||
return this.findInstitucionTipoCarritoById(
|
||||
attrs.id_institucion_tipo_carrito,
|
||||
)
|
||||
.then((institucionTipoCarrito) => {
|
||||
Object.assign(institucionTipoCarrito, attrs);
|
||||
return this.repositoryInstitucionTipoCarrito.save(
|
||||
|
Loading…
Reference in New Issue
Block a user