# Licence Model

## Request

- When requesting `graphql.getLicence()` a Licence instance is returned.

```javascript
try {
	const licence = await graphql.getLicence(id);
} catch (error) {
	throw error;
}
```

### Required Parameters

| Parameters  | Required | Type   | Description           | Example |
| ----------  | -------- | -------| ----------------------| ------------- |
| id          | Yes      | String | access licence Id to get licence details | xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |

**Response**: Returns an [Licence instance](../../src/models/licence.ts).

## Table of Contents

* [Initiate Licence Class](#initiate-licence-class)
* [Properties of the licence response](#properties-of-the-licence-response)
* [Prototypes](#prototypes)
* [Membership Licence Response](#membership-licence-response)
* [Membership rel Attribute Types](#membership-rel-attribute-types)

### Initiate Licence Class

```javascript
 const licenceInstance = new Licence(licenceData);
```
| Parameters  | Required | Type   | Description                              | Example |
| ----------  | -------- | ------ | ---------------------------------------- | --------|
| licenceData | Yes      | Object | Membership graphql licence data response | see [Membership Licence Response](#Membership-Licence-Response) |

### Properties of the Licence Response

| Properties  | Required | Type   | Description           | Example                                   |
| ----------  | ------ | --------------------- | ----------------------------------------- | ---|
| id             | Yes | String | ID of the licence                        | 1d2ca849... |
| allocateSeats  | Yes | Number | Number of seats already taken            | 83          |
| seatLimit      | Yes | Number | Number of seats available on the licence | 84          |
| status         | Yes | String | Validity of licence                      | active      |
| isB2c         | Yes | Boolean | Is this licence a B2c licence                     | true      |
| isB2b         | Yes | Boolean | Is this licence a B2b licence                     | false      |
| isSso         | Yes | Boolean | Is this licence a SSO licence                     | false      |
| types          | Yes | Array  | All the licence types                    | [ 'trial' ] |
| issueReason    | No  | String | Issue Reason                             | 'Subscribed with Google' |
| acquisitionContext | Yes | Array  | All acquisitionContext instances for that licence | see [Acquisition Context Model Response](ACQUISITION-CONTEXT.md#AcquisitionContext-Model-Response) |
| linkedContractId | No | String | The ID of the active contract linked to the licence, if available |  FT-xxxxxx |
| links | No | Array | List of related artifacts (e.g. the contract associated with the licence) | { rel: "contract", href: "https://financialtimes.my.salesforce.com/xxxxxxxxxxxxxxx, id: "FT-xxxxxx" } |
| products | Yes | Array | All the products associated with the licence | [{ 'name': 'FT.com Premium', 'code': 'P2' }] |
| administrator | Yes | Array | All the administrators associated with the licence | [{ userId: 'xxxxxxxx-xxxx-xxxx-xxxxx', licenceId: '1d2ca849...'} ] |

The membership response is mapped to the model [here](../../src/mappers/licence.ts).

### Prototypes

* [isValid](#isValid)
* [isManagedTrial](#isManagedTrial)
* [isProspectTrial](#isProspectTrial)
* [isCompanyLicence](#isCompanyLicence)
* [isContractLicence](#isContractLicence)
* [isBundleLicence](#isBundleLicence)
* [isComplimentaryLicence](#isComplimentaryLicence)
* [isBusinessDevelopmentLicence](#isBusinessDevelopmentLicence)
* [isProspectLicence](#isProspectLicence)
* [isSubscriptionLicence](#isSubscriptionLicence)
* [isB2cPartnershipLicence](#isB2cPartnershipLicence)
* [isLongRoomLicence](#isLongRoomLicence)
* [isDeveloperPortalLicence](#isDeveloperPortalLicence)
* [isSubscribedWithGoogle](#isSubscribedWithGoogle)

#### isValid

```javascript
const isLicenceValid = licenceInstance.isValid();
```

- Returns true if licence can still be active.
```
	Parameters: none
	Response: boolean
```

#### isManagedTrial

```javascript
const isManagedTrial = licenceInstance.isManagedTrial();
```

- Returns true if licence type is a trial.
```
	Parameters: none
	Response: boolean
```

#### isProspectTrial

```javascript
const isProspectTrial = licenceInstance.isProspectTrial();
```

- Returns true if licence type is a b2b-marketing-initiative.
```
	Parameters: none
	Response: boolean
```

#### isCompanyLicence

```javascript
const isCompanyLicence = licenceInstance.isCompanyLicence();
```

- Returns true if licence type is a contract, newspaper-bundle, complimentary, business-development or prospect.
```
	Parameters: none
	Response: boolean
```

#### isContractLicence

```javascript
const isContractLicence = licenceInstance.isContractLicence();
```

- Returns true if licence type is a contract.
```
	Parameters: none
	Response: boolean
```

#### isBundleLicence

```javascript
const isBundleLicence = licenceInstance.isBundleLicence();
```

- Returns true if licence type is newspaper-bundle.
```
	Parameters: none
	Response: boolean
```

#### isComplimentaryLicence

```javascript
const isComplimentaryLicence = licenceInstance.isComplimentaryLicence();
```

- Returns true if licence type is complimentary.
```
	Parameters: none
	Response: boolean
```

#### isBusinessDevelopmentLicence

```javascript
const isBusinessDevelopmentLicence = licenceInstance.isBusinessDevelopmentLicence();
```

- Returns true if licence type is business-development.
```
	Parameters: none
	Response: boolean
```

#### isProspectLicence

```javascript
const isProspectLicence = licenceInstance.isProspectLicence();
```

- Returns true if licence type is prospect.
```
	Parameters: none
	Response: boolean
```

#### isSubscriptionLicence

```javascript
const isSubscriptionLicence = licenceInstance.isSubscriptionLicence();
```

- Returns true if licence type is subscription.
```
	Parameters: none
	Response: boolean
```

#### isB2cPartnershipLicence

```javascript
const isB2cPartnershipLicence = licenceInstance.isB2cPartnershipLicence();
```

- Returns true if licence type is b2c-partnership.
```
	Parameters: none
	Response: boolean
```

#### isLongRoomLicence

```javascript
const isLongRoomLicence = licenceInstance.isLongRoomLicence();
```

- Returns true if licence type is longroom.
```
	Parameters: none
	Response: boolean
```

#### isDeveloperPortalLicence

```javascript
const isDeveloperPortalLicence = licenceInstance.isDeveloperPortalLicence();
```

- Returns true if licence type is developer-portal.
```
	Parameters: none
	Response: boolean
```

#### getAcquisitionContext

```javascript
const acquisitionContext = licenceInstance.getAcquisitionContext();
```

- Returns the first acquisition context on the licence

```
	Parameters: none
	Throws: EmptyResultError if there are no acquisition contexts
	Response: AcquisitionContext
```


#### isIpAllowed

```javascript
const isIpAllowed = licenceInstance.isIpAllowed(ip);
```

- Returns true if the IP given is in the allowed IP range or there are no restricted IPs.

| Parameters | Required | Type   | Description       | Example      |
| ---------- | -------- | ------ | ------------------| ------------ |
| ip         | Yes      | String | User's IP address | 111.22.33.44 |

#### isAllowedEmailDomain

```javascript
const isAllowedEmailDomain = licenceInstance.isAllowedEmailDomain(email);
```

- Returns true if the email domain is valid based on the acquisition context

| Parameters | Required | Type   | Description          | Example         |
| ---------- | -------- | ------ | ---------------------| --------------- |
| email      | Yes      | String | User's email address | foo@example.com |

#### isBlockedEmailDomain

```javascript
const isBlockedEmailDomain = licenceInstance.isBlockedEmailDomain(email);
```

- Returns true if the email domain is blocked based on the acquisition context

| Parameters | Required | Type   | Description          | Example         |
| ---------- | -------- | ------ | ---------------------| --------------- |
| email      | Yes      | String | User's email address | foo@example.com |

#### isMarketingCampaign

```javascript
const isMarketingCampaign = licenceInstance.isMarketingCampaign();
```

- Returns true if acquisition context is a marketing campaign

#### isSeatAvailable

```javascript
const isSeatAvailable = licenceInstance.isSeatAvailable();
```

- Returns true if the max seats threshold has not yet been reached

#### isUserAnAdminstrator

```javascript
const isUserAnAdminstrator = licenceInstance.isUserAnAdminstrator(userId);
```

| Parameters | Required | Type   | Description          | Example         |
| ---------- | -------- | ------ | ---------------------| --------------- |
| userId     | Yes      | String | User's ID | 'xxxxxxxx-xxxx-xxxx-xxxxx' |

- Returns true if the user is an administrator of this licence.

#### isSubscribedWithGoogle

```javascript
const isSubscribedWithGoogle = licenceInstance.isSubscribedWithGoogle();
```

- Returns true if licence issueReason is `Subscribed with Google`
```
	Parameters: none
	Response: boolean
```

### Membership Licence Response

```javascript
	{
		id: 'licenceId',
		details: {
			links: [
				{ rel: 'trial', id: 'FT-123' }
			],
			seatLimit: 100,
			status: 'active',
			issueReason: 'Subscribed with Google',
			products: [
				{ "name": "FT.com Premium" }
			]
		},
		seats: {
			allocated: 0
		},
		acqContexts: {
			displayName: 'Display Name',
			restrictIpRanges: false,
			signupContext: {
				accessLicence: {
					id: 'tokenId',
				},
				restrictEmailDomains: true,
				allowedEmailDomains: ['ExamPle.com', 'Example_2.com'],
				restrictBlockedEmailDomains: false,
				blockedEmailDomains: ['BLOCKED.com'],
				signupWelcomeText: 'Welcome to your free trial'
			}
		},
		administrators: [
			{
				userId: 'xxxxxxxx-xxxx-xxxx-xxxxx',
				licenceId: 'licenceId'
			}
		]
	}
```

### Membership rel Attribute Types

https://confluence.ft.com/display/MEMB/Licence+Links
