# CustomerSSO Service

The CustomerSSO service class is used to notify the Membership Customer SSO when a new user joins a licence of a SSO-enabled B2B client.

## Table of Contents
* [Usage](#usage)
* [Configuration](#configuration)
    * [API keys](#api-keys)
    * [How to obtain a key](#how-to-obtain-a-key)
* [Methods](#methods)
    * [updateUserData](#updateuserdata)
    * [linkUserAndStudentBeans](#linkuserandstudentbeans)

## Usage

```javascript
const { CustomerSSOService, ProductionConfiguration } = require('n-membership-sdk');
const config = new ProductionConfiguration({
    customerSsoApiKey: 'a-secret-key'
})
const service = new CustomerSSOService(config);
```

## Configuration

### API keys

API key used to access Membership's Customer SSO service over REST.

* customerSsoApiKey: for user data updating service
* studentBeansApiKey: for students beans linking service

### How to obtain a key

Do one of the following:

1. Check in Doppler for your app to see if a Customer SSO API key already exists.
2. Ask in the #membership-support Slack channel for an API key for the Customer SSO service.


## Methods

### updateUserData

Updates a ft.com user profile with the given SAML user ID.

```javascript
const result = CustomerSSoService.updateUserData(userId, samlUserId, email);
```

| Parameters  | Required | Type   | Example | Description   |
| ----------- | -------- | -------| --------| ------------- |
| userId | yes | String | xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | The ID of the user's FT.com profile |
| samlUserId      | yes       | String | zyoRrJB7YEJC...  | The user's SAML ID |
| email      | no       | String | email@addr.com  | E-mail address for users that belong to ssoByEmail licences |

Returns `true` if the API call was successful, otherwise throws an error.

### linkUserAndStudentBeans

Link a user to a a specific sbid_number, which is a unique id for Student Beans subscription in order to avoid abuses with this benefit. The expected behaviour for the services should be as follow:

Responses:
- 200 - if everything is OK storing the link between user and sbid
- 403 - if there is an authentication error e.g. no API key provided
- 409 - if there is a duplicate sbid_number already present in the DB - this means there is already a subscription for this sbid
- 5xx - if there are server-side errors

```javascript
const result = CustomerSSoService.linkUserAndStudentBeans(userId, sbidNumber);
```

| Parameters  | Required | Type   | Example | Description   |
| ----------- | -------- | -------| --------| ------------- |
| userId | yes | String | xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | The ID of the user's FT.com profile |
| sbidNumber      | yes       | String | zyoRrJB7YEJC...  | A unique id generated with Student Beans subscriptions |

Returns `true` if the API call was successful, otherwise throws an error. 