/
projectMembers.ts
145 lines (128 loc) · 5.39 KB
/
projectMembers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
import { ProjectInvitationItemModel, ProjectMember, ProjectMemberCursor, ProjectMembersApi, ProjectMembersPage } from '@/types/projectMembers';
import { HttpClient } from '@/utils/httpClient';
import { APIError } from '@/utils/error';
export class ProjectMembersHttpApi implements ProjectMembersApi {
private readonly http: HttpClient = new HttpClient();
private readonly ROOT_PATH: string = '/api/v0/projects';
/**
* Used for deleting team members from project.
*
* @param projectId
* @param emails
*/
public async delete(projectId: string, emails: string[]): Promise<void> {
const path = `${this.ROOT_PATH}/${projectId}/members?emails=${encodeURIComponent(emails.toString())}`;
const response = await this.http.delete(path);
if (!response.ok) {
const result = await response.json();
throw new APIError({
status: response.status,
message: result.error || 'Failed to delete project members and invitations',
requestID: response.headers.get('x-request-id'),
});
}
}
/**
* Used for fetching team members and invitations related to project.
*
* @param projectId
* @param cursor for pagination
*/
public async get(projectId: string, cursor: ProjectMemberCursor): Promise<ProjectMembersPage> {
const path = `${this.ROOT_PATH}/${projectId}/members?limit=${cursor.limit}&page=${cursor.page}&order=${cursor.page}&order-direction=${cursor.orderDirection}&search=${cursor.search}`;
const response = await this.http.get(path);
const result = await response.json();
if (!response.ok) {
throw new APIError({
status: response.status,
message: result.error || 'Failed to get project members and invitations',
requestID: response.headers.get('x-request-id'),
});
}
return this.getProjectMembersList(result);
}
/**
* Handles inviting a user to a project.
*
* @throws Error
*/
public async invite(projectID: string, email: string): Promise<void> {
const path = `${this.ROOT_PATH}/${projectID}/invite/${encodeURIComponent(email)}`;
const httpResponse = await this.http.post(path, null);
if (httpResponse.ok) return;
const result = await httpResponse.json();
throw new APIError({
status: httpResponse.status,
message: result.error || 'Failed to send project invitations',
requestID: httpResponse.headers.get('x-request-id'),
});
}
/**
* Handles resending invitations to project.
*
* @throws Error
*/
public async reinvite(projectID: string, emails: string[]): Promise<void> {
const path = `${this.ROOT_PATH}/${projectID}/reinvite`;
const body = { emails };
const httpResponse = await this.http.post(path, JSON.stringify(body));
if (httpResponse.ok) return;
const result = await httpResponse.json();
throw new APIError({
status: httpResponse.status,
message: result.error || 'Failed to resend project invitations',
requestID: httpResponse.headers.get('x-request-id'),
});
}
/**
* Get invite link for the specified project and email.
*
* @throws Error
*/
public async getInviteLink(projectID: string, email: string): Promise<string> {
const path = `${this.ROOT_PATH}/${projectID}/invite-link?email=${encodeURIComponent(email)}`;
const httpResponse = await this.http.get(path);
const result = await httpResponse.json();
if (httpResponse.ok) {
return result;
}
throw new APIError({
status: httpResponse.status,
message: result.error || 'Can not get invite link',
requestID: httpResponse.headers.get('x-request-id'),
});
}
/**
* Method for mapping project members page from json to ProjectMembersPage type.
*
* @param projectMembers anonymous object from json
*/
private getProjectMembersList(projectMembers: any): ProjectMembersPage { // eslint-disable-line @typescript-eslint/no-explicit-any
if (!projectMembers) {
return new ProjectMembersPage();
}
const projectMembersPage: ProjectMembersPage = new ProjectMembersPage();
projectMembersPage.projectMembers = projectMembers.projectMembers.map(key => new ProjectMember(
key.fullName,
key.shortName,
key.email,
new Date(key.joinedAt),
key.id,
));
projectMembersPage.projectInvitations = projectMembers.projectInvitations.map(key => new ProjectInvitationItemModel(
key.email,
new Date(key.createdAt),
key.expired,
));
projectMembersPage.search = projectMembers.search;
projectMembersPage.limit = projectMembers.limit;
projectMembersPage.order = projectMembers.order;
projectMembersPage.orderDirection = projectMembers.orderDirection;
projectMembersPage.pageCount = projectMembers.pageCount;
projectMembersPage.currentPage = projectMembers.currentPage;
projectMembersPage.totalCount = projectMembers.totalCount;
return projectMembersPage;
}
}