Skip to content

Commit

Permalink
GET /mappings: Fix edge case with direction=both (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Oct 17, 2024
1 parent c1be0bd commit 08fa06b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
32 changes: 19 additions & 13 deletions services/mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export class MappingService {
} else {
result = fromTo == "from" ? "to" : "from"
}
count -= 1
return result
}
// Converts value to regex object for MongoDB if it ends with `*`.
Expand Down Expand Up @@ -121,10 +120,12 @@ export class MappingService {
}

// Handle from/fromScheme/to/toScheme here
let criteria = ["from", "to"].map(part => {
count = direction == "both" ? 2 : 1
let or = []
while (count > 0) {
let criteria = []
let or = []
count = direction == "both" ? 2 : 1
while (count > 0) {
let and = []
for (const part of ["from", "to"]) {
const conceptOr = [], schemeOr = []
// Depending on `count` and `direction`, the value of `side` will either be "from" or "to"
const side = fromTo(part)
Expand All @@ -148,16 +149,21 @@ export class MappingService {
schemeOr.push({ [`${side}Scheme.uri`]: uri })
schemeOr.push({ [`${side}Scheme.notation`]: uri })
}
if (conceptOr.length && schemeOr.length) {
or.push({ $and: [{ $or: conceptOr }, { $or: schemeOr }] })
} else if (conceptOr.length) {
or = or.concat(conceptOr)
} else if (schemeOr.length) {
or = or.concat(schemeOr)
if (conceptOr.length) {
and.push({ $or: conceptOr })
}
if (schemeOr.length) {
and.push({ $or: schemeOr })
}
}
return { $or: or }
}).filter(entry => entry.$or.length > 0)
if (and.length) {
or.push({ $and: and })
}
count -= 1
}
if (or.length) {
criteria.push({ $or: or })
}
if (identifier) {
// Add identifier to criteria
criteria.push({ $or: identifier.split("|").map(id => ({ $or: [{ identifier: id }, { uri: id }] })) })
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,17 @@ describe("Express Server", () => {
assert.strictEqual(res.body?.length, 0)
})

it("should handle direction=both correctly for edge case with from and toScheme (#219)", async () => {
const res = await chai.request.execute(server.app)
.get("/mappings")
.query({
from: "612.112",
toScheme: "DDC",
direction: "both",
})
assert.strictEqual(res.body?.length, 0)
})

it("should GET only mappings from GND", done => {
// Add mappings to database
cpexec("yes | NODE_ENV=test ./bin/reset.js -t mappings && NODE_ENV=test ./bin/import.js mappings ./test/mappings/mappings-ddc.json", (err) => {
Expand Down

0 comments on commit 08fa06b

Please sign in to comment.