@@ -38,53 +38,29 @@ bedrock.events.on('bedrock-express.configure.routes', app => {
3838 ensureAuthenticated ,
3939 createValidateMiddleware ( { bodySchema : validators . createMeter ( ) } ) ,
4040 asyncHandler ( async ( req , res ) => {
41- /**
42- * @function createMeter
43- * @param {object } req - The request.
44- * @param {object } res - The response.
45- * @param {object } req.body - The request body.
46- * @param {string } req.body.meter - Meter to create.
47- * @param {string } req.user.account.id - User account ID.
48- * @description Check if the request user's account ID is in a list of
49- * meter creation policies. If account ID is found pass meter to create
50- * meter handler and return the result.
51- */
5241 if ( ! HANDLERS . createMeter ) {
5342 throw new BedrockError (
54- 'Missing required createMeter handler.' , 'NotFoundError ', {
55- httpStatusCode : 404 ,
56- public : true ,
43+ 'Meter creation not supported. ' , {
44+ name : 'NotSupportedError' ,
45+ details : { httpStatusCode : 400 , public : true }
5746 } ) ;
5847 }
5948
6049 const meter = req . body . meter ;
6150 const productId = meter . product . id ;
6251 const accountId = req . user . account . id ;
63- const accountPolicies = cfg . meterCreationPolicies [ accountId ] ;
64- const missingPolicy = ! ( accountPolicies && accountPolicies . create ) ;
52+ const accountPolicy = cfg . accountPolicies [ accountId ] ;
53+ const productAllowed = accountPolicy ?. meters ?. create ?. [ productId ] ;
6554
66- // check for account meter creation policy
67- if ( missingPolicy ) {
55+ if ( ! productAllowed ) {
6856 throw new BedrockError (
69- `Meter creation policy does not include account ID: ${ accountId } ` ,
70- 'NotAllowedError' , {
71- httpStatusCode : 403 ,
72- public : true ,
57+ `Creation of a meter for product " ${ productId } " by account ` +
58+ `" ${ accountId } " is not allowed.` , {
59+ name : 'NotAllowedError' ,
60+ details : { httpStatusCode : 403 , public : true }
7361 } ) ;
7462 }
7563
76- // ensure product ID is listed in policy & creation is permitted
77- const productInPolicy = accountPolicies . create [ productId ] ;
78-
79- if ( ! ( productInPolicy && productInPolicy . allowed ) ) {
80- throw new BedrockError (
81- `Policy is missing product ID: ${ productId } ` , 'NotAllowedError' , {
82- httpStatusCode : 403 ,
83- public : true
84- }
85- ) ;
86- }
87-
8864 // pass meter to create meter handler
8965 const response = await HANDLERS . createMeter ( { meter} ) ;
9066
0 commit comments