1
+ var pluginManager = require ( '../../plugins/pluginManager.js' ) ,
2
+ countlyDb = pluginManager . dbConnection ( ) ;
3
+
4
+ /**
5
+ * @name recoverWidget
6
+ * @description helper method for creating feedback widget with provided _id and app_id values
7
+ * @param {String } appId - application id
8
+ * @param {String } widgetId - feedback widget id
9
+ * @return {Promise } mongo db query promise
10
+ */
11
+ const recoverWidget = ( appId , widgetId ) => {
12
+ return new Promise ( ( resolve , reject ) => {
13
+ var widget = { } ;
14
+ widget . _id = countlyDb . ObjectID ( widgetId ) ;
15
+ widget . app_id = appId ;
16
+ widget . popup_header_text = "What's your opinion about this page?" ;
17
+ widget . popup_comment_callout = "Add comment" ;
18
+ widget . popup_email_callout = "Contact me by e-mail" ;
19
+ widget . popup_button_callout = "Send feedback" ;
20
+ widget . popup_thanks_message = "Thanks!" ;
21
+ widget . trigger_position = "bright" ;
22
+ widget . trigger_bg_color = "#fff" ;
23
+ widget . trigger_font_color = "#ddd" ;
24
+ widget . trigger_button_text = "Feedback" ;
25
+ widget . target_devices = { "phone" : true , "tablet" : true , "desktop" : true } ;
26
+ widget . target_page = "selected" ;
27
+ widget . target_pages = [ "/" ] ;
28
+ // we don't want to enable widget before user customize properties
29
+ widget . is_active = "false" ;
30
+ widget . hide_sticker = false ;
31
+
32
+ countlyDb . collection ( "feedback_widgets" ) . insert ( widget , function ( err ) {
33
+ if ( ! err ) {
34
+ resolve ( true ) ;
35
+ }
36
+ else {
37
+ // for cases trying to insert widget that already exist
38
+ if ( err . code === 11000 ) {
39
+ resolve ( false ) ;
40
+ }
41
+ reject ( err . message ) ;
42
+ }
43
+ } ) ;
44
+ } ) ;
45
+ } ;
46
+
47
+ /**
48
+ * @name getFeedbackCollectionList
49
+ * @description helper method for returns list of feedback collections in countly db
50
+ * @return {Promise } mongo db query promise
51
+ */
52
+ const getFeedbackCollectionList = ( ) => {
53
+ return new Promise ( ( resolve , reject ) => {
54
+ countlyDb . collections ( function ( err , collectionList ) {
55
+ if ( ! err ) {
56
+ let feedbackCollections = [ ] ;
57
+ collectionList . forEach ( function ( col ) {
58
+ let c = col . s . name ;
59
+ if ( c . substr ( 0 , 8 ) === "feedback" && c !== "feedback_widgets" ) {
60
+ feedbackCollections . push ( c ) ;
61
+ }
62
+ } ) ;
63
+ resolve ( feedbackCollections ) ;
64
+ }
65
+ else {
66
+ reject ( err ) ;
67
+ }
68
+ } ) ;
69
+ } ) ;
70
+ } ;
71
+
72
+ /**
73
+ * @name extractWidgetIds
74
+ * @description helper method for extracting widget_ids from existing feedback data
75
+ * @param {String } collectionName - name of feedback collection for extract widget_id process
76
+ * @returns {Promise } mongo db query promise
77
+ */
78
+ const extractWidgetIds = ( collectionName ) => {
79
+ return new Promise ( ( resolve , reject ) => {
80
+ let widgets = [ ] ;
81
+ countlyDb . collection ( collectionName ) . find ( { } ) . toArray ( function ( err , feedbacks ) {
82
+ if ( ! err ) {
83
+ feedbacks . forEach ( function ( feedback ) {
84
+ if ( widgets . indexOf ( feedback . widget_id ) === - 1 ) {
85
+ widgets . push ( feedback . widget_id ) ;
86
+ }
87
+ } ) ;
88
+ resolve ( widgets ) ;
89
+ }
90
+ else {
91
+ reject ( err ) ;
92
+ }
93
+ } ) ;
94
+ } ) ;
95
+ } ;
96
+
97
+ /**
98
+ * @name asyncForeach
99
+ * @description async for each method
100
+ * @param {Array } array - object array
101
+ * @param {Function } fn - callback function for iteration
102
+ * @param {Function } atEnd - callback function for end
103
+ */
104
+ function asyncForeach ( array , fn , atEnd ) {
105
+ var at = - 1 ;
106
+ /**
107
+ * @name next
108
+ * @description manual iteration method
109
+ * @param {Boolean } shouldBreak - boolean flag
110
+ */
111
+ function next ( shouldBreak ) {
112
+ if ( shouldBreak || ++ at === array . length ) {
113
+ if ( atEnd ) {
114
+ setTimeout ( atEnd ) ;
115
+ }
116
+ }
117
+ else {
118
+ setTimeout ( fn , 0 , array [ at ] , next ) ;
119
+ }
120
+ }
121
+ next ( ) ;
122
+ }
123
+
124
+ /**
125
+ * @name fixWidgetDisappear
126
+ * @description fix widget disappear problem
127
+ * @param {Object } ob - http object provided by endpoint
128
+ */
129
+ const fixWidgetDisappear = async ( ) => {
130
+ try {
131
+ // get collection list from countly db
132
+ let feedbackCollections = await getFeedbackCollectionList ( ) ;
133
+ let widgets = [ ] ;
134
+ // extract widget id's from current feedback data
135
+ asyncForeach ( feedbackCollections , async function ( collection , done ) {
136
+ let extractedWidgetIds = await extractWidgetIds ( collection ) ;
137
+ let appId = collection . substr ( 8 ) ;
138
+ for ( let i = 0 ; i < extractedWidgetIds . length ; i ++ ) {
139
+ widgets . push ( { "app_id" : appId , _id : extractedWidgetIds [ i ] } ) ;
140
+ }
141
+ done ( ) ;
142
+ } , function ( ) {
143
+ // recover widgets with extracted widget and app_id
144
+ asyncForeach ( widgets , async function ( widget , _done ) {
145
+ await recoverWidget ( widget . app_id , widget . _id ) ;
146
+ console . log ( "Widget(" + widget . _id + ") recovered for App(" + widget . app_id + ")" ) ;
147
+ _done ( ) ;
148
+ } , function ( ) {
149
+ console . log ( "Recovered related widgets from feedback data." ) ;
150
+ } ) ;
151
+ } ) ;
152
+ }
153
+ catch ( e ) {
154
+ console . log ( e ) ;
155
+ }
156
+ } ;
157
+
158
+ fixWidgetDisappear ( ) ;
0 commit comments