-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCategoryIngestionService.js
175 lines (157 loc) · 5.41 KB
/
CategoryIngestionService.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var _ = require('lodash');
var db = require('./sequelize-models');
module.exports = {
addNewSystemCategories(categoriesData) {
var categoriesDataForInsertion = categoriesData.map(category => {
category.type = 'SYSTEM';
return category;
});
return db.Category.bulkCreate(categoriesDataForInsertion, {
validate: true
// individualHooks: true
})
.then(newCategories => {
var newIds = _.map(newCategories, 'id');
console.log(`${newIds.length} categories inserted with ids ${newIds}`);
return newIds;
})
.catch(err => {
console.log(`[Error occured in creating categories] ${err}`);
return Promise.reject(err);
});
;
},
markSystemCategoryAsSuggested(categoryId) {
//change type to suggested where id = id
//if uniqueness constraint fails, find the system category object,
//find suggested category id where language, content_type, name are same as system,
//update p_c , changing suggested category id with systme category id,
//update sugg category to become inactive
//finally update systme category to suggested
console.log(`[1. mark ${categoryId} as suggested]`);
return db.Category.update({
type: 'SUGGESTED'
}, {
where: {
id: categoryId
}
})
.catch(err => {
console.log(typeof err, typeof err.errors[0], typeof err.errors[0].type);
console.log(err);
if(err.errors[0].type == 'unique violation') {
return db.Category.findById(categoryId)
.then(categoryObject => {
var categoryPlainObject = categoryObject.get({plain: true});
console.log(`[2. system category object is ${JSON.stringify(categoryPlainObject)}]`);
return db.Category.findOne({
where: {
language: categoryPlainObject.language,
content_type: categoryPlainObject.content_type,
type: 'SUGGESTED',
name: categoryPlainObject.name,
is_active: true
},
raw: true
})
;
})
.then(suggCategoryObject => {
console.log(`[3. suggested category object is ${JSON.stringify(suggCategoryObject)}]`);
var suggCategoryId = suggCategoryObject.id;
var transaction = db.sequelize.transaction(function (t) {
return db.PratilipiCategory.update({
category_id: categoryId
}, {
where: {
category_id: suggCategoryId
}
}, {transaction: t})
.then(affectedRows => {
console.log(`[4. Updated ${affectedRows} rows in p_c table]`);
return db.Category.update({
is_active: false
}, {
where: {
id: suggCategoryId
}
});
}, {transaction: t})
.then((affectedRows) => {
console.log(`[5. Updated ${affectedRows} rows in c table]`);
return db.Category.update({
type: 'SUGGESTED'
}, {
where: {
id: categoryId
}
})
}, {transaction: t});
});
return transaction;
})
} else {
return Promise.reject(err);
}
})
},
markSystemCategoriesAsSuggested(categoryIds) {
categoryIds = _.map(categoryIds, (category) => {
return parseInt(category.id);
});
var prChanges = [];
for(var i=0; i<categoryIds.length; i++) {
prChanges.push(module.exports.markSystemCategoryAsSuggested(categoryIds[i]));
}
return Promise.all(prChanges)
.catch(err => {
console.log(`[Error occured in deleting categories] ${err}`);
return Promise.reject(err);
});
// return db.Category.update(
// { type: 'SUGGESTED' },
// {
// where: {
// id: {
// $in: categoryIds
// }
// }
// }
// )
// .spread((affectedCount, affectedRows) => {
// console.log(`${affectedCount} rows changed to suggested`);
// return affectedCount;
// })
// .catch(err => {
// console.log(`[Error occured in deleting categories] ${err}`);
// return Promise.reject(err);
// });
},
updateNames(categoriesData) {
var prCategoriesUpdate = [];
for(var i=0; i<categoriesData.length; i++) {
var id = parseInt(categoriesData[i].id);
var name = categoriesData[i].name;
var prUpdate = db.Category.findById(id)
.then(categoryObject => {
console.log(categoryObject.get({plain:true}));
console.log((categoryObject.get({plain:true}).id).toString());
console.log(_.find(categoriesData, { 'id': (categoryObject.get({plain:true}).id).toString() }));
var nameToUpdate = _.find(categoriesData, { 'id': (categoryObject.get({plain:true}).id).toString() }).name;
return categoryObject.update({
name: nameToUpdate
});
});
prCategoriesUpdate.push(prUpdate);
}
return Promise.all(prCategoriesUpdate)
.then(() => {
console.log('Updated category names successfully');
return;
})
.catch(err => {
console.log(`[Error occured in updating names] ${err}`);
return Promise.reject(err);
});
}
};