@riocrypto/common 1.0.432 → 1.0.433

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,7 +27,7 @@ const missing_security_question_error_1 = require("../errors/missing-security-qu
27
27
  const admin_only_error_1 = require("../errors/admin-only-error");
28
28
  const not_broker_error_1 = require("../errors/not-broker-error");
29
29
  const authorize = (req, res, next, mongoose, authorizationTypes) => __awaiter(void 0, void 0, void 0, function* () {
30
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
30
+ var _a, _b, _c, _d, _e, _f, _g, _h;
31
31
  let missingKYC = false;
32
32
  if (authorizationTypes.includes(authorization_type_1.AuthorizationType.Auth) ||
33
33
  authorizationTypes.includes(authorization_type_1.AuthorizationType.UserNoKYC) ||
@@ -126,22 +126,25 @@ const authorize = (req, res, next, mongoose, authorizationTypes) => __awaiter(vo
126
126
  }
127
127
  }
128
128
  if (authorizationTypes.includes(authorization_type_1.AuthorizationType.Broker)) {
129
- let businessUser;
130
- const BusinessUser = yield (0, business_user_1.buildBusinessUser)(mongoose);
131
- businessUser = yield BusinessUser.findOne({
132
- authId: req.currentAuth.id,
133
- });
134
- if (!businessUser) {
135
- throw new Error();
136
- }
137
- if ((businessUser === null || businessUser === void 0 ? void 0 : businessUser.kycStatus) !== kyc_status_1.KYCStatus.businessApproved) {
138
- missingKYC = true;
139
- throw new Error();
140
- }
141
- if ((_g = businessUser.attributes) === null || _g === void 0 ? void 0 : _g.includes(user_attribute_1.UserAttribute.broker)) {
142
- throw new not_broker_error_1.NotBrokerError();
129
+ let brokerBusinessUser;
130
+ try {
131
+ const BusinessUser = yield (0, business_user_1.buildBusinessUser)(mongoose);
132
+ brokerBusinessUser = yield BusinessUser.findOne({
133
+ authId: req.currentAuth.id,
134
+ attributes: { $e: user_attribute_1.UserAttribute.broker },
135
+ });
136
+ if (!brokerBusinessUser) {
137
+ throw new Error();
138
+ }
139
+ if ((brokerBusinessUser === null || brokerBusinessUser === void 0 ? void 0 : brokerBusinessUser.kycStatus) !== kyc_status_1.KYCStatus.businessApproved) {
140
+ missingKYC = true;
141
+ throw new Error();
142
+ }
143
+ }
144
+ catch (err) {
145
+ brokerBusinessUser = null;
143
146
  }
144
- const user = businessUser;
147
+ const user = brokerBusinessUser;
145
148
  if (user) {
146
149
  req.user = user;
147
150
  }
@@ -155,14 +158,17 @@ const authorize = (req, res, next, mongoose, authorizationTypes) => __awaiter(vo
155
158
  }
156
159
  if ((authorizationTypes.includes(authorization_type_1.AuthorizationType.User) ||
157
160
  authorizationTypes.includes(authorization_type_1.AuthorizationType.UserNoKYC)) &&
158
- ((_h = req.cookies) === null || _h === void 0 ? void 0 : _h.jwt)) {
161
+ ((_g = req.cookies) === null || _g === void 0 ? void 0 : _g.jwt)) {
159
162
  throw new missing_user_error_1.MissingUserError();
160
163
  }
161
164
  if (authorizationTypes.includes(authorization_type_1.AuthorizationType.Admin) &&
162
165
  authorizationTypes.length === 1 &&
163
- ((_j = req.cookies) === null || _j === void 0 ? void 0 : _j.jwt)) {
166
+ ((_h = req.cookies) === null || _h === void 0 ? void 0 : _h.jwt)) {
164
167
  throw new admin_only_error_1.AdminOnlyError();
165
168
  }
169
+ if (!req.user && authorizationTypes.includes(authorization_type_1.AuthorizationType.Broker)) {
170
+ throw new not_broker_error_1.NotBrokerError();
171
+ }
166
172
  throw new not_authorized_error_1.NotAuthorizedError();
167
173
  }
168
174
  next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riocrypto/common",
3
- "version": "1.0.432",
3
+ "version": "1.0.433",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",