You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
4.0 KiB
200 lines
4.0 KiB
//Mock de base de datos
|
|
const store = require("./store");
|
|
|
|
require('dotenv').config();
|
|
const localIP = process.env.LOCAL;
|
|
|
|
function addSupply(
|
|
name,
|
|
unity,
|
|
providers,
|
|
imageSupply,
|
|
category,
|
|
description,
|
|
code,
|
|
location,
|
|
note,
|
|
quantity,
|
|
amount
|
|
) {
|
|
return new Promise((resolve, reject) => {
|
|
if (!name || !unity) {
|
|
console.error(
|
|
"[supplyController]: No se cargo correctamente la materia prima!"
|
|
);
|
|
reject("Los datos son incorrectos!");
|
|
}
|
|
|
|
imageUrl = "";
|
|
if (imageSupply) {
|
|
imageUrl =
|
|
`http://${localIP}:1997/app/images/supplies/` + imageSupply.filename;
|
|
}
|
|
|
|
const fullSupply = {
|
|
name: name,
|
|
unity: unity,
|
|
providers: providers,
|
|
code: code,
|
|
note: note,
|
|
category: category,
|
|
quantity: quantity,
|
|
location: location,
|
|
dateSupply: new Date(),
|
|
statusDB: true,
|
|
imageSupply: imageUrl,
|
|
description: description,
|
|
amount: amount
|
|
};
|
|
|
|
const fullOrder = {
|
|
idProvider: providers,
|
|
amount: amount,
|
|
quantity: quantity,
|
|
description: 'Es una orden',
|
|
date: new Date(),
|
|
statusDB: true,
|
|
};
|
|
|
|
store.add(fullSupply, fullOrder);
|
|
resolve(fullSupply);
|
|
});
|
|
}
|
|
|
|
function getSupplies(filterSupply) {
|
|
return new Promise((resolve, reject) => {
|
|
resolve(store.list(filterSupply));
|
|
});
|
|
}
|
|
|
|
function listProviders(filterSupply) {
|
|
return new Promise((resolve, reject) => {
|
|
resolve(store.listProviders(filterSupply));
|
|
})
|
|
}
|
|
|
|
|
|
function getDeletedSupplies(){
|
|
return new Promise ((resolve, reject) => {
|
|
resolve(store.getTrashSupplies());
|
|
})
|
|
}
|
|
|
|
|
|
function deleteSupply(id) {
|
|
return new Promise(async (resolve, reject) => {
|
|
if (!id) {
|
|
reject("Id inválido");
|
|
}
|
|
|
|
resolve(store.remove(id));
|
|
});
|
|
}
|
|
|
|
//udpate Supply
|
|
function update(id, name, image, description, location, category, code, providers, unity) {
|
|
return new Promise(async (resolve, reject) => {
|
|
if (!id/* || !name || !quantity || !unity || !category || !price */) {
|
|
reject("Revisar datos");
|
|
}else{
|
|
imageUrl = "";
|
|
if (image) {
|
|
imageUrl =
|
|
`http://${localIP}:1997/app/images/supplies/` + image.filename;
|
|
}
|
|
const result = await store.update(
|
|
id,
|
|
name,
|
|
imageUrl,
|
|
description,
|
|
location,
|
|
category,
|
|
code,
|
|
providers,
|
|
unity
|
|
|
|
|
|
);
|
|
|
|
resolve(result);
|
|
}
|
|
|
|
|
|
});
|
|
}
|
|
|
|
//delete supply statusDB false
|
|
function updateStatusDB(id) {
|
|
return new Promise(async (resolve, reject) => {
|
|
if (!id) {
|
|
reject("Revisar datos");
|
|
}
|
|
|
|
const result = await store.changeStatusDB(id);
|
|
resolve(result);
|
|
});
|
|
}
|
|
|
|
//assign provider
|
|
function assignProvider(id, idProvider) {
|
|
return new Promise(async (resolve, reject) => {
|
|
if (!id || !idProvider) {
|
|
reject("Revisar datos");
|
|
}
|
|
|
|
const result = await store.assignProvider(id, idProvider);
|
|
resolve(result);
|
|
});
|
|
}
|
|
|
|
//assign provider price
|
|
function assignProviderPrice(id, provider, price) {
|
|
return new Promise(async (resolve, reject) => {
|
|
if (!id || !provider || !price) {
|
|
reject("Revisar datos");
|
|
}
|
|
|
|
const result = await store.assignProviderPrice(id, provider, price);
|
|
|
|
resolve(result);
|
|
});
|
|
}
|
|
|
|
//remove provider
|
|
function removeProvider(id, idProvider) {
|
|
return new Promise(async (resolve, reject) => {
|
|
if (!id || !idProvider) {
|
|
reject("Revisar datos");
|
|
}
|
|
|
|
const result = await store.removeProvider(id, idProvider);
|
|
resolve(result);
|
|
});
|
|
}
|
|
|
|
//update note
|
|
function updateNote(id, note) {
|
|
console.log('desde controller', id, note)
|
|
return new Promise(async (resolve, reject) => {
|
|
if (!id) {
|
|
reject("revisar id");
|
|
} else {
|
|
const result = await store.updateNote(id, note);
|
|
resolve(result);
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
addSupply,
|
|
getSupplies,
|
|
update,
|
|
deleteSupply,
|
|
updateStatusDB,
|
|
assignProvider,
|
|
assignProviderPrice,
|
|
removeProvider,
|
|
getDeletedSupplies,
|
|
listProviders,
|
|
updateNote
|
|
};
|
|
|