Skip to content

Commit 08fa06b

Browse files
committed
GET /mappings: Fix edge case with direction=both (#219)
1 parent c1be0bd commit 08fa06b

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

services/mappings.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ export class MappingService {
8080
} else {
8181
result = fromTo == "from" ? "to" : "from"
8282
}
83-
count -= 1
8483
return result
8584
}
8685
// Converts value to regex object for MongoDB if it ends with `*`.
@@ -121,10 +120,12 @@ export class MappingService {
121120
}
122121

123122
// Handle from/fromScheme/to/toScheme here
124-
let criteria = ["from", "to"].map(part => {
125-
count = direction == "both" ? 2 : 1
126-
let or = []
127-
while (count > 0) {
123+
let criteria = []
124+
let or = []
125+
count = direction == "both" ? 2 : 1
126+
while (count > 0) {
127+
let and = []
128+
for (const part of ["from", "to"]) {
128129
const conceptOr = [], schemeOr = []
129130
// Depending on `count` and `direction`, the value of `side` will either be "from" or "to"
130131
const side = fromTo(part)
@@ -148,16 +149,21 @@ export class MappingService {
148149
schemeOr.push({ [`${side}Scheme.uri`]: uri })
149150
schemeOr.push({ [`${side}Scheme.notation`]: uri })
150151
}
151-
if (conceptOr.length && schemeOr.length) {
152-
or.push({ $and: [{ $or: conceptOr }, { $or: schemeOr }] })
153-
} else if (conceptOr.length) {
154-
or = or.concat(conceptOr)
155-
} else if (schemeOr.length) {
156-
or = or.concat(schemeOr)
152+
if (conceptOr.length) {
153+
and.push({ $or: conceptOr })
154+
}
155+
if (schemeOr.length) {
156+
and.push({ $or: schemeOr })
157157
}
158158
}
159-
return { $or: or }
160-
}).filter(entry => entry.$or.length > 0)
159+
if (and.length) {
160+
or.push({ $and: and })
161+
}
162+
count -= 1
163+
}
164+
if (or.length) {
165+
criteria.push({ $or: or })
166+
}
161167
if (identifier) {
162168
// Add identifier to criteria
163169
criteria.push({ $or: identifier.split("|").map(id => ({ $or: [{ identifier: id }, { uri: id }] })) })

test/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,17 @@ describe("Express Server", () => {
793793
assert.strictEqual(res.body?.length, 0)
794794
})
795795

796+
it("should handle direction=both correctly for edge case with from and toScheme (#219)", async () => {
797+
const res = await chai.request.execute(server.app)
798+
.get("/mappings")
799+
.query({
800+
from: "612.112",
801+
toScheme: "DDC",
802+
direction: "both",
803+
})
804+
assert.strictEqual(res.body?.length, 0)
805+
})
806+
796807
it("should GET only mappings from GND", done => {
797808
// Add mappings to database
798809
cpexec("yes | NODE_ENV=test ./bin/reset.js -t mappings && NODE_ENV=test ./bin/import.js mappings ./test/mappings/mappings-ddc.json", (err) => {

0 commit comments

Comments
 (0)