-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathLock.controller.js
More file actions
121 lines (112 loc) · 4.42 KB
/
Lock.controller.js
File metadata and controls
121 lines (112 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
const { LogManager, LogLevel } = require('@aliceo2/web-ui');
const { updateAndSendExpressResponseFromNativeError, InvalidInputError } = require('@aliceo2/web-ui');
const { DetectorLockAction } = require('./../common/lock/detectorLockAction.enum.js');
const { DetectorId } = require('./../common/detectorId.enum.js');
const {User} = require('./../dtos/User.js');
const LOG_FACILITY = 'cog/log-ctrl';
/**
* Controller for dealing with all API requests on actions and state of the locks used for detectors
*/
class LockController {
/**
* Constructor for initializing controller of locks
* @param {LockService} lockService - service to use to build information on runs
*/
constructor(lockService) {
this._logger = LogManager.getLogger(`${process.env.npm_config_log_label ?? 'cog'}/${LOG_FACILITY}`);
/**
* @type {LockService}
*/
this._lockService = lockService;
}
/**
* API - GET endpoint for retrieving current state of the locks
* @param {Request} _ - HTTP Request object
* @param {Response} res - HTTP Response object
* @returns {void}
*/
async getLocksStateHandler(_, res) {
try {
res.status(200).json(this._lockService.locksByDetectorToJSON());
} catch (error) {
updateAndSendExpressResponseFromNativeError(res, error);
}
}
/**
* API - PUT endpoint for updating the state of a detector lock
* @param {Request} req - HTTP Request object
* @param {Response} res - HTTP Response object
* @returns {void}
*/
async actionLockHandler(req, res) {
const {action, detectorId, shouldForce = false} = req.params;
const {personid, name, username, access} = req.session;
try {
if (!detectorId) {
throw new InvalidInputError('Missing detectorId');
}
if (!action || !DetectorLockAction[action.toLocaleUpperCase()]) {
throw new InvalidInputError(`Invalid action to apply on lock for detector: ${detectorId}`);
}
const user = new User(username, name, personid, access);
if (action.toLocaleUpperCase() === DetectorLockAction.TAKE) {
if (detectorId === DetectorId.ALL) {
Object.keys(this._lockService.locksByDetector)
.filter((detector) => detector !== DetectorId.TST) // Skip TST detector when locking all
.forEach((detector) => {
try {
this._lockService.takeLock(detector, user, shouldForce);
} catch (error) {
console.error(error);
}
});
} else {
this._lockService.takeLock(detectorId, user, shouldForce);
}
res.status(200).json(this._lockService.locksByDetectorToJSON());
} else if (action.toLocaleUpperCase() === DetectorLockAction.RELEASE) {
if (detectorId === DetectorId.ALL) {
Object.keys(this._lockService.locksByDetector)
.filter((detector) => detector !== DetectorId.TST) // Skip TST detector when releasing all
.forEach((detector) => {
try {
this._lockService.releaseLock(detector, user, shouldForce);
} catch (error) {
console.error(error);
}
});
} else {
this._lockService.releaseLock(detectorId, user, shouldForce);
}
res.status(200).json(this._lockService.locksByDetectorToJSON());
}
} catch (error) {
this._logger.errorMessage(error, {level: LogLevel.DEVELOPER, facility: LOG_FACILITY});
updateAndSendExpressResponseFromNativeError(res, error);
}
}
/**
* API - PUT endpoint for updating the state of a detector lock with force option
* @param {Request} req - HTTP Request object
* @param {Response} res - HTTP Response object
* @returns {void}
*/
async actionForceLockHandler(req, res) {
req.params.shouldForce = true;
this.actionLockHandler(req, res);
}
}
exports.LockController = LockController;