@b2y/ecommerce-common 1.1.5 → 1.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.
Files changed (84) hide show
  1. package/README.md +4 -4
  2. package/constants/AppConstants.js +14 -6
  3. package/constants/ReportConstants.js +14 -14
  4. package/constants/StatusMessageConstants.js +54 -17
  5. package/controller/LocationController.js +144 -0
  6. package/controller/SubscriptionAbstractController.js +161 -0
  7. package/controller/TenantAbstractController.js +349 -0
  8. package/controller/TenantSettingsAbstractController.js +70 -0
  9. package/dbconnection/Connect.js +432 -432
  10. package/enum/AccessModeEnum.js +7 -7
  11. package/enum/AddressTypeEnum.js +6 -6
  12. package/enum/BillingCycleEnum.js +5 -5
  13. package/enum/BooleanEnum.js +4 -4
  14. package/enum/BulkImportStatusEnum.js +6 -6
  15. package/enum/EntityTypeEnum.js +11 -11
  16. package/enum/FeatureTypeEnum.js +6 -6
  17. package/enum/GenderEnum.js +7 -7
  18. package/enum/NotificationStatusEnum.js +5 -5
  19. package/enum/NotificationTypeEnum.js +9 -9
  20. package/enum/OrderStatusEnum.js +7 -7
  21. package/enum/PaymentMethodEnum.js +6 -6
  22. package/enum/PaymentStatusEnum.js +6 -6
  23. package/enum/PaymentTypeEnum.js +6 -6
  24. package/enum/PlatformEnum.js +4 -4
  25. package/enum/RegistrationStatusEnum.js +5 -5
  26. package/enum/SortByEnum.js +7 -7
  27. package/enum/SubscriptionStatusEnum.js +7 -7
  28. package/index.js +25 -22
  29. package/model/Address.js +95 -95
  30. package/model/AttributeType.js +50 -50
  31. package/model/AttributeValue.js +64 -64
  32. package/model/Banner.js +78 -78
  33. package/model/Brand.js +76 -76
  34. package/model/Cart.js +76 -76
  35. package/model/Category.js +72 -72
  36. package/model/CategoryAttributeType.js +62 -62
  37. package/model/Colour.js +52 -52
  38. package/model/Customer.js +94 -94
  39. package/model/DeviceToken.js +51 -51
  40. package/model/Document.js +73 -73
  41. package/model/DynamicUIComponent.js +52 -52
  42. package/model/Feedback.js +79 -79
  43. package/model/Inventory.js +87 -83
  44. package/model/NotificationHistory.js +67 -67
  45. package/model/Order.js +94 -94
  46. package/model/OrderItem.js +98 -98
  47. package/model/OrderItemHistory.js +69 -69
  48. package/model/OrderStatus.js +48 -48
  49. package/model/Payment.js +101 -101
  50. package/model/PaymentMethod.js +36 -36
  51. package/model/PaymentStatus.js +36 -36
  52. package/model/PaymentType.js +36 -36
  53. package/model/Permission.js +55 -55
  54. package/model/Product.js +87 -87
  55. package/model/ProductGroup.js +48 -48
  56. package/model/ProductImport.js +55 -55
  57. package/model/ProductImportFailureAudits.js +57 -57
  58. package/model/ProductSpecification.js +65 -65
  59. package/model/ProductVariant.js +75 -75
  60. package/model/ProductVariantAttribute.js +58 -58
  61. package/model/Role.js +61 -61
  62. package/model/RolePermissionMapping.js +63 -63
  63. package/model/SpecificationType.js +41 -41
  64. package/model/Store.js +99 -99
  65. package/model/StoreUserMapping.js +44 -44
  66. package/model/SubscriptionFeature.js +49 -49
  67. package/model/SubscriptionPlan.js +66 -66
  68. package/model/SubscriptionPlanFeature.js +48 -48
  69. package/model/Tenant.js +91 -91
  70. package/model/TenantSettings.js +47 -47
  71. package/model/TenantSubscription.js +73 -73
  72. package/model/User.js +132 -132
  73. package/model/WishList.js +62 -62
  74. package/package.json +29 -29
  75. package/utility/AppUtil.js +65 -65
  76. package/utility/DateUtil.js +55 -55
  77. package/utility/ExcelUtil.js +125 -125
  78. package/utility/LocationUtility.js +130 -130
  79. package/utility/OrderTimeFilterUtil.js +88 -88
  80. package/utility/PdfUtil.js +64 -64
  81. package/utility/QueryUtil.js +261 -261
  82. package/utility/Razorpay.js +65 -65
  83. package/utility/ResolveAccessMode.js +38 -38
  84. package/utility/VariantPriceUtil.js +54 -54
@@ -0,0 +1,349 @@
1
+ const LocationUtility = require('../utility/LocationUtility');
2
+ const StatusMessage = require('../constants/StatusMessageConstants');
3
+ const AppUtil = require('../utility/AppUtil');
4
+ const SubscriptionStatusEnum = require('../enum/SubscriptionStatusEnum');
5
+ const AppConstants = require('../constants/AppConstants');
6
+
7
+ const updateTenant = (
8
+ Tenant,
9
+ TenantSubscription,
10
+ SubscriptionPlan,
11
+ sequelize,
12
+ logger,
13
+ ) => {
14
+ return async (req, res) => {
15
+ let transaction;
16
+ try {
17
+ const tenantId =
18
+ req.params && req.params.TenantID
19
+ ? req.params.TenantID
20
+ : req.body.TenantID;
21
+ if (!tenantId) {
22
+ logger.warn("TenantID is required for update.");
23
+ return AppUtil.generateResponse(
24
+ 400,
25
+ StatusMessage.FAILURE,
26
+ StatusMessage.TENANT_ID_REQUIRED_FIELD,
27
+ null,
28
+ res,
29
+ );
30
+ }
31
+
32
+ const existing = await Tenant.findOne({ where: { TenantID: tenantId } });
33
+ if (!existing) {
34
+ logger.warn("Tenant not found with ID:", tenantId);
35
+ return AppUtil.generateResponse(
36
+ 404,
37
+ StatusMessage.FAILURE,
38
+ StatusMessage.TENANT_NOT_FOUND,
39
+ null,
40
+ res,
41
+ );
42
+ }
43
+
44
+ const {
45
+ CompanyName,
46
+ CompanyCode,
47
+ Email,
48
+ PhoneNumber,
49
+ CountryCallingCode,
50
+ GSTNo,
51
+ CustomerPortalDomain,
52
+ AdminPortalDomain,
53
+ AddressLine,
54
+ CityName,
55
+ StateCode,
56
+ CountryCode,
57
+ Zipcode,
58
+ SubscriptionPlanID,
59
+ Status,
60
+ StartDate,
61
+ EndDate,
62
+ AutoRenew,
63
+ IsTrial,
64
+ GraceEndDate,
65
+ TrialEndDate,
66
+ } = req.body;
67
+
68
+ // Validate optional fields when provided
69
+ if (Email && !AppUtil.isValidEmail(Email)) {
70
+ logger.warn("Invalid email format provided for update.");
71
+ return AppUtil.generateResponse(
72
+ 400,
73
+ StatusMessage.FAILURE,
74
+ StatusMessage.INVALID_EMAIL_FORMAT,
75
+ null,
76
+ res,
77
+ );
78
+ }
79
+
80
+ if (PhoneNumber && !AppUtil.isValidPhone(PhoneNumber)) {
81
+ logger.warn("Invalid phone number format provided for update.");
82
+ return AppUtil.generateResponse(
83
+ 400,
84
+ StatusMessage.FAILURE,
85
+ StatusMessage.INVALID_PHONE_FORMAT,
86
+ null,
87
+ res,
88
+ );
89
+ }
90
+
91
+ if (CompanyCode !== undefined) {
92
+ if (
93
+ !Number.isInteger(Number(CompanyCode)) ||
94
+ Number(CompanyCode) <= 0
95
+ ) {
96
+ logger.warn("Invalid company code provided for update.");
97
+ return AppUtil.generateResponse(
98
+ 400,
99
+ StatusMessage.FAILURE,
100
+ StatusMessage.INVALID_COMPANY_CODE,
101
+ null,
102
+ res,
103
+ );
104
+ }
105
+ }
106
+
107
+ // Validate country code, state code and city name
108
+ if (CountryCode && StateCode && CityName && CountryCallingCode) {
109
+ const validation = await LocationUtility.validateCountryAndState(
110
+ CountryCode,
111
+ StateCode,
112
+ CityName,
113
+ CountryCallingCode,
114
+ logger,
115
+ );
116
+ if (!validation.isValid) {
117
+ return AppUtil.generateResponse(
118
+ 400,
119
+ StatusMessage.FAILURE,
120
+ validation.error,
121
+ null,
122
+ res,
123
+ );
124
+ }
125
+ }
126
+ if (Status && !Object.values(SubscriptionStatusEnum).includes(Status)) {
127
+ logger.warn("Invalid Subscription status");
128
+ return AppUtil.generateResponse(
129
+ 400,
130
+ StatusMessage.FAILURE,
131
+ StatusMessage.INVALID_SUBSCRIPTION_STATUS,
132
+ null,
133
+ res,
134
+ );
135
+ }
136
+
137
+ if (
138
+ AutoRenew !== undefined &&
139
+ typeof AutoRenew !== AppConstants.BOOLEAN
140
+ ) {
141
+ logger.warn("AutoRenew must be boolean type");
142
+ return AppUtil.generateResponse(
143
+ 400,
144
+ StatusMessage.FAILURE,
145
+ StatusMessage.INVALID_BOOLEAN_TYPE.replace("{field}", "AutoRenew"),
146
+ null,
147
+ res,
148
+ );
149
+ }
150
+ if (IsTrial !== undefined && typeof IsTrial !== AppConstants.BOOLEAN) {
151
+ logger.warn("IsTrial must be boolean type");
152
+ return AppUtil.generateResponse(
153
+ 400,
154
+ StatusMessage.FAILURE,
155
+ StatusMessage.INVALID_BOOLEAN_TYPE.replace("{field}", "IsTrial"),
156
+ null,
157
+ res,
158
+ );
159
+ }
160
+ transaction = await sequelize.transaction();
161
+ if (SubscriptionPlanID) {
162
+ const subscriptionPlan = await SubscriptionPlan.findOne({
163
+ where: {
164
+ SubscriptionPlanID,
165
+ IsActive: true,
166
+ },
167
+ transaction,
168
+ });
169
+ if (!subscriptionPlan) {
170
+ await transaction.rollback();
171
+ logger.warn("subscription plan not found");
172
+ return AppUtil.generateResponse(
173
+ 404,
174
+ StatusMessage.FAILURE,
175
+ StatusMessage.SUBSCRIPTION_PLAN_NOT_FOUND,
176
+ null,
177
+ res,
178
+ );
179
+ }
180
+ }
181
+ // Build update payload only with provided fields
182
+ const updatePayload = {};
183
+ const fields = {
184
+ CompanyName,
185
+ CompanyCode,
186
+ Email,
187
+ PhoneNumber,
188
+ CountryCode,
189
+ GSTNo,
190
+ CustomerPortalDomain,
191
+ AdminPortalDomain,
192
+ AddressLine,
193
+ CityName,
194
+ StateCode,
195
+ CountryCallingCode,
196
+ Zipcode,
197
+ };
198
+ Object.keys(fields).forEach((k) => {
199
+ if (fields[k] !== undefined)
200
+ updatePayload[k] =
201
+ k === AppConstants.COMPANY_CODE ? Number(fields[k]) : fields[k];
202
+ });
203
+
204
+ updatePayload.UpdatedBy =
205
+ AppConstants.UPDATED_BY || AppConstants.CREATED_BY;
206
+ updatePayload.UpdatedAt = new Date();
207
+
208
+ await existing.update(updatePayload, { transaction });
209
+
210
+ // update subscription
211
+ const updateSubscriptionPayload = {};
212
+ const updateSubscriptionfields = {
213
+ SubscriptionPlanID,
214
+ Status,
215
+ StartDate,
216
+ EndDate,
217
+ GraceEndDate,
218
+ TrialEndDate,
219
+ IsTrial,
220
+ AutoRenew,
221
+ };
222
+ Object.keys(updateSubscriptionfields).forEach((k) => {
223
+ if (updateSubscriptionfields[k] !== undefined)
224
+ updateSubscriptionPayload[k] = updateSubscriptionfields[k];
225
+ });
226
+ if (Object.keys(updateSubscriptionPayload).length !== 0) {
227
+ updateSubscriptionPayload.UpdatedBy = AppConstants.CREATED_BY;
228
+ updateSubscriptionPayload.UpdatedAt = new Date();
229
+
230
+ // find the existing subscription
231
+ const tenantSubscription = await TenantSubscription.findOne({
232
+ where: {
233
+ TenantID: tenantId,
234
+ },
235
+ transaction,
236
+ });
237
+ if (tenantSubscription) {
238
+ await tenantSubscription.update(updateSubscriptionPayload, {
239
+ transaction,
240
+ });
241
+ } else {
242
+ await TenantSubscription.create(
243
+ {
244
+ TenantID: tenantId,
245
+ ...updateSubscriptionPayload,
246
+ CreatedBy: AppConstants.CREATED_BY,
247
+ CreatedAt: new Date(),
248
+ },
249
+ { transaction },
250
+ );
251
+ }
252
+ }
253
+ await transaction.commit();
254
+
255
+ logger.info("Tenant updated successfully with ID:", tenantId);
256
+ return AppUtil.generateResponse(
257
+ 200,
258
+ StatusMessage.SUCCESS,
259
+ StatusMessage.TENANT_UPDATED_SUCCESS,
260
+ null,
261
+ res,
262
+ );
263
+ } catch (error) {
264
+ if (transaction) await transaction.rollback();
265
+
266
+ if (error.name === "SequelizeUniqueConstraintError") {
267
+ const detail = error.original?.detail || "";
268
+ if (detail.includes(AppConstants.COMPANY_NAME)) {
269
+ return AppUtil.generateResponse(
270
+ 400,
271
+ StatusMessage.FAILURE,
272
+ StatusMessage.COMPANY_NAME_EXISTS,
273
+ null,
274
+ res,
275
+ );
276
+ } else if (detail.includes(AppConstants.COMPANY_CODE)) {
277
+ return AppUtil.generateResponse(
278
+ 400,
279
+ StatusMessage.FAILURE,
280
+ StatusMessage.COMPANY_CODE_EXISTS,
281
+ null,
282
+ res,
283
+ );
284
+ } else if (detail.includes(AppConstants.EMAIL)) {
285
+ return AppUtil.generateResponse(
286
+ 400,
287
+ StatusMessage.FAILURE,
288
+ StatusMessage.EMAIL_EXISTS,
289
+ null,
290
+ res,
291
+ );
292
+ } else if (detail.includes(AppConstants.PHONE_NUMBER)) {
293
+ return AppUtil.generateResponse(
294
+ 400,
295
+ StatusMessage.FAILURE,
296
+ StatusMessage.PHONE_NUMBER_EXISTS,
297
+ null,
298
+ res,
299
+ );
300
+ } else if (detail.includes(AppConstants.CUSTOMER_PORTAL_DOMAIN)) {
301
+ return AppUtil.generateResponse(
302
+ 400,
303
+ StatusMessage.FAILURE,
304
+ StatusMessage.CUSTOMER_PORTAL_DOMAIN_EXISTS,
305
+ null,
306
+ res,
307
+ );
308
+ } else if (detail.includes(AppConstants.ADMIN_PORTAL_DOMAIN)) {
309
+ return AppUtil.generateResponse(
310
+ 400,
311
+ StatusMessage.FAILURE,
312
+ StatusMessage.ADMIN_PORTAL_DOMAIN_EXISTS,
313
+ null,
314
+ res,
315
+ );
316
+ }
317
+ return AppUtil.generateResponse(
318
+ 400,
319
+ StatusMessage.FAILURE,
320
+ StatusMessage.TENANT_ALREADY_EXISTS,
321
+ null,
322
+ res,
323
+ );
324
+ } else if (error.name === "SequelizeValidationError") {
325
+ const details = error.errors.map((e) => e.message);
326
+ logger.warn(`Validation Error detected: ${details.join("; ")}`);
327
+ return AppUtil.generateResponse(
328
+ 400,
329
+ StatusMessage.FAILURE,
330
+ StatusMessage.INVALID_REQUEST,
331
+ null,
332
+ res,
333
+ );
334
+ } else {
335
+ logger.error("Error in UpdateTenant:", error);
336
+ return AppUtil.generateResponse(
337
+ 500,
338
+ StatusMessage.FAILURE,
339
+ StatusMessage.TENANT_UPDATE_ERROR,
340
+ null,
341
+ res,
342
+ );
343
+ }
344
+ }
345
+ };
346
+ };
347
+
348
+ module.exports = { updateTenant };
349
+
@@ -0,0 +1,70 @@
1
+ const EntityTypeEnum = require('../enum/EntityTypeEnum');
2
+ const StatusMessage = require('../constants/StatusMessageConstants');
3
+ const AppUtil = require('../utility/AppUtil');
4
+
5
+ const getTenantSettingsById = (TenantSettings, Document, getPublicUrl, logger) => {
6
+ return async (req, res) => {
7
+ try {
8
+ const { id } = req.params;
9
+
10
+ const settings = await TenantSettings.findOne({
11
+ where: {
12
+ TenantID: id
13
+ },
14
+ attributes: ["TenantSettingID", "TenantID", "ThemeColour"],
15
+ });
16
+
17
+ if (!settings) {
18
+ return AppUtil.generateResponse(
19
+ 404,
20
+ StatusMessage.FAILURE,
21
+ StatusMessage.TENANT_SETTINGS_NOT_FOUND,
22
+ null,
23
+ res
24
+ );
25
+ }
26
+
27
+ const documents = await Document.findAll({
28
+ where: {
29
+ EntityType: EntityTypeEnum.TENANT_SETTINGS,
30
+ EntityID: settings.TenantSettingID
31
+ },
32
+ order: [["SortOrder", "ASC"]]
33
+ });
34
+
35
+ const logo = await Promise.all(
36
+ documents.map(async (doc) => ({
37
+ documentId: doc.DocumentID,
38
+ sortOrder: doc.SortOrder,
39
+ documentUrl: await getPublicUrl(doc, logger)
40
+ }))
41
+ );
42
+ const formattedSettings = {
43
+ TenantSettingID: settings.TenantSettingID,
44
+ TenantID: settings.TenantID,
45
+ ThemeColour: settings.ThemeColour,
46
+ Logo: logo
47
+ };
48
+
49
+ return AppUtil.generateResponse(
50
+ 200,
51
+ StatusMessage.SUCCESS,
52
+ null,
53
+ formattedSettings,
54
+ res
55
+ );
56
+
57
+ } catch (error) {
58
+ logger.error("Error fetching tenant settings:", error);
59
+ return AppUtil.generateResponse(
60
+ 500,
61
+ StatusMessage.FAILURE,
62
+ StatusMessage.SERVER_ERROR,
63
+ null,
64
+ res
65
+ );
66
+ }
67
+ }}
68
+
69
+ module.exports = {getTenantSettingsById};
70
+