@almatar/branding 0.1.5 → 0.1.6
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 +1 -1
- package/lib/index.js +1 -1
- package/lib/lib/BrandManager.js +6 -6
- package/lib/lib/Models/BrandModel.d.ts +8 -0
- package/lib/lib/Models/BrandModel.js +10 -0
- package/lib/lib/Storage.d.ts +4 -3
- package/lib/lib/Storage.js +36 -24
- package/lib/lib/TenantModel/MongooseModel.js +10 -4
- package/lib/lib/TenantModel/NestMongoose/TenantMongooseModule.js +1 -1
- package/lib/lib/request/PromiseRequest.js +1 -2
- package/lib/lib/request/TenantRequest.js +1 -1
- package/package.json +1 -2
package/lib/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import ContextNamespace from './lib/Storage';
|
|
|
2
2
|
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
|
-
import AlmatarBranding from
|
|
5
|
+
import AlmatarBranding from './lib/AlmatarBranding';
|
|
6
6
|
declare const _default: {
|
|
7
7
|
ContextNamespace: typeof ContextNamespace;
|
|
8
8
|
MultiTenant: typeof MultiTenant;
|
package/lib/index.js
CHANGED
package/lib/lib/BrandManager.js
CHANGED
|
@@ -57,7 +57,7 @@ var BrandManager = /** @class */ (function () {
|
|
|
57
57
|
if (req.headers['x-employee-brands']) {
|
|
58
58
|
return [2 /*return*/, req.headers['x-employee-brands'].split(',')];
|
|
59
59
|
}
|
|
60
|
-
reqBody =
|
|
60
|
+
reqBody = req.payload ? req.payload : req.body;
|
|
61
61
|
return [4 /*yield*/, this.loadBrands(req)];
|
|
62
62
|
case 1:
|
|
63
63
|
brands = _a.sent();
|
|
@@ -76,12 +76,12 @@ var BrandManager = /** @class */ (function () {
|
|
|
76
76
|
switch (_a.label) {
|
|
77
77
|
case 0:
|
|
78
78
|
_a.trys.push([0, 2, , 3]);
|
|
79
|
-
reqBody =
|
|
79
|
+
reqBody = req.payload ? req.payload : req.body;
|
|
80
80
|
options = {
|
|
81
81
|
url: AlmatarBranding_1.default.employeeAuthService + '/v1/user/brands',
|
|
82
82
|
headers: {
|
|
83
|
-
authorization: req.headers.authorization
|
|
84
|
-
}
|
|
83
|
+
authorization: req.headers.authorization,
|
|
84
|
+
},
|
|
85
85
|
};
|
|
86
86
|
return [4 /*yield*/, PromiseRequest_1.PromiseRequest.request(options)];
|
|
87
87
|
case 1:
|
|
@@ -89,7 +89,7 @@ var BrandManager = /** @class */ (function () {
|
|
|
89
89
|
if (employeeBrands.status === 200) {
|
|
90
90
|
brands = employeeBrands.data.map(function (brand) { return brand.slug; });
|
|
91
91
|
if (reqBody && reqBody.brand) {
|
|
92
|
-
return [2 /*return*/,
|
|
92
|
+
return [2 /*return*/, brands.indexOf(reqBody.brand) > -1 ? brands : null];
|
|
93
93
|
}
|
|
94
94
|
return [2 /*return*/, brands];
|
|
95
95
|
}
|
|
@@ -122,7 +122,7 @@ var BrandManager = /** @class */ (function () {
|
|
|
122
122
|
case 1:
|
|
123
123
|
brandsResponse = _a.sent();
|
|
124
124
|
if (brandsResponse.status === 200) {
|
|
125
|
-
brands = brandsResponse.data.
|
|
125
|
+
brands = brandsResponse.data.list.map(function (brand) { return brand.key; });
|
|
126
126
|
Storage_1.default.setBrands(brands);
|
|
127
127
|
return [2 /*return*/, brands];
|
|
128
128
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BrandModel = void 0;
|
|
4
|
+
var BrandModel = /** @class */ (function () {
|
|
5
|
+
function BrandModel(init) {
|
|
6
|
+
Object.assign(this, init);
|
|
7
|
+
}
|
|
8
|
+
return BrandModel;
|
|
9
|
+
}());
|
|
10
|
+
exports.BrandModel = BrandModel;
|
package/lib/lib/Storage.d.ts
CHANGED
|
@@ -7,7 +7,8 @@ export default class ContextNamespace {
|
|
|
7
7
|
static getBrand(): any;
|
|
8
8
|
static getEmployeeBrands(): any;
|
|
9
9
|
static getNamespace(): any;
|
|
10
|
-
static setBrands(brands:
|
|
11
|
-
static getBrands():
|
|
12
|
-
static getDefaultBrand():
|
|
10
|
+
static setBrands(brands: string[]): any;
|
|
11
|
+
static getBrands(): string[] | null;
|
|
12
|
+
static getDefaultBrand(): string | null;
|
|
13
|
+
static isValidBrand(brand: string): boolean;
|
|
13
14
|
}
|
package/lib/lib/Storage.js
CHANGED
|
@@ -45,11 +45,23 @@ var ContextNamespace = /** @class */ (function () {
|
|
|
45
45
|
}
|
|
46
46
|
/* Express Start */
|
|
47
47
|
ContextNamespace.bindENamespace = function (req, res, next) {
|
|
48
|
+
var _this = this;
|
|
48
49
|
ns.bindEmitter(req);
|
|
49
50
|
ns.bindEmitter(res);
|
|
50
|
-
ns.run(function () {
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
ns.run(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
52
|
+
var brandIdentifier;
|
|
53
|
+
return __generator(this, function (_a) {
|
|
54
|
+
switch (_a.label) {
|
|
55
|
+
case 0:
|
|
56
|
+
brandIdentifier = new BrandIdentifier_1.BrandIdentifier('hapi');
|
|
57
|
+
return [4 /*yield*/, brandIdentifier.getBrands()];
|
|
58
|
+
case 1:
|
|
59
|
+
_a.sent();
|
|
60
|
+
next();
|
|
61
|
+
return [2 /*return*/];
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}); });
|
|
53
65
|
};
|
|
54
66
|
ContextNamespace.setEBrand = function (req, res, next) {
|
|
55
67
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -80,11 +92,23 @@ var ContextNamespace = /** @class */ (function () {
|
|
|
80
92
|
/* Express End */
|
|
81
93
|
/* Hapi Start */
|
|
82
94
|
ContextNamespace.bindHNamespace = function (req, reply) {
|
|
95
|
+
var _this = this;
|
|
83
96
|
ns.bindEmitter(req.raw.req);
|
|
84
97
|
ns.bindEmitter(req.raw.res);
|
|
85
|
-
ns.run(function () {
|
|
86
|
-
|
|
87
|
-
|
|
98
|
+
ns.run(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
99
|
+
var brandIdentifier;
|
|
100
|
+
return __generator(this, function (_a) {
|
|
101
|
+
switch (_a.label) {
|
|
102
|
+
case 0:
|
|
103
|
+
brandIdentifier = new BrandIdentifier_1.BrandIdentifier('hapi');
|
|
104
|
+
return [4 /*yield*/, brandIdentifier.getBrands()];
|
|
105
|
+
case 1:
|
|
106
|
+
_a.sent();
|
|
107
|
+
reply();
|
|
108
|
+
return [2 /*return*/];
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}); });
|
|
88
112
|
};
|
|
89
113
|
ContextNamespace.setHBrand = function (req, reply) {
|
|
90
114
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -134,27 +158,15 @@ var ContextNamespace = /** @class */ (function () {
|
|
|
134
158
|
return ns.set('brands', brands);
|
|
135
159
|
};
|
|
136
160
|
ContextNamespace.getBrands = function () {
|
|
137
|
-
return
|
|
138
|
-
var brandIdentifier;
|
|
139
|
-
return __generator(this, function (_a) {
|
|
140
|
-
switch (_a.label) {
|
|
141
|
-
case 0:
|
|
142
|
-
if (!!ns.get('brands')) return [3 /*break*/, 2];
|
|
143
|
-
brandIdentifier = new BrandIdentifier_1.BrandIdentifier('hapi');
|
|
144
|
-
return [4 /*yield*/, brandIdentifier.getBrands()];
|
|
145
|
-
case 1:
|
|
146
|
-
_a.sent();
|
|
147
|
-
_a.label = 2;
|
|
148
|
-
case 2: return [2 /*return*/, ns.get('brands')];
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
});
|
|
161
|
+
return ns.get('brands');
|
|
152
162
|
};
|
|
153
163
|
ContextNamespace.getDefaultBrand = function () {
|
|
154
164
|
var brandIdentifier = new BrandIdentifier_1.BrandIdentifier('hapi');
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
165
|
+
return brandIdentifier.getDefaultBrand();
|
|
166
|
+
};
|
|
167
|
+
ContextNamespace.isValidBrand = function (brand) {
|
|
168
|
+
var _a;
|
|
169
|
+
return (_a = ns.get('brands')) === null || _a === void 0 ? void 0 : _a.includes(brand);
|
|
158
170
|
};
|
|
159
171
|
return ContextNamespace;
|
|
160
172
|
}());
|
|
@@ -9,21 +9,25 @@ var MongooseModel = /** @class */ (function () {
|
|
|
9
9
|
MongooseModel.prototype.createModel = function (name, schema, options, existingModels, skipBrand) {
|
|
10
10
|
var brands = [];
|
|
11
11
|
if (!skipBrand) {
|
|
12
|
-
brands =
|
|
12
|
+
brands = Storage_1.default.getEmployeeBrands()
|
|
13
|
+
? Storage_1.default.getEmployeeBrands()
|
|
14
|
+
: [Storage_1.default.getBrand()];
|
|
13
15
|
schema.add({ brand: String });
|
|
14
16
|
this.addPreReadQueries(schema);
|
|
15
17
|
this.addPreAggregateQueries(schema);
|
|
16
18
|
this.addPreSaveQueries(schema);
|
|
17
19
|
this.addPreInsertManyQueries(schema);
|
|
18
20
|
}
|
|
19
|
-
var modelName =
|
|
21
|
+
var modelName = skipBrand || !Array.isArray(brands) ? name : name + "-" + brands.join('-');
|
|
20
22
|
if (!existingModels[modelName]) {
|
|
21
23
|
existingModels[modelName] = this.mongoose.model(modelName, schema, options);
|
|
22
24
|
}
|
|
23
25
|
return existingModels[modelName];
|
|
24
26
|
};
|
|
25
27
|
MongooseModel.prototype.addPreCondition = function (next) {
|
|
26
|
-
var brands =
|
|
28
|
+
var brands = Storage_1.default.getEmployeeBrands()
|
|
29
|
+
? Storage_1.default.getEmployeeBrands()
|
|
30
|
+
: [Storage_1.default.getBrand()];
|
|
27
31
|
if (Array.isArray(brands)) {
|
|
28
32
|
// @ts-ignore
|
|
29
33
|
this.where({ brand: { $in: brands } });
|
|
@@ -45,7 +49,9 @@ var MongooseModel = /** @class */ (function () {
|
|
|
45
49
|
schema.pre('remove', this.addPreCondition);
|
|
46
50
|
};
|
|
47
51
|
MongooseModel.prototype.addPreAggregate = function (next) {
|
|
48
|
-
var brands =
|
|
52
|
+
var brands = Storage_1.default.getEmployeeBrands()
|
|
53
|
+
? Storage_1.default.getEmployeeBrands()
|
|
54
|
+
: [Storage_1.default.getBrand()];
|
|
49
55
|
if (Array.isArray(brands)) {
|
|
50
56
|
// @ts-ignore
|
|
51
57
|
this.pipeline().unshift({ $match: { brand: { $in: brands } } });
|
|
@@ -29,7 +29,7 @@ var TenantMongooseModule = /** @class */ (function (_super) {
|
|
|
29
29
|
}
|
|
30
30
|
TenantMongooseModule.forFeature = function (models, connectionName) {
|
|
31
31
|
if (models === void 0) { models = []; }
|
|
32
|
-
connectionName =
|
|
32
|
+
connectionName = connectionName ? connectionName + "Connection" : 'MongooseConnectionName';
|
|
33
33
|
var modelProviders = mongoose_providers_1.createMongooseProviders(connectionName, models);
|
|
34
34
|
return {
|
|
35
35
|
module: mongoose_1.MongooseModule,
|
|
@@ -55,7 +55,7 @@ var TenantRequest = /** @class */ (function () {
|
|
|
55
55
|
options.reqObject.headers['Content-Type'] = 'application/json';
|
|
56
56
|
options.reqObject.headers['x-brand'] = brand;
|
|
57
57
|
// tslint:disable-next-line:no-unused-expression
|
|
58
|
-
|
|
58
|
+
employeeBrands ? (options.reqObject.headers['x-employee-brands'] = employeeBrands.join(',')) : null;
|
|
59
59
|
return [4 /*yield*/, node_fetch_1.default(options.url, options.reqObject)
|
|
60
60
|
.then(function (res) {
|
|
61
61
|
return res.json();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@almatar/branding",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
"scripts": {
|
|
11
11
|
"build": "tsc",
|
|
12
12
|
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
|
|
13
|
-
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
|
|
14
13
|
"lint": "tslint -p tsconfig.json",
|
|
15
14
|
"test": "jest --config jestconfig.json",
|
|
16
15
|
"prepare": "npm run build",
|