Skip to content

Commit ddfa94d

Browse files
authored
Update the company.update method (#375)
* Update the company.update method Makes it clearer that this endpoint uses the Intercom ID for a company, and not the companyID that is optionally provided during creation closes #356 * Add a createOrUpdate company method This method actually creates or updates, so add a more appropriately named method and mark the old one as deprecated/
1 parent 168c255 commit ddfa94d

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

lib/company.ts

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export default class Company {
2121
this.client = client;
2222
this.scroll = new Scroll<Company>(this.client, this.baseUrl);
2323
}
24+
/**
25+
* @deprecated Use `client.companies.createOrUpdate()` instead.
26+
*/
2427
create({
2528
createdAt,
2629
companyId,
@@ -31,6 +34,34 @@ export default class Company {
3134
website,
3235
industry,
3336
customAttributes,
37+
}: CreateCompanyData) {
38+
return this.createOrUpdate({
39+
createdAt,
40+
companyId,
41+
name,
42+
monthlySpend,
43+
plan,
44+
size,
45+
website,
46+
industry,
47+
customAttributes,
48+
});
49+
}
50+
/**
51+
* Create or update a company by its `companyId`.
52+
*
53+
* Companies are looked up via the `companyId` field. If a company with the given `companyId` does not exist, it will be created. If a company with the given `companyId` does exist, it will be updated.
54+
**/
55+
createOrUpdate({
56+
createdAt,
57+
companyId,
58+
name,
59+
monthlySpend,
60+
plan,
61+
size,
62+
website,
63+
industry,
64+
customAttributes,
3465
}: CreateCompanyData) {
3566
const data = {
3667
remote_created_at: createdAt,
@@ -49,9 +80,23 @@ export default class Company {
4980
data,
5081
});
5182
}
83+
84+
/**
85+
* Update a single company by its `id`.
86+
* @param id - The `id` field is required for updating a company. This is distinct from the `companyId` field on the Company object , which is an identifier for the company in your database.
87+
* @param createdAt - The time the company was created by you.
88+
* @param name - The name of the company.
89+
* @param monthlySpend - The amount the company spends on your product each month. How much revenue the company generates for your business.
90+
* Note that this will truncate floats. i.e. it only allow for whole integers, 155.98 will be truncated to 155. Note that this has an upper limit of 2**31-1 or 2147483647..
91+
* @param plan - The name of the plan you have associated with the company.
92+
* @param size - The number of employees the company has.
93+
* @param website -The URL for this company's website. Please note that the value specified here is not validated. Accepts any string.
94+
* @param industry - The industry the company operates in.
95+
* @param customAttributes - A hash of key/value pairs containing any other data about the company you want Intercom to store.
96+
*/
5297
update({
98+
id,
5399
createdAt,
54-
companyId,
55100
name,
56101
monthlySpend,
57102
plan,
@@ -72,7 +117,7 @@ export default class Company {
72117
};
73118

74119
return this.client.put<CompanyObject>({
75-
url: `/${this.baseUrl}/${companyId}`,
120+
url: `/${this.baseUrl}/${id}`,
76121
data,
77122
});
78123
}
@@ -160,7 +205,9 @@ interface CreateCompanyData {
160205
customAttributes?: JavascriptObject;
161206
}
162207
//
163-
type UpdateCompanyData = CreateCompanyData;
208+
interface UpdateCompanyData extends Omit<CreateCompanyData, 'companyId'> {
209+
id: string;
210+
}
164211
//
165212
interface FindCompanyData {
166213
companyId?: string;

test/integration/companies.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,27 @@ describe('Companies', () => {
3434

3535
assert.notEqual(response, undefined);
3636
});
37+
it('createOrUpdate', async () => {
38+
const response = await client.companies.createOrUpdate({
39+
createdAt: dateToUnixTimestamp(new Date()),
40+
companyId: '46029',
41+
name: 'BestCompanyInc.',
42+
monthlySpend: 9001,
43+
plan: '1. Get pizzaid',
44+
size: 62049,
45+
website: 'http://the-best.one',
46+
industry: 'The Best One',
47+
customAttributes: {},
48+
});
49+
50+
createdCompany = response;
51+
52+
assert.notEqual(response, undefined);
53+
});
3754
it('update', async () => {
3855
const response = await client.companies.update({
3956
createdAt: dateToUnixTimestamp(new Date()),
40-
companyId: createdCompany.id,
57+
id: createdCompany.id,
4158
name: 'BestCompanyInc',
4259
monthlySpend: 9001,
4360
plan: '1. Get pizzaid',

test/unit/company.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('companies', () => {
6464
assert.deepStrictEqual({}, response);
6565
});
6666
it('should be updated', async () => {
67-
const company_id = '46029';
67+
const id = '625e90fc55ab113b6d92175f';
6868
const requestBody = {
6969
remote_created_at: dateToUnixTimestamp(new Date()),
7070
name: 'BestCompanyInc.',
@@ -77,12 +77,12 @@ describe('companies', () => {
7777
};
7878

7979
nock('https://api.intercom.io')
80-
.put(`/companies/${company_id}`, requestBody)
80+
.put(`/companies/${id}`, requestBody)
8181
.reply(200, {});
8282

8383
const response = await client.companies.update({
8484
createdAt: requestBody.remote_created_at,
85-
companyId: company_id,
85+
id: id,
8686
name: requestBody.name,
8787
monthlySpend: requestBody.monthly_spend,
8888
plan: requestBody.plan,

0 commit comments

Comments
 (0)