feat: implementar jornadas y validar aliases
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import Dexie, { type Table } from 'dexie';
|
||||
import type { Productor, Producto, MovimientoInventario, Venta, VentaLinea, Gasto, Ajuste, CuadreCaja } from '@toldo/core';
|
||||
import type { Productor, Producto, MovimientoInventario, Venta, VentaLinea, Gasto, Ajuste, CuadreCaja, JornadaToldo } from '@toldo/core';
|
||||
|
||||
export class ToldoDB extends Dexie {
|
||||
productores!: Table<Productor, string>;
|
||||
@@ -10,6 +10,7 @@ export class ToldoDB extends Dexie {
|
||||
gastos!: Table<Gasto, string>;
|
||||
ajustes!: Table<Ajuste, string>;
|
||||
cuadres!: Table<CuadreCaja, string>;
|
||||
jornadas!: Table<JornadaToldo, string>;
|
||||
|
||||
constructor() {
|
||||
super('ToldoConjuntoDB');
|
||||
@@ -23,6 +24,17 @@ export class ToldoDB extends Dexie {
|
||||
ajustes: 'id, fecha, productoId',
|
||||
cuadres: 'id, fecha'
|
||||
});
|
||||
this.version(2).stores({
|
||||
productores: 'id, nombre, activo',
|
||||
productos: 'id, productorId, nombrePresentacion, [productorId+nombrePresentacion]',
|
||||
movimientosInventario: 'id, fecha, tipo, productoId',
|
||||
ventas: 'id, fecha',
|
||||
ventaLineas: 'id, ventaId, productoId, formaPago',
|
||||
gastos: 'id, fecha',
|
||||
ajustes: 'id, fecha, productoId',
|
||||
cuadres: 'id, fecha',
|
||||
jornadas: 'id, fecha, estado'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
40
packages/data/src/repositories/JornadaRepository.ts
Normal file
40
packages/data/src/repositories/JornadaRepository.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { db } from '../db/schema';
|
||||
import type { JornadaToldo } from '@toldo/core';
|
||||
|
||||
export class JornadaRepository {
|
||||
async getAll(): Promise<JornadaToldo[]> {
|
||||
return db.jornadas.orderBy('fecha').reverse().toArray();
|
||||
}
|
||||
|
||||
async getAbierta(): Promise<JornadaToldo | undefined> {
|
||||
return db.jornadas.where('estado').equals('ABIERTA').first();
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<JornadaToldo | undefined> {
|
||||
return db.jornadas.get(id);
|
||||
}
|
||||
|
||||
async create(jornada: JornadaToldo): Promise<string> {
|
||||
// Solo puede haber una jornada abierta
|
||||
const abierta = await this.getAbierta();
|
||||
if (abierta) throw new Error('Ya existe una jornada abierta. Debe cerrarla primero.');
|
||||
return db.jornadas.add(jornada);
|
||||
}
|
||||
|
||||
async update(jornada: JornadaToldo): Promise<number> {
|
||||
return db.jornadas.put({ ...jornada, updatedAt: new Date() });
|
||||
}
|
||||
|
||||
async cerrar(id: string, cuadreId: string): Promise<number> {
|
||||
const jornada = await this.getById(id);
|
||||
if (!jornada) throw new Error('Jornada no encontrada');
|
||||
if (jornada.estado === 'CERRADA') throw new Error('Jornada ya cerrada');
|
||||
return db.jornadas.put({
|
||||
...jornada,
|
||||
estado: 'CERRADA',
|
||||
fechaCierre: new Date(),
|
||||
cuadreId,
|
||||
updatedAt: new Date()
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -5,3 +5,4 @@ export * from './GastoRepository';
|
||||
export * from './AjusteRepository';
|
||||
export * from './MovimientoInventarioRepository';
|
||||
export * from './CuadreRepository';
|
||||
export * from './JornadaRepository';
|
||||
|
||||
Reference in New Issue
Block a user