From b2e3fe51b07e4b9bb8ae82dba0d766c530d4d817 Mon Sep 17 00:00:00 2001 From: Olya Morozova Date: Tue, 29 Sep 2020 15:27:56 -0600 Subject: [PATCH] Adds region filter to service and user resources --- README.md | 14 ++++++++++++-- src/resources/service.test.ts | 11 +++++++++++ src/resources/service.ts | 25 +++++++++++++++++++++++++ src/resources/user.test.ts | 11 +++++++++++ src/resources/user.ts | 25 +++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1c2349e..b684242 100644 --- a/README.md +++ b/README.md @@ -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. @@ -629,7 +633,7 @@ 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() @@ -637,6 +641,7 @@ class Services { .by(user) .in(category) .invitable() + .located({ region }) .supporting(method) .on(page) .sortBy(sortable) @@ -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. @@ -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) diff --git a/src/resources/service.test.ts b/src/resources/service.test.ts index f66a6b1..a91195b 100644 --- a/src/resources/service.test.ts +++ b/src/resources/service.test.ts @@ -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); @@ -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') @@ -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'); @@ -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') @@ -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, diff --git a/src/resources/service.ts b/src/resources/service.ts index f6b9d54..806636b 100644 --- a/src/resources/service.ts +++ b/src/resources/service.ts @@ -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; @@ -23,6 +31,7 @@ export interface ServiceParameters { invite_only?: number; location?: number | string; preferred?: number; + province?: string; user?: number | string; } @@ -41,6 +50,8 @@ export interface ServiceResource extends Pageable, ConditionalResource { invitable(): this; + located(details: LocatableServiceParameters): this; + preferred(): this; supporting(method: number): this; @@ -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; @@ -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; } diff --git a/src/resources/user.test.ts b/src/resources/user.test.ts index c6832fd..b6462d0 100644 --- a/src/resources/user.test.ts +++ b/src/resources/user.test.ts @@ -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); @@ -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') @@ -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, }); @@ -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) @@ -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, diff --git a/src/resources/user.ts b/src/resources/user.ts index 179889d..993159e 100644 --- a/src/resources/user.ts +++ b/src/resources/user.ts @@ -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; @@ -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; } @@ -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; @@ -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 { const parameters = this.params(); let params: Filterable = {}; @@ -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;