-
Notifications
You must be signed in to change notification settings - Fork 3
/
clear_templates.js
94 lines (82 loc) · 2.28 KB
/
clear_templates.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* ### Хвосты характеристик
*
* @module clear_templates
*
*/
/**
* ### Переменные окружения
* DEBUG "wb:*,-not_this"
* DBPWD admin
* DBUSER admin
* COUCHPATH https://dh5.oknosoft.ru:210/wb_
*/
'use strict';
const debug = require('debug')('wb:clear_builder_props');
const PouchDB = require('pouchdb').plugin(require('pouchdb-find'));
debug('required');
// инициализируем параметры сеанса и метаданные
const {DBUSER: username, DBPWD: password, COUCHPATH, BOOKMARK} = process.env;
const src = new PouchDB(COUCHPATH, {
auth: {username, password},
skip_setup: true,
ajax: {timeout: 100000}
});
const limit = 400;
src.info()
.then((info) => {
debug(`connected to ${info.host}, doc count: ${info.doc_count}`);
})
.then(() => clear_bp(BOOKMARK))
.then(() => debug(`ok`));
function sleep(time, res) {
return new Promise((resolve) => {
setTimeout(() => resolve(res), time);
});
}
function clear_bp(bookmark) {
// получаем в ОЗУ все номенклатуры и единицы измерения
if(!bookmark) {
bookmark = null;
}
debug(`find ${bookmark}`);
return src.find({
selector: {
class_name: 'cat.characteristics',
obj_delivery_state: 'Шаблон',
},
fields: ['_id', '_rev', 'name', 'calc_order'],
limit,
bookmark,
})
.then(({docs, bookmark}) => {
if(docs.length) {
debug(`received ${docs.length} rows`);
// ищем заказы
const keys = docs.map((row) => `doc.calc_order|${row.calc_order}`);
return src.allDocs({keys})
.then(({rows}) => {
const set = new Set();
for(const {key, error} of rows) {
if(error === 'not_found') {
set.add(key.substr(15));
}
}
const del = [];
docs.forEach((doc) => {
if(set.has(doc.calc_order)) {
doc._deleted = true;
del.push(doc);
}
});
debug(`remove ${del.length} rows`);
return src.bulkDocs(del);
})
.then(() => sleep(2000, 0))
.then(() => clear_bp(bookmark));
}
})
.catch(err => {
debug(err)
});
}