Skip to content
Open
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
17 changes: 10 additions & 7 deletions src/common/interfaces/common.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export interface IProposalAcceptedMessage {
}

export interface ILimitsFilter {
limit: number;
skip: number;
order: string;
limit?: number;
skip?: number;
order?: string;
}

export interface IFilters<T, Y = null> {
Expand Down Expand Up @@ -69,14 +69,17 @@ export interface IDatafileFilter {
type?: string;
}

export type IFiltersV4<T, Y = null, Z = null> = IFilters<T, Y> & {
export type IFiltersV4<T, Y = null, Z = null> = Pick<
IFilters<T, Y>,
"where"
> & {
include?: Z;
limits?: ILimitsV4<T>;
fields?: (keyof Y)[];
};

export interface ILimitsV4<T = null> {
limit: number;
skip: number;
sort: Record<keyof T, "asc" | "desc">;
limit?: number;
skip?: number;
sort?: Record<keyof T, "asc" | "desc">;
}
32 changes: 8 additions & 24 deletions src/datasets/datasets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,35 +298,19 @@ export class DatasetsService {
}

async findOneComplete(
filter: FilterQuery<DatasetDocument>,
filter: IDatasetFiltersV4<DatasetDocument, IDatasetFields>,
): Promise<OutputDatasetDto | null> {
const whereFilter: FilterQuery<DatasetDocument> = filter.where ?? {};
const fieldsProjection: string[] = filter.fields ?? {};
const limits: QueryOptions<DatasetDocument> = filter.limits ?? {
filter.limits = filter.limits ?? {
skip: 0,
sort: { createdAt: "desc" },
sort: { createdAt: "desc" } as Record<
keyof DatasetDocument,
"asc" | "desc"
>,
};

const pipeline: PipelineStage[] = [{ $match: whereFilter }];
if (!isEmpty(fieldsProjection)) {
const projection = parsePipelineProjection(fieldsProjection);
pipeline.push({ $project: projection });
}

if (!isEmpty(limits.sort)) {
const sort = parsePipelineSort(limits.sort);
pipeline.push({ $sort: sort });
}

pipeline.push({ $skip: limits.skip || 0 });

this.addLookupFields(pipeline, filter.include);

const [data] = await this.datasetModel
.aggregate<OutputDatasetDto | undefined>(pipeline)
.exec();
const [data] = await this.findAllComplete(filter);

return data || null;
return (data as OutputDatasetDto) || null;
}

async count(
Expand Down
8 changes: 5 additions & 3 deletions src/datasets/interfaces/dataset-filters.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export interface IDatasetRelation {
scope: IDatasetScopes;
}

export type IDatasetFiltersV4<T, Y = null> = IFiltersV4<T, Y> & {
include?: DatasetLookupKeysEnum[] | IDatasetRelation[];
};
export type IDatasetFiltersV4<T, Y = null> = IFiltersV4<
T,
Y,
DatasetLookupKeysEnum[] | IDatasetRelation[]
>;
3 changes: 0 additions & 3 deletions test/DatasetV4.js
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,6 @@ describe("2500: Datasets v4 tests", () => {
},
},
include: ["all"],
fields: ["datasetName", "pid"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove the fields projection as a valid queryFilter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test (as the other) was wrong. It does not make sense to ask for these fields and then expect different fiedls to be present in the response IMO

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HayenNico The 'fields' filter is still present, as tested in other tests (eg 0207). It's used by findAllComplete, so it doesn't need special treatment in findOneComplete.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should keep this test though to document how fields interacts with includes. I guess the previous test was trying to show that the fields property only applied to dataset fields, and is overridden by includes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think this test case should stay in. It is true that the test doesn't work as expected for fields based on the current master branch, since the lookup was done differently in findOneComplete and findAllComplete (different ordering in aggregation pipeline, definitely a bug). This part was adressed in #2212, so at least the fixes for tests from that PR should be carried over to here.

limits: {
skip: 0,
sort: {
Expand All @@ -895,8 +894,6 @@ describe("2500: Datasets v4 tests", () => {

res.body.should.have.property("datasetName");
res.body.should.have.property("pid");
res.body.should.not.have.property("description");

res.body.should.have.property("pid");
res.body.should.have.property("instruments");
res.body.should.have.property("proposals");
Expand Down
3 changes: 0 additions & 3 deletions test/DatasetV4Public.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ describe("2600: Datasets v4 public endpoints tests", () => {
},
},
include: ["all"],
fields: ["datasetName", "pid"],
limits: {
skip: 0,
sort: {
Expand All @@ -444,8 +443,6 @@ describe("2600: Datasets v4 public endpoints tests", () => {

res.body.should.have.property("datasetName");
res.body.should.have.property("pid");
res.body.should.not.have.property("description");

res.body.should.have.property("pid");
res.body.should.have.property("instruments");
res.body.should.have.property("proposals");
Expand Down