@almatar/branding 0.1.5 → 0.1.7

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 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 "./lib/AlmatarBranding";
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
@@ -9,5 +9,5 @@ module.exports = {
9
9
  MultiTenant: MultiTenant_1.default,
10
10
  TenantMongooseModule: TenantMongooseModule_1.default,
11
11
  TenantRequest: TenantRequest_1.default,
12
- AlmatarBranding: AlmatarBranding_1.default
12
+ AlmatarBranding: AlmatarBranding_1.default,
13
13
  };
@@ -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 = (req.payload) ? req.payload : req.body;
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 = (req.payload) ? req.payload : req.body;
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*/, (brands.indexOf(reqBody.brand) > -1) ? brands : null];
92
+ return [2 /*return*/, brands.indexOf(reqBody.brand) > -1 ? brands : null];
93
93
  }
94
94
  return [2 /*return*/, brands];
95
95
  }
@@ -116,13 +116,14 @@ var BrandManager = /** @class */ (function () {
116
116
  case 0:
117
117
  _a.trys.push([0, 2, , 3]);
118
118
  option = {
119
- url: AlmatarBranding_1.default.dictionaryService + 'dictionary/brands',
119
+ url: AlmatarBranding_1.default.dictionaryService + 'brands',
120
120
  };
121
121
  return [4 /*yield*/, PromiseRequest_1.PromiseRequest.request(option)];
122
122
  case 1:
123
123
  brandsResponse = _a.sent();
124
124
  if (brandsResponse.status === 200) {
125
- brands = brandsResponse.data.map.list.map(function (brand) { return brand.key; });
125
+ Storage_1.default.setBrandsList(brandsResponse.data.list);
126
+ brands = brandsResponse.data.list.map(function (brand) { return brand.key; });
126
127
  Storage_1.default.setBrands(brands);
127
128
  return [2 /*return*/, brands];
128
129
  }
@@ -0,0 +1,9 @@
1
+ export declare class BrandModel {
2
+ id: number;
3
+ key: string;
4
+ name: string;
5
+ description: string;
6
+ isActive: boolean;
7
+ products: string[];
8
+ constructor(init?: Partial<BrandModel>);
9
+ }
@@ -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;
@@ -1,3 +1,4 @@
1
+ import { BrandModel } from './Models/BrandModel';
1
2
  export default class ContextNamespace {
2
3
  static bindENamespace(req: any, res: any, next: any): void;
3
4
  static setEBrand(req: any, res: any, next: any): Promise<void>;
@@ -7,7 +8,10 @@ export default class ContextNamespace {
7
8
  static getBrand(): any;
8
9
  static getEmployeeBrands(): any;
9
10
  static getNamespace(): any;
10
- static setBrands(brands: any): any;
11
- static getBrands(): Promise<any>;
12
- static getDefaultBrand(): any;
11
+ static setBrandsList(brandList: BrandModel[]): any;
12
+ static setBrands(brands: string[]): any;
13
+ static getBrands(): string[] | null;
14
+ static getDefaultBrand(): string | null;
15
+ static isValidBrand(brand: string): boolean;
16
+ static isBrandIncludesProduct(brandKey: string, product: string): boolean;
13
17
  }
@@ -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
- next();
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
- reply();
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 () {
@@ -130,31 +154,27 @@ var ContextNamespace = /** @class */ (function () {
130
154
  ContextNamespace.getNamespace = function () {
131
155
  return ns;
132
156
  };
157
+ ContextNamespace.setBrandsList = function (brandList) {
158
+ return ns.set('brandsList', brandList);
159
+ };
133
160
  ContextNamespace.setBrands = function (brands) {
134
161
  return ns.set('brands', brands);
135
162
  };
136
163
  ContextNamespace.getBrands = function () {
137
- return __awaiter(this, void 0, void 0, function () {
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
- });
164
+ return ns.get('brands');
152
165
  };
153
166
  ContextNamespace.getDefaultBrand = function () {
154
167
  var brandIdentifier = new BrandIdentifier_1.BrandIdentifier('hapi');
155
- var defaultBrand = brandIdentifier.getDefaultBrand();
156
- ns.set('defaultBrand', defaultBrand);
157
- return ns.get('defaultBrand');
168
+ return brandIdentifier.getDefaultBrand();
169
+ };
170
+ ContextNamespace.isValidBrand = function (brand) {
171
+ var _a;
172
+ return (_a = ns.get('brands')) === null || _a === void 0 ? void 0 : _a.includes(brand);
173
+ };
174
+ ContextNamespace.isBrandIncludesProduct = function (brandKey, product) {
175
+ var _a, _b;
176
+ var brand = (_a = ns.get('brandsList')) === null || _a === void 0 ? void 0 : _a.filter(function (b) { return b.key === brandKey; })[0];
177
+ return (_b = brand.products) === null || _b === void 0 ? void 0 : _b.includes(product);
158
178
  };
159
179
  return ContextNamespace;
160
180
  }());
@@ -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 = (Storage_1.default.getEmployeeBrands()) ? Storage_1.default.getEmployeeBrands() : [Storage_1.default.getBrand()];
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 = (skipBrand || !Array.isArray(brands)) ? name : name + "-" + brands.join("-");
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 = (Storage_1.default.getEmployeeBrands()) ? Storage_1.default.getEmployeeBrands() : [Storage_1.default.getBrand()];
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 = (Storage_1.default.getEmployeeBrands()) ? Storage_1.default.getEmployeeBrands() : [Storage_1.default.getBrand()];
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 = (connectionName) ? connectionName + "Connection" : 'MongooseConnectionName';
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,
@@ -49,8 +49,7 @@ var PromiseRequest = /** @class */ (function () {
49
49
  if (err) {
50
50
  reject(err);
51
51
  }
52
- ;
53
- body = (body) ? JSON.parse(body) : null;
52
+ body = body ? JSON.parse(body) : null;
54
53
  resolve(body);
55
54
  });
56
55
  })];
@@ -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
- (employeeBrands) ? options.reqObject.headers['x-employee-brands'] = employeeBrands.join(',') : null;
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.5",
3
+ "version": "0.1.7",
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",
package/readme.md CHANGED
@@ -77,6 +77,53 @@ handler: (request, reply) => {
77
77
  }
78
78
  ```
79
79
 
80
+ ### get brand
81
+
82
+ get brand from current context
83
+ ```
84
+ import { ContextNamespace } from '@almatar/branding';
85
+
86
+ handler: (request, reply) => {
87
+ const brand = ContextNamespace.getBrand();
88
+ }
89
+ ```
90
+
91
+ ### get default brand
92
+
93
+ get default brand
94
+ ```
95
+ import { ContextNamespace } from '@almatar/branding';
96
+
97
+ handler: (request, reply) => {
98
+ const defaultBrand = ContextNamespace.getDefaultBrand();
99
+ }
100
+ ```
101
+ ### validate brand
102
+
103
+ validate brand
104
+ ```
105
+ import { ContextNamespace } from '@almatar/branding';
106
+
107
+ handler: (request, reply) => {
108
+ /** use isValidBrand to validate if the incoming brand is valid */
109
+ if(ContextNamespace.isValidBrand(brand: string): boolean{
110
+ }
111
+
112
+ /** use isBrandIncludesProduct function to validate if product is related to brand*/
113
+ if(ContextNamespace.isBrandIncludesProduct(brandKey: string, product: string): boolean{
114
+ }
115
+ }
116
+ ```
117
+ ### get all available brands
118
+
119
+ get all available brands
120
+ ```
121
+ import { ContextNamespace } from '@almatar/branding';
122
+
123
+ handler: (request, reply) => {
124
+ const brands = ContextNamespace.getBrands();
125
+ }
126
+ ```
80
127
  ### build mongoose model
81
128
 
82
129
  ```