Client for managing groups using the RESTful API
parent
3a3de5dac7
commit
a6c4fbe02c
|
|
@ -4,10 +4,20 @@ import {
|
|||
ListView,
|
||||
} from "@libreccm/ccm-apiclient-commons";
|
||||
|
||||
import { Group, buildGroupFromRecord } from "../entities/group";
|
||||
import { buildIdentifierParam } from "@libreccm/ccm-apiclient-commons";
|
||||
|
||||
import {
|
||||
Group,
|
||||
buildGroupFromRecord,
|
||||
buildGroupUserMembershipFromRecord,
|
||||
GroupUserMembership,
|
||||
} from "../entities/group";
|
||||
|
||||
import * as Constants from "../constants";
|
||||
import { buildPartyRoleMembershipFromRecord } from "../entities/party";
|
||||
import {
|
||||
buildPartyRoleMembershipFromRecord,
|
||||
PartyRoleMembership,
|
||||
} from "../entities/party";
|
||||
import { buildRoleFromRecord } from "../entities/role";
|
||||
|
||||
/**
|
||||
|
|
@ -60,10 +70,12 @@ export class GroupsApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
async getGroup(groupIdentifier: string): Promise<Group> {
|
||||
async getGroup(groupIdentifier: string | number): Promise<Group> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.get(
|
||||
`${this.#GROUPS_API_PREFIX}/${groupIdentifier}`
|
||||
`${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
|
||||
groupIdentifier
|
||||
)}`
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
|
|
@ -79,5 +91,269 @@ export class GroupsApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
async addGroup(group: Group): Promise<string> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.post(
|
||||
`${this.#GROUPS_API_PREFIX}`,
|
||||
JSON.stringify(group)
|
||||
);
|
||||
if (response.ok) {
|
||||
return group.name;
|
||||
} else {
|
||||
throw `Failed to add group ${group.name}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to add group ${group.name}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async updateGroup(groupIdentifier: string, group: Group): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.put(
|
||||
`${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
|
||||
groupIdentifier
|
||||
)}`,
|
||||
JSON.stringify(group)
|
||||
);
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to update group ${groupIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to update group ${groupIdentifier}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteGroup(groupIdentifier: string): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.delete(
|
||||
`${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
|
||||
groupIdentifier
|
||||
)}`
|
||||
);
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to delete group ${name}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to delete group ${name}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteGroupById(groupId: number): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.delete(
|
||||
`${this.#GROUPS_API_PREFIX}/ID-${groupId}`
|
||||
);
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to delete group ${groupId}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to delete group ${groupId}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteGroupByUuid(groupUuid: number): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.delete(
|
||||
`${this.#GROUPS_API_PREFIX}/UUID-${groupUuid}`
|
||||
);
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to delete group ${groupUuid}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to delete group ${groupUuid}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async getMembers(
|
||||
groupIdentifier: string,
|
||||
limit: number,
|
||||
offset: number
|
||||
): Promise<ListView<GroupUserMembership>> {
|
||||
try {
|
||||
const groupParam: string = buildIdentifierParam(groupIdentifier);
|
||||
|
||||
const response: ApiResponse = await this.#apiClient.get(
|
||||
`${this.#GROUPS_API_PREFIX}/${groupParam}/members`,
|
||||
{
|
||||
limit: limit.toString(),
|
||||
offset: offset.toString(),
|
||||
}
|
||||
);
|
||||
if (response.ok) {
|
||||
const result: Record<
|
||||
string,
|
||||
unknown
|
||||
> = (await response.json()) as Record<string, unknown>;
|
||||
const list: Record<string, unknown>[] = result.list as Record<
|
||||
string,
|
||||
unknown
|
||||
>[];
|
||||
const members: GroupUserMembership[] = list.map((record) =>
|
||||
buildGroupUserMembershipFromRecord(record)
|
||||
);
|
||||
|
||||
return {
|
||||
list: members,
|
||||
count: result.count as number,
|
||||
limit: result.limit as number,
|
||||
offset: result.offset as number,
|
||||
};
|
||||
} else {
|
||||
throw `Failed to get members of group ${groupIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to get members of group ${groupIdentifier}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async addMember(
|
||||
groupIdentifier: string | number,
|
||||
userIdentifier: string | number
|
||||
): Promise<void> {
|
||||
try {
|
||||
const groupParam = buildIdentifierParam(groupIdentifier);
|
||||
const memberParam = buildIdentifierParam(userIdentifier);
|
||||
const response: ApiResponse = await this.#apiClient.put(
|
||||
`${
|
||||
this.#GROUPS_API_PREFIX
|
||||
}/${groupParam}/members/${memberParam}`
|
||||
);
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to add user ${userIdentifier} to
|
||||
group ${groupIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to add user ${userIdentifier} to
|
||||
group ${groupIdentifier}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async removeMember(
|
||||
groupIdentifier: string | number,
|
||||
userIdentifier: string | number
|
||||
): Promise<void> {
|
||||
try {
|
||||
const groupParam = buildIdentifierParam(groupIdentifier);
|
||||
const memberParam = buildIdentifierParam(userIdentifier);
|
||||
const response: ApiResponse = await this.#apiClient.delete(
|
||||
`${
|
||||
this.#GROUPS_API_PREFIX
|
||||
}/${groupParam}/members/${memberParam}`
|
||||
);
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to remove user ${userIdentifier} from
|
||||
group ${groupIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to remote user ${userIdentifier} from
|
||||
group ${groupIdentifier}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async getRoles(
|
||||
groupIdentifier: string
|
||||
): Promise<ListView<PartyRoleMembership>> {
|
||||
const groupParam: string = buildIdentifierParam(groupIdentifier);
|
||||
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.get(
|
||||
`${this.#GROUPS_API_PREFIX}/${groupParam}/roles`
|
||||
);
|
||||
if (response.ok) {
|
||||
const result: Record<
|
||||
string,
|
||||
unknown
|
||||
> = (await response.json()) as Record<string, unknown>;
|
||||
const list: Record<string, unknown>[] = result.list as Record<
|
||||
string,
|
||||
unknown
|
||||
>[];
|
||||
const roles: PartyRoleMembership[] = list.map((record) =>
|
||||
buildPartyRoleMembershipFromRecord(record)
|
||||
);
|
||||
|
||||
return {
|
||||
list: roles,
|
||||
count: result.count as number,
|
||||
limit: result.limit as number,
|
||||
offset: result.offset as number,
|
||||
};
|
||||
} else {
|
||||
throw `Failed to get roles of group ${groupIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to get roles of group ${groupIdentifier}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async addRole(
|
||||
groupIdentifier: string | number,
|
||||
roleIdentifier: string | number
|
||||
): Promise<void> {
|
||||
try {
|
||||
const groupParam = buildIdentifierParam(groupIdentifier);
|
||||
const roleParam = buildIdentifierParam(roleIdentifier);
|
||||
const response: ApiResponse = await this.#apiClient.put(
|
||||
`${
|
||||
this.#GROUPS_API_PREFIX
|
||||
}/${groupParam}/roles/${roleParam}`
|
||||
);
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to add role ${roleIdentifier} to
|
||||
group ${groupIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to add role ${roleIdentifier} to
|
||||
group ${groupIdentifier}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async removeRole(
|
||||
groupIdentifier: string | number,
|
||||
roleIdentifier: string | number
|
||||
): Promise<void> {
|
||||
try {
|
||||
const groupParam = buildIdentifierParam(groupIdentifier);
|
||||
const roleParam = buildIdentifierParam(roleIdentifier);
|
||||
const response: ApiResponse = await this.#apiClient.delete(
|
||||
`${
|
||||
this.#GROUPS_API_PREFIX
|
||||
}/${groupParam}/roles/${roleParam}`
|
||||
);
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to remove role ${roleIdentifier} from
|
||||
group ${groupIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to remote role ${roleIdentifier} from
|
||||
group ${groupIdentifier}: ${err}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue