Skip to content

Commit 9fc7688

Browse files
authored
fix: correct way to join attachments with datasets (#2131)
<!-- Follow semantic-release guidelines for the PR title, which is used in the changelog. Title should follow the format `<type>(<scope>): <subject>`, where - Type is one of: build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|BREAKING CHANGE - Scope (optional) describes the place of the change (eg a particular milestone) and is usually omitted - subject should be a non-capitalized one-line description in present imperative tense and not ending with a period See https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines for more details. --> ## Description <!-- Short description of the pull request --> Fixes the way a lookup on Attachments from Datasets is done, issue #2130 ## Motivation <!-- Background on use case, changes needed --> ## Fixes <!-- Please provide a list of the issues fixed by this PR --> * Bug fixed (#2130) the lookup structure is now reflecting the attachment's true output dto ## Changes: <!-- Please provide a list of the changes implemented by this PR --> * changes made ## Tests included - [x] Included for each change/fix? - [ ] Passing? <!-- Merge will not be approved unless tests pass --> ## Documentation - [ ] swagger documentation updated (required for API changes) - [ ] official documentation updated ### official documentation info <!-- If you have updated the official documentation, please provide PR # and URL of the updated pages -->
2 parents 04b816e + 81fa2dd commit 9fc7688

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/datasets/types/dataset-lookup.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,28 @@ export const DATASET_LOOKUP_FIELDS: Record<
5050
attachments: {
5151
$lookup: {
5252
from: "Attachment",
53-
localField: "pid",
54-
foreignField: "datasetId",
5553
as: "",
54+
let: { pid: "$pid" },
55+
pipeline: [
56+
{
57+
$match: {
58+
$expr: {
59+
$anyElementTrue: {
60+
$map: {
61+
input: "$relationships",
62+
as: "relationship",
63+
in: {
64+
$and: [
65+
{ $eq: ["$$relationship.targetId", "$$pid"] },
66+
{ $eq: ["$$relationship.targetType", "dataset"] },
67+
],
68+
},
69+
},
70+
},
71+
},
72+
},
73+
},
74+
],
5675
},
5776
},
5877
samples: {

test/DatasetV4Access.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const utils = require("./LoginUtils");
55
const { TestData } = require("./TestData");
6+
const { v4: uuidv4 } = require("uuid");
67

78
let user1Token = null;
89
let user2Token = null;
@@ -18,13 +19,15 @@ let origDatablockData1 = {
1819
...TestData.OrigDataBlockCorrect1,
1920
datasetId: null,
2021
};
22+
let attachmentId = null;
2123

2224
describe("2700: Datasets v4 access tests", () => {
2325
before(async () => {
2426
db.collection("Dataset").deleteMany({});
2527
db.collection("Proposal").deleteMany({});
2628
db.collection("Instrument").deleteMany({});
2729
db.collection("Sample").deleteMany({});
30+
db.collection("Attachment").deleteMany({});
2831

2932
accessTokenAdminIngestor = await utils.getToken(appUrl, {
3033
username: "adminIngestor",
@@ -126,6 +129,31 @@ describe("2700: Datasets v4 access tests", () => {
126129
.send({ ...TestData.CustomDatasetCorrect })
127130
.auth(accessTokenAdminIngestor, { type: "bearer" })
128131
.expect(TestData.EntryCreatedStatusCode);
132+
133+
const attachment = {
134+
...TestData.AttachmentCorrectV4,
135+
relationships: [
136+
{
137+
targetId: derivedDatasetMinPid,
138+
targetType: "dataset",
139+
},
140+
{
141+
targetId: sampleId,
142+
targetType: "sample",
143+
},
144+
],
145+
aid: uuidv4(),
146+
};
147+
148+
await request(appUrl)
149+
.post("/api/v4/attachments")
150+
.send(attachment)
151+
.auth(accessTokenAdminIngestor, { type: "bearer" })
152+
.expect(TestData.EntryCreatedStatusCode)
153+
.expect("Content-Type", /json/)
154+
.then((res) => {
155+
attachmentId = res.body.aid;
156+
});
129157
});
130158

131159
async function deleteDataset(item) {
@@ -218,6 +246,7 @@ describe("2700: Datasets v4 access tests", () => {
218246
"samples",
219247
"origdatablocks",
220248
"datablocks",
249+
"attachments",
221250
],
222251
};
223252

@@ -256,6 +285,13 @@ describe("2700: Datasets v4 access tests", () => {
256285
const [origdatablock] = firstDataset.origdatablocks;
257286
origdatablock.should.have.property("_id");
258287
origdatablock._id.should.be.eq(origDatablockId1);
288+
289+
firstDataset.should.have.property("attachments");
290+
firstDataset.attachments.should.be.a("array");
291+
firstDataset.attachments.should.have.length(1);
292+
const [attachments] = firstDataset.attachments;
293+
attachments.should.have.property("aid");
294+
attachments.aid.should.be.eq(attachmentId);
259295
});
260296
});
261297
});

0 commit comments

Comments
 (0)