@@ -16,13 +16,9 @@ module.exports.triggerByEvent = triggerByEvent;
1616 */
1717async function triggerByEvent ( payload ) {
1818 const allEvents = payload ?. events ;
19- const appKey = payload ?. app_key ;
20- if ( ! Array . isArray ( allEvents ) || ! appKey ) {
21- return ;
22- }
19+ const app = payload ?. app ;
2320
24- const app = await common . db . collection ( "apps" ) . findOne ( { key : appKey } ) ;
25- if ( ! app ) {
21+ if ( ! Array . isArray ( allEvents ) || ! app ) {
2622 return ;
2723 }
2824
@@ -34,22 +30,28 @@ async function triggerByEvent(payload) {
3430 ) ;
3531
3632 for ( let event of validNPSEvents ) {
37- const alert = await common . db . collection ( "alerts" ) . findOne ( {
33+ const alerts = await common . readBatcher . getMany ( "alerts" , {
3834 selectedApps : app . _id . toString ( ) ,
3935 alertDataSubType2 : event . segmentation . widget_id ,
4036 alertDataType : "nps" ,
4137 alertDataSubType : commonLib . TRIGGERED_BY_EVENT . nps ,
4238 } ) ;
43- if ( ! alert ) {
39+
40+ if ( ! alerts || ! alerts . length ) {
4441 continue ;
4542 }
4643
47- await commonLib . trigger ( { alert, app, date : new Date } , log ) ;
44+ // trigger all alerts
45+ await Promise . all ( alerts . map ( alert => commonLib . trigger ( {
46+ alert,
47+ app,
48+ date : new Date ,
49+ } , log ) ) ) ;
4850 }
4951}
5052
5153module . exports . check = async function ( { alertConfigs : alert , done, scheduledTo : date } ) {
52- const app = await common . db . collection ( "apps" ) . findOne ( { _id : ObjectId ( alert . selectedApps [ 0 ] ) } ) ;
54+ const app = await common . readBatcher . getOne ( "apps" , { _id : new ObjectId ( alert . selectedApps [ 0 ] ) } ) ;
5355 if ( ! app ) {
5456 log . e ( `App ${ alert . selectedApps [ 0 ] } couldn't be found` ) ;
5557 return done ( ) ;
@@ -62,7 +64,7 @@ module.exports.check = async function({ alertConfigs: alert, done, scheduledTo:
6264
6365 if ( compareType === commonLib . COMPARE_TYPE_ENUM . MORE_THAN ) {
6466 if ( metricValue > compareValue ) {
65- await commonLib . trigger ( { alert, app, metricValue, date } ) ;
67+ await commonLib . trigger ( { alert, app, metricValue, date } , log ) ;
6668 }
6769 }
6870 else {
@@ -78,7 +80,7 @@ module.exports.check = async function({ alertConfigs: alert, done, scheduledTo:
7880 : change <= - compareValue ;
7981
8082 if ( shouldTrigger ) {
81- await commonLib . trigger ( { alert, app, date, metricValue, metricValueBefore } ) ;
83+ await commonLib . trigger ( { alert, app, date, metricValue, metricValueBefore } , log ) ;
8284 }
8385 }
8486
@@ -91,7 +93,7 @@ module.exports.check = async function({ alertConfigs: alert, done, scheduledTo:
9193 * @param {string } nps - _id of the from feedback_widgets
9294 * @param {Date } date - date of the value you're looking for
9395 * @param {string } period - hourly|daily|monthly
94- * @param {string } score - detractor|passive|promoter
96+ * @param {string= } score - detractor|passive|promoter
9597 * @returns {Promise<number|undefined> } - a promise resolves to metric value or undefined
9698 */
9799async function getResponsesByDate ( app , nps , date , period , score ) {
@@ -132,13 +134,16 @@ async function getResponsesByDate(app, nps, date, period, score) {
132134 * Calculates the sum of all valid responses inside a nps{app_id} date record.
133135 * @param {object } scope - object scope: Daily or hourly object from db
134136 * @param {string } nps - feedback_widgets _id
135- * @param {string } score - detractor|passive|promoter
137+ * @param {string= } score - detractor|passive|promoter
136138 * @returns {number|undefined } - number of valid responses
137139 */
138140function sumOfAllResponses ( scope , nps , score ) {
139141 if ( ! scope ) {
140142 return ;
141143 }
144+ if ( ! score ) {
145+ score = "detractor|passive|promoter" ;
146+ }
142147
143148 const recordKeyReg = new RegExp ( "\\*\\*\\d{1,2}\\*\\*" + nps + "\\*\\*(" + score + ")$" ) ;
144149 let numberOfResponses ;
@@ -161,9 +166,9 @@ function sumOfAllResponses(scope, nps, score) {
161166
162167/*
163168(async function() {
164- const app = { _id: ObjectId("65c1f875a12e98a328d5eb9e "), timezone: "Europe/Istanbul" };
165- const nps = "65c383fcb46a4d172d7c5911 ";
166- const date = new Date("2024-02-07T12 :00:00.000Z");
169+ const app = {name: "test", _id: new ObjectId("6600901a71159e99a3434253 "), timezone: "Europe/Istanbul", plugins: null };
170+ const nps = "6600909ed476e1837317dc52 ";
171+ const date = new Date("2024-09-16T12 :00:00.000Z");
167172
168173 let data = await getResponsesByDate(app, nps, date, "monthly");
169174 console.log("monthly:", data);
0 commit comments