@@ -85,45 +85,41 @@ export const profileComplete = async (req, res, next) => {
85
85
return next ( ) ;
86
86
} ;
87
87
88
- export const restrictedAccess = ( owner ) => async ( req , res , next ) => {
89
- const company = await ( new CompanyService ( ) ) . findById ( owner , true ) ;
90
- let error = { } ;
91
-
92
- if ( req . params ?. companyId === req . user . company ) {
93
- let reason = ValidationReasons . UNKNOWN ;
88
+ export const canAccessProfile = ( companyId ) => async ( req , res , next ) => {
89
+ const company = await new CompanyService ( ) . findById ( companyId , true ) ;
94
90
95
- if ( company . isBlocked )
96
- reason = ValidationReasons . COMPANY_BLOCKED ;
97
- else if ( company . isDisabled )
98
- reason = ValidationReasons . COMPANY_DISABLED ;
99
-
100
- error = new APIError (
101
- HTTPStatus . OK ,
91
+ const notFound = ( ) =>
92
+ new APIError (
93
+ HTTPStatus . UNPROCESSABLE_ENTITY ,
102
94
ErrorTypes . VALIDATION_ERROR ,
103
- reason ,
104
- { company : company }
95
+ [
96
+ {
97
+ value : companyId ,
98
+ msg : ValidationReasons . COMPANY_NOT_FOUND ( companyId ) ,
99
+ param : "companyId" ,
100
+ location : "params" ,
101
+ } ,
102
+ ]
105
103
) ;
106
- } else {
107
- error = new APIError (
108
- HTTPStatus . FORBIDDEN ,
109
- ErrorTypes . FORBIDDEN ,
110
- ValidationReasons . NOT_FOUND
104
+
105
+ const errorOrNotFound = ( reason ) =>
106
+ companyId === req . user ?. company ?. toString ( ) || req . hasAdminPrivileges
107
+ ? new APIError ( HTTPStatus . FORBIDDEN , ErrorTypes . FORBIDDEN , reason )
108
+ : notFound ( ) ;
109
+
110
+ if ( ! company . hasFinishedRegistration )
111
+ return next (
112
+ errorOrNotFound ( ValidationReasons . REGISTRATION_NOT_FINISHED )
111
113
) ;
112
- }
113
114
114
- return next ( error ) ;
115
- } ;
115
+ if ( req . hasAdminPrivileges )
116
+ return next ( ) ;
116
117
117
- export const registrationStatus = ( owner ) => async ( req , res , next ) => {
118
- const company = await ( new CompanyService ( ) ) . findById ( owner , true ) ;
118
+ if ( company . isBlocked )
119
+ return next ( errorOrNotFound ( ValidationReasons . COMPANY_BLOCKED ) ) ;
119
120
120
- if ( ! company . hasFinishedRegistration ) {
121
- return next ( new APIError (
122
- HTTPStatus . FORBIDDEN ,
123
- ErrorTypes . FORBIDDEN ,
124
- ( req . params ?. companyId !== req . user . company ) ? ValidationReasons . NOT_FOUND : ValidationReasons . REGISTRATION_NOT_FINISHED
125
- ) ) ;
126
- }
121
+ if ( company . isDisabled && companyId !== req . user ?. company ?. toString ( ) )
122
+ return next ( notFound ( ) ) ;
127
123
128
124
return next ( ) ;
129
125
} ;
0 commit comments