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

feat: improve streeet search api #742

Merged
merged 4 commits into from
Jul 17, 2024
Merged
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
2 changes: 1 addition & 1 deletion addon/models/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export const countryOptions = [
"cy",
];

export const languageOptions = { de: 9901, fr: 9903, it: 9904 };
export const languageOptions = { de: 9901, rm: 9902, fr: 9903, it: 9904 };

export const periodOfConstructionOptions = [
8011, 8012, 8013, 8014, 8015, 8016, 8017, 8018, 8019, 8020, 8021, 8022, 8023,
Expand Down
51 changes: 51 additions & 0 deletions addon/services/languages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Service, { inject as service } from "@ember/service";
import { languageOptions } from "ember-ebau-gwr/models/options";

export default class LanguagesService extends Service {
@service config;

languages = {
AG: ["de"],
AR: ["de"],
AI: ["de"],
BL: ["de"],
BS: ["de"],
BE: ["de", "fr"],
FR: ["de", "fr"],
GE: ["de", "fr"],
GL: ["de"],
GR: ["de", "rm", "it"],
JU: ["fr"],
LU: ["de"],
NE: ["fr"],
NW: ["de"],
OW: ["de"],
SH: ["de"],
SZ: ["de"],
SO: ["de"],
SG: ["de"],
TI: ["it"],
TG: ["de"],
UR: ["de"],
VD: ["fr"],
VS: ["de", "fr"],
ZG: ["de"],
ZH: ["de"],
};

get availableLanguages() {
return this.config.cantonAbbreviation
? this.languages[this.config.cantonAbbreviation]
: ["de", "rm", "fr", "it"];
}

codeToLanguage(code) {
return Object.entries(languageOptions).find(
([, value]) => value === code,
)[0];
}

languageToCode(languageAbbreviation) {
return languageOptions[languageAbbreviation];
}
}
81 changes: 77 additions & 4 deletions addon/services/street.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { languageOptions } from "ember-ebau-gwr/models/options";
import { inject as service } from "@ember/service";
import { restartableTask, task } from "ember-concurrency";
import Street, { StreetList } from "ember-ebau-gwr/models/street";

import GWRService from "./gwr";

export default class StreetService extends GWRService {
@service languages;

cacheKey = "ESID";
cacheClass = Street;
cachedLanguageOverrides;

constructor(...args) {
super(...args);

this.resetCachedLanguages();
}

async get(ESID) {
if (!ESID) {
Expand All @@ -17,17 +27,80 @@ export default class StreetService extends GWRService {
return this.cache(street);
}

get primaryLanguage() {
return this.intl.primaryLocale?.split("-")?.[0];
}

get language() {
return languageOptions[this.intl.primaryLocale?.split("-")?.[0]];
return this.languages.languageToCode(this.primaryLanguage);
}

async search(query = {}) {
return super.search(query, query.EGID, {
@task
*_search(query) {
return yield super.search(query, query.EGID, {
xmlMethod: "getStreet",
urlPath: "streets",
listModel: StreetList,
listKey: "street",
searchKey: "streetWithoutStreetGeometryType",
});
}

resetCachedLanguages() {
this.cachedLanguageOverrides = new Set([]);
}

@restartableTask
*searchMultiple(query, useCachedLanguagesOnly) {
let results = [];

for (const lang of useCachedLanguagesOnly
? this.cachedLanguageOverrides
: this.languages.availableLanguages) {
const code = this.languages.languageToCode(lang);
czosel marked this conversation as resolved.
Show resolved Hide resolved
query.language = code;

results.push({
lang: [lang, code],
result: this._search.perform(query, query.EGID, {
xmlMethod: "getStreet",
urlPath: "streets",
listModel: StreetList,
listKey: "street",
searchKey: "streetWithoutStreetGeometryType",
}),
});
}

results = yield Promise.allSettled(
results.map(async ({ lang, result }) => {
return { lang, result: await result };
}),
);
return results
.filter(({ value }) => value.result?.length)
.reduce((previous, { value: { lang, result } }) => {
this.cachedLanguageOverrides.add(lang[0]);
return previous.concat(result);
}, []);
}

async search(query = {}) {
const lastQueryString =
this._search.lastPerformed?.args[0]?.description?.descriptionLong.replaceAll(
"*",
"",
) || "";
const currentQueryString =
query.description?.descriptionLong?.replaceAll("*", "") || "";

// In order to avoid unnecessary requests while typing (and thereby "refining"
// the search), we continue searching only with the languages for which we
// previously received results.
if (lastQueryString && currentQueryString.includes(lastQueryString)) {
return this.searchMultiple.perform(query, true);
czosel marked this conversation as resolved.
Show resolved Hide resolved
}
this.resetCachedLanguages();
return this.searchMultiple.perform(query);
}
}
37 changes: 37 additions & 0 deletions tests/unit/services/languages-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { setupTest } from "dummy/tests/helpers";
import { module, test } from "qunit";

module("Unit | Service | languages", function (hooks) {
setupTest(hooks);

test("available languages for a given canton", function (assert) {
const configService = this.owner.lookup("service:config");
const languagesService = this.owner.lookup("service:languages");

configService.cantonAbbreviation = "GR";

assert.deepEqual(languagesService.availableLanguages, ["de", "rm", "it"]);

configService.cantonAbbreviation = "BE";

assert.deepEqual(languagesService.availableLanguages, ["de", "fr"]);
});

test("transforms language abbrevation to gwr API language code", function (assert) {
const service = this.owner.lookup("service:languages");

assert.strictEqual(service.languageToCode("de"), 9901);
assert.strictEqual(service.languageToCode("rm"), 9902);
assert.strictEqual(service.languageToCode("fr"), 9903);
assert.strictEqual(service.languageToCode("it"), 9904);
});

test("transforms gwr API language code to language abbrevation", function (assert) {
const service = this.owner.lookup("service:languages");

assert.strictEqual(service.codeToLanguage(9901), "de");
assert.strictEqual(service.codeToLanguage(9902), "rm");
assert.strictEqual(service.codeToLanguage(9903), "fr");
assert.strictEqual(service.codeToLanguage(9904), "it");
});
});
Loading