Actualización CRUD módulo facturas externas y adición de validaciones
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import { ObjectId } from "mongoose";
|
||||
import { Types } from "mongoose";
|
||||
|
||||
export interface IExternalInvoice {
|
||||
|
||||
@ -14,22 +14,32 @@ export interface IExternalInvoice {
|
||||
|
||||
departmentId:number;
|
||||
|
||||
fuelTypeId:ObjectId;
|
||||
fuelTypeId:Types.ObjectId;
|
||||
|
||||
invoiceDate:Date;
|
||||
|
||||
gallons:number;
|
||||
gallonQuantity: number;
|
||||
|
||||
pricePerGallon:number;
|
||||
|
||||
totalAmount?:number;
|
||||
|
||||
createdBy:ObjectId;
|
||||
previousMileage: number;
|
||||
|
||||
approvedBy?:ObjectId;
|
||||
currentMileage: number;
|
||||
|
||||
distanceTraveled?: number;
|
||||
|
||||
createdBy:Types.ObjectId;
|
||||
|
||||
approvedBy?:Types.ObjectId;
|
||||
|
||||
comments?:string;
|
||||
|
||||
status:string;
|
||||
odometerWorking: boolean;
|
||||
|
||||
status:
|
||||
| "PENDIENTE"
|
||||
| "APROBADO"
|
||||
| "RECHAZADO";
|
||||
}
|
||||
@ -43,7 +43,7 @@ const externalInvoiceSchema = new mongoose.Schema({
|
||||
required:true
|
||||
},
|
||||
|
||||
gallons:{
|
||||
gallonQuantity:{
|
||||
type:Number,
|
||||
required:true
|
||||
},
|
||||
@ -58,6 +58,21 @@ const externalInvoiceSchema = new mongoose.Schema({
|
||||
default:0
|
||||
},
|
||||
|
||||
previousMileage:{
|
||||
type:Number,
|
||||
required:true,
|
||||
},
|
||||
|
||||
currentMileage:{
|
||||
type:Number,
|
||||
required:true
|
||||
},
|
||||
|
||||
distanceTraveled:{
|
||||
type:Number,
|
||||
default:0
|
||||
},
|
||||
|
||||
createdBy:{
|
||||
type:mongoose.Schema.Types.ObjectId,
|
||||
ref:"User",
|
||||
@ -73,6 +88,12 @@ const externalInvoiceSchema = new mongoose.Schema({
|
||||
type:String
|
||||
},
|
||||
|
||||
odometerWorking:{
|
||||
type:Boolean,
|
||||
default:true
|
||||
|
||||
},
|
||||
|
||||
status:{
|
||||
type:String,
|
||||
enum:[
|
||||
@ -89,20 +110,5 @@ const externalInvoiceSchema = new mongoose.Schema({
|
||||
versionKey:false
|
||||
});
|
||||
|
||||
//Calcular total antes de guardar
|
||||
|
||||
externalInvoiceSchema.pre(
|
||||
"save",
|
||||
async function () {
|
||||
|
||||
this.totalAmount =
|
||||
this.gallons *
|
||||
this.pricePerGallon;
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
export default mongoose.model(
|
||||
"ExternalInvoice",
|
||||
externalInvoiceSchema
|
||||
);
|
||||
export default mongoose.model( "ExternalInvoice", externalInvoiceSchema);
|
||||
@ -12,6 +12,14 @@ const controller = new ExternalInvoiceController();
|
||||
|
||||
invoicerouter.get("/", authMiddleware, controller.getAll);
|
||||
|
||||
//Buscar por estado
|
||||
|
||||
invoicerouter.get("/status/:status", authMiddleware, controller.getByStatus);
|
||||
|
||||
//Buscar por zona
|
||||
|
||||
invoicerouter.get("/zone/:zone", authMiddleware, controller.getByZone);
|
||||
|
||||
//Obtener factura por ID
|
||||
|
||||
invoicerouter.get("/:id", authMiddleware, controller.getById);
|
||||
@ -24,14 +32,6 @@ invoicerouter.post("/", authMiddleware, controller.create);
|
||||
|
||||
invoicerouter.put("/:id", authMiddleware, controller.update);
|
||||
|
||||
//Buscar por estado
|
||||
|
||||
invoicerouter.get("/status/:status", authMiddleware, controller.getByStatus);
|
||||
|
||||
//Buscar por zona
|
||||
|
||||
invoicerouter.get("/zone/:zone", authMiddleware, controller.getByZone);
|
||||
|
||||
//Eliminar factura
|
||||
|
||||
invoicerouter.delete("/:id", authMiddleware, controller.delete);
|
||||
|
||||
@ -1,31 +1,15 @@
|
||||
import ExternalInvoiceModel from "./externalInvoice.model";
|
||||
|
||||
import externalInvoiceModel from "./externalInvoice.model";
|
||||
import { IExternalInvoice } from "./externalInvoice.interface";
|
||||
|
||||
|
||||
|
||||
export class ExternalInvoiceService {
|
||||
|
||||
//Crear factura externa
|
||||
|
||||
async create(
|
||||
data: IExternalInvoice
|
||||
) {
|
||||
|
||||
// Calcular monto total
|
||||
data.totalAmount =
|
||||
data.gallons *
|
||||
data.pricePerGallon;
|
||||
const invoice =
|
||||
new ExternalInvoiceModel(data);
|
||||
|
||||
return await invoice.save();
|
||||
|
||||
}
|
||||
|
||||
//Obtener todas las facturas
|
||||
|
||||
async getAll() {
|
||||
|
||||
return await ExternalInvoiceModel
|
||||
return await externalInvoiceModel
|
||||
.find()
|
||||
.populate("fuelTypeId")
|
||||
.populate("createdBy")
|
||||
@ -36,13 +20,13 @@ export class ExternalInvoiceService {
|
||||
|
||||
}
|
||||
|
||||
// Obtener factura por ID
|
||||
|
||||
// Obtener factura por ID
|
||||
async getById(
|
||||
id: string
|
||||
) {
|
||||
|
||||
return await ExternalInvoiceModel
|
||||
return await externalInvoiceModel
|
||||
.findById(id)
|
||||
.populate("fuelTypeId")
|
||||
.populate("createdBy")
|
||||
@ -50,27 +34,69 @@ export class ExternalInvoiceService {
|
||||
|
||||
}
|
||||
|
||||
//Actualizar factura
|
||||
|
||||
//Crear factura externa
|
||||
async create(
|
||||
data: IExternalInvoice
|
||||
) {
|
||||
|
||||
if(data.currentMileage < data.previousMileage) {
|
||||
throw new Error ("El kilometraje no puede ser menor que el anterior");
|
||||
}
|
||||
|
||||
//Validar que los galones sean mayor a 0
|
||||
if(data.gallonQuantity <= 0){
|
||||
throw new Error ("La cantidad de galones debe ser mayor que 0");
|
||||
}
|
||||
|
||||
// Calcular monto total
|
||||
data.totalAmount = data.gallonQuantity * data.pricePerGallon;
|
||||
|
||||
//Calcular distancia recorrida
|
||||
data.distanceTraveled = data.currentMileage - data.previousMileage;
|
||||
|
||||
const invoice = new externalInvoiceModel(data);
|
||||
|
||||
return await invoice.save();
|
||||
|
||||
}
|
||||
|
||||
|
||||
//Actualizar factura
|
||||
async update(
|
||||
id: string,
|
||||
data: Partial<IExternalInvoice>
|
||||
) {
|
||||
|
||||
// Recalcular total si cambian datos
|
||||
if (
|
||||
data.gallons &&
|
||||
data.pricePerGallon
|
||||
) {
|
||||
|
||||
data.totalAmount =
|
||||
data.gallons *
|
||||
data.pricePerGallon;
|
||||
const invoice = await externalInvoiceModel.findById(id);
|
||||
|
||||
if(!invoice){
|
||||
return null;
|
||||
}
|
||||
|
||||
return await ExternalInvoiceModel
|
||||
.findByIdAndUpdate(
|
||||
const previousMileage = data.previousMileage ?? invoice.previousMileage;
|
||||
|
||||
const currentMileage = data.currentMileage ?? invoice.currentMileage;
|
||||
|
||||
const gallonQuantity = data.gallonQuantity ?? invoice.gallonQuantity;
|
||||
|
||||
const pricePerGallon = data.pricePerGallon ?? invoice.pricePerGallon;
|
||||
|
||||
//Recalcular distancia recorrida
|
||||
data.distanceTraveled = currentMileage - previousMileage;
|
||||
|
||||
//Validar kilometraje
|
||||
|
||||
if(data.distanceTraveled < 0){
|
||||
throw new Error("El kilometraje actual no puede ser menor que el anterior");
|
||||
}
|
||||
|
||||
|
||||
data.totalAmount = gallonQuantity * pricePerGallon;
|
||||
|
||||
|
||||
|
||||
const updateInvoice = await externalInvoiceModel.findByIdAndUpdate(
|
||||
id,
|
||||
data,
|
||||
{
|
||||
@ -78,41 +104,43 @@ export class ExternalInvoiceService {
|
||||
}
|
||||
);
|
||||
|
||||
return updateInvoice;
|
||||
|
||||
}
|
||||
|
||||
// Buscar por estado
|
||||
|
||||
// Buscar por estado
|
||||
async getByStatus(
|
||||
status: string
|
||||
) {
|
||||
|
||||
return await ExternalInvoiceModel
|
||||
return await externalInvoiceModel
|
||||
.find({
|
||||
status
|
||||
} as any);
|
||||
|
||||
}
|
||||
|
||||
// Buscar por zona
|
||||
|
||||
// Buscar por zona
|
||||
async getByZone(
|
||||
zone: string
|
||||
) {
|
||||
|
||||
return await ExternalInvoiceModel
|
||||
return await externalInvoiceModel
|
||||
.find({
|
||||
zone
|
||||
} as any);
|
||||
|
||||
}
|
||||
|
||||
//Eliminar factura
|
||||
|
||||
//Eliminar factura
|
||||
async delete(
|
||||
id: string
|
||||
) {
|
||||
|
||||
return await ExternalInvoiceModel
|
||||
return await externalInvoiceModel
|
||||
.findByIdAndDelete(id);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user