Skip to content

Commit

Permalink
improve: move counts to model
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusbsilva137 committed Nov 8, 2024
1 parent 068e021 commit e1f4463
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ export async function getContactVerificationStatistics(): Promise<IStats['contac
totalContactsWithoutChannels,
] = await Promise.all([
LivechatContacts.estimatedDocumentCount(),
LivechatContacts.countDocuments({ unknown: true }),
LivechatContacts.countUnknown(),
LivechatContacts.getStatistics().toArray(),
LivechatContacts.countDocuments({ 'channels.blocked': true }),
LivechatContacts.countDocuments({
'channels.blocked': true,
'channels': { $not: { $elemMatch: { $or: [{ blocked: false }, { blocked: { $exists: false } }] } } },
}),
LivechatContacts.countDocuments({ unknown: false }),
LivechatContacts.countDocuments({ channels: { $in: [undefined, []] } }),
LivechatContacts.countBlocked(),
LivechatContacts.countFullyBlocked(),
LivechatContacts.countVerified(),
LivechatContacts.countContactsWithoutChannels(),
]);

return {
Expand Down
23 changes: 23 additions & 0 deletions apps/meteor/server/models/raw/LivechatContacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,29 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> implements IL
});
}

countUnknown(): Promise<number> {
return this.countDocuments({ unknown: true });
}

countBlocked(): Promise<number> {
return this.countDocuments({ 'channels.blocked': true });
}

countFullyBlocked(): Promise<number> {
return this.countDocuments({
'channels.blocked': true,
'channels': { $not: { $elemMatch: { $or: [{ blocked: false }, { blocked: { $exists: false } }] } } },
});
}

countVerified(): Promise<number> {
return this.countDocuments({ unknown: false });
}

countContactsWithoutChannels(): Promise<number> {
return this.countDocuments({ channels: { $in: [undefined, []] } });
}

getStatistics(): AggregationCursor<{ totalConflicts: number; avgChannelsPerContact: number }> {
return this.col.aggregate<{ totalConflicts: number; avgChannelsPerContact: number }>(
[
Expand Down
5 changes: 5 additions & 0 deletions packages/model-typings/src/models/ILivechatContactsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@ export interface ILivechatContactsModel extends IBaseModel<ILivechatContact> {
options?: FindOptions<ILivechatContact>,
): Promise<ILivechatContact[]>;
findAllByVisitorId(visitorId: string): FindCursor<ILivechatContact>;
countUnknown(): Promise<number>;
countBlocked(): Promise<number>;
countFullyBlocked(): Promise<number>;
countVerified(): Promise<number>;
countContactsWithoutChannels(): Promise<number>;
getStatistics(): AggregationCursor<{ totalConflicts: number; avgChannelsPerContact: number }>;
}

0 comments on commit e1f4463

Please sign in to comment.