* insert_relationship saves the list of people who related to another record. * @param {object} data - a valid JSON object containing data to be inserted. * @param {function} callback - callback function to be executed on success.
(data, callback)
| 162 | * @param {function} callback - callback function to be executed on success. |
| 163 | */ |
| 164 | function insert_relationships (data, callback) { |
| 165 | let fields, rel_id, url, username; |
| 166 | const len = data.entries.length - 1; |
| 167 | |
| 168 | function insert_rows () { // inner function has access to outer variables |
| 169 | data.entries.forEach((p, i) => { // poor person's "async parallel": |
| 170 | const username = p.username; |
| 171 | // console.log('username:', username); |
| 172 | select_person(username, function(error1, result1) { |
| 173 | // console.log('L251 > result1: ', result1.rows[0]); |
| 174 | const person_id = result1.rows[0].id; |
| 175 | const query = `INSERT INTO relationships (${fields}) VALUES ($1, $2)` |
| 176 | const values = [person_id, rel_id]; |
| 177 | // console.log('query:', query, 'values:', values); |
| 178 | PG_CLIENT.query(query, values, function(error2, result2) { |
| 179 | utils.log_error(error2, result2, new Error().stack); |
| 180 | |
| 181 | if(i === len) { |
| 182 | return insert_next_page(data, callback); |
| 183 | } |
| 184 | }); |
| 185 | }); |
| 186 | }); // END data.entries.forEach |
| 187 | } |
| 188 | // there are three types of relationships, we switch based on data.type |
| 189 | switch (data.type) { |
| 190 | case 'stars': |
| 191 | fields = 'person_id, repo_id'; |
| 192 | select_repo(data.url, function (error, result) { |
| 193 | rel_id = result.rows[0].id; |
| 194 | insert_rows(); |
| 195 | }); // END select_repo |
| 196 | break; |
| 197 | case 'people': // this is a list of members of an organisation |
| 198 | fields = 'person_id, org_id'; |
| 199 | url = '/' + data.url.split('/')[2];// /orgs/dwyl/people > /dwyl |
| 200 | select_org(url, function (error, result) { |
| 201 | rel_id = result.rows[0].id; |
| 202 | insert_rows(); |
| 203 | }); // END select_org |
| 204 | break; |
| 205 | case 'followers': // this is a list of followers/following |
| 206 | fields = 'person_id, leader_id'; |
| 207 | username = data.url.split('/')[1]; // /dwylbot/followers > dwylbot |
| 208 | // console.log('username', username); |
| 209 | // list of followers: |
| 210 | select_person(username, function (error, result) { |
| 211 | rel_id = result.rows[0].id; |
| 212 | insert_rows(); |
| 213 | }); // END select_org |
| 214 | break; |
| 215 | case 'following': // pay attention to the subtle difference in fields order |
| 216 | fields = 'leader_id, person_id'; |
| 217 | username = data.url.split('/')[1]; // /dwylbot/following > dwylbot |
| 218 | // console.log('username', username); |
| 219 | // list of followers: |
| 220 | select_person(username, function (error, result) { |
| 221 | rel_id = result.rows[0].id; |
nothing calls this directly
no test coverage detected