Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Peter Lundin
cmdb
Commits
ca2c62c6
Commit
ca2c62c6
authored
Dec 22, 2017
by
Peter Lundin
Browse files
Delete organisation.js
parent
91d2c01b
Changes
1
Hide whitespace changes
Inline
Side-by-side
routes/organisation.js
deleted
100644 → 0
View file @
91d2c01b
var
express
=
require
(
'
express
'
);
var
router
=
express
.
Router
();
const
jQuery
=
require
(
'
jquery
'
);
const
path
=
require
(
'
path
'
);
const
config
=
require
(
'
./config
'
);
const
pg
=
require
(
'
pg
'
);
const
winston
=
require
(
'
winston
'
);
const
connectionString
=
process
.
env
.
DATABASE_URL
||
config
.
storageConfig
;
router
.
get
(
'
/list/nodes/:centralNode
'
,
(
req
,
res
,
next
)
=>
{
var
pool
=
new
pg
.
Pool
({
connectionString
:
connectionString
});
// Get a Postgres client from the connection pool
pool
.
connect
(
function
(
err
,
client
,
done
){
// Handle connection errors
if
(
err
)
{
done
();
return
res
.
status
(
500
).
json
({
success
:
false
,
data
:
err
});
}
// all leaf
var
sql
=
"
WITH RECURSIVE tree(id, display_name, parent_id) AS (
"
;
sql
+=
"
SELECT id, display_name, parent_id FROM cmdb.ci_family_organisation c WHERE family_type_id=0 and c.id = $1
"
;
sql
+=
"
UNION
"
;
sql
+=
"
SELECT sub.id, sub.display_name, sub.parent_id
"
;
sql
+=
"
FROM tree, cmdb.ci_family_organisation sub
"
;
sql
+=
"
WHERE tree.id = sub.parent_id)
"
;
sql
+=
"
SELECT id, display_name as label
"
;
sql
+=
"
FROM tree order by display_name
"
;
sql
+=
"
limit 100;
"
;
/*
too root
WITH RECURSIVE org(id, display_name, parent_organisation_id) AS (
SELECT id, display_name, parent_organisation_id FROM cmdb.organisation org WHERE org.id = 6
UNION
SELECT sub.id, sub.display_name, sub.parent_organisation_id
FROM org, cmdb.organisation sub
WHERE sub.id = org.parent_organisation_id
)
SELECT id, display_name, parent_organisation_id
FROM org
limit 10
*/
// var sql = "select id, concat(id, '. ', (select display_name from cmdb.organisation_type as ot where// ot.id=organisation_type), ': ', display_name) as label from cmdb.organisation where id >0;";
var
centralNode
=
req
.
params
[
'
centralNode
'
];
client
.
query
(
sql
,
[
centralNode
],
function
(
err
,
result
){
done
();
if
(
err
){
//
return
res
.
json
(
JSON
.
parse
(
'
{"Result":"ERROR", "Message":"
'
+
err
.
message
+
'
"}
'
));
}
else
{
return
res
.
json
(
result
.
rows
);
}
});
});
});
router
.
get
(
'
/list/edges/:centralNode
'
,
(
req
,
res
,
next
)
=>
{
var
pool
=
new
pg
.
Pool
({
connectionString
:
connectionString
});
// Get a Postgres client from the connection pool
pool
.
connect
(
function
(
err
,
client
,
done
){
// Handle connection errors
if
(
err
)
{
done
();
return
res
.
status
(
500
).
json
({
success
:
false
,
data
:
err
});
}
var
sql
=
"
WITH RECURSIVE tree(id, display_name, parent_id) AS (
"
;
sql
+=
"
SELECT id, display_name, parent_id FROM cmdb.ci_family_organisation c WHERE family_type_id=0 and c.id = $1
"
;
sql
+=
"
UNION
"
;
sql
+=
"
SELECT sub.id, sub.display_name, sub.parent_id
"
;
sql
+=
"
FROM tree, cmdb.ci_family_organisation sub
"
;
sql
+=
"
WHERE tree.id = sub.parent_id)
"
;
sql
+=
"
SELECT id as from, parent_id as to
"
;
sql
+=
"
FROM tree
"
;
sql
+=
"
limit 100;
"
;
/*
too root
WITH RECURSIVE org(id, display_name, parent_organisation_id) AS (
SELECT id, display_name, parent_organisation_id FROM cmdb.organisation org WHERE org.id = 6
UNION
SELECT sub.id, sub.display_name, sub.parent_organisation_id
FROM org, cmdb.organisation sub
WHERE sub.id = org.parent_organisation_id
)
SELECT id, display_name, parent_organisation_id
FROM org
limit 10
*/
var
centralNode
=
[
req
.
params
[
'
centralNode
'
]];
client
.
query
(
sql
,
centralNode
,
function
(
err
,
result
){
done
();
if
(
err
){
return
res
.
json
(
JSON
.
parse
(
'
{"Result":"ERROR", "Message":"
'
+
sql
+
'
"}
'
));
}
else
{
return
res
.
json
(
result
.
rows
);
}
});
});
});
router
.
post
(
'
/list/:parent_id
'
,
(
req
,
res
,
next
)
=>
{
var
pool
=
new
pg
.
Pool
({
connectionString
:
connectionString
,
});
// Get a Postgres client from the connection pool
pool
.
connect
(
function
(
err
,
client
,
done
){
// Handle connection errors
if
(
err
)
{
done
();
return
res
.
status
(
500
).
json
({
success
:
false
,
data
:
err
});
}
// SQL Query > Select Data
var
sql
=
"
select * ,(select count(*) as childs from cmdb.ci child where child.parent_id = parent.id and family_type_id=0) as childs
"
;
sql
+=
"
from cmdb.ci parent where parent.family_type_id=0 and parent.id>0 and parent.parent_id = $1;
"
;
var
parameter
=
req
.
params
[
'
parent_id
'
];
client
.
query
(
sql
,
[
parameter
],
function
(
err
,
result
){
done
();
if
(
err
){
return
res
.
json
(
JSON
.
parse
(
'
{"Result":"ERROR", "Message":"
'
+
sql
+
'
"}
'
));
}
else
{
var
response
=
'
{"Result":"OK", "Records":
'
;
response
+=
JSON
.
stringify
(
result
.
rows
);
response
+=
'
,"TotalRecordCount":
'
+
result
.
rowCount
+
'
}
'
;
return
res
.
json
(
JSON
.
parse
(
response
));
}
});
});
});
router
.
post
(
'
/parents
'
,
(
req
,
res
,
next
)
=>
{
var
pool
=
new
pg
.
Pool
({
connectionString
:
connectionString
,
});
// Get a Postgres client from the connection pool
pool
.
connect
(
function
(
err
,
client
,
done
){
// Handle connection errors
if
(
err
)
{
done
();
return
res
.
status
(
500
).
json
({
success
:
false
,
data
:
err
});
}
// SQL Query > Select Data
var
sql
=
"
select display_name as
\"
DisplayText
\"
, id as
\"
Value
\"
from cmdb.ci_family_organisation order by id;
"
;
client
.
query
(
sql
,
[],
function
(
err
,
result
){
done
();
if
(
err
){
return
res
.
json
(
JSON
.
parse
(
'
{"Result":"ERROR", "Message":"/parents"}
'
));
}
else
{
var
response
=
'
{"Result":"OK", "Options":
'
;
response
+=
JSON
.
stringify
(
result
.
rows
);
response
+=
'
,"TotalRecordCount":
'
+
result
.
rowCount
+
'
}
'
;
return
res
.
json
(
JSON
.
parse
(
response
));
}
});
});
});
router
.
post
(
'
/update
'
,
(
req
,
res
,
next
)
=>
{
var
id
=
req
.
body
.
id
;
var
display_name
=
req
.
body
.
display_name
;
var
description
=
req
.
body
.
description
;
var
organisation_type
=
req
.
body
.
organisation_type
;
var
organisation_level
=
req
.
body
.
organisation_level
;
var
parent_organisation_id
=
req
.
body
.
parent_organisation_id
;
var
pool
=
new
pg
.
Pool
({
connectionString
:
connectionString
});
// Get a Postgres client from the connection pool
pool
.
connect
(
function
(
err
,
client
,
done
){
// Handle connection errors
if
(
err
)
{
done
();
return
res
.
status
(
500
).
json
({
success
:
false
,
data
:
err
});
}
// SQL Query > Select Data
var
sql
=
"
update cmdb.organisation set display_name = $2, description = $3, organisation_type = $4, organisation_level = $5, parent_organisation_id = $6 where id=$1
"
;
var
values
=
[
id
,
display_name
,
description
,
organisation_type
,
organisation_level
,
parent_organisation_id
];
console
.
log
(
sql
);
client
.
query
(
sql
,
values
,
function
(
err
,
result
){
done
();
if
(
err
){
return
res
.
json
(
JSON
.
parse
(
'
{"Result":"ERROR", "Message":"/update"}
'
));
}
else
{
var
response
=
'
{"Result":"OK", "Records":
'
;
response
+=
JSON
.
stringify
(
result
.
rows
);
response
+=
'
,"TotalRecordCount":
'
+
result
.
rowCount
+
'
}
'
;
return
res
.
json
(
JSON
.
parse
(
response
));
}
});
});
});
module
.
exports
=
router
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment