tuain-ecosystem-lib
Version:
Servicio de gestión mensajería instantanea de la plataforma Tuain
205 lines (190 loc) • 4.89 kB
JavaScript
const { ObjectId } = require('mongodb');
const commonQueries = require('./common');
const { storeGroupMembers, storeGroups, stores: storesColl } = require('../collections');
function findStores(thirdPartyId, storeType, storeIds) {
const query = {};
if (Array.isArray(storeIds)) {
const thirdIds = storeIds.map((item) => ObjectId(`${item}`));
Object.assign(query, { _id: { $in: thirdIds } });
} else if (thirdPartyId) {
Object.assign(query, { thirdPartyId: ObjectId(thirdPartyId) });
}
if (storeType) {
query.storeType = storeType;
}
return query;
}
function findStoresWithGroups(thirdPartyId, storeType, storeIds) {
const matchQuery = {};
if (Array.isArray(storeIds)) {
const thirdIds = storeIds.map((item) => ObjectId(item));
Object.assign(matchQuery, { _id: { $in: thirdIds } });
} else if (thirdPartyId) {
Object.assign(matchQuery, { thirdPartyId: ObjectId(thirdPartyId) });
}
if (storeType) {
matchQuery.storeType = storeType;
}
const pipeline = [
{ $match: matchQuery },
{
$lookup: {
from: storeGroupMembers,
localField: '_id',
foreignField: 'storeId',
as: 'groups',
},
},
{
$project: {
'groups._id': 0,
'groups.author': 0,
'groups.creation': 0,
'groups.storeId': 0,
},
},
];
return pipeline;
}
function addStoreToThird(storeName, storeId) {
return {
$addToSet: {
stores: {
storeName,
storeId: ObjectId(storeId),
},
},
};
}
function findStoreGroupMember(storeId, groupId) {
return {
storeId: ObjectId(storeId),
groupId: ObjectId(groupId),
};
}
function findGroupMembers(groupId) {
return { groupId: ObjectId(groupId) };
}
function findGroupMembersDetail(groupId) {
const aggregateQuery = [];
aggregateQuery.push({ $match: { groupId: ObjectId(groupId) } });
aggregateQuery.push({
$lookup: {
from: storesColl,
localField: 'storeId',
foreignField: '_id',
as: 'store',
},
});
aggregateQuery.push({ $project: { _id: 0, store: 1 } });
aggregateQuery.push({ $unwind: { path: '$store' } });
aggregateQuery.push({ $replaceRoot: { newRoot: '$store' } });
return aggregateQuery;
}
function findGroupsMembers(groups) {
const groupIds = groups.map((item) => ObjectId(item));
return { groupId: { $in: groupIds } };
}
function findStoreGroups(stores) {
const storeIds = stores.map((item) => ObjectId(item));
return [
{
$match: { storeId: { $in: storeIds } },
},
{
$lookup: {
from: storeGroups,
localField: 'groupId',
foreignField: '_id',
as: 'groups',
},
},
{
$project: {
groups: 1,
storeId: 1,
_id: 0,
},
},
{
$unwind: { path: '$groups' },
},
{
$project: {
_id: '$groups._id',
ownerId: '$groups.ownerId',
name: '$groups.name',
// groupName: '$groups.groupName',
description: '$groups.description',
code: '$groups.code',
type: '$groups.type',
roleRelationId: '$groups.roleRelationId',
exclusive: '$groups.exclusive',
enabled: '$groups.enabled',
creation: '$groups.creation',
additionalData: '$groups.additionalData',
storeId: 1,
},
},
];
}
function findGroup(ownerId, name, groupType = null) {
const query = { ownerId: ObjectId(ownerId), name };
if (groupType) {
query.groupType = groupType;
}
return query;
}
function findStoreGroupsByOwner(ownerId, groupType = null) {
const queryObject = { ownerId: ObjectId(ownerId) };
if (groupType) {
queryObject.groupType = groupType;
}
return queryObject;
}
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 findStoreByUserId(userId) {
return { userId: ObjectId(userId) };
}
function findByIds(ids) {
const idArray = ids.map((id) => ObjectId(id));
return { _id: { $in: idArray } };
}
module.exports = {
...commonQueries,
// Stores
findByIds,
findStores,
findStoresWithGroups,
addStoreToThird,
findStoreGroupMember,
findGroupMembers,
findGroupMembersDetail,
findGroupsMembers,
findGroup,
findStoreGroupsByOwner,
findStoreGroups,
findStoreByUserId,
updateGroup,
};