-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…t_modified_user-created_at-updated_at [DEV-12883] Feature - Declare Contact Tags API + extend it
- Loading branch information
Showing
9 changed files
with
224 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import type { DeferredJobsApiClient } from '../../api'; | ||
import { routing } from '../../routing'; | ||
import { type Contact, type ContactTag, Query } from '../../types'; | ||
import { SortOrder } from '../../types'; | ||
|
||
import type { | ||
CreateOptions, | ||
CreateRequest, | ||
CreateResponse, | ||
ListOptions, | ||
ListResponse, | ||
MergeRequest, | ||
MergeResponse, | ||
SearchOptions, | ||
SearchResponse, | ||
UpdateOptions, | ||
UpdateRequest, | ||
UpdateResponse, | ||
} from './types'; | ||
|
||
export type Client = ReturnType<typeof createClient>; | ||
|
||
// Not putting these into the `./types` module to keep it local. | ||
type RawModificationResponse = { | ||
tag: ContactTag; | ||
deleted_tags_ids: ContactTag['id'][]; | ||
}; | ||
type RawDeleteResponse = { | ||
deleted_tags_ids: ContactTag['id'][]; | ||
}; | ||
|
||
export function createClient(api: DeferredJobsApiClient) { | ||
async function list(options: ListOptions = {}): Promise<ListResponse> { | ||
const { sortOrder } = options; | ||
const { tags } = await api.get<{ | ||
tags: ContactTag[]; | ||
}>(routing.contactsTagsUrl, { | ||
query: { | ||
sort: SortOrder.stringify(sortOrder), | ||
}, | ||
}); | ||
|
||
return { tags }; | ||
} | ||
|
||
async function search(options: SearchOptions = {}): Promise<SearchResponse> { | ||
const { query, sortOrder } = options; | ||
const { tags } = await api.post<{ | ||
tags: ContactTag[]; | ||
}>(routing.contactsTagsUrl, { | ||
query: { | ||
sort: SortOrder.stringify(sortOrder), | ||
query: Query.stringify(query), | ||
}, | ||
}); | ||
|
||
return { tags }; | ||
} | ||
|
||
async function get(id: ContactTag['id']): Promise<ContactTag> { | ||
const url = `${routing.contactsTagsUrl}/${id}`; | ||
const { tag } = await api.get<{ tag: ContactTag }>(url); | ||
|
||
return tag; | ||
} | ||
|
||
async function create( | ||
payload: CreateRequest, | ||
{ force = false }: CreateOptions = {}, | ||
): Promise<CreateResponse> { | ||
const data = await api.post<RawModificationResponse>(routing.contactsTagsUrl, { | ||
payload, | ||
query: { | ||
force: force || undefined, // Convert `false` to `undefined` | ||
}, | ||
}); | ||
return { | ||
tag: data.tag, | ||
deleted: data.deleted_tags_ids, | ||
}; | ||
} | ||
|
||
async function update( | ||
id: Contact['id'], | ||
payload: UpdateRequest, | ||
{ force = false }: UpdateOptions = {}, | ||
): Promise<UpdateResponse> { | ||
const url = `${routing.contactsTagsUrl}/${id}`; | ||
const data = await api.patch<RawModificationResponse>(url, { | ||
payload, | ||
query: { | ||
force: force || undefined, // Convert `false` to `undefined` | ||
}, | ||
}); | ||
return { | ||
tag: data.tag, | ||
deleted: data.deleted_tags_ids, | ||
}; | ||
} | ||
|
||
async function merge({ name, tags }: MergeRequest): Promise<MergeResponse> { | ||
const url = `${routing.contactsTagsUrl}/merge`; | ||
const data = await api.post<RawModificationResponse>(url, { | ||
payload: { name, tags }, | ||
}); | ||
return { | ||
tag: data.tag, | ||
deleted: data.deleted_tags_ids, | ||
}; | ||
} | ||
|
||
async function doDelete(id: ContactTag['id']) { | ||
const url = `${routing.contactsTagsUrl}/${id}`; | ||
const data = await api.delete<RawDeleteResponse>(url); | ||
return { deleted: data.deleted_tags_ids }; | ||
} | ||
|
||
async function doDeleteUnused() { | ||
const data = await api.delete<RawDeleteResponse>(routing.contactsTagsUrl, { | ||
query: { | ||
filter: 'unused', | ||
}, | ||
}); | ||
|
||
return { deleted: data.deleted_tags_ids }; | ||
} | ||
|
||
return { | ||
list, | ||
search, | ||
get, | ||
create, | ||
update, | ||
merge, | ||
delete: doDelete, | ||
deleteUnused: doDeleteUnused, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './Client'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { ContactTag, Query } from '../../types'; | ||
import type { SortOrder } from '../../types'; | ||
|
||
export interface ListOptions { | ||
sortOrder?: SortOrder | string; | ||
} | ||
|
||
export interface ListResponse { | ||
tags: ContactTag[]; | ||
} | ||
|
||
export interface SearchOptions extends ListOptions { | ||
query?: Query; | ||
} | ||
|
||
export type SearchResponse = ListResponse; | ||
|
||
export interface CreateRequest { | ||
name: string; | ||
} | ||
|
||
export interface CreateOptions { | ||
force?: boolean; | ||
} | ||
|
||
export interface CreateResponse { | ||
tag: ContactTag; | ||
deleted: ContactTag['id'][]; | ||
} | ||
|
||
export interface UpdateRequest { | ||
name: string; | ||
} | ||
|
||
export interface UpdateOptions { | ||
force?: boolean; | ||
} | ||
|
||
export interface UpdateResponse { | ||
tag: ContactTag; | ||
deleted: ContactTag['id'][]; | ||
} | ||
|
||
export interface MergeRequest { | ||
name: string; | ||
tags: ContactTag.Identifier[]; | ||
} | ||
|
||
export interface MergeResponse { | ||
tag: ContactTag; | ||
deleted: ContactTag['id'][]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { UserRef } from './User'; | ||
|
||
type Iso8601DateTime = string; | ||
|
||
export interface ContactTag { | ||
id: number; | ||
name: string; | ||
contacts_number: number; | ||
contacts_url: string; | ||
created_at: Iso8601DateTime | undefined; // May be undefined for older tags | ||
updated_at: Iso8601DateTime | undefined; // May be undefined for older tags | ||
creator: UserRef | undefined; // May be undefined for older tags | ||
last_updated_by_user: UserRef | undefined; // May be undefined for older tags | ||
} | ||
|
||
export namespace ContactTag { | ||
export type Identifier = ContactTag['id'] | ContactTag['name']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters