@riocrypto/common 1.0.429 → 1.0.431

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.
@@ -0,0 +1,8 @@
1
+ import { CustomError } from "./custom-error";
2
+ export declare class NotBrokerError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ }[];
8
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotBrokerError = void 0;
4
+ const custom_error_1 = require("./custom-error");
5
+ class NotBrokerError extends custom_error_1.CustomError {
6
+ constructor() {
7
+ super("To call this endpoint as a user, you must be a broker");
8
+ this.statusCode = 401;
9
+ Object.setPrototypeOf(this, NotBrokerError.prototype);
10
+ }
11
+ serializeErrors() {
12
+ return [
13
+ { message: "To call this endpoint as a user, you must be a broker" },
14
+ ];
15
+ }
16
+ }
17
+ exports.NotBrokerError = NotBrokerError;
package/build/index.d.ts CHANGED
@@ -21,6 +21,7 @@ export * from "./errors/missing-user-error";
21
21
  export * from "./errors/missing-kyc-approval-error";
22
22
  export * from "./errors/wrong-order-state-error";
23
23
  export * from "./errors/admin-only-error";
24
+ export * from "./errors/not-broker-error";
24
25
  export * from "./middlewares/error-handler";
25
26
  export * from "./middlewares/validate-request";
26
27
  export * from "./middlewares/current-auth";
package/build/index.js CHANGED
@@ -37,6 +37,7 @@ __exportStar(require("./errors/missing-user-error"), exports);
37
37
  __exportStar(require("./errors/missing-kyc-approval-error"), exports);
38
38
  __exportStar(require("./errors/wrong-order-state-error"), exports);
39
39
  __exportStar(require("./errors/admin-only-error"), exports);
40
+ __exportStar(require("./errors/not-broker-error"), exports);
40
41
  __exportStar(require("./middlewares/error-handler"), exports);
41
42
  __exportStar(require("./middlewares/validate-request"), exports);
42
43
  __exportStar(require("./middlewares/current-auth"), exports);
@@ -25,12 +25,14 @@ const missing_user_error_1 = require("../errors/missing-user-error");
25
25
  const missing_password_error_1 = require("../errors/missing-password-error");
26
26
  const missing_security_question_error_1 = require("../errors/missing-security-question-error");
27
27
  const admin_only_error_1 = require("../errors/admin-only-error");
28
+ const not_broker_error_1 = require("../errors/not-broker-error");
28
29
  const authorize = (req, res, next, mongoose, authorizationTypes) => __awaiter(void 0, void 0, void 0, function* () {
29
- var _a, _b, _c, _d, _e, _f, _g, _h;
30
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
30
31
  let missingKYC = false;
31
32
  if (authorizationTypes.includes(authorization_type_1.AuthorizationType.Auth) ||
32
33
  authorizationTypes.includes(authorization_type_1.AuthorizationType.UserNoKYC) ||
33
- authorizationTypes.includes(authorization_type_1.AuthorizationType.User)) {
34
+ authorizationTypes.includes(authorization_type_1.AuthorizationType.User) ||
35
+ authorizationTypes.includes(authorization_type_1.AuthorizationType.Broker)) {
34
36
  if ((_a = req.cookies) === null || _a === void 0 ? void 0 : _a.jwt) {
35
37
  try {
36
38
  const payload = jsonwebtoken_1.default.verify(req.cookies.jwt, process.env.JWT_KEY);
@@ -106,7 +108,6 @@ const authorize = (req, res, next, mongoose, authorizationTypes) => __awaiter(vo
106
108
  const BusinessUser = yield (0, business_user_1.buildBusinessUser)(mongoose);
107
109
  businessUser = yield BusinessUser.findOne({
108
110
  authId: req.currentAuth.id,
109
- attributes: { $ne: user_attribute_1.UserAttribute.deleted },
110
111
  });
111
112
  if (!businessUser) {
112
113
  throw new Error();
@@ -124,6 +125,27 @@ const authorize = (req, res, next, mongoose, authorizationTypes) => __awaiter(vo
124
125
  req.user = user;
125
126
  }
126
127
  }
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();
143
+ }
144
+ const user = businessUser;
145
+ if (user) {
146
+ req.user = user;
147
+ }
148
+ }
127
149
  if ((!req.currentAdminAuth && !req.currentAuth && !req.user) ||
128
150
  (req.currentAuth &&
129
151
  !authorizationTypes.includes(authorization_type_1.AuthorizationType.Auth) &&
@@ -133,12 +155,12 @@ const authorize = (req, res, next, mongoose, authorizationTypes) => __awaiter(vo
133
155
  }
134
156
  if ((authorizationTypes.includes(authorization_type_1.AuthorizationType.User) ||
135
157
  authorizationTypes.includes(authorization_type_1.AuthorizationType.UserNoKYC)) &&
136
- ((_g = req.cookies) === null || _g === void 0 ? void 0 : _g.jwt)) {
158
+ ((_h = req.cookies) === null || _h === void 0 ? void 0 : _h.jwt)) {
137
159
  throw new missing_user_error_1.MissingUserError();
138
160
  }
139
161
  if (authorizationTypes.includes(authorization_type_1.AuthorizationType.Admin) &&
140
162
  authorizationTypes.length === 1 &&
141
- ((_h = req.cookies) === null || _h === void 0 ? void 0 : _h.jwt)) {
163
+ ((_j = req.cookies) === null || _j === void 0 ? void 0 : _j.jwt)) {
142
164
  throw new admin_only_error_1.AdminOnlyError();
143
165
  }
144
166
  throw new not_authorized_error_1.NotAuthorizedError();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riocrypto/common",
3
- "version": "1.0.429",
3
+ "version": "1.0.431",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",