/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2021, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a // license agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// import { IHttpClient } from "./IHttpClient"; import { IShortUserDescription } from "./IUser"; import { json, userFullName, userInitials } from "./impl/Utils"; /** * Members are the {@link User | users} who have access to the {@link Project | project}. */ export class Member { private _data: any; protected projectId: string; protected httpClient: IHttpClient; /** * @param data - An object that implements member data storage. * @param projectId - Owner project ID. * @param httpClient - Http client. */ constructor(data: any, projectId: string, httpClient: IHttpClient) { this.httpClient = httpClient; this.projectId = projectId; this.data = data; } private internalGet() { return this.httpClient.get(`/projects/${this.projectId}/members/${this.id}`); } private internalPut(body?: ArrayBuffer | Blob | globalThis.File | FormData | object | string | null) { return this.httpClient.put(`/projects/${this.projectId}/members/${this.id}`, body); } private internalDelete() { return this.httpClient.delete(`/projects/${this.projectId}/members/${this.id}`); } /** * Raw member data received from the server. * * @readonly */ get data(): any { return this._data; } private set data(value: any) { this._data = value; this._data.user.avatarUrl = `${this.httpClient.serverUrl}/users/${this._data.user.userId}/avatar`; this._data.user.fullName = userFullName(this._data.user); this._data.user.initials = userInitials(this._data.user.fullName); } /** * Unique member ID. * * @readonly */ get id(): string { return this.data.id; } /** * Member role. See {@link Project#getRoles | Project.getRoles()} for more details. */ get role(): string { return this.data.role; } set role(value: string) { this.data.role = value; } /** * Member type. Can be `owner` or `user`. * * @readonly */ get type(): string { return this.data.type; } /** * User information. * * @property {string} userId - User ID. * @property {string} userName - User name. * @property {string} name - First name. * @property {string} lastName - Last name. * @property {string} fullName - Full name. * @property {string} initials - Initials. * @property {string} email - User email. * @property {string} avatarUrl - User avatar image URL. * @readonly */ get user(): IShortUserDescription { return this.data.user; } /** * Refresh member data. * * @async */ async checkout(): Promise { this.data = await json(this.internalGet()); return this; } /** * Update member data on the server. * * @async * @param data - Raw member data. */ async update(data: any): Promise { this.data = await json(this.internalPut(data)); return this; } /** * Remove a member from a project. * * @async * @returns Returns the raw data of a deleted member. */ delete(): Promise { return json(this.internalDelete()); } /** * Save member data changes to the server. Call this method to update member data on the * server after any changes. * * @async */ save(): Promise { return this.update(this.data); } }