# Account Group SDK

This provides a typescript implementation of the account group microservice

## Requirements

To setup the sdk you will need to:

Install the sdk to your project

```bash
npm install @vendasta/account-group-sdk --save
```

Next, you will need to import and setup your navigation module (generally in `app.module.ts`)

```typescript
import { AccountGroupModule } from "@vendasta/account-group-sdk";
…
@NgModule({
  …
  imports: [
    …, AccountGroupModule, …
  ]
})
```

### External Requirements

The Account Group sdk requires the following vendasta packages to be implemented by the project:

- `EnvironmentService` (https://www.npmjs.com/package/@vendasta/environment-service)
- `PartnerService` (https://www.npmjs.com/package/@vendasta/partner-service)
- `SessionService` (https://www.npmjs.com/package/@vendasta/session-service)

Please follow their instructions on how to set those packages up.

## Usage

Inject the AccountGroupService into your component and you will be able to start using it!

```typescript
import {AccountGroupService} from '@vendasta/account-group-sdk';

@Component({
  selector: '…',
  template: '…',
  styles: '…'
})
export class …Component {
  constructor(private accountGroupService: AccountGroupService) {
  }
}
```

### ProjectionFilters

Every call to the account group microservice requires a ProjectionFilter. A projection filter tells the microservice what data to return. By default all fields are false.

```typescript
interface ProjectionFilterInterface {
  accounts?: boolean;
  listingDistribution?: boolean;
  listingSyncPro?: boolean;
  associations?: boolean;
  accountGroupExternalIdentifiers?: boolean;
  socialUrls?: boolean;
  hoursOfOperation?: boolean;
  contactDetails?: boolean;
  snapshotReports?: boolean;
  legacyProductDetails?: boolean;
  richData?: boolean;
}
```

During construction, you can pass in all of the properties you want the api call to return:
```typescript
import {ProjectionFilter} from '@vendasta/account-group-sdk'

let pf = new ProjectionFilter({accounts: true, socialUrls: true, richData: true});
```

Or you can call `enableAll` to return all data.

```typescript
import {ProjectionFilter} from '@vendasta/account-group-sdk'

let pf = new ProjectionFilter().enableAll();
```

### Lookup

`lookup(projectionFilter: ProjectionFilter, marketIds: string[] = null, searchTerm: string = null, filters: LookupFilter = null, pageSize: number = 10): Observable<AccountGroup[]>`

Lookup returns many account groups matching the criteria specified. To fetch the next set of paged results from a lookup, call `loadMore` passing in the same criteria that was used in the original `lookup`.
 
`loadMore(projectionFilter: ProjectionFilter, marketIds: string[], searchTerm: string, filters: LookupFilter, pageSize: number = 10): Observable<AccountGroup[]>`

The list of account groups returned by `loadMore` will include the previous account groups fetched - there is no need to keep track of the list of account groups in your component.

Lookup provides some helper observables you can subscribe to:

`get hasMore(): Observable<boolean>` Whether or not there are more account groups to fetch 

`get accountGroups(): Observable<AccountGroup[]>` The current list of account groups fetched

`get totalResults(): Observable<number>` How many results are there total to fetch

`get loading(): Observable<boolean>` Whether or not the service is currently talking to the microservice


### Get

`get(accountGroupId: string, projectionFilter: ProjectionFilter): Observable<AccountGroup>`

Fetch a single account group by its ID

### GetMulti

`getMulti(accountGroupIds: string[], projectionFilter: ProjectionFilter): Observable<AccountGroup[]>`

Fetch many account groups by their IDs

### BulkUpdate

`bulkUpdate(accountGroupId: string, updateOperations: UpdateOperations): Observable<Response>`

To use the bulkUpdate function you will need to construct an UpdateOperations object and add the update operations you want to do to the account group.

There are 10 operations to choose from:
- `NapUpdateOperation`
- `SnapshotsUpdateOperation`
- `ListingDistributionDetailsUpdateOperation`
- `ListingSyncProUpdateOperation`
- `AccountGroupExternalIdentifiersUpdateOperation`
- `SocialURLsUpdateOperation`
- `HoursOfOperationUpdateOperation`
- `ContactDetailsUpdateOperation`
- `LegacyProductDetailsUpdateOperation`
- `RichDataUpdateOperation`

```typescript
import {UpdateOperations} from '@vendasta/account-group-sdk'

let operations: UpdateOperations = new UpdateOperations();

operations.addUpdateOperation(new NapUpdateOperation({key: value...}));
operations.addUpdateOperation(new SnapshotsUpdateOperation({key: value...}));
operations.addUpdateOperation(new ListingDistributionDetailsUpdateOperation({key: value...}));
operations.addUpdateOperation(new ListingSyncProUpdateOperation({key: value...}));
operations.addUpdateOperation(new AccountGroupExternalIdentifiersUpdateOperation({key: value...}));
operations.addUpdateOperation(new SocialURLsUpdateOperation({key: value...}));
operations.addUpdateOperation(new HoursOfOperationUpdateOperation({key: value...}));
operations.addUpdateOperation(new ContactDetailsUpdateOperation({key: value...}));
operations.addUpdateOperation(new LegacyProductDetailsUpdateOperation({key: value...}));
operations.addUpdateOperation(new RichDataUpdateOperation({key: value...}));
```

Only fields specified will be altered on the account group.
