Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xsalonx committed Apr 10, 2024
1 parent c0d74e4 commit fc8b944
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions lib/server/services/qualityControlFlag/QcFlagService.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ class QcFlagService {

/**
* Validate QC flag timestamps
* @param {{fromTime:number, toTime: number}} timestamps QC flag timestamps
* @param {{from: number, to: number}} timestamps QC flag timestamps
* @param {Run} targetRun run
* @throws {BadParameterError}
* @return {void}
* @return {{fromTime: number, toTime: number}} validated timestamps.
* If null was provided, given timestamp is replaced by trigger timestamps of the given run
*/
_validateQcFlagPeriod({ fromTime, toTime }, targetRun) {
_validateQcFlagPeriod({ from, to }, targetRun) {
const fromTime = from ?? targetRun.startTime;
const toTime = to ?? targetRun.endTime;

if (fromTime >= toTime) {
throw new BadParameterError('Parameter "to" timestamp must be greater than "from" timestamp');
}
Expand All @@ -72,18 +76,21 @@ class QcFlagService {
// eslint-disable-next-line max-len
throw new BadParameterError(`Given QC flag period (${fromTime}, ${toTime}) is out of run trigger period (${targetRun.startTime}, ${targetRun.endTime})`);
}
return { fromTime, toTime };
}

/**
* Validate QC flag DPL detector
* @param {dplDetector} dplDetector DPL detector
* @param {number} dplDetectorId DPL detector
* @throws {BadParameterError}
* @return {void}
*/
_validateQcFlagDplDetector(dplDetector) {
async _validateQcFlagDplDetector(dplDetectorId) {
const dplDetector = await DplDetectorRepository.findOne({ where: { id: dplDetectorId } });
if (!dplDetector?.name || NON_QC_DETECTORS.has(dplDetector.name)) {
throw new BadParameterError(`Invalid DPL detector (${dplDetector.name})`);
}
return dplDetector;
}

/**
Expand Down Expand Up @@ -117,8 +124,7 @@ class QcFlagService {
const user = await getUserOrFail({ userId, externalUserId });

// Check associations
const dplDetector = await DplDetectorRepository.findOne({ where: { id: dplDetectorId } });
this._validateQcFlagDplDetector(dplDetector);
const dplDetector = await this._validateQcFlagDplDetector(dplDetector);

const targetRun = await RunRepository.findOne({
subQuery: false,
Expand Down Expand Up @@ -146,10 +152,7 @@ class QcFlagService {
throw new BadParameterError(`There is not association between data pass with this id (${dataPassId}), run with this number (${runNumber}) and detector with this name (${dplDetector.name})`);
}

const fromTime = from ?? targetRun.startTime;
const toTime = to ?? targetRun.endTime;

this._validateQcFlagPeriod({ fromTime, toTime }, targetRun);
const { fromTime, toTime } = this._validateQcFlagPeriod({ from, to }, targetRun);

// Insert
const newInstance = await QcFlagRepository.insert({
Expand Down Expand Up @@ -201,8 +204,7 @@ class QcFlagService {
const user = await getUserOrFail({ userId, externalUserId });

// Check associations
const dplDetector = await DplDetectorRepository.findOne({ where: { id: dplDetectorId } });
this._validateQcFlagDplDetector(dplDetector);
const dplDetector = await this._validateQcFlagDplDetector(dplDetector);

const targetRun = await RunRepository.findOne({
subQuery: false,
Expand Down Expand Up @@ -230,10 +232,7 @@ class QcFlagService {
throw new BadParameterError(`There is not association between simulation pass with this id (${simulationPassId}), run with this number (${runNumber}) and detector with this name (${dplDetector.name})`);
}

const fromTime = from ?? targetRun.startTime;
const toTime = to ?? targetRun.endTime;

this._validateQcFlagPeriod({ fromTime, toTime }, targetRun);
const { fromTime, toTime } = this._validateQcFlagPeriod({ from, to }, targetRun);

// Insert
const newInstance = await QcFlagRepository.insert({
Expand Down

0 comments on commit fc8b944

Please sign in to comment.