updated code with dsc and device binding
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const axios = require('axios');
|
||||
const { BASE_URL } = require('../src/Services/ApiCall/ApiUrls');
|
||||
|
||||
const REGISTER_URL =
|
||||
`${BASE_URL}contractor/registerContractorStage1`;
|
||||
const DESIGNATION_URL =
|
||||
`${BASE_URL}contractor/getAllDesignationContractor`;
|
||||
const DEFAULT_PASSWORD = 'P@ssword123';
|
||||
|
||||
function getArgValue(name, fallback) {
|
||||
const prefix = `--${name}=`;
|
||||
const arg = process.argv.find(item => item.startsWith(prefix));
|
||||
if (!arg) return fallback;
|
||||
return arg.slice(prefix.length);
|
||||
}
|
||||
|
||||
function toPositiveInt(value, fallback) {
|
||||
const parsed = Number(value);
|
||||
if (!Number.isInteger(parsed) || parsed <= 0) return fallback;
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function buildMobile(seed, index) {
|
||||
// Keep 10 digits and start with 9 to match app-side validation.
|
||||
const dynamicPart = (seed * 1000 + index) % 999999999;
|
||||
return String(9000000000 + dynamicPart).padStart(10, '9').slice(0, 10);
|
||||
}
|
||||
|
||||
function toAlphabeticSuffix(index) {
|
||||
let n = index;
|
||||
let label = '';
|
||||
while (n > 0) {
|
||||
const rem = (n - 1) % 26;
|
||||
label = String.fromCharCode(65 + rem) + label;
|
||||
n = Math.floor((n - 1) / 26);
|
||||
}
|
||||
return label || 'A';
|
||||
}
|
||||
|
||||
async function getDefaultDesignation() {
|
||||
try {
|
||||
const { data } = await axios.get(DESIGNATION_URL, { timeout: 15000 });
|
||||
const list = data?.data;
|
||||
if (Array.isArray(list) && list.length > 0) {
|
||||
const firstId = Number(list[0]?.designationid);
|
||||
if (Number.isFinite(firstId) && firstId > 0) return firstId;
|
||||
}
|
||||
} catch (error) {
|
||||
const message = error?.response?.data?.message || error?.message || 'Unknown error';
|
||||
console.warn(`Failed to fetch designation list, using fallback id=1. ${message}`);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
async function registerUser({
|
||||
designation,
|
||||
emailPrefix,
|
||||
firstNamePrefix,
|
||||
seed,
|
||||
index,
|
||||
password,
|
||||
}) {
|
||||
const serial = seed * 1000 + index;
|
||||
const emailAdd = `${emailPrefix}${serial}@example.com`;
|
||||
const alphaSuffix = toAlphabeticSuffix(index);
|
||||
const payload = {
|
||||
firstName: `${firstNamePrefix}${alphaSuffix}`,
|
||||
middleName: '',
|
||||
lastName: 'Auto',
|
||||
dialCode: '+91',
|
||||
mobNo: buildMobile(seed, index),
|
||||
emailAdd,
|
||||
contractorType: 'Indian',
|
||||
designation,
|
||||
password,
|
||||
otherDesignation: null,
|
||||
conPassword: password,
|
||||
device: 'mobileApp',
|
||||
};
|
||||
|
||||
try {
|
||||
const { data } = await axios.post(REGISTER_URL, payload, { timeout: 20000 });
|
||||
const status = String(data?.status ?? data?.statusCode ?? '');
|
||||
const ok = status === '201' || status === '200' || status === 'SUCCESS';
|
||||
return {
|
||||
ok,
|
||||
email: emailAdd,
|
||||
mobile: payload.mobNo,
|
||||
message: data?.message || (ok ? 'Success' : 'Unexpected response'),
|
||||
rawStatus: status || 'unknown',
|
||||
details: data,
|
||||
};
|
||||
} catch (error) {
|
||||
const responseData = error?.response?.data;
|
||||
const apiMessage =
|
||||
responseData?.message ||
|
||||
responseData?.error ||
|
||||
error?.message ||
|
||||
'Request failed';
|
||||
return {
|
||||
ok: false,
|
||||
email: emailAdd,
|
||||
mobile: payload.mobNo,
|
||||
message: apiMessage,
|
||||
rawStatus: String(error?.response?.status || 'network_error'),
|
||||
details: responseData,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const count = toPositiveInt(getArgValue('count', '100'), 100);
|
||||
const concurrency = toPositiveInt(getArgValue('concurrency', '5'), 5);
|
||||
const startIndex = toPositiveInt(getArgValue('start', '1'), 1);
|
||||
const password = getArgValue('password', DEFAULT_PASSWORD);
|
||||
const emailPrefix = getArgValue('emailPrefix', 'bulkuser');
|
||||
const firstNamePrefix = getArgValue('firstNamePrefix', 'BulkUser');
|
||||
const seed = Number(String(Date.now()).slice(-6));
|
||||
const designation = await getDefaultDesignation();
|
||||
|
||||
console.log(
|
||||
`Creating ${count} users (start=${startIndex}, concurrency=${concurrency}, designation=${designation})...`,
|
||||
);
|
||||
|
||||
const indexes = Array.from({ length: count }, (_, i) => i + startIndex);
|
||||
let cursor = 0;
|
||||
const results = [];
|
||||
|
||||
async function worker() {
|
||||
while (true) {
|
||||
const current = cursor;
|
||||
cursor += 1;
|
||||
if (current >= indexes.length) break;
|
||||
const index = indexes[current];
|
||||
const result = await registerUser({
|
||||
designation,
|
||||
emailPrefix,
|
||||
firstNamePrefix,
|
||||
seed,
|
||||
index,
|
||||
password,
|
||||
});
|
||||
results.push(result);
|
||||
const marker = result.ok ? 'OK' : 'FAIL';
|
||||
console.log(`[${marker}] #${index} ${result.email} -> ${result.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(Array.from({ length: concurrency }, () => worker()));
|
||||
|
||||
const success = results.filter(item => item.ok).length;
|
||||
const failed = results.length - success;
|
||||
|
||||
console.log('\nSummary');
|
||||
console.log(`Total: ${results.length}`);
|
||||
console.log(`Success: ${success}`);
|
||||
console.log(`Failed: ${failed}`);
|
||||
|
||||
if (failed > 0) {
|
||||
console.log('\nFailed entries (up to 20):');
|
||||
results
|
||||
.filter(item => !item.ok)
|
||||
.slice(0, 20)
|
||||
.forEach(item => {
|
||||
console.log(`- ${item.email} (${item.mobile}) [${item.rawStatus}] ${item.message}`);
|
||||
if (item.details) {
|
||||
console.log(` details: ${JSON.stringify(item.details)}`);
|
||||
}
|
||||
});
|
||||
process.exitCode = 1;
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(error => {
|
||||
console.error('Bulk user creation crashed:', error?.message || error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user