# User Model

## Request

- When requesting `graphql.getUserDetails` a User instance is returned.

```javascript
try {
	const user = await graphql.getUserDetails(sessionId);
} catch (error) {
	throw error;
}
```

### Required Parameters

| Parameters   | Required | Type   | Description        | Example                             |
| ----------   | -------- | ------ | ------------------ | ----------------------------------- |
| sessionId | Yes      | String | This is the FTSession_s token which can be retrieved from [authorisation.login](../services/AUTHORISATION.md#login) | zyoRrJB7YEJC...|

**Response**: Returns an User instance: [UserClassResponse](../../src/models/user.ts).

## Table of Contents

* [Initiate User Class](#Initiate-User-Class)
* [Properties of the user response](#Properties-of-the-user-response)
* [Prototypes](#Prototypes)
* [Membership User Response](#Membership-User-Response)
* [Restricted Details](#Restricted-Details)

### Initiate User Class

```javascript
	const user = new User(userDetailsResponse, isProfileRestricted);
```

| Parameters          | Required | Type    | Description                               | Example      |
| ------------------- | -------- | ------- | ----------------------------------------- | ------------ |
| userDetailsResponse | Yes      | Object  | Membership graphql userBySession response | see [Membership User Response](#Membership-User-Response)  |
| isProfileRestricted | Yes      | Boolean | Is the FTSession_s token old?             | false        |

### Properties of the user response

| Properties     | Type   | Description           | Example                                   |
| -------------- | ------ | --------------------- | -------------------------------------- |
| details        | Object | Membership graphql userBySession response  | see [Membership User Response](#Membership-User-Response) |
| id             | String | User's ID                      | xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx|
| email          | String | User's email                   | email@example.com                    |
| secondaryEmail          | String | User's secondary email                   | email@email.com                    |
| title          | String | User's title                   | Mr                                  |
| firstName      | String | User's first name              | John                                |
| lastName       | String | User's last name               | Doe                                 |
| creationDate   | String | User's registration timestamp  | 2022-04-28T14:37:21.766Z            |
| demographics   | Object | User's demographics            | see demographics in [Membership User Response](#Membership-User-Response) |
| industry       | String | The industry the user works in | IT (IT/computing) |
| position       | String | The user's position at work    | PR (Professional) |
| responsibility | String | User's responsibility at work  | ITC (IT/computing) |
| paymentMethod  | String | User's choice of payment       | CREDITCARD |
| billingCurrencyCode | String | Three letter ISO-4217 currency code of user's billing country | GBP |
| isSSO          | Boolean | The user has signed up with Single Sign On | true |
| isB2C          | Boolean | The user is a B2C customer | true |
| isB2B          | Boolean | The user is a B2B customer | true |
| isStaff        | Boolean | The user is a member of FT staff | true |
| isRegistered   | Boolean | User has an FT profile but is not a current subscriber | true |
| isTrialist     | Boolean | User is a current trialist | true |
| isPremium      | Boolean | User has a subscription giving premium access | true |
| isEpaper      | Boolean | User has an E-Paper subscription | true |
| isProfileRestricted | Boolean | The user's profile is restricted and therefore some user data cannot be retrieved. Session Id may be old| false |
| sessionId      | String | This is the FTSession_s token which can be retrieved from [authorisation.login](../services/AUTHORISATION.md#login) | zyoRrJB7YEJC...|
| password       | String | The user's password  | fakePassword |
| jobTitle       | String | The user's job title | Doctor |
| graduationDate | String | The graduation date of user who is a student | 'MMYYYY' |
| organisationName | String | The user's company name | fakeCompany
| isDeferredBilling | Boolean | if the user is a isDeferredBilling or not | false
| licences       | Array  | All of the licences a user holds a seat on | [see licence model](../../src/models/licence.ts) |
| externalUserId | String | Representation of the user ID in the client' systems | 'any-string-really' |
| accountType | String | What type of account user holds. | B2C, B2B, [see more here](https://financialtimes.atlassian.net/wiki/spaces/SP/pages/8247705661/Zuora+Account+Types) |

### Prototypes

* [Get renewalDate](#Get-renewalDate)
* [Get primaryTelephone](#Get-primaryTelephone)
* [Set primaryTelephone](#Set-primaryTelephone)
* [homeAddress](#homeAddress)
* [Get country](#Get-country)
* [Set country](#Set-country)
* [Get postcode](#Get-postcode)
* [Set postcode](#Set-postcode)
* [formatForSubscription](#formatForSubscription)
* [activeSubscription](#activeSubscription)
* [activeSubscriptionName](#activeSubscriptionName)
* [allSubscriptions](#allSubscriptions)
* [Get renewalDate](#Get-renewalDate)

#### Get primaryTelephone

```javascript
const primaryTelephone = membershipSDK.primaryTelephone();
```

- Returns the user's primary, telephone number.
- This is [restricted](#Restricted-Details) information.

**Response**: `12345678`

#### Set primaryTelephone

```javascript
membershipSDK.primaryTelephone = number;
```

- Returns the user's newly set primary, telephone number.
- This is [restricted](#Restricted-Details) information.

| Parameters  | Required | Type   | Description                            | Example  |
| ----------  | -------- | ------ | -------------------------------------- | -------- |
| number      | Yes      | String | User's new, primary, telephone number  | 12345678 |

**Response**: `12345678`

#### homeAddress

```javascript
const homeAddress = membershipSDK.homeAddress();
```

- Returns user's country and postcode.
- This is [restricted](#Restricted-Details) information.

**Response**:
```javascript
		{
			country: 'GBR',
			postcode: 'SE1 9HL'
		}
```

#### Get country

```javascript
const country = membershipSDK.country();
```
- Returns the user's three letter ISO 3166-1 country code.
- This is [restricted](#Restricted-Details) information.

**Response**: `GBR`

#### Set country

```javascript
membershipSDK.country = countryCode;
```

- Returns the user's newly set three letter ISO 3166-1 country code.
- This is [restricted](#Restricted-Details) information.

| Parameters  | Required | Type   | Description                            | Example  |
| ----------  | -------- | ------ | -------------------------------------- | -------- |
| countryCode | Yes      | String | User's new, country code               | USA |

**Response**: `USA`

#### Get postcode

```javascript
const postcode = membershipSDK.postcode();
```

- Returns the user's newly set postcode.
- This is [restricted](#Restricted-Details) information.

**Response**: `SE19HL`

#### Set postcode

```javascript
membershipSDK.postcode = postcode;
```
- Returns the user's newly set postcode.
- This is [restricted](#Restricted-Details) information.

| Parameters  | Required | Type   | Description        | Example  |
| ----------  | -------- | ------ | ------------------ | -------- |
| postcode    | Yes      | String | User's postcode    | SE19HL   |

**Response**: `SE19HL`

#### formatForSubscription

```javascript
const formattedSubscription = membershipSDK.formatForSubscription();
```

- Returns an object with the user details needed for to creating a user's subscription.
- This contains [restricted](#Restricted-Details) information.

**Response**:
```javascript
    {
            id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
            email: 'email@example.com',
            secondaryEmail:'email@email.com',
            firstName: 'John',
            lastName: 'Doe',
            nonLogin: false,
            primaryTelephone: 123456789,
            homeAddress: {
                country: 'GBR',
                postcode: 'SE19HL'
            },
            demographics: {
                industry: {
                    code: 'IT'
                },
                position: {
                    code: 'PR'
                },
                responsibility: {
                    code: 'ITC'
                }
            }
    }

```

#### belongsToLicence

```javascript
const belongsToLicence = membershipSDK.belongsToLicence(licenceId);
```

- Returns true if user's licence array contains the ID of the licence they are trying to access.

| Parameters  | Required | Type   | Description        | Example  |
| ----------  | -------- | ------ | ------------------ | -------- |
| licenceId   | Yes      | String | ID of the licence a user is trying to access  | xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |

**Note** B2C users join individual licences.

#### activeSubscription

```javascript
const activeSubscription = membershipSDK.activeSubscription;
```
- The user's active subscription details.
- Returns instance of [UserSubscription](../../src/models/user-subscription.ts).
- See docs [here](./USER-SUBSCRIPTION.md).

#### activeSubscriptionName

```javascript
const activeSubscription = membershipSDK.activeSubscriptionName;
```
- Attempts to resolve the user's active subscription to a friendly name.

#### allSubscriptions

```javascript
const allSubscriptions = membershipSDK.allSubscriptions;
```
- All user's subscription details (active and inactive).
- Returns an array of [UserSubscription](../../src/models/user-subscription.ts) instances.
- See docs [here](./USER-SUBSCRIPTION.md)

#### Get renewalDate

```javascript
const renewalDate = membershipSDK.renewalDate();
```

- Returns the renewal date for the user's currently active subscription.
- Returns `null` if the user has no active subscriptions.

**Response**: `2012-16-02T00:00:00.000Z` || `null`

### Membership User Response

```javascript
{
    access: {
        isB2b: false,
        isB2c: true
    },
    id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    profiles: {
        basic: {
            email: 'email@example.com',
            secondaryEmail:'email@email.com',
            firstName: 'John',
            lastName: 'Doe',
            title: null,
            creationDate: '2022-04-28T14:37:21.766Z',
            country: 'GBR'
        },
        demographics: {
            industry: {
                code: 'IT'
            },
            position: {
                code: 'PR'
            },
            responsibility: {
                code: 'ITC'
            }
        },
        restricted: {
            homeAddress: {
                country: 'GBR',
                postcode: 'SE19HL'
            },
            primaryTelephone: '123456789'
        }
    },
    session: {
        sessionId: 'zyoRrJB7YEJC...'
    },
    subscriber: {
        billingAccount: {
            currencyCode: 'GBP',
            isDeferredBilling: false,
            isExternal: false,
            paymentMethod: {
                type: 'CREDITCARD'
            }
        },
        status: {
            currentSubscriber: true
        }
    }
}
```

### Restricted Details

- When a session Id is old, some details that cannot be returned as they are restricted.
- An error will be thrown when trying to access those details.

**Error Message**: `Insufficient authorization to see restricted user profile`

**Restricted Details**:

* homeAddress
* country
* postcode
* primaryTelephone
