UNPKG

2.72 kBJavaScriptView Raw
1var contractorRepository = require('./contractor-repository.js')
2var LINQ = require('node-linq').LINQ;
3var config = require('./config.js');
4
5var library = function() {
6
7 this.contratante = null;
8 this.contratantesIds = [];
9
10
11 this.getDataBaseConnection = function(contratante_id, sgdb, application) {
12 if (!this.contratante) {
13 console.log("No contratante Database Loaded");
14 return;
15 }
16
17 var connection = new LINQ(this.contratante).Where(function(m) {
18 return m.contractor._id == contratante_id && m.databaseType.toLowerCase() == sgdb.toLowerCase() && m.application.handle.toLowerCase() == application.toLowerCase();
19 }).FirstOrDefault();
20
21 try {
22 var response = {
23 "Servidor": connection.databaseType == "MONGODB" ? (connection.server + ":" + connection.port) : connection.server,
24 "Usuario": connection.user,
25 "Senha": connection.password,
26 "BancoDeDados": connection.database
27 };
28 } catch (error) {
29 throw error;
30 }
31
32 return response;
33 };
34
35 this.getContractors = function(callback_success, callback_error) {
36 var self = this;
37 if (this.contratante)
38 if (callback_success) {
39 callback_success(this.contratante);
40 return;
41 }
42
43 function success(data) {
44 self.contratante = data;
45 self.contratantesIds = getContractorsIds(data);
46 if (callback_success)
47 callback_success(data);
48 }
49
50 function error(err) {
51 if (callback_error)
52 callback_error(err);
53 }
54
55 contractorRepository.getConnections().then(function(response) {
56 success(response.records);
57 }).catch(function(err) {
58 error(err);
59 });
60 };
61
62 /*
63 TODO: Este método sera descontinuado favor não criar
64 novas implementações com ele
65 */
66 this.getContratantes = function(callback_success, callback_error) {
67 return this.getContractors(callback_success, callback_error);
68 };
69
70 function getContractorsIds(data) {
71 var contratantes_ids = new LINQ(data).Select(function(item) {
72 return item.contractor._id;
73 }).ToArray();
74
75 return arrayDistinct(contratantes_ids);
76 };
77
78 function arrayDistinct(arr) {
79 var result = [];
80
81 for (i = 0; i < arr.length; i++) {
82
83 if (new LINQ(result).Any(function(item) { return item == arr[i] }) == false) {
84 result.push(arr[i]);
85 }
86 }
87
88 return result;
89 };
90};
91
92module.exports = new library();
\No newline at end of file