@b2y/ecommerce-common 1.1.2 → 1.1.5
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/README.md +4 -4
- package/constants/AppConstants.js +6 -6
- package/constants/ReportConstants.js +14 -14
- package/constants/StatusMessageConstants.js +17 -17
- package/dbconnection/Connect.js +432 -432
- package/enum/AccessModeEnum.js +7 -7
- package/enum/AddressTypeEnum.js +6 -6
- package/enum/BillingCycleEnum.js +5 -5
- package/enum/BooleanEnum.js +4 -4
- package/enum/BulkImportStatusEnum.js +6 -6
- package/enum/EntityTypeEnum.js +11 -11
- package/enum/FeatureTypeEnum.js +6 -6
- package/enum/GenderEnum.js +7 -7
- package/enum/NotificationStatusEnum.js +5 -5
- package/enum/NotificationTypeEnum.js +9 -9
- package/enum/OrderStatusEnum.js +7 -7
- package/enum/PaymentMethodEnum.js +6 -6
- package/enum/PaymentStatusEnum.js +6 -6
- package/enum/PaymentTypeEnum.js +6 -6
- package/enum/PlatformEnum.js +4 -4
- package/enum/RegistrationStatusEnum.js +5 -5
- package/enum/SortByEnum.js +7 -7
- package/enum/SubscriptionStatusEnum.js +7 -7
- package/index.js +22 -22
- package/model/Address.js +95 -95
- package/model/AttributeType.js +50 -50
- package/model/AttributeValue.js +64 -64
- package/model/Banner.js +78 -78
- package/model/Brand.js +76 -76
- package/model/Cart.js +76 -76
- package/model/Category.js +72 -72
- package/model/CategoryAttributeType.js +62 -62
- package/model/Colour.js +52 -52
- package/model/Customer.js +94 -94
- package/model/DeviceToken.js +51 -51
- package/model/Document.js +73 -70
- package/model/DynamicUIComponent.js +52 -52
- package/model/Feedback.js +79 -79
- package/model/Inventory.js +83 -83
- package/model/NotificationHistory.js +67 -67
- package/model/Order.js +94 -94
- package/model/OrderItem.js +98 -98
- package/model/OrderItemHistory.js +69 -69
- package/model/OrderStatus.js +48 -48
- package/model/Payment.js +101 -101
- package/model/PaymentMethod.js +36 -36
- package/model/PaymentStatus.js +36 -36
- package/model/PaymentType.js +36 -36
- package/model/Permission.js +55 -55
- package/model/Product.js +87 -82
- package/model/ProductGroup.js +48 -48
- package/model/ProductImport.js +55 -56
- package/model/ProductImportFailureAudits.js +57 -57
- package/model/ProductSpecification.js +65 -65
- package/model/ProductVariant.js +75 -75
- package/model/ProductVariantAttribute.js +58 -58
- package/model/Role.js +61 -61
- package/model/RolePermissionMapping.js +63 -63
- package/model/SpecificationType.js +41 -41
- package/model/Store.js +99 -99
- package/model/StoreUserMapping.js +44 -44
- package/model/SubscriptionFeature.js +49 -49
- package/model/SubscriptionPlan.js +66 -66
- package/model/SubscriptionPlanFeature.js +48 -48
- package/model/Tenant.js +91 -91
- package/model/TenantSettings.js +47 -47
- package/model/TenantSubscription.js +73 -73
- package/model/User.js +132 -132
- package/model/WishList.js +62 -62
- package/package.json +29 -29
- package/utility/AppUtil.js +65 -65
- package/utility/DateUtil.js +55 -55
- package/utility/ExcelUtil.js +125 -125
- package/utility/LocationUtility.js +130 -130
- package/utility/OrderTimeFilterUtil.js +88 -88
- package/utility/PdfUtil.js +64 -64
- package/utility/QueryUtil.js +261 -261
- package/utility/Razorpay.js +65 -65
- package/utility/ResolveAccessMode.js +38 -38
- package/utility/VariantPriceUtil.js +54 -54
package/utility/ExcelUtil.js
CHANGED
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
const ExcelJS = require('exceljs');
|
|
2
|
-
const { EXCEL_METADATA, EXCEL_DEFAULT_THEME } = require("../constants/ReportConstants");
|
|
3
|
-
|
|
4
|
-
class ExcelUtil {
|
|
5
|
-
|
|
6
|
-
// Create workbook with metadata
|
|
7
|
-
static createWorkbook() {
|
|
8
|
-
const workbook = new ExcelJS.Workbook();
|
|
9
|
-
workbook.creator = EXCEL_METADATA.CREATOR;
|
|
10
|
-
workbook.lastModifiedBy = EXCEL_METADATA.LAST_MODIFIED_BY;
|
|
11
|
-
workbook.created = new Date();
|
|
12
|
-
workbook.modified = new Date();
|
|
13
|
-
return workbook;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// Safe worksheet name
|
|
17
|
-
static createWorksheet(workbook, sheetName) {
|
|
18
|
-
const cleanSheetName = sheetName
|
|
19
|
-
.replace(/[\\/*[\]:?]/g, "")
|
|
20
|
-
.substring(0, 31);
|
|
21
|
-
|
|
22
|
-
return workbook.addWorksheet(cleanSheetName);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Setup headers using theme from constants
|
|
26
|
-
static setupHeaders(worksheet, headers) {
|
|
27
|
-
worksheet.columns = headers;
|
|
28
|
-
|
|
29
|
-
const headerRow = worksheet.getRow(1);
|
|
30
|
-
headerRow.eachCell(cell => {
|
|
31
|
-
cell.font = {
|
|
32
|
-
bold: true,
|
|
33
|
-
color: { argb: EXCEL_DEFAULT_THEME.HEADER_FONT_COLOR },
|
|
34
|
-
size: 11
|
|
35
|
-
};
|
|
36
|
-
cell.fill = {
|
|
37
|
-
type: "pattern",
|
|
38
|
-
pattern: "solid",
|
|
39
|
-
fgColor: { argb: EXCEL_DEFAULT_THEME.HEADER_FILL_COLOR }
|
|
40
|
-
};
|
|
41
|
-
cell.alignment = { horizontal: "center", vertical: "middle", wrapText: true };
|
|
42
|
-
cell.border = {
|
|
43
|
-
top: { style: "thin" },
|
|
44
|
-
left: { style: "thin" },
|
|
45
|
-
bottom: { style: "thin" },
|
|
46
|
-
right: { style: "thin" }
|
|
47
|
-
};
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
worksheet.views = [{ state: "frozen", ySplit: 1 }];
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
static cleanValue(val) {
|
|
54
|
-
if (val == null) return "";
|
|
55
|
-
|
|
56
|
-
if (typeof val === "string") {
|
|
57
|
-
// eslint-disable-next-line no-control-regex
|
|
58
|
-
return val.replace(/[\x00-\x1F\x7F]/g, "");
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (typeof val === "object") {
|
|
62
|
-
return JSON.stringify(val).substring(0, 32767);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return val;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
static cleanRow(row) {
|
|
69
|
-
const clean = {};
|
|
70
|
-
for (let key in row) clean[key] = ExcelUtil.cleanValue(row[key]);
|
|
71
|
-
return clean;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
static styleDataRows(worksheet) {
|
|
75
|
-
worksheet.eachRow((row, idx) => {
|
|
76
|
-
if (idx !== 1) {
|
|
77
|
-
row.eachCell(cell => {
|
|
78
|
-
const isNumber = typeof cell.value === 'number';
|
|
79
|
-
cell.alignment = { horizontal: isNumber ? 'right' : 'left', vertical: "middle", wrapText: true };
|
|
80
|
-
cell.border = {
|
|
81
|
-
top: { style: "thin" },
|
|
82
|
-
left: { style: "thin" },
|
|
83
|
-
bottom: { style: "thin" },
|
|
84
|
-
right: { style: "thin" }
|
|
85
|
-
};
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
static autoAdjustColumns(worksheet) {
|
|
92
|
-
worksheet.columns.forEach(col => {
|
|
93
|
-
let maxLength = 10;
|
|
94
|
-
col.eachCell({ includeEmpty: true }, cell => {
|
|
95
|
-
const val = cell.value ? cell.value.toString() : "";
|
|
96
|
-
maxLength = Math.max(maxLength, val.length + 2);
|
|
97
|
-
});
|
|
98
|
-
col.width = Math.min(maxLength, 50);
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
static async writeToResponse(workbook, res, filename, logger) {
|
|
103
|
-
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
104
|
-
const finalName = `${filename}_${timestamp}.xlsx`;
|
|
105
|
-
|
|
106
|
-
res.setHeader("Content-Disposition", `attachment; filename="${finalName}"`);
|
|
107
|
-
res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
108
|
-
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
109
|
-
res.setHeader("Pragma", "no-cache");
|
|
110
|
-
res.setHeader("Expires", "0");
|
|
111
|
-
res.setHeader("Content-Transfer-Encoding", "binary");
|
|
112
|
-
|
|
113
|
-
try {
|
|
114
|
-
await workbook.xlsx.write(res);
|
|
115
|
-
res.end();
|
|
116
|
-
} catch (err) {
|
|
117
|
-
logger.error("Excel generation error:", err);
|
|
118
|
-
if (!res.headersSent) {
|
|
119
|
-
res.status(500).json({ error: "Excel generation failed" });
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
module.exports = ExcelUtil;
|
|
1
|
+
const ExcelJS = require('exceljs');
|
|
2
|
+
const { EXCEL_METADATA, EXCEL_DEFAULT_THEME } = require("../constants/ReportConstants");
|
|
3
|
+
|
|
4
|
+
class ExcelUtil {
|
|
5
|
+
|
|
6
|
+
// Create workbook with metadata
|
|
7
|
+
static createWorkbook() {
|
|
8
|
+
const workbook = new ExcelJS.Workbook();
|
|
9
|
+
workbook.creator = EXCEL_METADATA.CREATOR;
|
|
10
|
+
workbook.lastModifiedBy = EXCEL_METADATA.LAST_MODIFIED_BY;
|
|
11
|
+
workbook.created = new Date();
|
|
12
|
+
workbook.modified = new Date();
|
|
13
|
+
return workbook;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Safe worksheet name
|
|
17
|
+
static createWorksheet(workbook, sheetName) {
|
|
18
|
+
const cleanSheetName = sheetName
|
|
19
|
+
.replace(/[\\/*[\]:?]/g, "")
|
|
20
|
+
.substring(0, 31);
|
|
21
|
+
|
|
22
|
+
return workbook.addWorksheet(cleanSheetName);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Setup headers using theme from constants
|
|
26
|
+
static setupHeaders(worksheet, headers) {
|
|
27
|
+
worksheet.columns = headers;
|
|
28
|
+
|
|
29
|
+
const headerRow = worksheet.getRow(1);
|
|
30
|
+
headerRow.eachCell(cell => {
|
|
31
|
+
cell.font = {
|
|
32
|
+
bold: true,
|
|
33
|
+
color: { argb: EXCEL_DEFAULT_THEME.HEADER_FONT_COLOR },
|
|
34
|
+
size: 11
|
|
35
|
+
};
|
|
36
|
+
cell.fill = {
|
|
37
|
+
type: "pattern",
|
|
38
|
+
pattern: "solid",
|
|
39
|
+
fgColor: { argb: EXCEL_DEFAULT_THEME.HEADER_FILL_COLOR }
|
|
40
|
+
};
|
|
41
|
+
cell.alignment = { horizontal: "center", vertical: "middle", wrapText: true };
|
|
42
|
+
cell.border = {
|
|
43
|
+
top: { style: "thin" },
|
|
44
|
+
left: { style: "thin" },
|
|
45
|
+
bottom: { style: "thin" },
|
|
46
|
+
right: { style: "thin" }
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
worksheet.views = [{ state: "frozen", ySplit: 1 }];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static cleanValue(val) {
|
|
54
|
+
if (val == null) return "";
|
|
55
|
+
|
|
56
|
+
if (typeof val === "string") {
|
|
57
|
+
// eslint-disable-next-line no-control-regex
|
|
58
|
+
return val.replace(/[\x00-\x1F\x7F]/g, "");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (typeof val === "object") {
|
|
62
|
+
return JSON.stringify(val).substring(0, 32767);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return val;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static cleanRow(row) {
|
|
69
|
+
const clean = {};
|
|
70
|
+
for (let key in row) clean[key] = ExcelUtil.cleanValue(row[key]);
|
|
71
|
+
return clean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static styleDataRows(worksheet) {
|
|
75
|
+
worksheet.eachRow((row, idx) => {
|
|
76
|
+
if (idx !== 1) {
|
|
77
|
+
row.eachCell(cell => {
|
|
78
|
+
const isNumber = typeof cell.value === 'number';
|
|
79
|
+
cell.alignment = { horizontal: isNumber ? 'right' : 'left', vertical: "middle", wrapText: true };
|
|
80
|
+
cell.border = {
|
|
81
|
+
top: { style: "thin" },
|
|
82
|
+
left: { style: "thin" },
|
|
83
|
+
bottom: { style: "thin" },
|
|
84
|
+
right: { style: "thin" }
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
static autoAdjustColumns(worksheet) {
|
|
92
|
+
worksheet.columns.forEach(col => {
|
|
93
|
+
let maxLength = 10;
|
|
94
|
+
col.eachCell({ includeEmpty: true }, cell => {
|
|
95
|
+
const val = cell.value ? cell.value.toString() : "";
|
|
96
|
+
maxLength = Math.max(maxLength, val.length + 2);
|
|
97
|
+
});
|
|
98
|
+
col.width = Math.min(maxLength, 50);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static async writeToResponse(workbook, res, filename, logger) {
|
|
103
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
104
|
+
const finalName = `${filename}_${timestamp}.xlsx`;
|
|
105
|
+
|
|
106
|
+
res.setHeader("Content-Disposition", `attachment; filename="${finalName}"`);
|
|
107
|
+
res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
108
|
+
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
109
|
+
res.setHeader("Pragma", "no-cache");
|
|
110
|
+
res.setHeader("Expires", "0");
|
|
111
|
+
res.setHeader("Content-Transfer-Encoding", "binary");
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
await workbook.xlsx.write(res);
|
|
115
|
+
res.end();
|
|
116
|
+
} catch (err) {
|
|
117
|
+
logger.error("Excel generation error:", err);
|
|
118
|
+
if (!res.headersSent) {
|
|
119
|
+
res.status(500).json({ error: "Excel generation failed" });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
module.exports = ExcelUtil;
|
|
@@ -1,131 +1,131 @@
|
|
|
1
|
-
const { getCountries, getStatesOfCountry, getCitiesOfState } = require('@countrystatecity/countries');
|
|
2
|
-
|
|
3
|
-
// Helper to resolve country and state names from codes
|
|
4
|
-
async function resolveCountryAndState(countryCode, stateCode, logger) {
|
|
5
|
-
// eslint-disable-next-line no-useless-catch
|
|
6
|
-
try {
|
|
7
|
-
const trimmedCountry = countryCode ? String(countryCode).trim() : "";
|
|
8
|
-
const trimmedState = stateCode ? String(stateCode).trim() : "";
|
|
9
|
-
|
|
10
|
-
let countryName = trimmedCountry;
|
|
11
|
-
let stateName = trimmedState;
|
|
12
|
-
|
|
13
|
-
if (trimmedCountry) {
|
|
14
|
-
try {
|
|
15
|
-
const countries = await getCountries();
|
|
16
|
-
const country = countries.find(
|
|
17
|
-
(c) =>
|
|
18
|
-
c.iso2 === trimmedCountry || c.iso2 === trimmedCountry.toUpperCase()
|
|
19
|
-
);
|
|
20
|
-
if (country) countryName = country.name;
|
|
21
|
-
} catch (err) {
|
|
22
|
-
logger.error("Error resolving country name:", err);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (trimmedState) {
|
|
27
|
-
try {
|
|
28
|
-
const states = await getStatesOfCountry(trimmedCountry);
|
|
29
|
-
const state = states.find(
|
|
30
|
-
(s) =>
|
|
31
|
-
s.iso2 === trimmedState ||
|
|
32
|
-
s.state_code === trimmedState ||
|
|
33
|
-
s.state_code === trimmedState.toUpperCase()
|
|
34
|
-
);
|
|
35
|
-
if (state) stateName = state.name;
|
|
36
|
-
} catch (err) {
|
|
37
|
-
logger.error("Error resolving state name:", err);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return { countryName, stateName };
|
|
42
|
-
} catch (err) {
|
|
43
|
-
throw err;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Helper to validate country and state codes
|
|
48
|
-
async function validateCountryAndState(countryCode, stateCode, cityName, countryCallingCode, logger) {
|
|
49
|
-
try {
|
|
50
|
-
const trimmedCountryCode = countryCode ? String(countryCode).trim() : "";
|
|
51
|
-
const trimmedStateCode = stateCode ? String(stateCode).trim() : "";
|
|
52
|
-
const trimmedCityName = cityName ? cityName.trim().toLowerCase() : "";
|
|
53
|
-
const trimmedCountryCallingCode = countryCallingCode ? countryCallingCode.replace('+', '').trim() : "";
|
|
54
|
-
// Validate country code exists
|
|
55
|
-
if (trimmedCountryCode) {
|
|
56
|
-
const countries = await getCountries();
|
|
57
|
-
const validCountry = countries.find(
|
|
58
|
-
(c) =>
|
|
59
|
-
c.iso2 === trimmedCountryCode ||
|
|
60
|
-
c.iso2 === trimmedCountryCode.toUpperCase()
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
if (!validCountry) {
|
|
64
|
-
return {
|
|
65
|
-
isValid: false,
|
|
66
|
-
error: `Invalid country code: ${countryCode}`,
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Validate state code if provided
|
|
71
|
-
if (trimmedStateCode) {
|
|
72
|
-
const states = await getStatesOfCountry(trimmedCountryCode);
|
|
73
|
-
const validState = states.find(
|
|
74
|
-
(s) =>
|
|
75
|
-
s.iso2 === trimmedStateCode ||
|
|
76
|
-
s.state_code === trimmedStateCode ||
|
|
77
|
-
s.state_code === trimmedStateCode.toUpperCase()
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
if (!validState) {
|
|
81
|
-
return {
|
|
82
|
-
isValid: false,
|
|
83
|
-
error: `Invalid state code: ${stateCode} for country: ${countryCode}`,
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
if (trimmedCityName) {
|
|
88
|
-
const cities = await getCitiesOfState(
|
|
89
|
-
trimmedCountryCode,
|
|
90
|
-
trimmedStateCode
|
|
91
|
-
);
|
|
92
|
-
const cityFound = cities.some(
|
|
93
|
-
(city) => city.name.trim().toLowerCase() === trimmedCityName
|
|
94
|
-
);
|
|
95
|
-
if (!cityFound) {
|
|
96
|
-
return {
|
|
97
|
-
isValid: false,
|
|
98
|
-
error: `Invalid city name for state code ${stateCode} and country code ${countryCode}`,
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
if (trimmedCountryCallingCode) {
|
|
103
|
-
const countries = await getCountries();
|
|
104
|
-
const validCallingCode = countries.find(
|
|
105
|
-
(country) =>
|
|
106
|
-
country.iso2 === trimmedCountryCode &&
|
|
107
|
-
country.phonecode === trimmedCountryCallingCode
|
|
108
|
-
);
|
|
109
|
-
if (!validCallingCode) {
|
|
110
|
-
return {
|
|
111
|
-
isValid: false,
|
|
112
|
-
error: `Invalid country calling code for country code ${trimmedCountryCode}`,
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return { isValid: true };
|
|
119
|
-
} catch (err) {
|
|
120
|
-
logger.error("Error during country/state/city validation:", err);
|
|
121
|
-
return {
|
|
122
|
-
isValid: false,
|
|
123
|
-
error: "Error validating country code, state code and city name",
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
module.exports = {
|
|
129
|
-
resolveCountryAndState,
|
|
130
|
-
validateCountryAndState
|
|
1
|
+
const { getCountries, getStatesOfCountry, getCitiesOfState } = require('@countrystatecity/countries');
|
|
2
|
+
|
|
3
|
+
// Helper to resolve country and state names from codes
|
|
4
|
+
async function resolveCountryAndState(countryCode, stateCode, logger) {
|
|
5
|
+
// eslint-disable-next-line no-useless-catch
|
|
6
|
+
try {
|
|
7
|
+
const trimmedCountry = countryCode ? String(countryCode).trim() : "";
|
|
8
|
+
const trimmedState = stateCode ? String(stateCode).trim() : "";
|
|
9
|
+
|
|
10
|
+
let countryName = trimmedCountry;
|
|
11
|
+
let stateName = trimmedState;
|
|
12
|
+
|
|
13
|
+
if (trimmedCountry) {
|
|
14
|
+
try {
|
|
15
|
+
const countries = await getCountries();
|
|
16
|
+
const country = countries.find(
|
|
17
|
+
(c) =>
|
|
18
|
+
c.iso2 === trimmedCountry || c.iso2 === trimmedCountry.toUpperCase()
|
|
19
|
+
);
|
|
20
|
+
if (country) countryName = country.name;
|
|
21
|
+
} catch (err) {
|
|
22
|
+
logger.error("Error resolving country name:", err);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (trimmedState) {
|
|
27
|
+
try {
|
|
28
|
+
const states = await getStatesOfCountry(trimmedCountry);
|
|
29
|
+
const state = states.find(
|
|
30
|
+
(s) =>
|
|
31
|
+
s.iso2 === trimmedState ||
|
|
32
|
+
s.state_code === trimmedState ||
|
|
33
|
+
s.state_code === trimmedState.toUpperCase()
|
|
34
|
+
);
|
|
35
|
+
if (state) stateName = state.name;
|
|
36
|
+
} catch (err) {
|
|
37
|
+
logger.error("Error resolving state name:", err);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return { countryName, stateName };
|
|
42
|
+
} catch (err) {
|
|
43
|
+
throw err;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Helper to validate country and state codes
|
|
48
|
+
async function validateCountryAndState(countryCode, stateCode, cityName, countryCallingCode, logger) {
|
|
49
|
+
try {
|
|
50
|
+
const trimmedCountryCode = countryCode ? String(countryCode).trim() : "";
|
|
51
|
+
const trimmedStateCode = stateCode ? String(stateCode).trim() : "";
|
|
52
|
+
const trimmedCityName = cityName ? cityName.trim().toLowerCase() : "";
|
|
53
|
+
const trimmedCountryCallingCode = countryCallingCode ? countryCallingCode.replace('+', '').trim() : "";
|
|
54
|
+
// Validate country code exists
|
|
55
|
+
if (trimmedCountryCode) {
|
|
56
|
+
const countries = await getCountries();
|
|
57
|
+
const validCountry = countries.find(
|
|
58
|
+
(c) =>
|
|
59
|
+
c.iso2 === trimmedCountryCode ||
|
|
60
|
+
c.iso2 === trimmedCountryCode.toUpperCase()
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
if (!validCountry) {
|
|
64
|
+
return {
|
|
65
|
+
isValid: false,
|
|
66
|
+
error: `Invalid country code: ${countryCode}`,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Validate state code if provided
|
|
71
|
+
if (trimmedStateCode) {
|
|
72
|
+
const states = await getStatesOfCountry(trimmedCountryCode);
|
|
73
|
+
const validState = states.find(
|
|
74
|
+
(s) =>
|
|
75
|
+
s.iso2 === trimmedStateCode ||
|
|
76
|
+
s.state_code === trimmedStateCode ||
|
|
77
|
+
s.state_code === trimmedStateCode.toUpperCase()
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
if (!validState) {
|
|
81
|
+
return {
|
|
82
|
+
isValid: false,
|
|
83
|
+
error: `Invalid state code: ${stateCode} for country: ${countryCode}`,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (trimmedCityName) {
|
|
88
|
+
const cities = await getCitiesOfState(
|
|
89
|
+
trimmedCountryCode,
|
|
90
|
+
trimmedStateCode
|
|
91
|
+
);
|
|
92
|
+
const cityFound = cities.some(
|
|
93
|
+
(city) => city.name.trim().toLowerCase() === trimmedCityName
|
|
94
|
+
);
|
|
95
|
+
if (!cityFound) {
|
|
96
|
+
return {
|
|
97
|
+
isValid: false,
|
|
98
|
+
error: `Invalid city name for state code ${stateCode} and country code ${countryCode}`,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (trimmedCountryCallingCode) {
|
|
103
|
+
const countries = await getCountries();
|
|
104
|
+
const validCallingCode = countries.find(
|
|
105
|
+
(country) =>
|
|
106
|
+
country.iso2 === trimmedCountryCode &&
|
|
107
|
+
country.phonecode === trimmedCountryCallingCode
|
|
108
|
+
);
|
|
109
|
+
if (!validCallingCode) {
|
|
110
|
+
return {
|
|
111
|
+
isValid: false,
|
|
112
|
+
error: `Invalid country calling code for country code ${trimmedCountryCode}`,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return { isValid: true };
|
|
119
|
+
} catch (err) {
|
|
120
|
+
logger.error("Error during country/state/city validation:", err);
|
|
121
|
+
return {
|
|
122
|
+
isValid: false,
|
|
123
|
+
error: "Error validating country code, state code and city name",
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
module.exports = {
|
|
129
|
+
resolveCountryAndState,
|
|
130
|
+
validateCountryAndState
|
|
131
131
|
};
|