Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handler for LM services #189

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions conf/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,52 @@ module.exports = {
consumer_secret: 'xxxxx',
scope: 'fastighetochsamfallighet_direkt_v31_read'
},
lmservices: {
apps: {
defaultApp: {
url: 'https://api.lantmateriet.se/',
url_token: "https://apimanager.lantmateriet.se/oauth2/token",
url_revoke: "https://apimanager.lantmateriet.se/oauth2/revoke",
consumer_key: 'XXXXXXXXXXXXXXXXXXXXXXXXXXX',
consumer_secret: 'XXXXXXXXXXXXXXXXXXXXXXXXXXX',
scope: 'belagenhetsadress_direkt_v42_read byggnad_direkt_v3_read fastighetochsamfallighet_direkt_v31_read gemensamhetsanlaggning_direkt_v21_read inskrivning_direkt_v3_read markreglerande_bestammelse_direkt_v2_read rattighet_direkt_v21_read taxering_direkt'
}
},
services: {
belagenhetsadress: {
app: 'defaultApp',
url: 'https://api.lantmateriet.se/distribution/produkter/belagenhetsadress/v4.2'
},
byggnad: {
app: 'defaultApp',
url: 'https://api.lantmateriet.se/distribution/produkter/byggnad/v3'
},
fastighetsamfallighet: {
app: 'defaultApp',
url: 'https://api.lantmateriet.se/distribution/produkter/fastighetsamfallighet/v3.1'
},
gemensamhetsanlaggning: {
app: 'defaultApp',
url: 'https://api.lantmateriet.se/distribution/produkter/gemensamhetsanlaggning/v2.1'
},
inskrivning: {
app: 'defaultApp',
url: 'https://api.lantmateriet.se/distribution/produkter/inskrivning/v3'
},
markreglerandebestammelse: {
app: 'defaultApp',
url: 'https://api.lantmateriet.se/distribution/produkter/markreglerandebestammelse/v2'
},
rattighet: {
app: 'defaultApp',
url: 'https://api.lantmateriet.se/distribution/produkter/rattighet/v2.1'
},
taxering: {
app: 'defaultApp',
url: 'https://api.lantmateriet.se/distribution/produkter/taxering/v3'
}
}
},
cors: {
origin: '*',
methods: ['GET', 'PUT', 'POST', 'OPTIONS', 'PATCH', 'DELETE'],
Expand Down
229 changes: 229 additions & 0 deletions handlers/lmservices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
const axios = require('axios').default;
const conf = require('../conf/config');
const lmtokenhandler = require('../handlers/lmtokenhandler');

function handleError(err) {
console.log(err);
}

const makeRequest = async function makeRequest(opt) {
return new Promise((resolve, reject) => {
axios({
url: opt.url,
method: opt.method,
headers: opt.headers,
data: opt.data
})
.then(response => {
resolve(response.data);
}).catch(err => {
console.log(err);
reject('Promise is rejected');
})
})
}

const lmservices = async function lmservices(params = {}) {
const {
typ,
fastighet,
id = [],
includeData = 'total'
} = params;
let returnObj = {};
if (conf && conf.lmservices && conf.lmservices.services && conf.lmservices.services[typ]) {
const serviceConf = conf.lmservices.services[typ];
if (conf.lmservices.apps && conf.lmservices.apps[serviceConf.app]) {
const appConf = conf.lmservices.apps[serviceConf.app];
const tokenObject = await lmtokenhandler(appConf);
if (tokenObject) {
if (typ === 'belagenhetsadress') {
if (fastighet) {
const response = await makeRequest({
url: `${serviceConf.url}/registerenhet/${fastighet}?includeData=${includeData}`,
method: 'GET',
headers: {
'Authorization': `Bearer ${tokenObject.token}`
}
}).catch(handleError);
returnObj.belagenhetsadress = response;
}
}
if (typ === 'byggnad') {
let ids = id;
if (fastighet) {
ids = await makeRequest({
url: `${serviceConf.url}/referens/beror/${fastighet}`,
method: 'GET',
headers: {
'Authorization': `Bearer ${tokenObject.token}`
}
}).catch(handleError);
}
if (ids && ids.length > 0) {
const response = await makeRequest({
url: `${serviceConf.url}/?includeData=${includeData}`,
method: 'POST',
headers: {
'Authorization': `Bearer ${tokenObject.token}`
},
data: ids
}).catch(handleError);
returnObj.byggnad = response;
}
}
if (typ === 'fastighetsamfallighet') {
if (fastighet) {
const response = await makeRequest({
url: `${serviceConf.url}/${fastighet}?includeData=${includeData}`,
method: 'GET',
headers: {
'Authorization': `Bearer ${tokenObject.token}`
}
}).catch(handleError);
returnObj.fastighetsamfallighet = response;
}
}
if (typ === 'gemensamhetsanlaggning') {
let ids = id;
if (fastighet) {
const refs = await makeRequest({
url: `${serviceConf.url}/referens/delagande/${fastighet}`,
method: 'GET',
headers: {
'Authorization': `Bearer ${tokenObject.token}`
}
}).catch(handleError);
if (refs) {
ids = refs.map((ref) => ref.objektidentitet)
}
}
if (ids && ids.length > 0) {
const response = await makeRequest({
url: `${serviceConf.url}/?includeData=${includeData}`,
method: 'POST',
headers: {
'Authorization': `Bearer ${tokenObject.token}`
},
data: ids
}).catch(handleError);
if (response.features?.length > 0) {
for (const ga of response.features) {
const response2 = await lmservices({ typ: 'rattighet', fastighet: ga.id });
if (response2.rattighet?.features?.length > 0) {
ga.rattighet = response2.rattighet;
}
}
}
returnObj.gemensamhetsanlaggning = response;
}
}
if (typ === 'inskrivning') {
if (fastighet) {
const response = await makeRequest({
url: `${serviceConf.url}/beror/${fastighet}?includeData=${includeData}`,
method: 'GET',
headers: {
'Authorization': `Bearer ${tokenObject.token}`
}
}).catch(handleError);
returnObj.inskrivning = response;
}
}
if (typ === 'markreglerandebestammelse') {
let ids = id;
if (fastighet) {
const refs = await makeRequest({
url: `${serviceConf.url}/referens/beror/${fastighet}`,
method: 'GET',
headers: {
'Authorization': `Bearer ${tokenObject.token}`
}
}).catch(handleError);
if (refs) {
ids = refs.map((ref) => ref.objektidentitet)
}
}
if (ids && ids.length > 0) {
const response = await makeRequest({
url: `${serviceConf.url}/?includeData=${includeData}`,
method: 'POST',
headers: {
'Authorization': `Bearer ${tokenObject.token}`
},
data: ids
}).catch(handleError);
returnObj.markreglerandebestammelse = response;
}
}
if (typ === 'rattighet') {
let ids;
if (id) {
ids = { "id": id }
}
if (fastighet) {
const refs = await makeRequest({
url: `${serviceConf.url}/referens/beror/${fastighet}`,
method: 'GET',
headers: {
'Authorization': `Bearer ${tokenObject.token}`
}
}).catch(handleError);
if (refs) {
ids = {
"id": refs.map((ref) => ref.objektidentitet)
}
}
}
if (ids && ids.id && ids.id.length > 0) {
const response = await makeRequest({
url: `${serviceConf.url}/?includeData=${includeData}`,
method: 'POST',
headers: {
'Authorization': `Bearer ${tokenObject.token}`
},
data: ids
}).catch(handleError);
returnObj.rattighet = response;
}
}
if (typ === 'taxering') {
let ids;
if (id && id.length > 0) {
ids = {
"taxeringsenhetsnummer": id
}
}
if (fastighet) {
const refs = await makeRequest({
url: `${serviceConf.url}/referens/beror/${fastighet}`,
method: 'GET',
headers: {
'Authorization': `Bearer ${tokenObject.token}`
}
}).catch(handleError);
if (refs) {
ids = {
"taxeringsenhetsnummer": refs.taxeringsenhetsreferens.map((ref) => ref.taxeringsenhetsnummer)
}
}
}
if (ids && ids.taxeringsenhetsnummer && ids.taxeringsenhetsnummer.length > 0) {
const response = await makeRequest({
url: `${serviceConf.url}/?includeData=${includeData}`,
method: 'POST',
headers: {
'Authorization': `Bearer ${tokenObject.token}`
},
data: ids
}).catch(handleError);
returnObj.taxering = response;
}
}
} else { returnObj = { error: 'No token' } }
} else { returnObj = { error: 'Error in configuration' } }
} else { returnObj = { error: 'Error in configuration' } }
return returnObj;
}
module.exports = lmservices;

68 changes: 34 additions & 34 deletions handlers/lmtokenhandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ const axios = require('axios').default;
const tokens = {};

const revokeToken = async function revokeToken(proxyOptions) {
if(!tokens[proxyOptions.id]){
if (!tokens[proxyOptions.id]) {
return;
}
return new Promise((resolve, reject) => {
axios({
url: proxyOptions.url_revoke,
method: 'POST',
headers: {
'Authorization': 'Basic ' + Buffer.from(proxyOptions.consumer_key + ':' + proxyOptions.consumer_secret).toString('base64')
},
params: {
'token': tokens[proxyOptions.id].token
}
})
url: proxyOptions.url_revoke,
method: 'POST',
headers: {
'Authorization': 'Basic ' + Buffer.from(proxyOptions.consumer_key + ':' + proxyOptions.consumer_secret).toString('base64')
},
params: {
'token': tokens[proxyOptions.id].token
}
})
.then(response => {
delete tokens[proxyOptions.id];
resolve();
Expand All @@ -30,16 +30,16 @@ const revokeToken = async function revokeToken(proxyOptions) {
const createToken = async function createToken(proxyOptions) {
return new Promise((resolve, reject) => {
axios({
url: proxyOptions.url_token,
method: 'POST',
headers: {
'Authorization': 'Basic ' + Buffer.from(proxyOptions.consumer_key + ':' + proxyOptions.consumer_secret).toString('base64')
},
params: {
'scope': proxyOptions.scope || 'default',
'grant_type': 'client_credentials'
}
})
url: proxyOptions.url_token,
method: 'POST',
headers: {
'Authorization': 'Basic ' + Buffer.from(proxyOptions.consumer_key + ':' + proxyOptions.consumer_secret).toString('base64')
},
params: {
'scope': proxyOptions.scope || 'default',
'grant_type': 'client_credentials'
}
})
.then(response => {
resolve({
token: response.data.access_token,
Expand All @@ -53,21 +53,21 @@ const createToken = async function createToken(proxyOptions) {
}

const getToken = async function getToken(proxyOptions) {
const options = proxyOptions;
const id = options.id || 'default';
let tokenObject = tokens[id];
if(!tokenObject){
tokenObject = await createToken(options);
tokens[id] = tokenObject;
return tokenObject;
} else if (!tokenObject.tokenExpires || Math.floor(Date.now() / 1000) > tokenObject.tokenExpires) {
if (tokenObject.tokenExpires && Math.floor(Date.now() / 1000) > tokenObject.tokenExpires) {
await revokeToken(options);
}
tokenObject = await createToken(options);
tokens[id] = tokenObject;
return tokenObject;
const options = proxyOptions;
const id = options?.id || 'default';
let tokenObject = tokens[id];
if (!tokenObject) {
tokenObject = await createToken(options);
tokens[id] = tokenObject;
return tokenObject;
} else if (!tokenObject.tokenExpires || Math.floor(Date.now() / 1000) > tokenObject.tokenExpires) {
if (tokenObject.tokenExpires && Math.floor(Date.now() / 1000) > tokenObject.tokenExpires) {
await revokeToken(options);
}
tokenObject = await createToken(options);
tokens[id] = tokenObject;
return tokenObject;
}
return tokenObject;
}

Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.