Skip to content

Commit

Permalink
Feature/delete user data (#188)
Browse files Browse the repository at this point in the history
Feature/delete user data
  • Loading branch information
germanattanasio committed Jun 17, 2019
2 parents 0715cd5 + 7123772 commit ffd44d8
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 15 deletions.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ This middleware plugin for [Botkit](http://howdy.ai/botkit) allows developers to
+ [Intent matching](#intent-matching)
- [`before` and `after`](#before-and-after)
- [Dynamic workspace](#dynamic-workspace)

* [Information security](#information-security)
+ [Labeling user data](#labeling-user-data)
+ [Deleting user data](#deleting-user-data)
</details>

## Middleware Overview
Expand All @@ -45,6 +47,7 @@ This middleware plugin for [Botkit](http://howdy.ai/botkit) allows developers to
* `readContext`: used in [implementing event handlers](#implementing-event-handlers).
* `before`: [pre-process](#before-and-after) requests before sending to Watson Assistant (formerly Conversation).
* `after`: [post-process](#before-and-after) responses before forwarding them to Botkit.
* `deleteUserData`: [deletes](#information-security) all data associated with a specified customer ID.


## Installation
Expand Down Expand Up @@ -371,6 +374,36 @@ async handleHelloEvent = (bot, message) => {
controller.on('hello', handleHelloEvent);
```

## Information security

It may be necessary to be to able delete message logs associated with particular customer in order to comply with
[GDPR or HIPPA regulations](https://cloud.ibm.com/docs/services/assistant?topic=assistant-information-security#information-security)

### Labeling User Data

Messages can be labeled with customer id by adding `x-watson-metadata` header to request in `before` hook:

```js
watsonMiddleware.before = async (message, payload) => {
// it is up to you to implement calculateCustomerId function
customerId = calculateCustomerId(payload.context);
payload.headers['X-Watson-Metadata'] = 'customer_id=' + customerId;

return payload;
};
```

### Deleting User Data

```js
try {
await watsonMiddleware.deleteUserData(customerId);
//Customer data was deleted successfully
} catch (e) {
//Failed to delete
}
```

## License

This library is licensed under Apache 2.0. Full license text is available in [LICENSE](LICENSE).
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ export declare class WatsonMiddleware {
updateContext(user: string, context: Context): Promise<{
context: Context;
}>;
deleteUserData(customerId: string): Promise<void>;
}
23 changes: 18 additions & 5 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 24 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export class WatsonMiddleware {
if (config.minimum_confidence) {
this.minimumConfidence = config.minimum_confidence;
}

debug(
'Creating Assistant object with parameters: ' +
JSON.stringify(this.config, null, 2),
);
this.conversation = new AssistantV1(this.config);
}

public hear(patterns: string[], message: Botkit.BotkitMessage): boolean {
Expand Down Expand Up @@ -105,14 +111,6 @@ export class WatsonMiddleware {
message: Botkit.BotkitMessage,
contextDelta: ContextDelta,
): Promise<void> {
if (!this.conversation) {
debug(
'Creating Assistant object with parameters: ' +
JSON.stringify(this.config, null, 2),
);
this.conversation = new AssistantV1(this.config);
}

if (
(!message.text && message.type !== 'welcome') ||
this.ignoreType.indexOf(message.type) !== -1 ||
Expand Down Expand Up @@ -219,4 +217,22 @@ export class WatsonMiddleware {
context: context,
});
}

public async deleteUserData(customerId: string) {
const params = {
customer_id: customerId,
return_response: true,
};
try {
const response = await this.conversation.deleteUserData(params);
debug('deleteUserData response', response);
} catch (err) {
throw new Error(
'Failed to delete user data, response code: ' +
err.code +
', message: ' +
err.message,
);
}
}
}
61 changes: 61 additions & 0 deletions test/middleware-delete-user-data.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright 2016-2019 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { WatsonMiddleware } from '../lib/index';
import nock = require('nock');

//Watson Assistant params
const service = {
username: 'batman',
password: 'bruce-wayne',
url: 'http://ibm.com:80',
version: '2018-07-10',
};

const workspaceId = 'zyxwv-54321';

const customerId = 'XXXXXX';

const middleware = new WatsonMiddleware({
...service,
workspace_id: workspaceId,
});

beforeEach(function() {
nock.disableNetConnect();
});

afterEach(function() {
nock.cleanAll();
});

test('makes delete request', async () => {
nock(service.url)
.delete(`/v1/user_data?version=2018-07-10&customer_id=${customerId}`)
.reply(202, '');

await middleware.deleteUserData(customerId);
});

test('throws error on unexpected response code', async () => {
nock(service.url)
.delete(`/v1/user_data?version=2018-07-10&customer_id=${customerId}`)
.reply(404, '');

return await expect(middleware.deleteUserData(customerId)).rejects.toThrow(
'Failed to delete user data, response code: 404, message: null',
);
});

0 comments on commit ffd44d8

Please sign in to comment.