tuain-ecosystem-lib
Version:
Servicio de gestión mensajería instantanea de la plataforma Tuain
326 lines (301 loc) • 7.49 kB
JavaScript
const { ObjectId } = require('mongodb');
const commonQueries = require('./common');
const {
thirdPartyGroups: thirdPartyGroupsColl, thirdParties: thirdPartiesColl,
thirdPartyGroupMembers: thirdPartyGroupMembersColl,
} = require('../collections');
function findThirdParty(idDocType, idDocNumber) {
return { idDocType, idDocNumber };
}
function findThirdPartyByUserId(userId) {
return { userId: ObjectId(userId) };
}
function findThirdparties(thirdPartyIds) {
const query = {};
if (Array.isArray(thirdPartyIds)) {
const thirdIds = thirdPartyIds.map(item => ObjectId(item));
Object.assign(query, { _id: { $in: thirdIds } });
}
return query;
}
function findRoleThirdParties(roleId) {
return { roles: { $elemMatch: { roleId } } };
}
function findThirdRole(thirdPartyId, roleId) {
return {
_id: ObjectId(thirdPartyId),
roles: { $elemMatch: { roleId: ObjectId(roleId) } },
};
}
function addRole(roleData) {
return { $push: { roles: { ...roleData } } };
}
function removeRole(roleId) {
return { $pull: { roles: { roleId } } };
}
function findGroup(owner, name, groupType = null) {
const queryObject = { ownerId: ObjectId(owner) };
if (groupType) {
queryObject.groupType = groupType;
}
if (name) {
queryObject.name = name;
}
return queryObject;
}
function saveGroup(groupData) {
const newGroupData = {
...groupData,
enabled: false,
creation: new Date(),
ownerId: ObjectId(groupData?.ownerId),
};
return newGroupData;
}
function updateGroup(groupData) {
const querySet = {
...groupData,
lastUpdate: new Date(),
};
if (querySet.additionalData) {
Object.entries(querySet.additionalData).forEach(([key, val]) => {
if (val === null || val === undefined) {
return;
}
if (typeof val === 'object' && Object.keys(val)?.length > 0) {
Object.entries(val).forEach(([key2, val2]) => {
querySet[`additionalData.${key}.${key2}`] = val2;
});
} else {
querySet[`additionalData.${key}`] = val;
}
});
delete querySet.additionalData;
}
return { $set: { ...querySet } };
}
function findGroupMembers(groupId) {
return { groupId: ObjectId(groupId) };
}
function findGroupMembersDetail(groupId) {
const aggregateQuery = [];
aggregateQuery.push({ $match: { groupId: ObjectId(groupId) } });
aggregateQuery.push({
$lookup: {
from: thirdPartiesColl,
localField: 'thirdPartyId',
foreignField: '_id',
as: 'agent',
},
});
aggregateQuery.push({ $project: { _id: 0, agent: 1 } });
aggregateQuery.push({ $unwind: { path: '$agent' } });
aggregateQuery.push({ $replaceRoot: { newRoot: '$agent' } });
aggregateQuery.push({
$lookup: {
from: thirdPartyGroupMembersColl,
localField: '_id',
foreignField: 'thirdPartyId',
as: 'groups',
},
});
return aggregateQuery;
}
function findOwnerGroups(ownerId, groupType = null) {
const queryObject = { ownerId: ObjectId(ownerId) };
if (groupType) {
queryObject.groupType = groupType;
}
return queryObject;
}
function findThirdPartyGroups(thirdParty) {
const thirdPartyId = ObjectId(thirdParty);
return [{
$match: { thirdPartyId },
},
{
$lookup: {
from: thirdPartyGroupsColl,
localField: 'groupId',
foreignField: '_id',
as: 'groups',
},
},
{
$project: {
groups: 1,
_id: 0,
},
},
{
$unwind: { path: '$groups' },
},
{
$project: {
_id: '$groups._id',
ownerId: '$groups.ownerId',
name: '$groups.name',
roleRelationId: '$groups.roleRelationId',
exclusive: '$groups.exclusive',
groupType: '$groups.groupType',
additionalData: '$groups.additionalData',
businessModel: '$groups.businessModel',
enabled: '$groups.enabled',
creation: '$groups.creation',
},
},
];
}
function findThirdsByPrimary(primaryThirdPartyId, roleRelationId) {
const primaryThirdParty = ObjectId(primaryThirdPartyId);
return [{
$match: { primaryThirdParty, roleRelationId },
},
{
$lookup: {
from: thirdPartiesColl,
localField: 'secondaryThirdParty',
foreignField: '_id',
as: 'thirdParty',
},
},
{
$project: {
thirdParty: 1,
_id: 0,
},
},
{
$unwind: { path: '$thirdParty' },
},
];
}
function findThirdsBySecondary(secondaryThirdPartyId, roleRelationId) {
const secondaryThirdParty = ObjectId(secondaryThirdPartyId);
return [{
$match: { secondaryThirdParty, roleRelationId },
},
{
$lookup: {
from: thirdPartiesColl,
localField: 'primaryThirdParty',
foreignField: '_id',
as: 'thirdParty',
},
},
{
$project: {
thirdParty: 1,
_id: 0,
},
},
{
$unwind: { path: '$thirdParty' },
},
];
}
function findRelatedThirdParties(roleRelationId, primaryFields) {
const primaryFieldsProjection = {};
primaryFields.forEach((field) => {
const primaryFieldName = `primaryThirdParty.${field}`;
primaryFieldsProjection[primaryFieldName] = 1;
});
return [{
$match: { roleRelationId },
},
{
$lookup: {
from: 'ThirdParties',
localField: 'secondaryThirdParty',
foreignField: '_id',
as: 'secondaryThirdParty',
},
},
{
$unwind: { path: '$secondaryThirdParty' },
},
{
$lookup: {
from: 'ThirdParties',
localField: 'primaryThirdParty',
foreignField: '_id',
as: 'primaryThirdParty',
},
},
{
$project: {
...primaryFieldsProjection,
secondaryThirdParty: 1,
_id: 0,
},
},
{
$unwind: { path: '$primaryThirdParty' },
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: ['$secondaryThirdParty', '$$ROOT'],
},
},
},
{
$project: { secondaryThirdParty: 0 },
}];
}
function findThirdGroup(thirdPartyId, groupId) {
return { thirdPartyId, groupId };
}
function findMembers(groupId) {
return { groupId };
}
function findThirdRelation(primaryThirdParty, secondaryThirdParty, roleRelationId) {
return {
primaryThirdParty: ObjectId(primaryThirdParty),
secondaryThirdParty: ObjectId(secondaryThirdParty),
roleRelationId,
};
}
function addUserReference(userName, device) {
return { $addToSet: { users: { userName, device } } };
}
function findEnabledProducts(thirdPartyId, productId, subproductId, groupId = null) {
const query = { thirdPartyId: ObjectId(thirdPartyId) };
if (productId) { query.productId = ObjectId(productId); }
if (subproductId) { query.subproductId = ObjectId(subproductId); }
if (groupId) { query.groupId = ObjectId(groupId); }
return query;
}
function findThirdPartiesInGroup(groupId, thirdPartyIds) {
const findQuery = { groupId: ObjectId(groupId) };
const queryThirdParties = thirdPartyIds.map(thrd => ObjectId(thrd));
findQuery.thirdPartyId = { $in: queryThirdParties };
return findQuery;
}
module.exports = {
...commonQueries,
findThirdParty,
findThirdPartyByUserId,
findThirdparties,
findRoleThirdParties,
findThirdRole,
addRole,
removeRole,
findGroup,
saveGroup,
updateGroup,
findGroupMembers,
findGroupMembersDetail,
findOwnerGroups,
findThirdPartyGroups,
findThirdsByPrimary,
findThirdsBySecondary,
findRelatedThirdParties,
findThirdGroup,
findMembers,
findThirdRelation,
addUserReference,
// Queries de productos y terceros
findEnabledProducts,
findThirdPartiesInGroup,
};