tuain-ecosystem-lib
Version:
Servicio de gestión mensajería instantanea de la plataforma Tuain
207 lines (181 loc) • 3.76 kB
JavaScript
const { ObjectId } = require('mongodb');
const { findByName, findById, updateObj, findByAttributes } = require('./common');
const PROCESSOR_COLLECTION = 'productProccessors';
function findProductByFamily(productFamilyId, name = null) {
if (name) {
return { productFamilyId, name };
}
return { productFamilyId };
}
function findSubProdByProd(productId, name = null) {
if (name) {
return { productId, name };
}
return { productId };
}
function addCategories(categories) {
return {
$addToSet: { categories: { $each: categories } },
};
}
function addInputVar(inputVar) {
return {
$addToSet: { inputVars: inputVar },
};
}
function removeInputVar(name) {
return { $pull: { inputVars: { name } } };
}
function addOutputVar(outputVar) {
return {
$addToSet: { outputVars: outputVar },
};
}
function removeOutputVar(name) {
return { $pull: { outputVars: { name } } };
}
function addStage(stage) {
return {
$addToSet: { stages: stage },
};
}
function removeStage(name) {
return { $pull: { stages: { name } } };
}
function addVoucherAttribute(voucherAttribute) {
return {
$addToSet: { voucherAttributes: voucherAttribute },
};
}
function removeVoucherAttribute(name) {
return { $pull: { voucherAttributes: { name } } };
}
function addProperty(property) {
return {
$addToSet: { properties: property },
};
}
function removeProperty(name) {
return { $pull: { properties: { name } } };
}
function addFlow(flow) {
return {
$addToSet: { flows: flow },
};
}
function removeFlow(name) {
return { $pull: { flows: { name } } };
}
function addInputVarOption(id, name, option) {
return [
{
_id: ObjectId(id),
inputVars: { $elemMatch: { name } },
},
{
$addToSet: { 'inputVars.$.options': option },
},
];
}
function removeInputVarOption(id, name, value) {
return [
{
_id: ObjectId(id),
inputVars: { $elemMatch: { name } },
},
{
$pull: { 'inputVars.$.options': { value } },
},
];
}
function addSendAttribute(attribute) {
return {
$addToSet: { sendAttributes: attribute },
};
}
function removeSendAttribute(properties) {
return {
$pull: { sendAttributes: properties },
};
}
function addReceiveAttribute(attribute) {
return {
$addToSet: { receiveAttributes: attribute },
};
}
function removeReceiveAttribute(properties) {
return {
$pull: { receiveAttributes: properties },
};
}
function findProduct(productId) {
const query = [
{
$match: { _id: ObjectId(productId) },
},
{
$lookup: {
from: PROCESSOR_COLLECTION,
localField: 'proccessorId',
foreignField: '_id',
as: 'processor',
},
},
{
$addFields: {
processor: { $arrayElemAt: ['$processor', 0] },
},
},
];
return query;
}
function findProductByName(name) {
const query = [
{
$match: { name },
},
{
$lookup: {
from: PROCESSOR_COLLECTION,
localField: 'proccessorId',
foreignField: '_id',
as: 'processor',
},
},
{
$addFields: {
processor: { $arrayElemAt: ['$processor', 0] },
},
},
];
return query;
}
module.exports = {
findByName,
findById,
updateObj,
findByAttributes,
findProductByFamily,
findSubProdByProd,
addCategories,
addInputVar,
removeInputVar,
addInputVarOption,
removeInputVarOption,
addOutputVar,
removeOutputVar,
addStage,
removeStage,
addVoucherAttribute,
removeVoucherAttribute,
addProperty,
removeProperty,
addFlow,
removeFlow,
addSendAttribute,
addReceiveAttribute,
removeSendAttribute,
removeReceiveAttribute,
findProduct,
findProductByName,
};