Skip to content

Commit

Permalink
implement create product
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomontero committed Feb 4, 2025
1 parent af472e9 commit 3a426f9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
17 changes: 14 additions & 3 deletions src/cmd/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,22 @@ module.exports = class ParticleApi {
);
}

createProduct({ name, type, org }) {
createProduct({ name, description = '', platformId, orgSlug, locationOptIn = false } = {}) {
return this._wrap(
this.api.post({
uri: `/v1${org ? `/orgs/${org}` : ''}/products`,
form: { name, type },
uri: `/v1${orgSlug ? `/orgs/${orgSlug}` : '/user'}/products`,
data: {
product: {
name,
description,
platform_id: platformId,
settings: {
location: {
opt_in: locationOptIn
}
},
}
},
auth: this.accessToken
})
);
Expand Down
30 changes: 21 additions & 9 deletions src/cmd/setup-tachyon.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const FlashCommand = require('./flash');
const CloudCommand = require('./cloud');
const { sha512crypt } = require('sha512crypt-node');
const DownloadManager = require('../lib/download-manager');
const { platformForId } = require('../lib/platform');
const { platformForId, PLATFORMS } = require('../lib/platform');
const path = require('path');

module.exports = class SetupTachyonCommands extends CLICommandBase {
Expand Down Expand Up @@ -263,7 +263,7 @@ Welcome to the Particle Tachyon setup! This interactive command:
let productId = await this._getProduct(orgName, orgSlug);

if (!productId) {
productId = await this._createProduct(orgSlug);
productId = await this._createProduct({ orgSlug });
}
return productId;
}
Expand Down Expand Up @@ -295,6 +295,7 @@ Welcome to the Particle Tachyon setup! This interactive command:

async _getProduct(orgName, orgSlug) {
const productsResp = await this.api.getProducts(orgSlug);
let newProductName = 'Create a new product';

//if orgSlug is not null, filter for this org from product.organization_id
//if orgSlug is null, filter for an empty field in product.organization_id
Expand All @@ -311,9 +312,9 @@ Welcome to the Particle Tachyon setup! This interactive command:
return null; // No products available
}

const selectedProductName = await this._promptForProduct(products.map(product => product.name));
const selectedProductName = await this._promptForProduct([newProductName, ...products.map(product => product.name)]);

const selectedProduct = products.find(p => p.name === selectedProductName);
const selectedProduct = selectedProductName !== newProductName ? (selectedProductName !== products.find(p => p.name === selectedProductName)) : null;

return selectedProduct?.id || null;
}
Expand All @@ -331,11 +332,22 @@ Welcome to the Particle Tachyon setup! This interactive command:
return product;
}

async _createProduct() {
// It appears that CLI code base does not have a method to create a product readily available
// TODO: Discuss with the team to add a method to create a product
// For now though, we will return an error
throw new Error('No products available. Create a product in the console and return to continue.');
async _createProduct({ orgSlug }) {
const platformId = PLATFORMS.find(p => p.name === 'tachyon').id;
const question = [{
type: 'input',
name: 'productName',
message: 'Enter the product name:',
}, {
type: 'confirm',
name: 'locationOptIn',
message: 'Would you like to opt in to location services? (y/n):',
default: true
}];
const { productName, locationOptIn } = await this.ui.prompt(question);
const { product } = await this.api.createProduct({ name: productName, platformId, orgSlug, locationOptIn });
this.ui.write(`Product ${product.name} created successfully!`);
return product?.id;
}

async _userConfiguration() {
Expand Down

0 comments on commit 3a426f9

Please sign in to comment.