Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

fix(close): create opportunities with the selected status ID

Rudy Celekli8b3b9135def0
diff --git a/tests/clis/close-opportunity-status.test.cjs b/tests/clis/close-opportunity-status.test.cjs
new file mode 100644
index 0000000..eb8f44f
--- /dev/null
+++ b/tests/clis/close-opportunity-status.test.cjs
@@ -0,0 +1,57 @@
+const { test } = require('node:test')
+const assert = require('node:assert/strict')
+const { spawnSync } = require('node:child_process')
+const path = require('node:path')
+const cli = path.resolve(__dirname, '../../tools/clis/close.js')
+function run(args, oracle = '') {
+ const code = `global.fetch = async (url, options) => {
+ const assert = require('node:assert/strict');
+ const parsed = new URL(url);
+ const body = options.body ? JSON.parse(options.body) : null;
+ ${oracle}
+ return new Response(JSON.stringify({accepted:true, url, body}), {status:200});
+ }; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath, ['-e', code], {
+ encoding: 'utf8', timeout: 10000,
+ env: { ...process.env, CLOSE_API_KEY: 'fixture-token' },
+ })
+}
+function result(r) { assert.equal(r.status, 0, r.stderr); return JSON.parse(r.stdout) }
+
+for (const flag of ['--status-id','--status']) {
+ test(`${flag} selects the actual opportunity status ID`, () => {
+ assert.equal(result(run(['opportunities','create','--lead-id','lead_123','--value','50000',flag,'stat_won'], `
+ assert.equal(options.method, 'POST');
+ assert.equal(parsed.pathname, '/api/v1/opportunity/');
+ assert.deepEqual(body, {lead_id:'lead_123',value:50000,status_id:'stat_won'});
+ `)).accepted, true)
+ })
+}
+for (const type of ['active','won','lost']) {
+ test(`ambiguous ${type} status type cannot fall back to a default opportunity stage`, () => {
+ const p = result(run(['opportunities','create','--lead-id','lead_123','--value','50000','--status',type], "throw new Error('unexpected opportunity creation')"))
+ assert.match(p.error, /--status-id/)
+ })
+}
+test('explicit status ID takes precedence over the legacy alias', () => {
+ result(run(['opportunities','create','--lead-id','lead_123','--value','50000','--status','won','--status-id','stat_123'], `
+ assert.equal(body.status_id, 'stat_123');
+ assert.equal('status_type' in body, false);
+ `))
+})
+test('unspecified status still uses the organization default', () => {
+ result(run(['opportunities','create','--lead-id','lead_123','--value','0'], `
+ assert.deepEqual(body, {lead_id:'lead_123',value:0});
+ `))
+})
+test('preview shows the selected status ID with credentials redacted', () => {
+ const p = result(run(['opportunities','create','--lead-id','lead_123','--value','50000','--status-id','stat_123','--dry-run'], "throw new Error('unexpected fetch')"))
+ assert.equal(p.body.status_id, 'stat_123')
+ assert.equal(p.headers.Authorization, 'Basic ***')
+})
+test('opportunity list retains the provider-supported status filter', () => {
+ result(run(['opportunities','list','--status','won'], `
+ assert.equal(parsed.searchParams.get('status'), 'won');
+ assert.equal(options.method, 'GET');
+ `))
+})
diff --git a/tools/clis/close.js b/tools/clis/close.js
index aef2d84..6512d68 100755
--- a/tools/clis/close.js
+++ b/tools/clis/close.js
@@ -144,7 +144,12 @@ async function main() {
if (!leadId) { result = { error: '--lead-id required' }; break }
if (!value) { result = { error: '--value required (in cents)' }; break }
const body = { lead_id: leadId, value: parseInt(value) }
- if (args.status) body.status_type = args.status
+ const statusId = args['status-id'] || args.status
+ if (['active', 'won', 'lost'].includes(statusId)) {
+ result = { error: '--status-id must be an opportunity status ID (stat_...), not a status type. Retrieve IDs from GET /status/opportunity/.' }
+ break
+ }
+ if (statusId) body.status_id = statusId
result = await api('POST', '/opportunity/', body)
break
}
@@ -211,7 +216,7 @@ async function main() {
opportunities: {
list: 'opportunities list [--status <status>]',
get: 'opportunities get --id <id>',
- create: 'opportunities create --lead-id <id> --value <cents> [--status <status>]',
+ create: 'opportunities create --lead-id <id> --value <cents> [--status-id <stat_id>] [--status <stat_id>]',
},
activities: {
list: 'activities list [--lead-id <id>] [--type <type>]',
diff --git a/tools/integrations/close.md b/tools/integrations/close.md
index b32c386..c950b60 100644
--- a/tools/integrations/close.md
+++ b/tools/integrations/close.md
@@ -76,10 +76,19 @@ POST https://api.close.com/api/v1/opportunity/
{
"lead_id": "lead_xxx",
"value": 50000,
- "status_type": "active"
+ "status_id": "stat_xxx"
}
```
+To select a stage when creating an opportunity, retrieve an ID from
+`GET https://api.close.com/api/v1/status/opportunity/` and pass
+`--status-id stat_xxx` to the CLI. `--status` is accepted as an ID alias.
+The `active`, `won`, and `lost` types describe statuses; they cannot select a
+specific stage when creating an opportunity. Omitting both flags uses the
+organization default status. See the official
+[create opportunity](https://developer.close.com/api/resources/opportunities/create)
+and [list opportunity statuses](https://developer.close.com/api/resources/opportunity-statuses/list) contracts.
+
### List Activities
```bash