Skip to content

Commit

Permalink
Adds region filter to service and user resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Olya Morozova authored and Olya Morozova committed Sep 30, 2020
1 parent 4439c56 commit b2e3fe5
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,10 @@ Set a filter which will tell the API to return only individual type services. _(

Set a filter which will tell the API to return services that are specifically invite only.

- `located(details: LocatableServiceParameters)`

Set certain filters which will tell the API to return services that match the locatable details you provide.

- `on(page: number)`

Set the page offset which you want to view.
Expand All @@ -629,14 +633,15 @@ class Services {
this.api = new OpenApi();
}

async get({ category, limit, location, method, page, sortable, user }) {
async get({ category, limit, location, method, page, region, sortable, user }) {
return await this.api
.services()
.assigned()
.at(location)
.by(user)
.in(category)
.invitable()
.located({ region })
.supporting(method)
.on(page)
.sortBy(sortable)
Expand Down Expand Up @@ -756,6 +761,10 @@ Set a filter which will tell the API to return a user matching the provided iden

Send the API request using the pre-set filters.

- `located(details: LocatableUserParameters)`

Set certain filters which will tell the API to return users that match the locatable details you provide.

- `on(page: number)`

Set the page offset which you want to view.
Expand Down Expand Up @@ -786,11 +795,12 @@ class Users {
this.api = new OpenApi();
}

async get({ limit, location, method, page, services, sortable }) {
async get({ limit, location, method, page, region, services, sortable }) {
return await this.api
.users()
.assigned()
.at(location)
.located({ region })
.performing(services)
.supporting(method)
.on(page)
Expand Down
11 changes: 11 additions & 0 deletions src/resources/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ it('will set location filter using a string', async () => {
});
});

it('will set the locatable filters as supplied', async () => {
const resource = new Service(mockAxios);
const region = 'SK';

expect(resource.located({ region })).toHaveProperty('filters', { region })
});

it('will set meeting method filter using a number', async () => {
const resource = new Service(mockAxios);

Expand Down Expand Up @@ -136,6 +143,7 @@ it('can string all filterable options together', async () => {
.in(3)
.invitable()
.individual()
.located({ region: 'SK' })
.preferred()
.supporting(MeetingMethods.PHONE_CALL)
.sortBy('created')
Expand All @@ -151,6 +159,7 @@ it('can string all filterable options together', async () => {
location: 1,
method: MeetingMethods.PHONE_CALL,
preferred: 1,
region: 'SK',
user: 2,
});
expected.toHaveProperty('sortable', 'created');
Expand All @@ -177,6 +186,7 @@ it('can get services with additional parameters', async () => {
.in(3)
.invitable()
.individual()
.located({ region: 'SK' })
.preferred()
.supporting(MeetingMethods.PHONE_CALL)
.sortBy('created')
Expand All @@ -194,6 +204,7 @@ it('can get services with additional parameters', async () => {
'filter[invite_only]': 1,
'filter[location]': 1,
'filter[preferred]': 1,
'filter[province]': 'SK',
'filter[user]': 2,
limit: 5,
page: 1,
Expand Down
25 changes: 25 additions & 0 deletions src/resources/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import { combine } from '../helpers/filters';
import { Filterable, Pageable } from '../index';
import Conditional, { ConditionalResource } from './conditional';

type LocatableServiceDetail = 'region';

export interface LocatableServiceParameters {
[key: string]: any;
region?: string;
}

export interface ServiceFilter {
[key: string]: any;
assigned?: boolean;
category?: number | string;
group?: number;
Expand All @@ -23,6 +31,7 @@ export interface ServiceParameters {
invite_only?: number;
location?: number | string;
preferred?: number;
province?: string;
user?: number | string;
}

Expand All @@ -41,6 +50,8 @@ export interface ServiceResource extends Pageable, ConditionalResource {

invitable(): this;

located(details: LocatableServiceParameters): this;

preferred(): this;

supporting(method: number): this;
Expand Down Expand Up @@ -128,6 +139,16 @@ export default class Service extends Conditional implements ServiceResource {
return this;
}

public located(details: LocatableServiceParameters): this {
const keys = Object.keys(details) as LocatableServiceDetail[];

keys.map(key => {
this.filters[key] = details[key];
});

return this;
}

public preferred(): this {
this.filters.preferred = 1;

Expand Down Expand Up @@ -189,6 +210,10 @@ export default class Service extends Conditional implements ServiceResource {
params.preferred = this.filters.preferred;
}

if (typeof this.filters.region !== 'undefined') {
params.province = this.filters.region;
}

if (typeof this.filters.user !== 'undefined') {
params.user = this.filters.user;
}
Expand Down
11 changes: 11 additions & 0 deletions src/resources/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ it('will set location filter using a string', async () => {
});
});

it('will set the locatable filters as supplied', async () => {
const resource = new User(mockAxios);
const region = 'SK';

expect(resource.located({ region })).toHaveProperty('filters', { region })
});

it('will set meeting method filter using a number', async () => {
const resource = new User(mockAxios);

Expand Down Expand Up @@ -116,6 +123,7 @@ it('can string all filterable options together', async () => {
resource
.assigned()
.at(1)
.located({ region: 'SK' })
.performing([1, 2])
.supporting(MeetingMethods.PHONE_CALL)
.sortBy('created')
Expand All @@ -128,6 +136,7 @@ it('can string all filterable options together', async () => {
assigned: true,
location: 1,
method: MeetingMethods.PHONE_CALL,
region: 'SK',
services: [1, 2],
user: 1,
});
Expand All @@ -151,6 +160,7 @@ it('can get users with additional parameters', async () => {
await resource
.assigned()
.at(1)
.located({ region: 'SK' })
.performing([1, 2])
.supporting(MeetingMethods.PHONE_CALL)
.find(1)
Expand All @@ -166,6 +176,7 @@ it('can get users with additional parameters', async () => {
'filter[client_view_meeting_method]': MeetingMethods.PHONE_CALL,
'filter[location]': 1,
'filter[meeting_method]': MeetingMethods.PHONE_CALL,
'filter[province]': 'SK',
'filter[service]': [1, 2],
'filter[user]': 1,
limit: 5,
Expand Down
25 changes: 25 additions & 0 deletions src/resources/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import { combine } from '../helpers/filters';
import { Filterable, Pageable } from '../index';
import Conditional, { ConditionalResource } from './conditional';

type LocatableUserDetail = 'region';

export interface LocatableUserParameters {
[key: string]: any;
region?: string;
}

export interface UserFilter {
[key: string]: any;
assigned?: boolean;
services?: number | number[] | string | string[];
location?: number | string;
Expand All @@ -17,6 +25,7 @@ export interface UserParameters {
client_view_meeting_method?: number;
service?: number | number[] | string | string[];
location?: number | string;
province?: string;
user?: number | string;
meeting_method?: number;
}
Expand All @@ -28,6 +37,8 @@ export interface UserResource extends Pageable, ConditionalResource {

find(user: number | string): this;

located(details: LocatableUserParameters): this;

performing(services: number | number[] | string | string[]): this;

supporting(method: number): this;
Expand Down Expand Up @@ -68,6 +79,16 @@ export default class User extends Conditional implements UserResource {
return this;
}

public located(details: LocatableUserParameters): this {
const keys = Object.keys(details) as LocatableUserDetail[];

keys.map(key => {
this.filters[key] = details[key];
});

return this;
}

public async get(): Promise<any> {
const parameters = this.params();
let params: Filterable<UserParameters> = {};
Expand Down Expand Up @@ -136,6 +157,10 @@ export default class User extends Conditional implements UserResource {
params.location = this.filters.location;
}

if (typeof this.filters.region !== 'undefined') {
params.province = this.filters.region;
}

if (typeof this.filters.method !== 'undefined') {
params.client_view_meeting_method = this.filters.method;
params.meeting_method = this.filters.method;
Expand Down

0 comments on commit b2e3fe5

Please sign in to comment.