@b2y/ecommerce-common 1.2.7 → 1.2.9
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/index.js +2 -1
- package/model/Product.js +10 -0
- package/model/ProductVariant.js +10 -0
- package/package.json +1 -1
- package/scripts/RawQueryBuilder.js +163 -0
package/index.js
CHANGED
|
@@ -7,6 +7,7 @@ const TenantSettingsAbstractController = require('./controller/TenantSettingsAbs
|
|
|
7
7
|
const LocationController = require('./controller/LocationController');
|
|
8
8
|
const CommonAppConstants = require('./constants/AppConstants');
|
|
9
9
|
const CommonStatusMessage = require('./constants/StatusMessageConstants');
|
|
10
|
+
const RawQueryBuilder = require('./scripts/RawQueryBuilder');
|
|
10
11
|
const enums = {};
|
|
11
12
|
const enumDir = path.join(__dirname, 'enum');
|
|
12
13
|
fs.readdirSync(enumDir).forEach((file) => {
|
|
@@ -24,4 +25,4 @@ fs.readdirSync(utilDir).forEach((file) => {
|
|
|
24
25
|
}
|
|
25
26
|
})
|
|
26
27
|
|
|
27
|
-
module.exports = {initializeModels, enums, utils, CommonAppConstants, CommonStatusMessage, SubscriptionAbstractController, TenantAbstractController, TenantSettingsAbstractController, LocationController};
|
|
28
|
+
module.exports = {initializeModels, enums, utils, CommonAppConstants, CommonStatusMessage, SubscriptionAbstractController, TenantAbstractController, TenantSettingsAbstractController, LocationController, RawQueryBuilder};
|
package/model/Product.js
CHANGED
|
@@ -63,6 +63,16 @@ module.exports = (sequelize) => {
|
|
|
63
63
|
allowNull: false,
|
|
64
64
|
defaultValue: {}
|
|
65
65
|
},
|
|
66
|
+
GroupBy: {
|
|
67
|
+
type: DataTypes.UUID,
|
|
68
|
+
allowNull: true,
|
|
69
|
+
references: {
|
|
70
|
+
model: 'AttributeType',
|
|
71
|
+
key: 'AttributeTypeID'
|
|
72
|
+
},
|
|
73
|
+
onDelete: 'SET NULL',
|
|
74
|
+
onUpdate: 'CASCADE'
|
|
75
|
+
},
|
|
66
76
|
CreatedBy: {
|
|
67
77
|
type: DataTypes.UUID,
|
|
68
78
|
allowNull: false
|
package/model/ProductVariant.js
CHANGED
|
@@ -51,6 +51,16 @@ module.exports = (sequelize) => {
|
|
|
51
51
|
type: DataTypes.BOOLEAN,
|
|
52
52
|
defaultValue: true
|
|
53
53
|
},
|
|
54
|
+
GroupByAttributeValue: {
|
|
55
|
+
type: DataTypes.UUID,
|
|
56
|
+
allowNull: true,
|
|
57
|
+
references: {
|
|
58
|
+
model: 'AttributeType',
|
|
59
|
+
key: 'AttributeTypeID'
|
|
60
|
+
},
|
|
61
|
+
onDelete: 'SET NULL',
|
|
62
|
+
onUpdate: 'CASCADE'
|
|
63
|
+
},
|
|
54
64
|
CreatedBy: {
|
|
55
65
|
type: DataTypes.UUID,
|
|
56
66
|
allowNull: false
|
package/package.json
CHANGED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
class QueryBuilder{
|
|
2
|
+
static productQueryBuilder(filters) {
|
|
3
|
+
const {
|
|
4
|
+
DB_SCHEMA,
|
|
5
|
+
// Common filters
|
|
6
|
+
brandIds,
|
|
7
|
+
allCategoryIDs,
|
|
8
|
+
searchText,
|
|
9
|
+
|
|
10
|
+
// Customer filters
|
|
11
|
+
attributeValueIds,
|
|
12
|
+
min,
|
|
13
|
+
max,
|
|
14
|
+
discountPercentage,
|
|
15
|
+
|
|
16
|
+
// Admin filters
|
|
17
|
+
IsActive,
|
|
18
|
+
storeIds,
|
|
19
|
+
|
|
20
|
+
// Context
|
|
21
|
+
isAdmin = false,
|
|
22
|
+
isCustomer = false,
|
|
23
|
+
} = filters;
|
|
24
|
+
|
|
25
|
+
const schemaPrefix = `"${DB_SCHEMA}"`;
|
|
26
|
+
|
|
27
|
+
return `(
|
|
28
|
+
WITH filtered_variants AS (
|
|
29
|
+
SELECT
|
|
30
|
+
pv."ProductVariantID",
|
|
31
|
+
pv."ProductID",
|
|
32
|
+
pv."GroupByAttributeValue",
|
|
33
|
+
inv."SellingPrice",
|
|
34
|
+
inv."DiscountPercentage"
|
|
35
|
+
|
|
36
|
+
FROM ${schemaPrefix}."ProductVariant" pv
|
|
37
|
+
|
|
38
|
+
JOIN ${schemaPrefix}."Inventory" inv
|
|
39
|
+
ON pv."ProductVariantID" = inv."ProductVariantID"
|
|
40
|
+
|
|
41
|
+
WHERE pv."TenantID" = :tenantID
|
|
42
|
+
|
|
43
|
+
${isCustomer ? `AND pv."IsActive" = true` : ""}
|
|
44
|
+
|
|
45
|
+
${
|
|
46
|
+
isAdmin && IsActive !== undefined
|
|
47
|
+
? `AND pv."IsActive" = :IsActive`
|
|
48
|
+
: ""
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
${
|
|
52
|
+
storeIds?.length
|
|
53
|
+
? `AND inv."StoreID" IN (:storeIds)`
|
|
54
|
+
: ""
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
${
|
|
58
|
+
isCustomer && min !== null && min !== undefined
|
|
59
|
+
? `AND inv."SellingPrice" >= :min`
|
|
60
|
+
: ""
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
${
|
|
64
|
+
isCustomer && max !== null && max !== undefined
|
|
65
|
+
? `AND inv."SellingPrice" <= :max`
|
|
66
|
+
: ""
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
${
|
|
70
|
+
isCustomer &&
|
|
71
|
+
discountPercentage !== null &&
|
|
72
|
+
discountPercentage !== undefined
|
|
73
|
+
? `AND inv."DiscountPercentage" >= :discountPercentage`
|
|
74
|
+
: ""
|
|
75
|
+
}
|
|
76
|
+
),
|
|
77
|
+
filtered_products AS (
|
|
78
|
+
SELECT
|
|
79
|
+
p."ProductID",
|
|
80
|
+
p."GroupBy"
|
|
81
|
+
|
|
82
|
+
FROM ${schemaPrefix}."Product" p
|
|
83
|
+
|
|
84
|
+
LEFT JOIN ${schemaPrefix}."Brand" b
|
|
85
|
+
ON p."BrandID" = b."BrandID"
|
|
86
|
+
|
|
87
|
+
LEFT JOIN ${schemaPrefix}."Category" c
|
|
88
|
+
ON p."CategoryID" = c."CategoryID"
|
|
89
|
+
|
|
90
|
+
WHERE p."TenantID" = :tenantID
|
|
91
|
+
|
|
92
|
+
${isCustomer ? `AND p."IsActive" = true` : ""}
|
|
93
|
+
|
|
94
|
+
${isAdmin && IsActive !== undefined ? `AND p."IsActive" = :IsActive` : ""}
|
|
95
|
+
|
|
96
|
+
${brandIds?.length ? `AND p."BrandID" IN (:brandIds)` : ""}
|
|
97
|
+
|
|
98
|
+
${allCategoryIDs?.length ? `AND p."CategoryID" IN (:allCategoryIDs)` : ""}
|
|
99
|
+
|
|
100
|
+
${
|
|
101
|
+
searchText
|
|
102
|
+
? `
|
|
103
|
+
AND (
|
|
104
|
+
p."ProductName" ILIKE :search OR
|
|
105
|
+
p."ProductDescription" ILIKE :search OR
|
|
106
|
+
b."BrandName" ILIKE :search OR
|
|
107
|
+
c."CategoryName" ILIKE :search
|
|
108
|
+
)`
|
|
109
|
+
: ""
|
|
110
|
+
}
|
|
111
|
+
),
|
|
112
|
+
|
|
113
|
+
ranked_variants AS (
|
|
114
|
+
SELECT
|
|
115
|
+
fv."ProductVariantID",
|
|
116
|
+
fv."ProductID",
|
|
117
|
+
fv."GroupByAttributeValue",
|
|
118
|
+
fv."SellingPrice",
|
|
119
|
+
fp."GroupBy",
|
|
120
|
+
|
|
121
|
+
CASE
|
|
122
|
+
WHEN fp."GroupBy" IS NULL
|
|
123
|
+
THEN fv."ProductVariantID"::text
|
|
124
|
+
ELSE
|
|
125
|
+
fv."ProductID"::text || '_' || fv."GroupByAttributeValue"::text
|
|
126
|
+
END AS "groupKey",
|
|
127
|
+
|
|
128
|
+
ROW_NUMBER() OVER (
|
|
129
|
+
PARTITION BY
|
|
130
|
+
CASE
|
|
131
|
+
WHEN fp."GroupBy" IS NULL
|
|
132
|
+
THEN fv."ProductVariantID"::text
|
|
133
|
+
ELSE
|
|
134
|
+
fv."ProductID"::text || '_' || fv."GroupByAttributeValue"::text
|
|
135
|
+
END
|
|
136
|
+
ORDER BY fv."SellingPrice" ASC
|
|
137
|
+
) as rn
|
|
138
|
+
|
|
139
|
+
FROM filtered_variants fv
|
|
140
|
+
|
|
141
|
+
JOIN filtered_products fp
|
|
142
|
+
ON fv."ProductID" = fp."ProductID"
|
|
143
|
+
|
|
144
|
+
${
|
|
145
|
+
isCustomer && attributeValueIds?.length
|
|
146
|
+
? `
|
|
147
|
+
AND EXISTS (
|
|
148
|
+
SELECT 1
|
|
149
|
+
FROM ${schemaPrefix}."ProductVariantAttribute" pva
|
|
150
|
+
WHERE pva."ProductVariantID" = fv."ProductVariantID"
|
|
151
|
+
AND pva."AttributeValueID" IN (:attributeValueIds)
|
|
152
|
+
)`
|
|
153
|
+
: ""
|
|
154
|
+
}
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
SELECT "ProductVariantID"
|
|
158
|
+
FROM ranked_variants
|
|
159
|
+
WHERE rn = 1
|
|
160
|
+
)`;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
module.exports = RawQueryBuilder;
|