@marteye/studio-cli
Version:
CLI for MartEye Studio API
148 lines (144 loc) • 6.23 kB
JavaScript
;
var commander = require('commander');
var studio = require('../utils/studio.js');
var output = require('../utils/output.js');
function customersCommand() {
const customers = new commander.Command('customers')
.description('Customer operations');
customers
.command('list <market-id>')
.description('List customers (paginated)')
.option('--last-id <id>', 'Last customer ID for pagination')
.action(async (marketId, options, command) => {
const globalOptions = command.optsWithGlobals ? command.optsWithGlobals() : command.parent?.parent?.opts() || {};
try {
const studio$1 = studio.createStudioInstance(globalOptions);
const result = await studio$1.customers.list(marketId, options.lastId);
if (result.hasMore) {
console.log(`More results available. Use --last-id ${result.lastId} to fetch next page`);
}
output.output(result.customers, globalOptions);
}
catch (err) {
output.error(err.message, err.response?.data);
process.exit(1);
}
});
customers
.command('get <market-id> <customer-id>')
.description('Get customer details')
.action(async (marketId, customerId, _options, command) => {
const globalOptions = command.optsWithGlobals ? command.optsWithGlobals() : command.parent?.parent?.opts() || {};
try {
const studio$1 = studio.createStudioInstance(globalOptions);
const result = await studio$1.customers.get(marketId, customerId);
output.output(result, globalOptions);
}
catch (err) {
output.error(err.message, err.response?.data);
process.exit(1);
}
});
customers
.command('create <market-id>')
.description('Create new customer')
.requiredOption('--name <name>', 'Display name')
.option('--email <email>', 'Email address')
.option('--phone <phone>', 'Phone number')
.option('--account-prefix <prefix>', 'Account number prefix')
.option('--account-number <number>', 'Account number')
.option('--cph <cph>', 'CPH number')
.option('--farm-name <name>', 'Farm name')
.option('--address1 <address>', 'Address line 1')
.option('--address2 <address>', 'Address line 2')
.option('--city <city>', 'City')
.option('--province <province>', 'Province/State')
.option('--zip <zip>', 'ZIP/Postal code')
.option('--country <country>', 'Country code')
.action(async (marketId, options, command) => {
const globalOptions = command.optsWithGlobals ? command.optsWithGlobals() : command.parent?.parent?.opts() || {};
try {
const studio$1 = studio.createStudioInstance(globalOptions);
// Build customer data
const customerData = {
displayName: options.name,
address: {}
};
if (options.email)
customerData.email = options.email;
if (options.phone)
customerData.phone = options.phone;
if (options.accountPrefix)
customerData.accountNumberPrefix = options.accountPrefix;
if (options.accountNumber)
customerData.accountNumber = options.accountNumber;
if (options.cph)
customerData.cphNumber = options.cph;
if (options.farmName)
customerData.farmName = options.farmName;
// Address fields
if (options.address1)
customerData.address.address1 = options.address1;
if (options.address2)
customerData.address.address2 = options.address2;
if (options.city)
customerData.address.city = options.city;
if (options.province)
customerData.address.province = options.province;
if (options.zip)
customerData.address.zip = options.zip;
if (options.country)
customerData.address.country = options.country;
const result = await studio$1.customers.create(marketId, customerData);
output.success(`Customer ID: ${result.id}`);
output.output(result, globalOptions);
}
catch (err) {
output.error(err.message, err.response?.data);
process.exit(1);
}
});
customers
.command('find-by-account <market-id> <account-number>')
.description('Find customer by account number')
.action(async (marketId, accountNumber, _options, command) => {
const globalOptions = command.optsWithGlobals ? command.optsWithGlobals() : command.parent?.parent?.opts() || {};
try {
const studio$1 = studio.createStudioInstance(globalOptions);
const result = await studio$1.customers.getByAccountNumber(marketId, accountNumber);
if (result) {
output.output(result, globalOptions);
}
else {
process.exit(1);
}
}
catch (err) {
output.error(err.message, err.response?.data);
process.exit(1);
}
});
customers
.command('find-by-marteye <market-id> <marteye-uid>')
.description('Find customer by MartEye UID')
.action(async (marketId, marteyeUid, _options, command) => {
const globalOptions = command.optsWithGlobals ? command.optsWithGlobals() : command.parent?.parent?.opts() || {};
try {
const studio$1 = studio.createStudioInstance(globalOptions);
const result = await studio$1.customers.getByMartEyeUid(marketId, marteyeUid);
if (result) {
output.output(result, globalOptions);
}
else {
process.exit(1);
}
}
catch (err) {
output.error(err.message, err.response?.data);
process.exit(1);
}
});
return customers;
}
exports.customersCommand = customersCommand;
//# sourceMappingURL=customers.js.map