Skip to content

Commit 3d391d9

Browse files
committed
Implemented permanent delete user by params endpoint
1 parent 59849b3 commit 3d391d9

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,22 @@ client.users.archive({ id: '1234' }, callback);
148148
```
149149

150150
```node
151-
// Permanently delete a user user by id (https://developers.intercom.com/v2.0/reference#delete-users)
151+
// Permanently delete a user by id (https://developers.intercom.com/v2.0/reference#delete-users)
152152
const intercomUserId = '123'
153153
client.users.requestPermanentDeletion(intercomUserId, callback);
154154
```
155155

156+
```node
157+
// Permanently delete a user by id in params
158+
client.users.requestPermanentDeletionByParams({ id: '55b9eaf' }, callback);
159+
160+
// Permanently delete a user by user_id
161+
client.users.requestPermanentDeletionByParams({ user_id: 'foobar' }, callback);
162+
163+
// Permanently delete a user by email
164+
client.users.requestPermanentDeletionByParams({ email: '[email protected]' }, callback);
165+
```
166+
156167
## Leads
157168

158169
```node

lib/user.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,25 @@ export default class User {
4646
requestPermanentDeletion(intercom_user_id, f) {
4747
return this.client.post('/user_delete_requests', { intercom_user_id }, f);
4848
}
49+
requestPermanentDeletionByParams(params, f) {
50+
if (params.id) {
51+
return this.requestPermanentDeletion(params.id, f);
52+
}
53+
54+
return this.find(params)
55+
.then((res) => this.requestPermanentDeletion(res.body.id))
56+
.then((res) => {
57+
if (f) {
58+
return f(null, res);
59+
}
60+
61+
return res;
62+
}).catch((err) => {
63+
if (f) {
64+
return f(err);
65+
}
66+
67+
throw err;
68+
});
69+
}
4970
}

test/user.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,65 @@ describe('users', () => {
116116
done();
117117
});
118118
});
119+
it('should permanently delete users by Intercom user ID in params', (done) => {
120+
nock('https://api.intercom.io')
121+
.post('/user_delete_requests', { intercom_user_id: 'foo' })
122+
.reply(200, { id: 10 });
123+
const client = new Client('foo', 'bar').usePromises();
124+
client.users.requestPermanentDeletionByParams({id: 'foo'}).then((r) => {
125+
assert.equal(200, r.statusCode);
126+
assert.deepStrictEqual({ id: 10 }, r.body);
127+
done();
128+
});
129+
});
130+
it('should permanently delete users by user_id', (done) => {
131+
nock('https://api.intercom.io')
132+
.get('/users')
133+
.query({ user_id: 'foo' })
134+
.reply(200, { id: 10 })
135+
.post('/user_delete_requests', { intercom_user_id: 10 })
136+
.reply(200, { id: 10 });
137+
const client = new Client('foo', 'bar').usePromises();
138+
client.users.requestPermanentDeletionByParams({user_id: 'foo'}).then((r) => {
139+
assert.equal(200, r.statusCode);
140+
assert.deepStrictEqual({ id: 10 }, r.body);
141+
done();
142+
});
143+
});
144+
it('should permanently delete users by email', (done) => {
145+
nock('https://api.intercom.io')
146+
.get('/users')
147+
.query({ email: 'foo' })
148+
.reply(200, { id: 10 })
149+
.post('/user_delete_requests', { intercom_user_id: 10 })
150+
.reply(200, { id: 10 });
151+
const client = new Client('foo', 'bar').usePromises();
152+
client.users.requestPermanentDeletionByParams({email: 'foo'}).then((r) => {
153+
assert.equal(200, r.statusCode);
154+
assert.deepStrictEqual({ id: 10 }, r.body);
155+
done();
156+
});
157+
});
158+
it('should callback with errors if calls fail', (done) => {
159+
nock('https://api.intercom.io')
160+
.get('/users')
161+
.query({ email: 'foo' })
162+
.reply(200, {type: 'error.list'});
163+
const client = new Client('foo', 'bar');
164+
client.users.requestPermanentDeletionByParams({email: 'foo'}, (err) => {
165+
assert.equal(true, err instanceof Error);
166+
done();
167+
});
168+
});
169+
it('should reject promises if calls fail', (done) => {
170+
nock('https://api.intercom.io')
171+
.get('/users')
172+
.query({ email: 'foo' })
173+
.reply(200, {type: 'error.list'});
174+
const client = new Client('foo', 'bar').usePromises();
175+
client.users.requestPermanentDeletionByParams({email: 'foo'}).catch(err => {
176+
assert.equal(true, err instanceof Error);
177+
done();
178+
});
179+
});
119180
});

0 commit comments

Comments
 (0)