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

feature: rajapinta projektin hakemiselle Velhosta #14

Merged
merged 4 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ out
backend/src/velho/aineistopalvelu
backend/src/velho/hakupalvelu
backend/src/velho/projektirekisteri
backend/src/velho/metadata.json

# Gradle
tools/velho/.gradle
tools/velho/build
!tools/velho/gradle/wrapper/gradle-wrapper.jar
tools/velho/buildSrc/.gradle
tools/velho/buildSrc/build

.report
.nyc_output
Expand Down
2 changes: 1 addition & 1 deletion .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
exit: true,
bail: true,
slow: 1000,
timeout:10000,
timeout:60000,
recursive: true,
file: ['./backend/test/setup.js']
}
26 changes: 23 additions & 3 deletions backend/integrationtest/velho/velhoClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { velho } from "../../src/velho/velhoClient";
import * as log from "loglevel";

describe("VelhoClient", () => {
let oid: string;
let name: string;

it("should authenticate to Velho", async () => {
expect(await velho.authenticate()).not.be.null;
});
Expand All @@ -13,9 +16,26 @@ describe("VelhoClient", () => {
const searchResult = await velho.searchProjects("tampere");
expect(searchResult).not.null;
expect(searchResult).not.be.empty;
log.info(searchResult[0]);
const exactSearchResult = await velho.searchProjects(searchResult[0].name, true);
const firstSearchResult = searchResult[0];
log.debug(firstSearchResult);
name = firstSearchResult.name;
oid = firstSearchResult.oid;
log.debug("nimi", name);
});

it("should list one exact project from Velho", async () => {
expect(oid).to.not.be.null;
expect(name).to.not.be.null;
const exactSearchResult = await velho.searchProjects(name, true);
expect(exactSearchResult).not.be.empty;
expect(exactSearchResult[0].oid).to.be.equal(searchResult[0].oid);
log.debug(exactSearchResult);
expect(exactSearchResult[0].oid).to.be.equal(oid);
});

it("should load project from Velho", async () => {
expect(oid).to.not.be.null;
const searchResult = await velho.loadProject(oid);
expect(searchResult).not.null;
log.debug(JSON.stringify(searchResult, null, 2));
});
});
4 changes: 4 additions & 0 deletions backend/src/apiHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getVelhoSuunnitelmasByName } from "./handler/getVelhoSuunnitelmasByName
import { identifyUser } from "./service/userService";
import { getCurrentUser } from "./handler/getCurrentUser";
import { listAllUsers } from "./handler/listAllUsers";
import { loadProjekti } from "./handler/projectHandler";

const logLevel = process.env.LOG_LEVEL ? process.env.LOG_LEVEL : "info";
log.setLevel(logLevel as any);
Expand All @@ -18,6 +19,7 @@ type AppSyncEventArguments = {
suunnitelmaId?: string;
suunnitelmaName?: string;
requireExactMatch?: boolean;
oid?: string;
};

export async function handleEvent(event: AppSyncResolverEvent<AppSyncEventArguments>) {
Expand All @@ -38,6 +40,8 @@ export async function handleEvent(event: AppSyncResolverEvent<AppSyncEventArgume
return await getCurrentUser();
case "listAllUsers":
return await listAllUsers();
case "lataaProjekti":
return await loadProjekti(event.arguments.oid);
default:
return null;
}
Expand Down
9 changes: 9 additions & 0 deletions backend/src/error/velhoError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class VelhoError extends Error {
constructor(m?: string, error?: any) {
super(m ? m + (error ? JSON.stringify(error.response?.data) : "") : "");

// Make the log show VelhoError instead of Error in the stack trace
this.name = new.target.name;
Object.setPrototypeOf(this, new.target.prototype);
}
}
8 changes: 8 additions & 0 deletions backend/src/handler/projectHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { requireVaylaUser } from "../service/userService";
import { velho } from "../velho/velhoClient";

export function loadProjekti(oid: string) {
requireVaylaUser();

return velho.loadProject(oid);
}
69 changes: 48 additions & 21 deletions backend/src/velho/velhoAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
import { VelhoHakuTulos } from "../api/apiModel";
import { Projekti, VelhoHakuTulos } from "../api/apiModel";
import { ProjektiProjekti, ProjektiProjektiOminaisuudetVaylamuotoEnum } from "./projektirekisteri";
// @ts-ignore
import { default as metadataJson } from "./metadata.json";

function adaptVaylamuoto(vaylamuodot: string[]) {
return vaylamuodot
.map((vaylamuoto) => {
switch (vaylamuoto) {
case "tie":
return "Tiesuunnitelma";
case "rata":
return "Ratasuunnitelma";
default:
return vaylamuoto;
}
})
.join(", ");
function extractValuesIntoMap(field: string) {
const values = {};
const definition = metadataJson.info["x-velho-nimikkeistot"][field];
const keyTitleMap = definition.nimikkeistoversiot[definition["uusin-nimikkeistoversio"]];
Object.keys(keyTitleMap).forEach((key) => {
return (values[key] = keyTitleMap[key].otsikko);
});
return values;
}

const metadata = (() => {
const tilat = extractValuesIntoMap("projekti/tila");
const vaiheet = extractValuesIntoMap("projekti/vaihe");
const organisaatiot = extractValuesIntoMap("projekti/organisaatio");
const toteutusAjankohdat = extractValuesIntoMap("projekti/arvioitu-toteutusajankohta");
return { tilat, vaiheet, organisaatiot, toteutusAjankohdat };
})();

function adaptVaylamuoto(vaylamuodot: Set<ProjektiProjektiOminaisuudetVaylamuotoEnum>) {
const values = [];
vaylamuodot.forEach((value) => values.push(`${value}`));
return values;
}

export function adaptSearchResults(searchResults: any): VelhoHakuTulos[] {
return searchResults.map((result) => {
return {
oid: result.oid,
name: result.ominaisuudet.nimi,
type: adaptVaylamuoto(result.ominaisuudet.vaylamuoto),
} as VelhoHakuTulos;
});
if (searchResults) {
return searchResults.map((result) => {
return {
oid: result.oid,
name: result.ominaisuudet.nimi,
type: result.ominaisuudet.vaylamuoto,
} as VelhoHakuTulos;
});
}
}

export function adaptProjecti(data: ProjektiProjekti): Projekti {
return {
__typename: "Projekti",
oid: "" + data.oid,
tila: metadata.tilat[`${data.ominaisuudet.tila}`],
tyyppi: metadata.vaiheet[`${data.ominaisuudet.vaihe}`],
nimi: data.ominaisuudet.nimi,
vaylamuoto: adaptVaylamuoto(data.ominaisuudet.vaylamuoto),
organisaatio: metadata.organisaatiot[`${data.ominaisuudet.tilaajaorganisaatio}`],
toteutusAjankohta: metadata.toteutusAjankohdat[`${data.ominaisuudet["arvioitu-toteutusajankohta"]}`],
};
}
87 changes: 69 additions & 18 deletions backend/src/velho/velhoClient.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import * as log from "loglevel";
import { config } from "../config";
import * as HakuPalvelu from "./hakupalvelu/api";
import * as HakuPalvelu from "./hakupalvelu";
import * as ProjektiRekisteri from "./projektirekisteri";
import { VelhoHakuTulos } from "../api/apiModel";
import { adaptSearchResults } from "./velhoAdapter";
import { Configuration } from "./hakupalvelu";
import { adaptProjecti, adaptSearchResults } from "./velhoAdapter";
import { VelhoError } from "../error/velhoError";
import { AxiosResponse } from "axios";

const axios = require("axios");

axios.interceptors.request.use((request) => {
log.debug("Request", JSON.stringify(request.headers) + "\n" + request.data);
return request;
});

axios.interceptors.response.use((response) => {
log.debug("Response", response.status + " " + response.statusText + "\n" + response.data);
return response;
});

axios.defaults.timeout = 28000;

const velhoApiURL = config.velhoApiURL;

function checkResponseIsOK(response: AxiosResponse, message: string) {
if (response.status !== 200) {
throw new VelhoError("Error while communicating with Velho: " + message + " Status:" + response?.statusText);
}
}

export class VelhoClient {
private accessTokenExpires;
private accessToken;
Expand All @@ -26,16 +46,17 @@ export class VelhoClient {
return this.accessToken;
}

public async searchProjects(name: string, requireExactMatch?: boolean): Promise<VelhoHakuTulos[]> {
public async searchProjects(term: string, requireExactMatch?: boolean): Promise<VelhoHakuTulos[]> {
try {
const hakuApi = await this.createHakuApi();
let searchClause;
if (requireExactMatch) {
searchClause = ["tekstikysely", ["projekti/projekti", "ominaisuudet", "nimi"], name];
searchClause = ["yhtasuuri", ["projekti/projekti", "ominaisuudet", "nimi"], term];
} else {
searchClause = ["sisaltaa-tekstin", ["projekti/projekti", "ominaisuudet", "nimi"], name];
// TODO: add kunta field into target field as well
searchClause = ["sisaltaa-tekstin", ["projekti/projekti", "ominaisuudet", "nimi"], term];
}
const result = await hakuApi.hakupalveluApiV1HakuKohdeluokatPost({
const response = await hakuApi.hakupalveluApiV1HakuKohdeluokatPost({
asetukset: {
palautettavat_kentat: [
["projekti/projekti", "oid"],
Expand All @@ -45,29 +66,59 @@ export class VelhoClient {
tyyppi: HakuPalvelu.HakulausekeAsetuksetTyyppiEnum.Kohdeluokkahaku,
jarjesta: [[["projekti/projekti", "ominaisuudet", "nimi"], "nouseva" as any]],
},
lauseke: searchClause,
lauseke: [
"ja",
[
"joukossa",
["projekti/projekti", "ominaisuudet", "vaihe"],
["vaihe/vaihe04", "vaihe/vaihe10", "vaihe/vaihe12"],
],
searchClause,
],
kohdeluokat: ["projekti/projekti"],
});
checkResponseIsOK(response, "searchProjects");
const data = response.data;
const resultCount = data?.osumia || 0;
if (requireExactMatch) {
log.info(result.data.osumia + " search results for exact term: " + name);
log.info(resultCount + " search results for exact term: " + term);
} else {
log.info(result.data.osumia + " search results for term: " + name);
log.info(resultCount + " search results for term: " + term);
}
return adaptSearchResults(result.data.osumat);
return adaptSearchResults(data.osumat);
} catch (e) {
throw new VelhoError(e.message, e);
}
}

public async loadProject(oid: string) {
const projektiApi = await this.createProjektiRekisteriApi();
let response;
try {
response = await projektiApi.projektirekisteriApiV1ProjektiProjektiOidGet(oid);
} catch (e) {
log.error(e);
throw new Error(e);
throw new VelhoError(e.message, e);
}
checkResponseIsOK(response, "loadProject with oid '" + oid + "'");
return adaptProjecti(response.data);
}

private async createHakuApi() {
return new HakuPalvelu.HakuApi(
new Configuration({
basePath: velhoApiURL,
baseOptions: { headers: { Authorization: "Bearer " + (await this.authenticate()) } },
})
return new HakuPalvelu.HakuApi(new HakuPalvelu.Configuration(await this.getVelhoApiConfiguration()));
}

private async createProjektiRekisteriApi() {
return new ProjektiRekisteri.ProjektiApi(
new ProjektiRekisteri.Configuration(await this.getVelhoApiConfiguration())
);
}

private async getVelhoApiConfiguration() {
return {
basePath: velhoApiURL,
baseOptions: { headers: { Authorization: "Bearer " + (await this.authenticate()) } },
};
}
}

export const velho = new VelhoClient();
16 changes: 16 additions & 0 deletions backend/test/velho/__snapshots__/velhoAdapter.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`VelhoAdapter should adapt project from Velho successfully 1`] = `
Object {
"__typename": "Projekti",
"nimi": "Kaupungin sisääntulotien (mt 123) parantaminen välillä valtatien 1 eritasoliittymä - Päätie, Helsinki",
"oid": "100",
"organisaatio": "Pirkanmaan ELY-keskus",
"tila": "Vastaanotettu",
"toteutusAjankohta": "Toteutettavissa arviolta noin 1 vuoden päästä nykyhetkestä",
"tyyppi": "Tiesuunnitelma/Tie- ja rakennussuunnitelma",
"vaylamuoto": Array [
"tie",
],
}
`;
Loading