Skip to content

Commit 160ce7a

Browse files
committed
refactor
1 parent 72d43f1 commit 160ce7a

3 files changed

Lines changed: 48 additions & 10 deletions

File tree

lib/domain/dtos/GetAllRunsDto.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@ const Joi = require('joi');
1515
const PaginationDto = require('./PaginationDto');
1616
const { RunFilterDto } = require('./filters/RunFilterDto.js');
1717
const { DtoFactory } = require('./DtoFactory');
18+
const { singleRunsCollectionCustomCheck } = require('./utils.js');
1819

1920
const QueryDto = Joi.object({
2021
filter: RunFilterDto,
2122
page: PaginationDto,
2223
sort: DtoFactory.order(['id', 'runNumber', 'text']),
2324
include: Joi.object({ effectiveQcFlags: Joi.boolean().custom((effectiveQcFlags, helpers) => {
2425
const [, { filter: { dataPassIds, simulationPassIds, lhcPeriodIds } = {} }] = helpers.state.ancestors;
25-
const runsCollectionFilters = [dataPassIds, simulationPassIds, lhcPeriodIds].filter(({ length } = {}) => length >= 1);
26-
if (runsCollectionFilters.length !== 1 || runsCollectionFilters[0].length !== 1) {
27-
return helpers.message('Including effectiveQcFlags is allowed only when filtering by one and exactly one of: ' +
28-
'dataPassIds, simulationPassIds, lhcPeriodIds with exactly one ID.');
29-
}
26+
27+
singleRunsCollectionCustomCheck(
28+
{ dataPassIds, simulationPassIds, lhcPeriodIds },
29+
helpers,
30+
'Including effectiveQcFlags is allowed only when ' +
31+
'the dataPassIds, simulationPassIds and lhcPeriodIds filters collectively contain exactly one ID',
32+
);
3033

3134
return effectiveQcFlags;
3235
}) }),

lib/domain/dtos/filters/RunFilterDto.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const { RUN_QUALITIES } = require('../../enums/RunQualities.js');
1717
const { IntegerComparisonDto, FloatComparisonDto } = require('./NumericalComparisonDto.js');
1818
const { RUN_CALIBRATION_STATUS } = require('../../enums/RunCalibrationStatus.js');
1919
const { RUN_DEFINITIONS } = require('../../enums/RunDefinition.js');
20+
const { singleRunsCollectionCustomCheck } = require('../utils.js');
2021

2122
const DetectorsFilterDto = Joi.object({
2223
operator: Joi.string().valid('or', 'and', 'none').required(),
@@ -128,12 +129,13 @@ exports.RunFilterDto = Joi.object({
128129
})
129130
.custom((detectorsQcObj, helpers) => {
130131
const [{ dataPassIds, simulationPassIds, lhcPeriodIds }] = helpers.state.ancestors;
131-
const runsCollectionFilters = [dataPassIds, simulationPassIds, lhcPeriodIds].filter(({ length } = {}) => length >= 1);
132132

133-
if (runsCollectionFilters.length !== 1 || runsCollectionFilters[0].length !== 1) {
134-
return helpers.message('Filtering by detector not-bad fraction is allowed only with exactly one of: ' +
135-
'dataPassIds, simulationPassIds, lhcPeriodIds with exactly one ID.');
136-
}
133+
singleRunsCollectionCustomCheck(
134+
{ dataPassIds, simulationPassIds, lhcPeriodIds },
135+
helpers,
136+
'Filtering by detector not-bad fraction is allowed only when ' +
137+
'the dataPassIds, simulationPassIds and lhcPeriodIds filters collectively contain exactly one ID',
138+
);
137139

138140
return detectorsQcObj;
139141
}),

lib/domain/dtos/utils.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license
3+
* Copyright CERN and copyright holders of ALICE O2. This software is
4+
* distributed under the terms of the GNU General Public License v3 (GPL
5+
* Version 3), copied verbatim in the file "COPYING".
6+
*
7+
* See http://alice-o2.web.cern.ch/license for full licensing information.
8+
*
9+
* In applying this license CERN does not waive the privileges and immunities
10+
* granted to it by virtue of its status as an Intergovernmental Organization
11+
* or submit itself to any jurisdiction.
12+
*/
13+
14+
/**
15+
* Used in Joi schemas to assert that the dataPassIds, simulationPassIds and lhcPeriodIds filters collectively contain exactly one ID
16+
*
17+
* @param {object} collections runs ids collections
18+
* @param {number[]} collections.dataPassIds data pass ids
19+
* @param {number[]} collections.simulationPassIds simulation pass ids
20+
* @param {number[]} collections.lhcPeriodIds LHC periods ids
21+
* @param {Joi.helpers} helpers joi helpers
22+
* @param {string} message to be send in case of filters refer to more than one runs collection
23+
* @returns {void}
24+
*/
25+
const singleRunsCollectionCustomCheck = ({ dataPassIds, simulationPassIds, lhcPeriodIds }, helpers, message) => {
26+
const runsCollectionFilters = [dataPassIds, simulationPassIds, lhcPeriodIds].filter(({ length } = {}) => length >= 1);
27+
28+
if (runsCollectionFilters.length !== 1 || runsCollectionFilters[0].length !== 1) {
29+
return helpers.message(message);
30+
}
31+
};
32+
33+
module.exports.singleRunsCollectionCustomCheck = singleRunsCollectionCustomCheck;

0 commit comments

Comments
 (0)