@almatar/branding 1.0.0-beta.3.6 → 1.0.0-beta.3.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.
|
@@ -53,6 +53,7 @@ export declare class TenantRepository<T extends ObjectLiteral> extends Repositor
|
|
|
53
53
|
* Apply brand filter to query builder
|
|
54
54
|
* Handles both cases: brand as JSON array and brand as string
|
|
55
55
|
* Uses $in equivalent for multiple brands from x-employee-brands
|
|
56
|
+
* When column is varchar/string, only IN clause is used (JSON_CONTAINS would fail on non-JSON).
|
|
56
57
|
*/
|
|
57
58
|
private applyBrandFilter;
|
|
58
59
|
}
|
|
@@ -251,29 +251,33 @@ class TenantRepository extends typeorm_1.Repository {
|
|
|
251
251
|
* Apply brand filter to query builder
|
|
252
252
|
* Handles both cases: brand as JSON array and brand as string
|
|
253
253
|
* Uses $in equivalent for multiple brands from x-employee-brands
|
|
254
|
+
* When column is varchar/string, only IN clause is used (JSON_CONTAINS would fail on non-JSON).
|
|
254
255
|
*/
|
|
255
256
|
applyBrandFilter(query, tableAlias) {
|
|
256
257
|
const brands = this.getBrandsFromContext();
|
|
257
258
|
if (brands && brands.length > 0) {
|
|
258
|
-
|
|
259
|
-
// 1. Brand column is JSON array: Check if ANY of the user's brands exist in the entity's brand array
|
|
260
|
-
// Using JSON_CONTAINS for each brand (equivalent to $in for arrays)
|
|
261
|
-
// 2. Brand column is string: Check if entity's brand is IN the user's brands array
|
|
262
|
-
// Using IN clause (equivalent to $in for strings)
|
|
263
|
-
// For JSON array: Check if any user brand is contained in entity's brand array
|
|
264
|
-
const jsonArrayConditions = brands.map((brandItem, index) => {
|
|
265
|
-
return `JSON_CONTAINS(${tableAlias}.${this.brandColumn}, :brand${index})`;
|
|
266
|
-
}).join(' OR ');
|
|
267
|
-
// For string: Check if entity's brand is in user's brands (IN clause)
|
|
268
|
-
const stringInPlaceholders = brands.map((brandItem, index) => `:brandStr${index}`).join(', ');
|
|
269
|
-
// Combine both conditions: (JSON array match) OR (string IN match)
|
|
270
|
-
const brandConditions = `(${jsonArrayConditions}) OR (${tableAlias}.${this.brandColumn} IN (${stringInPlaceholders}))`;
|
|
259
|
+
const stringInPlaceholders = brands.map((_, index) => `:brandStr${index}`).join(', ');
|
|
271
260
|
const brandParams = {};
|
|
272
261
|
brands.forEach((brand, index) => {
|
|
273
|
-
brandParams[`brand${index}`] = JSON.stringify(brand);
|
|
274
262
|
brandParams[`brandStr${index}`] = brand;
|
|
275
263
|
});
|
|
276
|
-
|
|
264
|
+
const brandColumnMeta = this.metadata.columns.find((c) => c.propertyName === this.brandColumn);
|
|
265
|
+
const isJsonColumn = brandColumnMeta && String(brandColumnMeta.type).toLowerCase() === 'json';
|
|
266
|
+
if (isJsonColumn) {
|
|
267
|
+
// JSON column: JSON_CONTAINS OR string IN (both for flexibility)
|
|
268
|
+
const jsonArrayConditions = brands.map((_, index) => {
|
|
269
|
+
return `JSON_CONTAINS(${tableAlias}.${this.brandColumn}, :brand${index})`;
|
|
270
|
+
}).join(' OR ');
|
|
271
|
+
brands.forEach((brand, index) => {
|
|
272
|
+
brandParams[`brand${index}`] = JSON.stringify(brand);
|
|
273
|
+
});
|
|
274
|
+
const brandConditions = `(${jsonArrayConditions}) OR (${tableAlias}.${this.brandColumn} IN (${stringInPlaceholders}))`;
|
|
275
|
+
query.andWhere(`(${brandConditions})`, brandParams);
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
// String/varchar column: only IN clause (JSON_CONTAINS would fail)
|
|
279
|
+
query.andWhere(`${tableAlias}.${this.brandColumn} IN (${stringInPlaceholders})`, brandParams);
|
|
280
|
+
}
|
|
277
281
|
}
|
|
278
282
|
return query;
|
|
279
283
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -10,8 +10,7 @@ this package make microservice able to handle different brands
|
|
|
10
10
|
import { AlmatarBranding } from '@almatar/branding';
|
|
11
11
|
|
|
12
12
|
AlmatarBranding.setup({
|
|
13
|
-
|
|
14
|
-
debug: false // set to false to disable all package console logs (default: true)
|
|
13
|
+
employeeAuthService: config.EMPLOYEE_AUTH_SERVICE
|
|
15
14
|
});
|
|
16
15
|
|
|
17
16
|
```
|