@almatar/branding 1.0.0-beta.2 → 1.0.0-beta.3
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.
- package/lib/index.d.ts +6 -0
- package/lib/index.js +11 -5
- package/lib/lib/AlmatarBranding.js +6 -9
- package/lib/lib/BrandIdentifier.js +54 -110
- package/lib/lib/BrandManager.js +132 -192
- package/lib/lib/Models/BrandModel.js +3 -4
- package/lib/lib/Models/EmployeeBrandModel.js +3 -4
- package/lib/lib/Storage.js +78 -159
- package/lib/lib/TenantModel/Mongoose/MultiTenant.js +13 -17
- package/lib/lib/TenantModel/MongooseModel.js +53 -39
- package/lib/lib/TenantModel/NestMongoose/TenantMongooseModule.js +12 -31
- package/lib/lib/TenantModel/NestMongoose/mongoose.providers.js +8 -9
- package/lib/lib/TenantModel/TypeORM/TenantEntitySubscriber.d.ts +27 -0
- package/lib/lib/TenantModel/TypeORM/TenantEntitySubscriber.js +68 -0
- package/lib/lib/TenantModel/TypeORM/TenantHttpInterceptor.d.ts +26 -0
- package/lib/lib/TenantModel/TypeORM/TenantHttpInterceptor.js +92 -0
- package/lib/lib/TenantModel/TypeORM/TenantRepository.d.ts +58 -0
- package/lib/lib/TenantModel/TypeORM/TenantRepository.js +281 -0
- package/lib/lib/request/PromiseRequest.js +37 -99
- package/lib/lib/request/TenantRequest.js +36 -98
- package/package.json +5 -1
package/lib/index.d.ts
CHANGED
|
@@ -3,11 +3,17 @@ import MultiTenant from './lib/TenantModel/Mongoose/MultiTenant';
|
|
|
3
3
|
import TenantMongooseModule from './lib/TenantModel/NestMongoose/TenantMongooseModule';
|
|
4
4
|
import TenantRequest from './lib/request/TenantRequest';
|
|
5
5
|
import AlmatarBranding from './lib/AlmatarBranding';
|
|
6
|
+
import { TenantRepository } from './lib/TenantModel/TypeORM/TenantRepository';
|
|
7
|
+
import { TenantEntitySubscriber } from './lib/TenantModel/TypeORM/TenantEntitySubscriber';
|
|
8
|
+
import { TenantHttpInterceptor } from './lib/TenantModel/TypeORM/TenantHttpInterceptor';
|
|
6
9
|
declare const _default: {
|
|
7
10
|
ContextNamespace: typeof ContextNamespace;
|
|
8
11
|
MultiTenant: typeof MultiTenant;
|
|
9
12
|
TenantMongooseModule: typeof TenantMongooseModule;
|
|
10
13
|
TenantRequest: typeof TenantRequest;
|
|
11
14
|
AlmatarBranding: typeof AlmatarBranding;
|
|
15
|
+
TenantRepository: typeof TenantRepository;
|
|
16
|
+
TenantEntitySubscriber: typeof TenantEntitySubscriber;
|
|
17
|
+
TenantHttpInterceptor: typeof TenantHttpInterceptor;
|
|
12
18
|
};
|
|
13
19
|
export = _default;
|
package/lib/index.js
CHANGED
|
@@ -2,15 +2,21 @@
|
|
|
2
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
const Storage_1 = __importDefault(require("./lib/Storage"));
|
|
6
|
+
const MultiTenant_1 = __importDefault(require("./lib/TenantModel/Mongoose/MultiTenant"));
|
|
7
|
+
const TenantMongooseModule_1 = __importDefault(require("./lib/TenantModel/NestMongoose/TenantMongooseModule"));
|
|
8
|
+
const TenantRequest_1 = __importDefault(require("./lib/request/TenantRequest"));
|
|
9
|
+
const AlmatarBranding_1 = __importDefault(require("./lib/AlmatarBranding"));
|
|
10
|
+
const TenantRepository_1 = require("./lib/TenantModel/TypeORM/TenantRepository");
|
|
11
|
+
const TenantEntitySubscriber_1 = require("./lib/TenantModel/TypeORM/TenantEntitySubscriber");
|
|
12
|
+
const TenantHttpInterceptor_1 = require("./lib/TenantModel/TypeORM/TenantHttpInterceptor");
|
|
10
13
|
module.exports = {
|
|
11
14
|
ContextNamespace: Storage_1.default,
|
|
12
15
|
MultiTenant: MultiTenant_1.default,
|
|
13
16
|
TenantMongooseModule: TenantMongooseModule_1.default,
|
|
14
17
|
TenantRequest: TenantRequest_1.default,
|
|
15
18
|
AlmatarBranding: AlmatarBranding_1.default,
|
|
19
|
+
TenantRepository: TenantRepository_1.TenantRepository,
|
|
20
|
+
TenantEntitySubscriber: TenantEntitySubscriber_1.TenantEntitySubscriber,
|
|
21
|
+
TenantHttpInterceptor: TenantHttpInterceptor_1.TenantHttpInterceptor,
|
|
16
22
|
};
|
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
AlmatarBranding.setup = function (configs) {
|
|
3
|
+
class AlmatarBranding {
|
|
4
|
+
static setup(configs) {
|
|
7
5
|
this.employeeAuthService = configs.employeeAuthService;
|
|
8
6
|
this.dictionaryService = configs.dictionaryService;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
AlmatarBranding.dictionaryService = '';
|
|
12
|
-
return AlmatarBranding;
|
|
13
|
-
}());
|
|
7
|
+
}
|
|
8
|
+
}
|
|
14
9
|
exports.default = AlmatarBranding;
|
|
10
|
+
AlmatarBranding.employeeAuthService = '';
|
|
11
|
+
AlmatarBranding.dictionaryService = '';
|
|
@@ -18,120 +18,65 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
18
18
|
__setModuleDefault(result, mod);
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
22
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
31
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
32
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
33
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
34
|
-
function step(op) {
|
|
35
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
36
|
-
while (_) try {
|
|
37
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
38
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
39
|
-
switch (op[0]) {
|
|
40
|
-
case 0: case 1: t = op; break;
|
|
41
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
42
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
43
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
44
|
-
default:
|
|
45
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
46
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
47
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
48
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
49
|
-
if (t[2]) _.ops.pop();
|
|
50
|
-
_.trys.pop(); continue;
|
|
51
|
-
}
|
|
52
|
-
op = body.call(thisArg, _);
|
|
53
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
54
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
58
22
|
exports.BrandIdentifier = void 0;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
23
|
+
const Boom = __importStar(require("@hapi/boom"));
|
|
24
|
+
const BrandManager_1 = require("./BrandManager");
|
|
25
|
+
class BrandIdentifier {
|
|
26
|
+
constructor(type) {
|
|
63
27
|
this.type = type;
|
|
64
28
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (!(req.headers.authorization || req.headers['x-employee-brands'])) return [3 /*break*/, 3];
|
|
76
|
-
return [4 /*yield*/, brandManager.getConsoleBrands(req)];
|
|
77
|
-
case 2:
|
|
78
|
-
brand = _a.sent();
|
|
79
|
-
if (brand) {
|
|
80
|
-
if (brand.length) {
|
|
81
|
-
return [2 /*return*/, brand];
|
|
82
|
-
}
|
|
83
|
-
return [2 /*return*/, brandManager.getDefaultBrand()];
|
|
84
|
-
}
|
|
85
|
-
// If no brand found but we have authorization, try extracting from token directly
|
|
86
|
-
if (req.headers.authorization) {
|
|
87
|
-
tokenBrands = brandManager.extractBrandFromToken(req.headers.authorization);
|
|
88
|
-
if (tokenBrands && tokenBrands.length > 0) {
|
|
89
|
-
return [2 /*return*/, tokenBrands];
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
// Return empty array instead of null when no brand found in token
|
|
93
|
-
// tslint:disable-next-line no-console
|
|
94
|
-
console.log('[BrandIdentifier] No brand found in token, returning empty array');
|
|
95
|
-
return [2 /*return*/, []];
|
|
96
|
-
case 3: return [2 /*return*/, brandManager.getDefaultBrand()];
|
|
29
|
+
async getBrand(req) {
|
|
30
|
+
const brandManager = new BrandManager_1.BrandManager(this.type);
|
|
31
|
+
if (req.headers['x-brand']) {
|
|
32
|
+
return brandManager.getB2CBrand(req);
|
|
33
|
+
}
|
|
34
|
+
else if (req.headers.authorization || req.headers['x-employee-brands']) {
|
|
35
|
+
const brand = await brandManager.getConsoleBrands(req);
|
|
36
|
+
if (brand) {
|
|
37
|
+
if (brand.length) {
|
|
38
|
+
return brand;
|
|
97
39
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
switch (_a.label) {
|
|
106
|
-
case 0:
|
|
107
|
-
brandManager = new BrandManager_1.BrandManager(this.type);
|
|
108
|
-
_a.label = 1;
|
|
109
|
-
case 1:
|
|
110
|
-
_a.trys.push([1, 3, , 4]);
|
|
111
|
-
return [4 /*yield*/, brandManager.getBrands()];
|
|
112
|
-
case 2:
|
|
113
|
-
brands = _a.sent();
|
|
114
|
-
if (!brands) {
|
|
115
|
-
// tslint:disable-next-line no-console
|
|
116
|
-
console.log('[BrandIdentifier] Could not fetch brands list from dictionary service, continuing without it');
|
|
117
|
-
return [2 /*return*/, null];
|
|
118
|
-
}
|
|
119
|
-
return [2 /*return*/, brands];
|
|
120
|
-
case 3:
|
|
121
|
-
err_1 = _a.sent();
|
|
122
|
-
// tslint:disable-next-line no-console
|
|
123
|
-
console.log('[BrandIdentifier] Error fetching brands list, continuing without it:', err_1);
|
|
124
|
-
return [2 /*return*/, null];
|
|
125
|
-
case 4: return [2 /*return*/];
|
|
40
|
+
return brandManager.getDefaultBrand();
|
|
41
|
+
}
|
|
42
|
+
// If no brand found but we have authorization, try extracting from token directly
|
|
43
|
+
if (req.headers.authorization) {
|
|
44
|
+
const tokenBrands = brandManager.extractBrandFromToken(req.headers.authorization);
|
|
45
|
+
if (tokenBrands && tokenBrands.length > 0) {
|
|
46
|
+
return tokenBrands;
|
|
126
47
|
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
48
|
+
}
|
|
49
|
+
// Return empty array instead of null when no brand found in token
|
|
50
|
+
// tslint:disable-next-line no-console
|
|
51
|
+
console.log('[BrandIdentifier] No brand found in token, returning empty array');
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return brandManager.getDefaultBrand();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async getBrands() {
|
|
59
|
+
const brandManager = new BrandManager_1.BrandManager(this.type);
|
|
60
|
+
try {
|
|
61
|
+
const brands = await brandManager.getBrands();
|
|
62
|
+
if (!brands) {
|
|
63
|
+
// tslint:disable-next-line no-console
|
|
64
|
+
console.log('[BrandIdentifier] Could not fetch brands list from dictionary service, continuing without it');
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
return brands;
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
// tslint:disable-next-line no-console
|
|
71
|
+
console.log('[BrandIdentifier] Error fetching brands list, continuing without it:', err);
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
getDefaultBrand() {
|
|
76
|
+
const brandManager = new BrandManager_1.BrandManager(this.type);
|
|
132
77
|
return brandManager.getDefaultBrand();
|
|
133
|
-
}
|
|
134
|
-
|
|
78
|
+
}
|
|
79
|
+
error(message) {
|
|
135
80
|
switch (this.type) {
|
|
136
81
|
case 'hapi':
|
|
137
82
|
return Boom.unauthorized(message);
|
|
@@ -142,7 +87,6 @@ var BrandIdentifier = /** @class */ (function () {
|
|
|
142
87
|
default:
|
|
143
88
|
return new Error(message);
|
|
144
89
|
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
}());
|
|
90
|
+
}
|
|
91
|
+
}
|
|
148
92
|
exports.BrandIdentifier = BrandIdentifier;
|
package/lib/lib/BrandManager.js
CHANGED
|
@@ -1,83 +1,47 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
4
|
};
|
|
41
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
6
|
exports.BrandManager = void 0;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
7
|
+
const AlmatarBranding_1 = __importDefault(require("./AlmatarBranding"));
|
|
8
|
+
const Storage_1 = __importDefault(require("./Storage"));
|
|
9
|
+
const PromiseRequest_1 = require("./request/PromiseRequest");
|
|
10
|
+
class BrandManager {
|
|
11
|
+
constructor(type) {
|
|
48
12
|
this.defaultBrand = 'almatar';
|
|
49
13
|
this.type = type;
|
|
50
14
|
}
|
|
51
|
-
|
|
15
|
+
getB2CBrand(req) {
|
|
52
16
|
return req.headers['x-brand'];
|
|
53
|
-
}
|
|
17
|
+
}
|
|
54
18
|
/**
|
|
55
19
|
* Extract brand from JWT token payload
|
|
56
20
|
* @param token - JWT token string (with or without Bearer prefix)
|
|
57
21
|
*/
|
|
58
|
-
|
|
22
|
+
extractBrandFromToken(token) {
|
|
59
23
|
try {
|
|
60
24
|
// Remove 'Bearer ' prefix if present
|
|
61
|
-
|
|
25
|
+
const cleanToken = token.replace(/^Bearer\s+/i, '');
|
|
62
26
|
// JWT format: header.payload.signature
|
|
63
|
-
|
|
27
|
+
const parts = cleanToken.split('.');
|
|
64
28
|
if (parts.length !== 3) {
|
|
65
29
|
// tslint:disable-next-line no-console
|
|
66
30
|
console.log('[BrandManager] Invalid JWT token format - expected 3 parts');
|
|
67
31
|
return null;
|
|
68
32
|
}
|
|
69
33
|
// Decode payload (base64url)
|
|
70
|
-
|
|
34
|
+
const payload = parts[1];
|
|
71
35
|
// Replace URL-safe base64 characters
|
|
72
|
-
|
|
36
|
+
const base64 = payload.replace(/-/g, '+').replace(/_/g, '/');
|
|
73
37
|
// Add padding if needed
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
38
|
+
const padded = base64 + '='.repeat((4 - (base64.length % 4)) % 4);
|
|
39
|
+
const decoded = Buffer.from(padded, 'base64').toString('utf-8');
|
|
40
|
+
const tokenPayload = JSON.parse(decoded);
|
|
77
41
|
// Extract brand from token payload
|
|
78
42
|
if (tokenPayload.brand) {
|
|
79
43
|
// Brand can be array or single string
|
|
80
|
-
|
|
44
|
+
let brands;
|
|
81
45
|
if (Array.isArray(tokenPayload.brand)) {
|
|
82
46
|
brands = tokenPayload.brand;
|
|
83
47
|
}
|
|
@@ -102,159 +66,135 @@ var BrandManager = /** @class */ (function () {
|
|
|
102
66
|
console.log('[BrandManager] Error extracting brand from token:', err);
|
|
103
67
|
return null;
|
|
104
68
|
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
69
|
+
}
|
|
70
|
+
async getConsoleBrands(req) {
|
|
71
|
+
// Priority 1: Check x-employee-brands header first
|
|
72
|
+
if (req.headers['x-employee-brands']) {
|
|
73
|
+
const brands = req.headers['x-employee-brands'].split(',').map((b) => b.trim()).filter((b) => b);
|
|
74
|
+
// tslint:disable-next-line no-console
|
|
75
|
+
console.log('[BrandManager] Extracted brands from x-employee-brands header:', brands);
|
|
76
|
+
if (brands.length > 0) {
|
|
77
|
+
return brands;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Priority 2: Try to extract brand from JWT token
|
|
81
|
+
if (req.headers.authorization) {
|
|
82
|
+
const tokenBrands = this.extractBrandFromToken(req.headers.authorization);
|
|
83
|
+
if (tokenBrands && tokenBrands.length > 0) {
|
|
84
|
+
// tslint:disable-next-line no-console
|
|
85
|
+
console.log('[BrandManager] Using brands extracted from token:', tokenBrands);
|
|
86
|
+
return tokenBrands;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// DO NOT call loadBrands() here - it makes HTTP requests that can cause infinite loops
|
|
90
|
+
// If we don't have brand from token/header, return null instead
|
|
91
|
+
// tslint:disable-next-line no-console
|
|
92
|
+
console.log('[BrandManager] No brand found in token or headers, returning null (skipping loadBrands to prevent loops)');
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
async loadBrands(req) {
|
|
96
|
+
try {
|
|
97
|
+
const reqBody = req.payload ? req.payload : req.body;
|
|
98
|
+
const options = {
|
|
99
|
+
url: AlmatarBranding_1.default.employeeAuthService + '/v1/user/brands',
|
|
100
|
+
method: 'GET',
|
|
101
|
+
headers: {
|
|
102
|
+
authorization: req.headers.authorization,
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
const employeeBrands = await PromiseRequest_1.PromiseRequest.request(options);
|
|
106
|
+
// Check if response is valid and has the expected structure
|
|
107
|
+
if (employeeBrands && employeeBrands.status === 200 && employeeBrands.data) {
|
|
108
|
+
// Handle different possible response structures
|
|
109
|
+
const brandsData = Array.isArray(employeeBrands.data)
|
|
110
|
+
? employeeBrands.data
|
|
111
|
+
: (employeeBrands.data.data || employeeBrands.data.list || []);
|
|
112
|
+
if (Array.isArray(brandsData) && brandsData.length > 0) {
|
|
113
|
+
const brands = brandsData
|
|
114
|
+
.map((brand) => {
|
|
115
|
+
if (typeof brand === 'string') {
|
|
116
|
+
return brand;
|
|
117
|
+
}
|
|
118
|
+
return (brand === null || brand === void 0 ? void 0 : brand.slug) || '';
|
|
119
|
+
})
|
|
120
|
+
.filter((b) => !!b);
|
|
121
|
+
if (reqBody && reqBody.brand) {
|
|
122
|
+
return brands.indexOf(reqBody.brand) > -1 ? brands : null;
|
|
117
123
|
}
|
|
124
|
+
// tslint:disable-next-line no-console
|
|
125
|
+
console.log('[BrandManager] Successfully loaded brands from employee auth service:', brands);
|
|
126
|
+
return brands;
|
|
118
127
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (tokenBrands && tokenBrands.length > 0) {
|
|
123
|
-
// tslint:disable-next-line no-console
|
|
124
|
-
console.log('[BrandManager] Using brands extracted from token:', tokenBrands);
|
|
125
|
-
return [2 /*return*/, tokenBrands];
|
|
126
|
-
}
|
|
128
|
+
else {
|
|
129
|
+
// tslint:disable-next-line no-console
|
|
130
|
+
console.log('[BrandManager] Invalid response structure from employee auth service, expected array but got:', typeof brandsData);
|
|
127
131
|
}
|
|
128
|
-
|
|
129
|
-
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
130
134
|
// tslint:disable-next-line no-console
|
|
131
|
-
console.log('[BrandManager]
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
case 0:
|
|
142
|
-
_a.trys.push([0, 2, , 3]);
|
|
143
|
-
reqBody = req.payload ? req.payload : req.body;
|
|
144
|
-
options = {
|
|
145
|
-
url: AlmatarBranding_1.default.employeeAuthService + '/v1/user/brands',
|
|
146
|
-
method: 'GET',
|
|
147
|
-
headers: {
|
|
148
|
-
authorization: req.headers.authorization,
|
|
149
|
-
},
|
|
150
|
-
};
|
|
151
|
-
return [4 /*yield*/, PromiseRequest_1.PromiseRequest.request(options)];
|
|
152
|
-
case 1:
|
|
153
|
-
employeeBrands = _a.sent();
|
|
154
|
-
// Check if response is valid and has the expected structure
|
|
155
|
-
if (employeeBrands && employeeBrands.status === 200 && employeeBrands.data) {
|
|
156
|
-
brandsData = Array.isArray(employeeBrands.data)
|
|
157
|
-
? employeeBrands.data
|
|
158
|
-
: (employeeBrands.data.data || employeeBrands.data.list || []);
|
|
159
|
-
if (Array.isArray(brandsData) && brandsData.length > 0) {
|
|
160
|
-
brands = brandsData
|
|
161
|
-
.map(function (brand) {
|
|
162
|
-
if (typeof brand === 'string') {
|
|
163
|
-
return brand;
|
|
164
|
-
}
|
|
165
|
-
return (brand === null || brand === void 0 ? void 0 : brand.slug) || '';
|
|
166
|
-
})
|
|
167
|
-
.filter(function (b) { return !!b; });
|
|
168
|
-
if (reqBody && reqBody.brand) {
|
|
169
|
-
return [2 /*return*/, brands.indexOf(reqBody.brand) > -1 ? brands : null];
|
|
170
|
-
}
|
|
171
|
-
// tslint:disable-next-line no-console
|
|
172
|
-
console.log('[BrandManager] Successfully loaded brands from employee auth service:', brands);
|
|
173
|
-
return [2 /*return*/, brands];
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
// tslint:disable-next-line no-console
|
|
177
|
-
console.log('[BrandManager] Invalid response structure from employee auth service, expected array but got:', typeof brandsData);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
else {
|
|
181
|
-
// tslint:disable-next-line no-console
|
|
182
|
-
console.log('[BrandManager] Failed to load brands from employee auth service, status:', employeeBrands === null || employeeBrands === void 0 ? void 0 : employeeBrands.status, 'data:', employeeBrands === null || employeeBrands === void 0 ? void 0 : employeeBrands.data);
|
|
183
|
-
}
|
|
184
|
-
return [2 /*return*/, null];
|
|
185
|
-
case 2:
|
|
186
|
-
err_1 = _a.sent();
|
|
187
|
-
// tslint:disable-next-line no-console
|
|
188
|
-
console.log('[BrandManager] Error loading brands:', err_1);
|
|
189
|
-
return [2 /*return*/, null];
|
|
190
|
-
case 3: return [2 /*return*/];
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
};
|
|
135
|
+
console.log('[BrandManager] Failed to load brands from employee auth service, status:', employeeBrands === null || employeeBrands === void 0 ? void 0 : employeeBrands.status, 'data:', employeeBrands === null || employeeBrands === void 0 ? void 0 : employeeBrands.data);
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
catch (err) {
|
|
140
|
+
// tslint:disable-next-line no-console
|
|
141
|
+
console.log('[BrandManager] Error loading brands:', err);
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
195
145
|
/**
|
|
196
146
|
* get all brands list
|
|
197
147
|
* @param req
|
|
198
148
|
*/
|
|
199
|
-
|
|
149
|
+
async getBrands() {
|
|
200
150
|
var _a;
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
if (
|
|
216
|
-
|
|
217
|
-
if (Array.isArray(brandsList) && brandsList.length > 0) {
|
|
218
|
-
Storage_1.default.setBrandsList(brandsList);
|
|
219
|
-
brands = brandsList
|
|
220
|
-
.map(function (brand) {
|
|
221
|
-
if (typeof brand === 'string') {
|
|
222
|
-
return brand;
|
|
223
|
-
}
|
|
224
|
-
return (brand === null || brand === void 0 ? void 0 : brand.key) || '';
|
|
225
|
-
})
|
|
226
|
-
.filter(function (b) { return !!b; });
|
|
227
|
-
Storage_1.default.setBrands(brands);
|
|
228
|
-
// tslint:disable-next-line:no-console
|
|
229
|
-
console.log('[BrandManager] Successfully loaded brands from dictionary service:', brands);
|
|
230
|
-
return [2 /*return*/, brands];
|
|
231
|
-
}
|
|
232
|
-
else {
|
|
233
|
-
// tslint:disable-next-line:no-console
|
|
234
|
-
console.log('[BrandManager] Invalid response structure from dictionary service, expected array but got:', typeof brandsList);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
else {
|
|
238
|
-
// tslint:disable-next-line:no-console
|
|
239
|
-
console.log('[BrandManager] Failed to get brands from dictionary service, status:', brandsResponse === null || brandsResponse === void 0 ? void 0 : brandsResponse.status, 'data:', brandsResponse === null || brandsResponse === void 0 ? void 0 : brandsResponse.data);
|
|
151
|
+
try {
|
|
152
|
+
const option = {
|
|
153
|
+
url: AlmatarBranding_1.default.dictionaryService + 'brands',
|
|
154
|
+
method: 'GET',
|
|
155
|
+
};
|
|
156
|
+
const brandsResponse = await PromiseRequest_1.PromiseRequest.request(option);
|
|
157
|
+
// Check if response is valid and has the expected structure
|
|
158
|
+
if (brandsResponse && brandsResponse.status === 200 && brandsResponse.data) {
|
|
159
|
+
// Handle different possible response structures
|
|
160
|
+
const brandsList = brandsResponse.data.list || ((_a = brandsResponse.data.data) === null || _a === void 0 ? void 0 : _a.list) || brandsResponse.data;
|
|
161
|
+
if (Array.isArray(brandsList) && brandsList.length > 0) {
|
|
162
|
+
Storage_1.default.setBrandsList(brandsList);
|
|
163
|
+
const brands = brandsList
|
|
164
|
+
.map((brand) => {
|
|
165
|
+
if (typeof brand === 'string') {
|
|
166
|
+
return brand;
|
|
240
167
|
}
|
|
241
|
-
return
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
168
|
+
return (brand === null || brand === void 0 ? void 0 : brand.key) || '';
|
|
169
|
+
})
|
|
170
|
+
.filter((b) => !!b);
|
|
171
|
+
Storage_1.default.setBrands(brands);
|
|
172
|
+
// tslint:disable-next-line:no-console
|
|
173
|
+
console.log('[BrandManager] Successfully loaded brands from dictionary service:', brands);
|
|
174
|
+
return brands;
|
|
248
175
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
176
|
+
else {
|
|
177
|
+
// tslint:disable-next-line:no-console
|
|
178
|
+
console.log('[BrandManager] Invalid response structure from dictionary service, expected array but got:', typeof brandsList);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
// tslint:disable-next-line:no-console
|
|
183
|
+
console.log('[BrandManager] Failed to get brands from dictionary service, status:', brandsResponse === null || brandsResponse === void 0 ? void 0 : brandsResponse.status, 'data:', brandsResponse === null || brandsResponse === void 0 ? void 0 : brandsResponse.data);
|
|
184
|
+
}
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
catch (err) {
|
|
188
|
+
// tslint:disable-next-line:no-console
|
|
189
|
+
console.log('[BrandManager] Error getting brands from dictionary service:', err);
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
252
193
|
/**
|
|
253
194
|
* get default brand
|
|
254
195
|
*/
|
|
255
|
-
|
|
196
|
+
getDefaultBrand() {
|
|
256
197
|
return this.defaultBrand;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
}());
|
|
198
|
+
}
|
|
199
|
+
}
|
|
260
200
|
exports.BrandManager = BrandManager;
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BrandModel = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
class BrandModel {
|
|
5
|
+
constructor(init) {
|
|
6
6
|
Object.assign(this, init);
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
}());
|
|
8
|
+
}
|
|
10
9
|
exports.BrandModel = BrandModel;
|