@steedos-labs/content-compliance-bev 0.0.3 → 0.0.4

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.
Files changed (64) hide show
  1. package/dist/index.js +1 -1
  2. package/lib/actions/collection.d.ts +2 -0
  3. package/lib/actions/collection.js +26 -0
  4. package/lib/actions/collection.js.map +1 -0
  5. package/lib/actions/spotCheckMaterial.d.ts +20 -0
  6. package/lib/actions/spotCheckMaterial.js +543 -90
  7. package/lib/actions/spotCheckMaterial.js.map +1 -1
  8. package/lib/index.js +112 -33
  9. package/lib/index.js.map +1 -1
  10. package/lib/methods/approval_engine.js +182 -149
  11. package/lib/methods/approval_engine.js.map +1 -1
  12. package/lib/methods/content_hub.js +23 -38
  13. package/lib/methods/content_hub.js.map +1 -1
  14. package/lib/methods/material.js +6 -4
  15. package/lib/methods/material.js.map +1 -1
  16. package/lib/methods/task.d.ts +1 -1
  17. package/lib/methods/task.js +10 -18
  18. package/lib/methods/task.js.map +1 -1
  19. package/main/default/objects/pepsico_material/buttons/pepsico_material_approve.button.yml +10 -0
  20. package/main/default/objects/pepsico_material/buttons/pepsico_material_reject.button.yml +10 -0
  21. package/main/default/objects/pepsico_material/buttons/pepsico_material_spot_check.button.yml +1 -1
  22. package/main/default/objects/pepsico_material/buttons/pepsico_upload_supplement.button.yml +1 -0
  23. package/main/default/objects/pepsico_material/fields/approved_time.field.yml +5 -0
  24. package/main/default/objects/pepsico_material/fields/modified.field.yml +1 -0
  25. package/main/default/objects/pepsico_material/fields/modified_by.field.yml +1 -0
  26. package/main/default/objects/pepsico_material/fields/spot_check_pool_year_month.field.yml +9 -0
  27. package/main/default/objects/pepsico_material/fields/spot_check_type.field.yml +1 -1
  28. package/main/default/objects/pepsico_material/listviews/kol_midtier.listview.yml +3 -3
  29. package/main/default/objects/pepsico_material/listviews/spot_check.listview.yml +9 -5
  30. package/main/default/objects/pepsico_material/pepsico_material.object.yml +1 -1
  31. package/main/default/objects/pepsico_material_approval/buttons/pepsico_submit.button.yml +4 -0
  32. package/main/default/objects/pepsico_material_approval/fields/approved_time.field.yml +5 -0
  33. package/main/default/objects/pepsico_material_approval/fields/material_owner.field.yml +2 -2
  34. package/main/default/objects/pepsico_material_approval/fields/modified.field.yml +1 -0
  35. package/main/default/objects/pepsico_material_approval/fields/modified_by.field.yml +1 -0
  36. package/main/default/objects/pepsico_material_approval_data/fields/modified.field.yml +1 -0
  37. package/main/default/objects/pepsico_material_approval_data/fields/modified_by.field.yml +1 -0
  38. package/main/default/objects/pepsico_material_data/fields/modified.field.yml +1 -0
  39. package/main/default/objects/pepsico_material_data/fields/modified_by.field.yml +1 -0
  40. package/main/default/objects/pepsico_material_spot_check/fields/modified.field.yml +1 -0
  41. package/main/default/objects/pepsico_material_spot_check/fields/modified_by.field.yml +1 -0
  42. package/main/default/objects/pepsico_spot_check_pool/buttons/send_mail.button.yml +39 -0
  43. package/main/default/objects/pepsico_spot_check_pool/fields/logs.field.yml +8 -0
  44. package/main/default/objects/pepsico_spot_check_pool/fields/mail_send.field.yml +8 -0
  45. package/main/default/objects/pepsico_spot_check_pool/fields/mail_send_date.field.yml +9 -0
  46. package/main/default/objects/pepsico_spot_check_pool/fields/micro_pepsico_material.field.yml +11 -0
  47. package/main/default/objects/pepsico_spot_check_pool/fields/mid_tier_pepsico_material.field.yml +11 -0
  48. package/main/default/objects/pepsico_spot_check_pool/fields/year_month.field.yml +8 -0
  49. package/main/default/objects/pepsico_spot_check_pool/listviews/all.listview.yml +18 -0
  50. package/main/default/objects/pepsico_spot_check_pool/pepsico_spot_check_pool.object.yml +27 -0
  51. package/main/default/objects/pepsico_spot_check_pool/permissions/admin.permission.yml +15 -0
  52. package/main/default/objects/pepsico_spot_check_pool/permissions/user.permission.yml +14 -0
  53. package/main/default/objects/pepsico_spot_check_report/fields/modified_by.field.yml +1 -1
  54. package/main/default/objects/pepsico_tag_increase/fields/modified.field.yml +1 -0
  55. package/main/default/objects/pepsico_tag_increase/fields/modified_by.field.yml +1 -0
  56. package/main/default/pages/pepsico_content_review_detail.page.amis.json +16 -4
  57. package/main/default/pages/pepsico_material_detail.page.amis.json +18 -0
  58. package/main/default/pages/pepsico_material_detail.page.yml +3 -0
  59. package/main/default/pages/pepsico_midtier_spot_check.page.amis.json +45 -17
  60. package/main/default/pages/pepsico_spot_check.page.amis.json +1 -2
  61. package/main/default/pages/pepsico_spot_check_dco.page.amis.json +1 -2
  62. package/main/default/triggers/pepsico_material_approval_changName.trigger.yml +9 -1
  63. package/main/default/triggers/pepsico_material_approval_change.trigger.yml +2 -2
  64. package/package.json +1 -1
@@ -1,12 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.clickReportSpotEmail = exports.sendMonthlySpotCheckReport = exports.clickSpotEmail = exports.spotCheckMaterial = void 0;
3
+ exports.generateSpotCheckPool = exports.clickPoolSpotEmail = exports.clickReportSpotEmail = exports.sendMonthlySpotCheckReport = exports.clickSpotEmail = exports.spotCheckMaterial = void 0;
4
4
  var tslib_1 = require("tslib");
5
5
  var lodash_1 = require("lodash");
6
- var const_1 = require("../const");
7
6
  var _ = require("lodash");
8
- var content_hub_1 = require("../methods/content_hub");
9
7
  var axios_1 = require("axios");
8
+ var collection_1 = require("./collection");
10
9
  var moment = require('moment');
11
10
  var sendEmail = function (from, to, subject, text, html, cc) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
12
11
  var result;
@@ -26,17 +25,6 @@ var sendEmail = function (from, to, subject, text, html, cc) { return tslib_1.__
26
25
  })];
27
26
  case 1:
28
27
  result = _a.sent();
29
- console.log('sendEmail===>', "".concat(process.env.LOCAL_ROOT_URL || process.env.ROOT_URL, "/api/v6/email/send"), {
30
- from: from,
31
- to: to.email,
32
- subject: subject,
33
- text: text,
34
- html: html
35
- }, {
36
- headers: {
37
- 'Authorization': "Bearer apikey,".concat(process.env.STEEDOS_API_KEY)
38
- }
39
- }, result);
40
28
  return [2, JSON.stringify(result.data)];
41
29
  }
42
30
  });
@@ -82,17 +70,17 @@ exports.spotCheckMaterial = {
82
70
  },
83
71
  handler: function (ctx) {
84
72
  return tslib_1.__awaiter(this, void 0, void 0, function () {
85
- var user, _a, mid, description, pepsico_spot_check_status, logger, material, materialApproval, users, users_1, users_1_1, to, e_1_1, error_1, spaceUsers, subject, spaceUsers_1, spaceUsers_1_1, u, lid, html, result, e_2_1, error_2;
86
- var e_1, _b, e_2, _c;
87
- return tslib_1.__generator(this, function (_d) {
88
- switch (_d.label) {
73
+ var user, _a, mid, description, pepsico_spot_check_status, logger, material, checkRole, checkField, checkUsers, spot_checked_done, spot_checked_done_time, now, materialNew_1, materialApproval, materialNew, canSendMessage, users, users_1, users_1_1, to, e_1_1, error_1, spaceUsers, subject, spaceUsers_1, spaceUsers_1_1, u, lid, html, result, e_2_1, error_2;
74
+ var _b, _c, e_1, _d, e_2, _e;
75
+ return tslib_1.__generator(this, function (_f) {
76
+ switch (_f.label) {
89
77
  case 0:
90
78
  user = ctx.meta.user;
91
79
  _a = ctx.params, mid = _a.mid, description = _a.description, pepsico_spot_check_status = _a.pepsico_spot_check_status;
92
80
  ctx.getObject = this.getObject;
93
81
  return [4, this.getLogger()];
94
82
  case 1:
95
- logger = _d.sent();
83
+ logger = _f.sent();
96
84
  ctx.logger = logger;
97
85
  if (!mid) {
98
86
  return [2, {
@@ -103,7 +91,7 @@ exports.spotCheckMaterial = {
103
91
  }
104
92
  return [4, ctx.getObject('pepsico_material').findOne(mid)];
105
93
  case 2:
106
- material = _d.sent();
94
+ material = _f.sent();
107
95
  if (!material) {
108
96
  return [2, {
109
97
  status: 500,
@@ -111,25 +99,107 @@ exports.spotCheckMaterial = {
111
99
  data: {}
112
100
  }];
113
101
  }
114
- if (material.spot_checked) {
102
+ if (material.spot_checked_done) {
115
103
  return [2, {
116
104
  status: 500,
117
105
  msg: "已抽查, 请勿重复操作!",
118
106
  data: {}
119
107
  }];
120
108
  }
121
- return [4, ctx.getObject('pepsico_material').directUpdate(mid, {
122
- spot_checked: true,
123
- spot_check_time: new Date(),
109
+ checkRole = '';
110
+ checkField = '';
111
+ checkUsers = '';
112
+ spot_checked_done = false;
113
+ spot_checked_done_time = null;
114
+ if (!(material.spot_check_type === '固定人员抽查' || material.spot_check_type === '四部门抽查')) return [3, 8];
115
+ if (material.spot_check_type === '固定人员抽查') {
116
+ if (material.guding_spot_check != true && _.includes(material.guding_spot_check_user, user.userId)) {
117
+ checkRole = '固定人员';
118
+ checkField = 'guding_spot_check';
119
+ checkUsers = 'guding_spot_check_user';
120
+ spot_checked_done = true;
121
+ }
122
+ }
123
+ else if (material.spot_check_type === '四部门抽查') {
124
+ if (material.legal_spot_check != true && _.includes(material.legal_spot_check_user, user.userId)) {
125
+ checkRole = 'Legal';
126
+ checkField = 'legal_spot_check';
127
+ checkUsers = 'legal_spot_check_user';
128
+ }
129
+ else if (material.sra_spot_check != true && _.includes(material.sra_spot_check_user, user.userId)) {
130
+ checkRole = 'SRA';
131
+ checkField = 'sra_spot_check';
132
+ checkUsers = 'sra_spot_check_user';
133
+ }
134
+ else if (material.ca_spot_check != true && _.includes(material.ca_spot_check_user, user.userId)) {
135
+ checkRole = 'CA';
136
+ checkField = 'ca_spot_check';
137
+ checkUsers = 'ca_spot_check_user';
138
+ }
139
+ else if (material.ns_spot_check != true && _.includes(material.ns_spot_check_user, user.userId)) {
140
+ checkRole = 'NS';
141
+ checkField = 'ns_spot_check';
142
+ checkUsers = 'ns_spot_check_user';
143
+ }
144
+ }
145
+ if (!checkRole) {
146
+ return [2, {
147
+ status: 500,
148
+ msg: "已抽查或无抽查权限!",
149
+ data: {}
150
+ }];
151
+ }
152
+ now = new Date();
153
+ return [4, ctx.getObject('pepsico_material_spot_check').insert({
154
+ name: checkRole,
155
+ owner: user.userId,
124
156
  pepsico_spot_check: user.userId,
157
+ role: checkRole,
158
+ pepsico_spot_check_status: pepsico_spot_check_status,
159
+ spot_check_time: now,
125
160
  spot_description: description,
126
- pepsico_spot_check_status: pepsico_spot_check_status
161
+ pepsico_material: mid,
162
+ pepsico_material_approval: material.pepsico_material_approval,
163
+ space: material.space,
164
+ created: now,
165
+ created_by: user.userId,
166
+ modified: now,
167
+ modified_by: user.userId
127
168
  })];
128
169
  case 3:
129
- _d.sent();
130
- return [4, ctx.getObject('pepsico_material_approval').findOne(material.pepsico_material_approval)];
170
+ _f.sent();
171
+ return [4, ctx.getObject('pepsico_material').directUpdate(mid, (_b = {
172
+ spot_checked_done: spot_checked_done,
173
+ spot_checked_done_time: spot_checked_done_time
174
+ },
175
+ _b[checkField] = true,
176
+ _b.$pull = (_c = {},
177
+ _c[checkUsers] = user.userId,
178
+ _c),
179
+ _b))];
131
180
  case 4:
132
- materialApproval = _d.sent();
181
+ _f.sent();
182
+ if (!(material.spot_check_type === '四部门抽查')) return [3, 7];
183
+ return [4, ctx.getObject('pepsico_material').findOne(mid)];
184
+ case 5:
185
+ materialNew_1 = _f.sent();
186
+ if (!(materialNew_1.legal_spot_check && materialNew_1.sra_spot_check && materialNew_1.ca_spot_check && materialNew_1.ns_spot_check)) return [3, 7];
187
+ return [4, ctx.getObject('pepsico_material').directUpdate(mid, {
188
+ spot_checked_done: true,
189
+ spot_checked_done_time: new Date()
190
+ })];
191
+ case 6:
192
+ _f.sent();
193
+ _f.label = 7;
194
+ case 7: return [3, 9];
195
+ case 8: return [2, {
196
+ status: 500,
197
+ msg: "此素材不需要抽查",
198
+ data: {}
199
+ }];
200
+ case 9: return [4, ctx.getObject('pepsico_material_approval').findOne(material.pepsico_material_approval)];
201
+ case 10:
202
+ materialApproval = _f.sent();
133
203
  if (!materialApproval) {
134
204
  return [2, {
135
205
  status: 500,
@@ -137,24 +207,33 @@ exports.spotCheckMaterial = {
137
207
  data: {}
138
208
  }];
139
209
  }
140
- if (!(pepsico_spot_check_status === true && _.includes(const_1.DCO_TYPES, materialApproval.classification))) return [3, 6];
141
- return [4, (0, content_hub_1.sendByMaterial)(ctx, mid)];
142
- case 5:
143
- _d.sent();
144
- _d.label = 6;
145
- case 6:
210
+ return [4, ctx.getObject('pepsico_material').findOne(mid)];
211
+ case 11:
212
+ materialNew = _f.sent();
213
+ canSendMessage = false;
214
+ if (materialNew.spot_check_type === '固定人员抽查') {
215
+ if (material.guding_spot_check) {
216
+ canSendMessage = true;
217
+ }
218
+ }
219
+ else if (materialNew.spot_check_type === '四部门抽查') {
220
+ if (material.legal_spot_check && material.sra_spot_check && material.ca_spot_check && material.ns_spot_check) {
221
+ canSendMessage = true;
222
+ }
223
+ }
224
+ if (!canSendMessage) return [3, 35];
146
225
  users = (0, lodash_1.uniq)((0, lodash_1.compact)([materialApproval.owner, materialApproval.created_by]));
147
226
  console.log('通知用户:', JSON.stringify(users));
148
- _d.label = 7;
149
- case 7:
150
- _d.trys.push([7, 16, , 17]);
151
- _d.label = 8;
152
- case 8:
153
- _d.trys.push([8, 13, 14, 15]);
227
+ _f.label = 12;
228
+ case 12:
229
+ _f.trys.push([12, 21, , 22]);
230
+ _f.label = 13;
231
+ case 13:
232
+ _f.trys.push([13, 18, 19, 20]);
154
233
  users_1 = tslib_1.__values(users), users_1_1 = users_1.next();
155
- _d.label = 9;
156
- case 9:
157
- if (!!users_1_1.done) return [3, 12];
234
+ _f.label = 14;
235
+ case 14:
236
+ if (!!users_1_1.done) return [3, 17];
158
237
  to = users_1_1.value;
159
238
  console.log('站内通知用户:', to);
160
239
  return [4, ctx.broker.call('notifications.add', {
@@ -172,51 +251,51 @@ exports.spotCheckMaterial = {
172
251
  from: user.userId,
173
252
  to: to
174
253
  })];
175
- case 10:
176
- _d.sent();
177
- _d.label = 11;
178
- case 11:
254
+ case 15:
255
+ _f.sent();
256
+ _f.label = 16;
257
+ case 16:
179
258
  users_1_1 = users_1.next();
180
- return [3, 9];
181
- case 12: return [3, 15];
182
- case 13:
183
- e_1_1 = _d.sent();
259
+ return [3, 14];
260
+ case 17: return [3, 20];
261
+ case 18:
262
+ e_1_1 = _f.sent();
184
263
  e_1 = { error: e_1_1 };
185
- return [3, 15];
186
- case 14:
264
+ return [3, 20];
265
+ case 19:
187
266
  try {
188
- if (users_1_1 && !users_1_1.done && (_b = users_1.return)) _b.call(users_1);
267
+ if (users_1_1 && !users_1_1.done && (_d = users_1.return)) _d.call(users_1);
189
268
  }
190
269
  finally { if (e_1) throw e_1.error; }
191
270
  return [7];
192
- case 15: return [3, 17];
193
- case 16:
194
- error_1 = _d.sent();
271
+ case 20: return [3, 22];
272
+ case 21:
273
+ error_1 = _f.sent();
195
274
  console.log(error_1);
196
- return [3, 17];
197
- case 17:
198
- _d.trys.push([17, 29, , 30]);
275
+ return [3, 22];
276
+ case 22:
277
+ _f.trys.push([22, 34, , 35]);
199
278
  return [4, ctx.getObject('space_users').find({
200
279
  filters: ['user', 'in', users]
201
280
  })];
202
- case 18:
203
- spaceUsers = _d.sent();
281
+ case 23:
282
+ spaceUsers = _f.sent();
204
283
  subject = '【系统通知】素材抽检结果通知';
205
- _d.label = 19;
206
- case 19:
207
- _d.trys.push([19, 26, 27, 28]);
284
+ _f.label = 24;
285
+ case 24:
286
+ _f.trys.push([24, 31, 32, 33]);
208
287
  spaceUsers_1 = tslib_1.__values(spaceUsers), spaceUsers_1_1 = spaceUsers_1.next();
209
- _d.label = 20;
210
- case 20:
211
- if (!!spaceUsers_1_1.done) return [3, 25];
288
+ _f.label = 25;
289
+ case 25:
290
+ if (!!spaceUsers_1_1.done) return [3, 30];
212
291
  u = spaceUsers_1_1.value;
213
292
  return [4, ctx.getObject('pepsico_mail_send_log')._makeNewID()];
214
- case 21:
215
- lid = _d.sent();
216
- html = "\n <p>\u60A8\u63D0\u4EA4\u7684\u7D20\u6750\u5DF2\u7ECF\u88AB\u62BD\u67E5\u3002\u8BF7\u70B9\u51FB\u4E0B\u9762\u7684\u94FE\u63A5\u767B\u5F55\u7CFB\u7EDF\u67E5\u770B\u62BD\u67E5\u610F\u89C1\uFF1A</p>\n <a href=\"".concat(process.env.ROOT_URL, "/api/pepsico-content/material/click/spot-check/").concat(mid, "/").concat(lid, "\">").concat(materialApproval.name, ":").concat(material.name, "</a>\n ");
293
+ case 26:
294
+ lid = _f.sent();
295
+ html = "\n <p>\u60A8\u63D0\u4EA4\u7684\u7D20\u6750\u5DF2\u7ECF\u88AB\u62BD\u67E5\u3002\u8BF7\u70B9\u51FB\u4E0B\u9762\u7684\u94FE\u63A5\u767B\u5F55\u7CFB\u7EDF\u67E5\u770B\u62BD\u67E5\u610F\u89C1\uFF1A</p>\n <a href=\"".concat(process.env.ROOT_URL, "/api/pepsico-content/material/click/spot-check/").concat(mid, "/").concat(lid, "\">").concat(materialApproval.name, ":").concat(material.name, "</a>\n ");
217
296
  return [4, sendEmail(process.env.B6_EMAIL_FROM, u.email, subject, '', html, '')];
218
- case 22:
219
- result = _d.sent();
297
+ case 27:
298
+ result = _f.sent();
220
299
  return [4, ctx.getObject('pepsico_mail_send_log').insert({
221
300
  _id: lid,
222
301
  name: subject,
@@ -227,30 +306,30 @@ exports.spotCheckMaterial = {
227
306
  pepsico_material: material._id,
228
307
  result: result
229
308
  }, user)];
230
- case 23:
231
- _d.sent();
232
- _d.label = 24;
233
- case 24:
309
+ case 28:
310
+ _f.sent();
311
+ _f.label = 29;
312
+ case 29:
234
313
  spaceUsers_1_1 = spaceUsers_1.next();
235
- return [3, 20];
236
- case 25: return [3, 28];
237
- case 26:
238
- e_2_1 = _d.sent();
314
+ return [3, 25];
315
+ case 30: return [3, 33];
316
+ case 31:
317
+ e_2_1 = _f.sent();
239
318
  e_2 = { error: e_2_1 };
240
- return [3, 28];
241
- case 27:
319
+ return [3, 33];
320
+ case 32:
242
321
  try {
243
- if (spaceUsers_1_1 && !spaceUsers_1_1.done && (_c = spaceUsers_1.return)) _c.call(spaceUsers_1);
322
+ if (spaceUsers_1_1 && !spaceUsers_1_1.done && (_e = spaceUsers_1.return)) _e.call(spaceUsers_1);
244
323
  }
245
324
  finally { if (e_2) throw e_2.error; }
246
325
  return [7];
247
- case 28: return [3, 30];
248
- case 29:
249
- error_2 = _d.sent();
326
+ case 33: return [3, 35];
327
+ case 34:
328
+ error_2 = _f.sent();
250
329
  console.log(error_2);
251
330
  logger === null || logger === void 0 ? void 0 : logger.error("[\u7D20\u6750\u62BD\u67E5]-".concat(mid), { error: error_2.message });
252
- return [3, 30];
253
- case 30: return [2, {
331
+ return [3, 35];
332
+ case 35: return [2, {
254
333
  status: 0,
255
334
  data: {}
256
335
  }];
@@ -392,7 +471,7 @@ exports.sendMonthlySpotCheckReport = {
392
471
  return tslib_1.__generator(this, function (_d) {
393
472
  switch (_d.label) {
394
473
  case 0: return [4, ctx.getObject('pepsico_material').directFind({
395
- filters: [['spot_checked', '=', true], ['product', '=', brandLeader.name], ['classification', '=', brandLeader.classification], ['spot_check_time', '>=', monthStart], ['spot_check_time', '<=', monthEnd]]
474
+ filters: [['spot_checked_done', '=', true], ['product', '=', brandLeader.name], ['classification', '=', brandLeader.classification], ['spot_checked_done_time', '>=', monthStart], ['spot_checked_done_time', '<=', monthEnd]]
396
475
  })];
397
476
  case 1:
398
477
  list = _d.sent();
@@ -550,4 +629,378 @@ exports.clickReportSpotEmail = {
550
629
  });
551
630
  }
552
631
  };
632
+ exports.clickPoolSpotEmail = {
633
+ rest: {
634
+ method: 'GET',
635
+ authorization: false,
636
+ fullPath: '/api/pepsico-content/material/click/spot-pool/:pid'
637
+ },
638
+ handler: function (ctx) {
639
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
640
+ var user, pid, logger, pool;
641
+ return tslib_1.__generator(this, function (_a) {
642
+ switch (_a.label) {
643
+ case 0:
644
+ user = ctx.meta.user;
645
+ pid = ctx.params.pid;
646
+ if (!user) {
647
+ ctx.meta.$statusCode = 302;
648
+ ctx.meta.$location = "/accounts/a/#/login?redirect_uri=" + encodeURIComponent("/api/pepsico-content/material/click/spot-pool/".concat(pid));
649
+ return [2];
650
+ }
651
+ ctx.getObject = this.getObject;
652
+ return [4, this.getLogger()];
653
+ case 1:
654
+ logger = _a.sent();
655
+ ctx.logger = logger;
656
+ if (!pid) {
657
+ return [2, {
658
+ status: 500,
659
+ msg: "无效的标识",
660
+ data: {}
661
+ }];
662
+ }
663
+ return [4, ctx.getObject('pepsico_spot_check_pool').findOne(pid)];
664
+ case 2:
665
+ pool = _a.sent();
666
+ if (!pool) {
667
+ return [2, {
668
+ status: 500,
669
+ msg: "无效的标识",
670
+ data: {}
671
+ }];
672
+ }
673
+ ctx.meta.$statusCode = 302;
674
+ ctx.meta.$location = "/app/pepsico_content_review/page/pepsico_spot_check?pid=".concat(pid);
675
+ return [2];
676
+ }
677
+ });
678
+ });
679
+ }
680
+ };
681
+ var BRAND_MAPPING = {
682
+ '1654151762098375': '百事可乐',
683
+ '16541517620125500': '美年达',
684
+ '16541517620100467': '7UP',
685
+ '1654151762089410': '佳得乐',
686
+ '168895721414': '新品牌',
687
+ '2232511041099040': '新业务',
688
+ '168895721416': 'Sales'
689
+ };
690
+ function getSpotCheckPool(yearMonth) {
691
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
692
+ var client, collection, startDate, endDate, query, materials, groupedData_1, samplingPools, error_5;
693
+ return tslib_1.__generator(this, function (_a) {
694
+ switch (_a.label) {
695
+ case 0:
696
+ _a.trys.push([0, 3, 4, 7]);
697
+ return [4, (0, collection_1.getCollection)('pepsico_material')];
698
+ case 1:
699
+ collection = _a.sent();
700
+ startDate = new Date("".concat(yearMonth, "-01"));
701
+ endDate = new Date(startDate);
702
+ endDate.setMonth(endDate.getMonth() + 1);
703
+ query = {
704
+ approved_time: {
705
+ $gte: startDate,
706
+ $lt: endDate
707
+ },
708
+ void_material: {
709
+ $ne: true
710
+ }
711
+ };
712
+ return [4, collection.find(query).toArray()];
713
+ case 2:
714
+ materials = _a.sent();
715
+ console.log("[".concat(yearMonth, "] \u627E\u5230 ").concat(materials.length, " \u4E2A\u5BA1\u6279\u5B8C\u6210\u7684\u7D20\u6750"));
716
+ groupedData_1 = {
717
+ 'Mid-tier': {},
718
+ 'Micro': {}
719
+ };
720
+ materials.forEach(function (material) {
721
+ var classification = material.kol_classification;
722
+ var product = material.product;
723
+ if (groupedData_1[classification] && BRAND_MAPPING[product]) {
724
+ if (!groupedData_1[classification][product]) {
725
+ groupedData_1[classification][product] = [];
726
+ }
727
+ groupedData_1[classification][product].push(material);
728
+ }
729
+ });
730
+ samplingPools = {
731
+ 'Mid-tier': generateClassificationSpotCheckPool(groupedData_1['Mid-tier']),
732
+ 'Micro': generateClassificationSpotCheckPool(groupedData_1['Micro']),
733
+ count: materials.length
734
+ };
735
+ return [2, samplingPools];
736
+ case 3:
737
+ error_5 = _a.sent();
738
+ console.error('Error generating sampling pool:', error_5);
739
+ throw error_5;
740
+ case 4:
741
+ if (!client) return [3, 6];
742
+ return [4, client.close()];
743
+ case 5:
744
+ _a.sent();
745
+ _a.label = 6;
746
+ case 6: return [7];
747
+ case 7: return [2];
748
+ }
749
+ });
750
+ });
751
+ }
752
+ function generateClassificationSpotCheckPool(brandMaterials) {
753
+ var e_4, _a;
754
+ var result = {
755
+ samplingPool: [],
756
+ statistics: {
757
+ total: 0,
758
+ byBrand: {}
759
+ }
760
+ };
761
+ var totalMaterials = 0;
762
+ var brandCounts = {};
763
+ Object.keys(brandMaterials).forEach(function (brandId) {
764
+ var count = brandMaterials[brandId].length;
765
+ brandCounts[brandId] = count;
766
+ totalMaterials += count;
767
+ result.statistics.byBrand[BRAND_MAPPING[brandId]] = {
768
+ total: count,
769
+ sampled: 0
770
+ };
771
+ });
772
+ result.statistics.total = totalMaterials;
773
+ if (totalMaterials === 0) {
774
+ return result;
775
+ }
776
+ var targetSampleCount = Math.max(1, Math.floor(totalMaterials * 0.1));
777
+ var targetBrandSamples = {};
778
+ var remainingSamples = targetSampleCount;
779
+ var pepsiTarget = Math.floor(totalMaterials * 0.04);
780
+ if (brandCounts['1654151762098375']) {
781
+ targetBrandSamples['1654151762098375'] = Math.min(pepsiTarget, brandCounts['1654151762098375']);
782
+ remainingSamples -= targetBrandSamples['1654151762098375'];
783
+ }
784
+ var gatoradeTarget = Math.floor(totalMaterials * 0.03);
785
+ if (brandCounts['1654151762089410']) {
786
+ targetBrandSamples['1654151762089410'] = Math.min(gatoradeTarget, brandCounts['1654151762089410']);
787
+ remainingSamples -= targetBrandSamples['1654151762089410'];
788
+ }
789
+ var otherBrandsTarget = Math.floor(totalMaterials * 0.03);
790
+ var otherBrands = Object.keys(brandCounts).filter(function (brandId) {
791
+ return brandId !== '1654151762098375' && brandId !== '1654151762089410';
792
+ });
793
+ if (otherBrands.length > 0 && remainingSamples > 0) {
794
+ var otherBrandsTotal_1 = otherBrands.reduce(function (sum, brandId) { return sum + brandCounts[brandId]; }, 0);
795
+ otherBrands.forEach(function (brandId) {
796
+ var proportion = brandCounts[brandId] / otherBrandsTotal_1;
797
+ targetBrandSamples[brandId] = Math.min(Math.floor(otherBrandsTarget * proportion), brandCounts[brandId]);
798
+ remainingSamples -= targetBrandSamples[brandId];
799
+ });
800
+ }
801
+ if (remainingSamples > 0) {
802
+ var allBrands = Object.keys(brandCounts)
803
+ .sort(function (a, b) { return brandCounts[b] - brandCounts[a]; });
804
+ try {
805
+ for (var allBrands_1 = tslib_1.__values(allBrands), allBrands_1_1 = allBrands_1.next(); !allBrands_1_1.done; allBrands_1_1 = allBrands_1.next()) {
806
+ var brandId = allBrands_1_1.value;
807
+ var currentSampled = targetBrandSamples[brandId] || 0;
808
+ var available = brandCounts[brandId] - currentSampled;
809
+ if (available > 0 && remainingSamples > 0) {
810
+ var addSamples = Math.min(available, remainingSamples);
811
+ targetBrandSamples[brandId] = (targetBrandSamples[brandId] || 0) + addSamples;
812
+ remainingSamples -= addSamples;
813
+ }
814
+ if (remainingSamples <= 0)
815
+ break;
816
+ }
817
+ }
818
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
819
+ finally {
820
+ try {
821
+ if (allBrands_1_1 && !allBrands_1_1.done && (_a = allBrands_1.return)) _a.call(allBrands_1);
822
+ }
823
+ finally { if (e_4) throw e_4.error; }
824
+ }
825
+ }
826
+ Object.keys(targetBrandSamples).forEach(function (brandId) {
827
+ var _a;
828
+ var samplesNeeded = targetBrandSamples[brandId];
829
+ var materials = brandMaterials[brandId];
830
+ if (samplesNeeded > 0 && materials.length > 0) {
831
+ var sampled = shuffleArray(tslib_1.__spreadArray([], tslib_1.__read(materials), false)).slice(0, samplesNeeded);
832
+ (_a = result.samplingPool).push.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(sampled), false));
833
+ result.statistics.byBrand[BRAND_MAPPING[brandId]].sampled = samplesNeeded;
834
+ }
835
+ });
836
+ return result;
837
+ }
838
+ function shuffleArray(array) {
839
+ var _a;
840
+ var result = tslib_1.__spreadArray([], tslib_1.__read(array), false);
841
+ for (var i = result.length - 1; i > 0; i--) {
842
+ var j = Math.floor(Math.random() * (i + 1));
843
+ _a = tslib_1.__read([result[j], result[i]], 2), result[i] = _a[0], result[j] = _a[1];
844
+ }
845
+ return result;
846
+ }
847
+ exports.generateSpotCheckPool = {
848
+ rest: {
849
+ method: 'GET',
850
+ authorization: false,
851
+ fullPath: '/api/pepsico-content/material/generate/spot-check-pools/:pid'
852
+ },
853
+ handler: function (ctx) {
854
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
855
+ var user, pid, logger, pool, yearMonth, pools, logs, _a, _b, material, approval, e_5_1, _c, _d, material, approval, e_6_1, subject, html, toUsers, users, error_6, ccUsers, result, error_7;
856
+ var e_5, _e, e_6, _f;
857
+ return tslib_1.__generator(this, function (_g) {
858
+ switch (_g.label) {
859
+ case 0:
860
+ user = ctx.meta.user;
861
+ pid = ctx.params.pid;
862
+ ctx.getObject = this.getObject;
863
+ return [4, this.getLogger()];
864
+ case 1:
865
+ logger = _g.sent();
866
+ ctx.logger = logger;
867
+ if (user && !user.is_space_admin) {
868
+ throw new Error('无权限');
869
+ }
870
+ return [4, ctx.getObject('pepsico_spot_check_pool').findOne(pid)];
871
+ case 2:
872
+ pool = _g.sent();
873
+ if (!pool) {
874
+ throw new Error('无效的id');
875
+ }
876
+ yearMonth = pool.year_month;
877
+ _g.label = 3;
878
+ case 3:
879
+ _g.trys.push([3, 29, , 30]);
880
+ return [4, getSpotCheckPool(yearMonth)];
881
+ case 4:
882
+ pools = _g.sent();
883
+ console.log('=== 腰部(Mid-tier)抽查池子 ===');
884
+ console.log("\u603B\u62BD\u6837\u6570: ".concat(pools['Mid-tier'].samplingPool.length), _.map(pools['Mid-tier'].samplingPool, function (item) { return _.pick(item, ['_id', 'product']); }));
885
+ console.log('品牌统计:', pools['Mid-tier'].statistics.byBrand);
886
+ console.log('\n=== 尾部(Micro)抽查池子 ===');
887
+ console.log("\u603B\u62BD\u6837\u6570: ".concat(pools['Micro'].samplingPool.length), _.map(pools['Micro'].samplingPool, function (item) { return _.pick(item, ['_id', 'product']); }));
888
+ console.log('品牌统计:', pools['Micro'].statistics.byBrand);
889
+ logs = "\n [".concat(yearMonth, "] \u627E\u5230 ").concat(pools.count, " \u4E2A\u5BA1\u6279\u5B8C\u6210\u7684\u7D20\u6750 \r\n\n === \u8170\u90E8(Mid-tier)\u62BD\u67E5\u6C60\u5B50 === \r\n\n \u603B\u62BD\u6837\u6570: ").concat(pools['Mid-tier'].samplingPool.length, ", ").concat(JSON.stringify(_.map(pools['Mid-tier'].samplingPool, function (item) { return _.pick(item, ['_id', 'product']); })), " \r\n\n \u54C1\u724C\u7EDF\u8BA1:, ").concat(JSON.stringify(pools['Mid-tier'].statistics.byBrand), " \r\n\n \n=== \u5C3E\u90E8(Micro)\u62BD\u67E5\u6C60\u5B50 ===' \r\n\n \u603B\u62BD\u6837\u6570: ").concat(pools['Micro'].samplingPool.length, ", ").concat(JSON.stringify(_.map(pools['Micro'].samplingPool, function (item) { return _.pick(item, ['_id', 'product']); })), " \r\n\n \u54C1\u724C\u7EDF\u8BA1:, ").concat(JSON.stringify(pools['Micro'].statistics.byBrand), " \r\n\n ");
890
+ _g.label = 5;
891
+ case 5:
892
+ _g.trys.push([5, 11, 12, 13]);
893
+ _a = tslib_1.__values(pools['Mid-tier'].samplingPool), _b = _a.next();
894
+ _g.label = 6;
895
+ case 6:
896
+ if (!!_b.done) return [3, 10];
897
+ material = _b.value;
898
+ return [4, ctx.getObject('pepsico_material_approval').findOne(material.pepsico_material_approval)];
899
+ case 7:
900
+ approval = _g.sent();
901
+ return [4, ctx.getObject('pepsico_material').directUpdate(material._id, {
902
+ spot_check_pool_year_month: yearMonth,
903
+ spot_checked: true,
904
+ spot_check_type: '四部门抽查',
905
+ legal_spot_check_user: approval.legal_spot_check,
906
+ sra_spot_check_user: approval.sra_spot_check,
907
+ ns_spot_check_user: approval.ns_spot_check,
908
+ ca_spot_check_user: approval.ca_spot_check
909
+ })];
910
+ case 8:
911
+ _g.sent();
912
+ _g.label = 9;
913
+ case 9:
914
+ _b = _a.next();
915
+ return [3, 6];
916
+ case 10: return [3, 13];
917
+ case 11:
918
+ e_5_1 = _g.sent();
919
+ e_5 = { error: e_5_1 };
920
+ return [3, 13];
921
+ case 12:
922
+ try {
923
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
924
+ }
925
+ finally { if (e_5) throw e_5.error; }
926
+ return [7];
927
+ case 13:
928
+ _g.trys.push([13, 19, 20, 21]);
929
+ _c = tslib_1.__values(pools['Micro'].samplingPool), _d = _c.next();
930
+ _g.label = 14;
931
+ case 14:
932
+ if (!!_d.done) return [3, 18];
933
+ material = _d.value;
934
+ return [4, ctx.getObject('pepsico_material_approval').findOne(material.pepsico_material_approval)];
935
+ case 15:
936
+ approval = _g.sent();
937
+ return [4, ctx.getObject('pepsico_material').directUpdate(material._id, {
938
+ spot_check_pool_year_month: yearMonth,
939
+ spot_checked: true,
940
+ spot_check_type: '固定人员抽查',
941
+ guding_spot_check_user: approval.special_spot_check || []
942
+ })];
943
+ case 16:
944
+ _g.sent();
945
+ _g.label = 17;
946
+ case 17:
947
+ _d = _c.next();
948
+ return [3, 14];
949
+ case 18: return [3, 21];
950
+ case 19:
951
+ e_6_1 = _g.sent();
952
+ e_6 = { error: e_6_1 };
953
+ return [3, 21];
954
+ case 20:
955
+ try {
956
+ if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
957
+ }
958
+ finally { if (e_6) throw e_6.error; }
959
+ return [7];
960
+ case 21:
961
+ subject = '【系统通知】抽查素材池通知';
962
+ html = "\n <p>\u4EB2\u7231\u7684\u7528\u6237\uFF0C</p>\n <p>".concat(yearMonth, "\u6708\u62BD\u67E5\u7D20\u6750\u6C60\u5DF2\u751F\u6210\u3002 \u8BF7\u70B9\u51FB\u4E0B\u9762\u94FE\u63A5\u67E5\u770B").concat(yearMonth, "\u6708\u9700\u8981\u7684\u62BD\u67E5\u7D20\u6750\u3002</p>\n <a href=\"").concat(process.env.ROOT_URL, "/api/pepsico-content/material/click/spot-pool/").concat(pid, "\">").concat(yearMonth, "</a>\n <p>\u6709\u4EFB\u4F55\u7591\u95EE\uFF0C\u8BF7\u8054\u7CFB\u7CFB\u7EDF\u7BA1\u7406\u5458\u3002</p>\n <p>\u8C22\u8C22\uFF01</p>\n ");
963
+ toUsers = [];
964
+ return [4, ctx.getObject('permission_set').directFind({
965
+ filters: ['name', '=', 'spot_check_users']
966
+ })];
967
+ case 22:
968
+ users = _g.sent();
969
+ if (!(users.length > 0)) return [3, 26];
970
+ _g.label = 23;
971
+ case 23:
972
+ _g.trys.push([23, 25, , 26]);
973
+ return [4, ctx.getObject('space_users').find({ filters: ['user', 'in', users[0].users || []] })];
974
+ case 24:
975
+ toUsers = _g.sent();
976
+ return [3, 26];
977
+ case 25:
978
+ error_6 = _g.sent();
979
+ console.log(error_6);
980
+ return [3, 26];
981
+ case 26:
982
+ ccUsers = [];
983
+ return [4, sendEmail(process.env.B6_EMAIL_FROM, _.join(_.map(toUsers, 'email'), ';'), subject, '', html, _.join(_.map(ccUsers, 'email'), ';'))];
984
+ case 27:
985
+ result = _g.sent();
986
+ return [4, ctx.getObject('pepsico_spot_check_pool').directUpdate(pid, {
987
+ mail_send: true,
988
+ mail_send_date: new Date(),
989
+ mid_tier_pepsico_material: _.map(pools['Mid-tier'].samplingPool, '_id'),
990
+ micro_pepsico_material: _.map(pools['Micro'].samplingPool, '_id'),
991
+ logs: logs + '\r\n' + '=== 发送邮件 ===' + result
992
+ })];
993
+ case 28:
994
+ _g.sent();
995
+ return [3, 30];
996
+ case 29:
997
+ error_7 = _g.sent();
998
+ console.error('执行失败:', error_7);
999
+ return [3, 30];
1000
+ case 30: return [2];
1001
+ }
1002
+ });
1003
+ });
1004
+ }
1005
+ };
553
1006
  //# sourceMappingURL=spotCheckMaterial.js.map