@ -1099,73 +1099,45 @@ export class LivechatRoomsRaw extends BaseRaw {
}
findRoomsWithCriteria ( { agents , roomName , departmentId , open , createdAt , closedAt , tags , customFields , options = { } } ) {
const match = {
$match : {
t : 'l' ,
} ,
const query = {
t : 'l' ,
} ;
if ( agents ) {
match . $match . $or = [ { 'servedBy._id' : { $in : agents } } , { 'servedBy.username' : { $in : agents } } ] ;
query . $or = [ { 'servedBy._id' : { $in : agents } } , { 'servedBy.username' : { $in : agents } } ] ;
}
if ( roomName ) {
match . $match . fname = new RegExp ( roomName , 'i' ) ;
query . fname = new RegExp ( roomName , 'i' ) ;
}
if ( departmentId ) {
match . $match . departmentId = departmentId ;
query . departmentId = departmentId ;
}
if ( open !== undefined ) {
match . $match . open = { $exists : open } ;
query . open = { $exists : open } ;
}
if ( createdAt ) {
match . $match . ts = { } ;
query . ts = { } ;
if ( createdAt . start ) {
match . $match . ts . $gte = new Date ( createdAt . start ) ;
query . ts . $gte = new Date ( createdAt . start ) ;
}
if ( createdAt . end ) {
match . $match . ts . $lte = new Date ( createdAt . end ) ;
query . ts . $lte = new Date ( createdAt . end ) ;
}
}
if ( closedAt ) {
match . $match . closedAt = { } ;
query . closedAt = { } ;
if ( closedAt . start ) {
match . $match . closedAt . $gte = new Date ( closedAt . start ) ;
query . closedAt . $gte = new Date ( closedAt . start ) ;
}
if ( closedAt . end ) {
match . $match . closedAt . $lte = new Date ( closedAt . end ) ;
query . closedAt . $lte = new Date ( closedAt . end ) ;
}
}
if ( tags ) {
match . $match . tags = { $in : tags } ;
query . tags = { $in : tags } ;
}
if ( customFields ) {
match . $match . $and = Object . keys ( customFields ) . map ( ( key ) => ( { [ ` livechatData. ${ key } ` ] : new RegExp ( customFields [ key ] , 'i' ) } ) ) ;
}
const sort = { $sort : options . sort || { name : 1 } } ;
const firstParams = [ match , sort ] ;
if ( options . offset ) {
firstParams . push ( { $skip : options . offset } ) ;
query . $and = Object . keys ( customFields ) . map ( ( key ) => ( { [ ` livechatData. ${ key } ` ] : new RegExp ( customFields [ key ] , 'i' ) } ) ) ;
}
if ( options . count ) {
firstParams . push ( { $limit : options . count } ) ;
}
const lookup = {
$lookup : {
from : 'rocketchat_livechat_department' ,
localField : 'departmentId' ,
foreignField : '_id' ,
as : 'department' ,
} ,
} ;
const unwind = {
$unwind : {
path : '$department' ,
preserveNullAndEmptyArrays : true ,
} ,
} ;
const params = [ ... firstParams , lookup , unwind ] ;
if ( options . fields ) {
params . push ( { $project : options . fields } ) ;
}
return this . col . aggregate ( params ) . toArray ( ) ;
return this . find ( query , { sort : options . sort || { name : 1 } , skip : options . offset , limit : options . count } ) ;
}
}