Update Member Data

* Implemented member updates
* Updated async method names with async suffix
This commit is contained in:
Colin Williams
2023-03-18 08:55:37 -07:00
committed by GitHub
parent d4358301b6
commit a627aaa423
4 changed files with 88 additions and 18 deletions
+14 -9
View File
@@ -1,6 +1,6 @@
import HttpUtils from './httpUtils'
import type { Group } from './group'
import type { Member } from './member'
import type { Member, MemberUpdate } from './member'
const D4H_FETCH_LIMIT = 250
const D4H_BASE_URL = 'https://api.d4h.org/v2'
@@ -31,7 +31,7 @@ export default class D4H {
/**************** MEMBERS *******************/
/********************************************/
async getMember(id: number, options?: GetMemberOptions): Promise<Member> {
getMemberAsync(id: number, options?: GetMemberOptions): Promise<Member> {
const url = new URL(`${D4H_BASE_URL}/team/members/${id}`)
if (options !== undefined) {
@@ -42,10 +42,10 @@ export default class D4H {
}
}
return await this._httpUtils.get<Member>(url)
return this._httpUtils.getAsync<Member>(url)
}
async getMembers(options?: GetMembersOptions): Promise<Member[]> {
getMembersAsync(options?: GetMembersOptions): Promise<Member[]> {
const url = new URL(`${D4H_BASE_URL}/team/members`)
if (options !== undefined) {
@@ -64,19 +64,24 @@ export default class D4H {
}
}
return await this._httpUtils.getMany(url)
return this._httpUtils.getManyAsync(url)
}
updateMemberAsync(id: number, updates: MemberUpdate): Promise<Member> {
const url = new URL(`${D4H_BASE_URL}/team/members/${id}`)
return this._httpUtils.putAsync(url, updates)
}
/********************************************/
/***************** GROUPS *******************/
/********************************************/
async getGroup(id: number): Promise<Group> {
getGroupAsync(id: number): Promise<Group> {
const url = new URL(`${D4H_BASE_URL}/team/groups/${id}`)
return await this._httpUtils.get<Group>(url)
return this._httpUtils.getAsync<Group>(url)
}
async getGroups(options?: GetGroupsOptions): Promise<Group[]> {
getGroupsAsync(options?: GetGroupsOptions): Promise<Group[]> {
const url = new URL(`${D4H_BASE_URL}/team/groups`)
if (options !== undefined) {
@@ -91,6 +96,6 @@ export default class D4H {
}
}
return await this._httpUtils.getMany(url)
return this._httpUtils.getManyAsync(url)
}
}
+28 -6
View File
@@ -9,6 +9,11 @@ interface D4HError {
statusCode: number
}
enum HttpMethod {
Get = 'GET',
Put = 'PUT',
}
export default class HttpUtils {
private readonly _fetchLimit: number
private readonly _token: string
@@ -22,16 +27,25 @@ export default class HttpUtils {
this._token = token
}
async get<DataType>(url: URL): Promise<DataType> {
const method = 'GET'
async requestAsync<TBody, TResponse>(url: URL, method: HttpMethod, body?: TBody): Promise<TResponse> {
const headers = {
'Authorization': `Bearer ${this._token}`,
'Content-Type': 'application/json'
}
console.log(url)
const options: RequestInit = {
method,
headers,
}
if (body) {
options.body = JSON.stringify(body)
}
const rawResponse = await fetch(url.toString(), { method, headers })
const response = await rawResponse.json() as D4HResponse<DataType> & D4HError
const rawResponse = await fetch(url.toString(), options)
const response = await rawResponse.json() as D4HResponse<TResponse> & D4HError
if (response.statusCode !== 200) {
const d4hError = response as D4HError
@@ -40,8 +54,12 @@ export default class HttpUtils {
return response.data
}
async getAsync<DataType>(url: URL): Promise<DataType> {
return this.requestAsync<never, DataType>(url, HttpMethod.Get)
}
async getMany<DataType>(url: URL): Promise<DataType[]> {
async getManyAsync<DataType>(url: URL): Promise<DataType[]> {
let results: DataType[] = []
let offset = 0
@@ -52,7 +70,7 @@ export default class HttpUtils {
urlWithOffset.searchParams.append('offset', offset.toString())
urlWithOffset.searchParams.append('limit', this._fetchLimit.toString())
const newResults = await this.get<DataType[]>(urlWithOffset)
const newResults = await this.getAsync<DataType[]>(urlWithOffset)
results = results.concat(newResults)
offset += this._fetchLimit
@@ -63,4 +81,8 @@ export default class HttpUtils {
return results
}
async putAsync<TBody, TResponse>(url: URL, body: TBody): Promise<TResponse> {
return this.requestAsync(url, HttpMethod.Put, body)
}
}
+32
View File
@@ -46,4 +46,36 @@ export interface Member {
ref: string;
status: MemberStatus;
workphone: string;
}
export interface MemberUpdate {
name?: string | null
ref?: string | null
id_tag?: string | null
status_id?: number
status_custom_id?: number
retired_reason_id?: number
date_leave?: Date
date_join?: Date
position?: string | null
role_id?: number
cost_per_hour?: number
cost_per_use?: number
address_street?: string | null
address_city?: string | null
address_region?: string | null
address_postcode?: string | null
address_country?: string | null
lat?: number
lng?: number
gridref?: string | null
location_bookmark_id?: number
email?: string | null
phone_mobile?: string | null
phone_home?: string | null
phone_work?: string | null
pager?: string | null
pager_email?: string | null
address?: string | null
notes?: string | null
}