flexbiz-server 12.5.30 → 12.5.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "flexbiz-server",
3
3
  "main": "./server/app.js",
4
4
  "description": "Flexible Server",
5
- "version": "12.5.30",
5
+ "version": "12.5.32",
6
6
  "author": {
7
7
  "name": "Van Truong Pham",
8
8
  "email": "invncur@gmail.com"
@@ -1,13 +1,252 @@
1
- const async=require("async"),_=require("lodash"),redisCache=require("../libs/redis-cache"),view=async($user$$,$self$$,$rows$$,$next$$,$options$$)=>{const $model$$=$self$$.model,$name$$=$self$$.name,$listinfo_code$$=$options$$?.req?.query?.["listinfo-code"]||$options$$?.req?.query?.listinfo_code||$options$$?.req?.headers?.["listinfo-code"]||$options$$?.req?.headers?.["Listinfo-Code"];let $fields_load$$="";$options$$&&$options$$.req&&$options$$.req.query&&$options$$.req.query.fields&&($fields_load$$=
2
- $options$$.req.query.fields);let $schema_paths$$=$model$$.schema.paths;for(let $index$$=0;$index$$<$rows$$.length;$index$$++){const $item$$=$rows$$[$index$$];$item$$._id&&typeof $item$$._id!=="string"&&($item$$._id=$item$$._id.toString());delete $item$$.__v;$item$$.stt_sorted=$index$$;delete $item$$.from_cached;_.has($schema_paths$$,"exfields")&&!$item$$.exfields&&($item$$.exfields={})}let $items$$=[];$options$$&&$options$$.req&&$options$$.req.query_cache&&!$options$$.req.query.refresh_required?await new Promise($resolve$$=>
3
- {setImmediate(async()=>{let $cacheData$$=await redisCache.getCacheReport($options$$.req.user.email,($options$$.req.user.current_id_app||"system").toString(),$self$$.name,$options$$.req.query_cache,0,$options$$.req.query.page);if($cacheData$$)for(let $index$$=0;$index$$<$rows$$.length;$index$$++){const $item$$=$rows$$[$index$$];let $cacheds$$=$cacheData$$.filter($r$$=>$r$$._id===$item$$._id.toString());if($cacheds$$.length>0)for(let $cached$$ of $cacheds$$)$cached$$.from_cached=!0,$items$$.push($cached$$);
4
- else $items$$.push($item$$)}else $items$$=$rows$$;$resolve$$()})}):$items$$=$rows$$;async.parallel({_Participant_created:$callback$$=>{setImmediate(async()=>{_.has($schema_paths$$,"id_app")&&await $items$$.filter($item$$=>$item$$.user_created&&!$item$$.from_cached).asyncJoinModel2($user$$.current_id_app,"participant",{where:{user_created:"email"},fields:{name_user_created:"name"}});await $items$$.filter($item$$=>$item$$.user_created&&!$item$$.name_user_created&&!$item$$.from_cached).asyncJoinModel2(null,
5
- "user",{where:{user_created:"email"},fields:{name_user_created:"name"}});$callback$$(null)})},_Participant_updated:$callback$$=>{setImmediate(async()=>{_.has($schema_paths$$,"id_app")&&await $items$$.filter($item$$=>$item$$.user_updated&&!$item$$.from_cached).asyncJoinModel2($user$$.current_id_app,global.getModel("participant"),{where:{user_updated:"email"},fields:{name_user_updated:"name"}});await $items$$.filter($item$$=>$item$$.user_updated&&!$item$$.name_user_updated&&!$item$$.from_cached).asyncJoinModel2(null,
6
- global.getModel("user"),{where:{user_updated:"email"},fields:{name_user_updated:"name"}});$callback$$(null)})},_approve:$callback$$=>{!$fields_load$$||$fields_load$$.indexOf("approve_data")>=0?async.map($items$$,($item$$,$next$$)=>{setImmediate(()=>{global.getModel("approve").find({id_ct:$item$$._id.toString()},{data:0,request_content:0}).sort({data_created:-1}).lean().then(async $rs$$=>{setImmediate(async()=>{$item$$.approve_data=$rs$$||[];$item$$.approved_latest=$item$$.approve_data.filter($a$$=>
7
- $a$$.user_approved||$a$$.user_denied)[0];$item$$.approve_next=$item$$.approve_data.filter($a$$=>!$a$$.user_approved&&!$a$$.user_denied)[0];await $item$$.approve_data.filter($r$$=>$r$$.user_approved).asyncJoinModel2($item$$.id_app,global.getModel("participant"),{where:{user_approved:"email"},fields:{user_approved_name:"name",signature_image:"signature_image"}});await $item$$.approve_data.filter($r$$=>$r$$.user_denied).asyncJoinModel2($item$$.id_app,global.getModel("participant"),{where:{user_denied:"email"},
8
- fields:{user_denied_name:"name",signature_image:"signature_image"}});$next$$(null)})}).catch($e$$=>{console.error($e$$)})})},()=>{$callback$$(null)}):$callback$$(null)},transfer_ct:$callback$$=>{if(_.has($schema_paths$$,"id_ct_chuyen")&&$user$$.current_app_info&&$user$$.current_app_info.options&&$user$$.current_app_info.options.id_app_thue)async.map($items$$,($item$$,$next$$)=>{setImmediate(()=>{$model$$.findOne({id_app:$user$$.current_app_info.options.id_app_thue,id_ct_chuyen:$item$$._id.toString()},
9
- {_id:1}).lean().then(async $rs$$=>{$rs$$&&($item$$.id_ct_nhan=$rs$$._id);$next$$(null)}).catch($e$$=>{console.error($e$$)})})},()=>{$callback$$(null)});else return $callback$$()},posted:$callback$$=>{$options$$&&$options$$.req&&($options$$.req.query.show_posted_book||$options$$.req.query.show_not_post_book)?async.map($items$$,($item$$,$next$$)=>{setImmediate(async()=>{let $book$$=$options$$.req.query.show_posted_book||$options$$.req.query.show_not_post_book,$exists$$=await global.getModel($book$$).findOne({id_ct:$item$$._id.toString()},
10
- {_id:1});$item$$[`${$book$$}_posted`]=!!$exists$$;$next$$()})},()=>{$callback$$(null)}):$callback$$()}},$e$$=>{if($e$$)return $next$$($e$$);if($options$$&&$options$$.req&&($options$$.req.query.show_posted_book||$options$$.req.query.show_not_post_book)){let $book$$=$options$$.req.query.show_posted_book||$options$$.req.query.show_not_post_book;$items$$=$items$$.filter($r$$=>$options$$.req.query.show_posted_book?$r$$[`${$book$$}_posted`]:!$r$$[`${$book$$}_posted`])}let $items_need_view$$=$items$$.filter($i$$=>
11
- !$i$$.from_cached),$items_cached$$=$items$$.filter($i$$=>$i$$.from_cached);async.series({view:$callback$$=>{if($self$$.view)try{$self$$.view($user$$,$items_need_view$$,function($error$$,$rs$$){setImmediate(()=>{if($error$$)return $callback$$($error$$);$items_need_view$$=$rs$$||$items_need_view$$;$callback$$(null,$items_need_view$$)})},$options$$)}catch($e$$){console.error("Error on view of",$name$$,$e$$),$callback$$(null,$items_need_view$$)}else $callback$$(null,$items_need_view$$)},dynamicView:$callback$$=>
12
- {$self$$.dynamicView?$self$$.dynamicView($user$$,$items_need_view$$,function($error$$,$rs$$){if($error$$)return $callback$$($error$$);$items_need_view$$=$rs$$||$items_need_view$$;$callback$$(null,$items_need_view$$)},{listinfo_code:$listinfo_code$$,req:$options$$?.req}):$callback$$(null,$items_need_view$$)},viewRequired:$callback$$=>{$items$$=[...$items_cached$$,...$items_need_view$$].sort(($a$$,$b$$)=>$a$$.stt_sorted-$b$$.stt_sorted);if($self$$.viewRequired)try{$self$$.viewRequired($user$$,$items$$,
13
- function($error$$,$rs$$){if($error$$)return $callback$$($error$$);$items$$=$rs$$||$items$$;$callback$$(null,$items$$)},$options$$)}catch($e$$){console.error("Error on viewRequired of",$name$$,$e$$),$callback$$(null,$items$$)}else $callback$$(null,$items$$)}},$error$$=>{if($error$$)return $next$$($error$$);$next$$(null,$items$$)})})};module.exports=view;
1
+ const async = require("async");
2
+ const _ = require("lodash");
3
+ const redisCache = require("../libs/redis-cache");
4
+ const view = async (user,self,rows,next,options)=> {
5
+ const model = self.model;
6
+ const name = self.name;
7
+ const listinfo_code = options?.req?.query?.["listinfo-code"] || options?.req?.query?.["listinfo_code"] || options?.req?.headers?.["listinfo-code"] || options?.req?.headers?.["Listinfo-Code"];
8
+ let fields_load="";
9
+ if(options && options.req && options.req.query && options.req.query.fields){
10
+ fields_load = options.req.query.fields;
11
+ }
12
+ //
13
+ let schema_paths = model.schema.paths;
14
+ for(let index=0;index<rows.length;index++){
15
+ const item = rows[index];
16
+ if(item._id && typeof(item._id)!=="string"){
17
+ item._id = item._id.toString();
18
+ }
19
+ delete item.__v;
20
+ item.stt_sorted = index;
21
+ delete item.from_cached;
22
+ if (_.has(schema_paths, 'exfields')) {
23
+ if(!item.exfields){
24
+ item.exfields = {};
25
+ }
26
+ }
27
+ //xoá refs cache
28
+ //delete item.__refs;
29
+ }
30
+ let items =[];
31
+ //get view of item from cache
32
+ if(options && options.req && options.req.query_cache && !options.req.query.refresh_required){
33
+ await new Promise(resolve=>{
34
+ setImmediate(async()=>{
35
+ let cacheData = await redisCache.getCacheReport(options.req.user.email,(options.req.user.current_id_app||"system").toString(),self.name,options.req.query_cache,0,options.req.query.page);
36
+ if(cacheData){
37
+ for(let index=0;index<rows.length;index++){
38
+ const item = rows[index];
39
+ let cacheds = cacheData.filter(r=>r._id===item._id.toString());
40
+ if(cacheds.length>0){
41
+ for(let cached of cacheds){
42
+ cached.from_cached = true;
43
+ items.push(cached);
44
+ }
45
+ }else{
46
+ items.push(item);
47
+ }
48
+ }
49
+ }else{
50
+ items = rows;
51
+ }
52
+ resolve();
53
+ })
54
+ })
55
+ }else{
56
+ items = rows;
57
+ }
58
+ //
59
+ async.parallel({
60
+ _Participant_created:(callback)=>{
61
+ setImmediate(async ()=>{
62
+ if (_.has(schema_paths, 'id_app')) {
63
+ await items.filter(item=>item.user_created && !item.from_cached).asyncJoinModel2(user.current_id_app,"participant",{where:{user_created:"email"},fields:{name_user_created:"name"}});
64
+ }
65
+ await items.filter(item=>item.user_created && !item.name_user_created && !item.from_cached).asyncJoinModel2(null,"user",{where:{user_created:"email"},fields:{name_user_created:"name"}});
66
+ callback(null);
67
+ })
68
+ },
69
+ _Participant_updated:(callback)=>{
70
+ setImmediate(async ()=>{
71
+ if (_.has(schema_paths, 'id_app')) {
72
+ await items.filter(item=>item.user_updated && !item.from_cached).asyncJoinModel2(user.current_id_app,global.getModel('participant'),{where:{user_updated:"email"},fields:{name_user_updated:"name"}});
73
+ }
74
+ await items.filter(item=>item.user_updated && !item.name_user_updated && !item.from_cached).asyncJoinModel2(null,global.getModel('user'),{where:{user_updated:"email"},fields:{name_user_updated:"name"}});
75
+ callback(null);
76
+ })
77
+ },
78
+ _approve:(callback)=>{
79
+ if(!fields_load || fields_load.indexOf("approve_data")>=0){
80
+ async.map(items,(item,next)=>{
81
+ setImmediate(()=>{
82
+
83
+ global.getModel('approve').find({id_ct:item._id.toString()},{
84
+ data:0,request_content:0,
85
+ }).sort({data_created:-1}).lean().then(async (rs)=>{
86
+ //console.log("🔥[viewHandler] lấy dữ liệu duyệt:",{ma_ct:item.ma_ct,id_ct:item._id.toString()},rs.length);
87
+ setImmediate(async ()=>{
88
+ item.approve_data = rs||[];
89
+ item.approved_latest = item.approve_data.filter(a=>a.user_approved || a.user_denied)[0];
90
+ item.approve_next = item.approve_data.filter(a=>!a.user_approved && !a.user_denied)[0];
91
+
92
+ await item.approve_data.filter(r=>r.user_approved).asyncJoinModel2(item.id_app,global.getModel('participant'),{where:{user_approved:"email"},fields:{user_approved_name:"name",signature_image:"signature_image"}})
93
+ await item.approve_data.filter(r=>r.user_denied).asyncJoinModel2(item.id_app,global.getModel('participant'),{where:{user_denied:"email"},fields:{user_denied_name:"name",signature_image:"signature_image"}})
94
+
95
+ next(null);
96
+ })
97
+ }).catch(e=>{
98
+ console.error(e);
99
+ })
100
+ })
101
+ },()=>{
102
+ callback(null);
103
+ })
104
+ }else{
105
+ callback(null);
106
+ }
107
+ },
108
+ transfer_ct:(callback)=>{
109
+ if(_.has(schema_paths, 'id_ct_chuyen') && user.current_app_info && user.current_app_info.options && user.current_app_info.options.id_app_thue){
110
+ async.map(items,(item,next)=>{
111
+ setImmediate(()=>{
112
+ model.findOne({id_app:user.current_app_info.options.id_app_thue,id_ct_chuyen:item._id.toString()},{_id:1}).lean().then(async (rs)=>{
113
+ if(rs){
114
+ item.id_ct_nhan = rs._id;
115
+ }
116
+ next(null);
117
+ }).catch(e=>{
118
+ console.error(e)
119
+ })
120
+ })
121
+ },()=>{
122
+ callback(null);
123
+ })
124
+ }else{
125
+ return callback()
126
+ }
127
+ },
128
+ posted:(callback)=>{
129
+ if(options && options.req && (options.req.query.show_posted_book || options.req.query.show_not_post_book) ){
130
+ async.mapLimit(items,10,(item,next)=>{
131
+ setImmediate(async ()=>{
132
+ let book = options.req.query.show_posted_book || options.req.query.show_not_post_book;
133
+ let fields ={_id:1};
134
+ //cần kiểm tra từng vật tư xem có vật tư nào không được post sổ không
135
+ if(book=="sokho" && item.details?.length>0){
136
+ await item.details.asyncJoinModel2(item.id_app,"dmvt",{where:"ma_vt",fields:"tg_tk"});
137
+ fields.ma_vt =1;
138
+ let exists = await global.getModel(book).find({id_ct:item._id.toString()},fields).lean();
139
+ if(exists.length>0){
140
+ let ds_vt_voucher = item.details.filter(d=>d.tg_tk).map(d=>d.ma_vt).sort((a,b)=>a-b);
141
+ let ds_vt_book = exists.map(d=>d.ma_vt).sort((a,b)=>a-b);
142
+
143
+ if(JSON.stringify(ds_vt_voucher) === JSON.stringify(ds_vt_book)){
144
+ item[`${book}_posted`] = true;//Tất cả các vật tư đã post
145
+ }else{
146
+ item[`${book}_posted`] = false;
147
+ }
148
+
149
+ }else{
150
+ item[`${book}_posted`] = false;
151
+ }
152
+ }else{
153
+ let exists = await global.getModel(book).findOne({id_ct:item._id.toString()},fields).lean();
154
+ item[`${book}_posted`] = (!!exists);
155
+ }
156
+
157
+ next();
158
+ })
159
+ },()=>{
160
+ callback(null);
161
+ })
162
+ }else{
163
+ callback()
164
+ }
165
+ }
166
+ }, (e)=>{
167
+ if(e) return next(e);
168
+ //only show posted book or not post book
169
+ if(options && options.req && (options.req.query.show_posted_book || options.req.query.show_not_post_book) ){
170
+ let book = options.req.query.show_posted_book || options.req.query.show_not_post_book;
171
+ items = items.filter(r=>{
172
+ if(options.req.query.show_posted_book) return r[`${book}_posted`]
173
+ return !r[`${book}_posted`]
174
+ })
175
+ }
176
+ //handle view
177
+ let items_need_view = items.filter(i=>!i.from_cached);
178
+
179
+ //lưu các ref đã join để tránh join lại trên dynamicView
180
+ /*if(self.dynamicView){
181
+ items_need_view.forEach(r=>{
182
+ r.__refs={}
183
+ })
184
+ }*/
185
+ //
186
+ let items_cached = items.filter(i=>i.from_cached);
187
+ async.series({
188
+ view:(callback)=>{
189
+ if(self.view){
190
+ try{
191
+ self.view(user, items_need_view, function(error,rs) {
192
+ setImmediate(()=>{
193
+ if (error) {
194
+ return callback(error);
195
+ }
196
+ items_need_view = rs||items_need_view;
197
+ callback(null,items_need_view);
198
+ })
199
+ },options);
200
+ }catch(e){
201
+ console.error("Error on view of",name,e);
202
+ callback(null,items_need_view);
203
+ }
204
+ }else{
205
+ callback(null,items_need_view);
206
+ }
207
+ },
208
+ dynamicView:(callback)=>{
209
+ if(self.dynamicView){
210
+ self.dynamicView(user, items_need_view, function(error,rs) {
211
+ if (error) {
212
+ return callback(error);
213
+ }
214
+ items_need_view = rs||items_need_view;
215
+ callback(null,items_need_view);
216
+ },{listinfo_code,req:options?.req})
217
+ }else{
218
+ callback(null,items_need_view);
219
+ }
220
+ },
221
+ viewRequired:(callback)=>{
222
+ items = [...items_cached,...items_need_view].sort((a,b)=>{
223
+ return a.stt_sorted-b.stt_sorted
224
+ })
225
+ if(self.viewRequired){
226
+ try{
227
+ self.viewRequired(user, items, function(error,rs) {
228
+ if (error) {
229
+ return callback(error);
230
+ }
231
+ items = rs||items;
232
+ callback(null,items);
233
+ },options);
234
+ }catch(e){
235
+ console.error("Error on viewRequired of",name,e);
236
+ callback(null,items);
237
+ }
238
+ }else{
239
+ callback(null,items);
240
+ }
241
+ }
242
+ },(error)=>{
243
+ if(error) return next(error);
244
+ //xoá các refs
245
+ /*items.forEach(r=>{
246
+ delete r.__refs;
247
+ })*/
248
+ next(null,items);
249
+ })
250
+ })
251
+ }
252
+ module.exports =view;