Denys Vuika ddc6f36ab4
Prettier upgrade and e2e type checks (#1522)
* upgrade prettier

* noImplicitAny rule

* fix type

* update tsconfig

* upgrade to 150 print width
2020-07-14 10:03:23 +01:00

277 lines
8.7 KiB
TypeScript
Executable File

/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2020 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { RepoApi } from '../repo-api';
import { Logger } from '@alfresco/adf-testing';
import {
SiteBody,
SiteMemberRoleBody,
SiteMemberBody,
SiteEntry,
SiteMembershipRequestEntry,
SitesApi as AdfSiteApi,
SiteMemberEntry
} from '@alfresco/js-api';
import { SITE_VISIBILITY, SITE_ROLES } from '../../../../configs';
import { Utils } from '../../../../utilities/utils';
export class SitesApi extends RepoApi {
sitesApi = new AdfSiteApi(this.alfrescoJsApi);
constructor(username?: string, password?: string) {
super(username, password);
}
async getSite(siteId: string) {
try {
await this.apiAuth();
return await this.sitesApi.getSite(siteId);
} catch (error) {
this.handleError(`${this.constructor.name} ${this.getSite.name}`, error);
return null;
}
}
async getSites() {
try {
await this.apiAuth();
return await this.sitesApi.listSiteMembershipsForPerson(this.getUsername());
} catch (error) {
this.handleError(`${this.constructor.name} ${this.getSites.name}`, error);
return null;
}
}
async getDocLibId(siteId: string) {
try {
await this.apiAuth();
return (await this.sitesApi.listSiteContainers(siteId)).list.entries[0].entry.id;
} catch (error) {
this.handleError(`${this.constructor.name} ${this.getDocLibId.name}`, error);
return null;
}
}
async getVisibility(siteId: string) {
try {
const site = await this.getSite(siteId);
return site.entry.visibility;
} catch (error) {
this.handleError(`${this.constructor.name} ${this.getVisibility.name}`, error);
return null;
}
}
async getDescription(siteId: string) {
try {
const site = await this.getSite(siteId);
return site.entry.description;
} catch (error) {
this.handleError(`${this.constructor.name} ${this.getDescription.name}`, error);
return null;
}
}
async getTitle(siteId: string) {
try {
const site = await this.getSite(siteId);
return site.entry.title;
} catch (error) {
this.handleError(`${this.constructor.name} ${this.getTitle.name}`, error);
return null;
}
}
async createSite(title: string, visibility?: string, description?: string, siteId?: string): Promise<SiteEntry | null> {
const site = {
title,
visibility: visibility || SITE_VISIBILITY.PUBLIC,
description: description,
id: siteId || title
} as SiteBody;
try {
await this.apiAuth();
return await this.sitesApi.createSite(site);
} catch (error) {
this.handleError(`${this.constructor.name} ${this.createSite.name}`, error);
return null;
}
}
async createSitePrivate(title: string, description?: string, siteId?: string): Promise<SiteEntry> {
return this.createSite(title, SITE_VISIBILITY.PRIVATE, description, siteId);
}
async createSiteModerated(title: string, description?: string, siteId?: string): Promise<SiteEntry> {
return this.createSite(title, SITE_VISIBILITY.MODERATED, description, siteId);
}
async createSites(titles: string[], visibility?: string): Promise<any> {
try {
return titles.reduce(async (previous: any, current: any) => {
await previous;
return this.createSite(current, visibility);
}, Promise.resolve());
} catch (error) {
this.handleError(`${this.constructor.name} ${this.createSites.name}`, error);
}
}
async createSitesPrivate(siteNames: string[]): Promise<any> {
return this.createSites(siteNames, SITE_VISIBILITY.PRIVATE);
}
async deleteSite(siteId: string, permanent: boolean = true) {
try {
await this.apiAuth();
return await this.sitesApi.deleteSite(siteId, { permanent });
} catch (error) {
this.handleError(`${this.constructor.name} ${this.deleteSite.name}`, error);
}
}
async deleteSites(siteIds: string[], permanent: boolean = true) {
try {
return siteIds.reduce(async (previous, current) => {
await previous;
return this.deleteSite(current, permanent);
}, Promise.resolve());
} catch (error) {
this.handleError(`${this.constructor.name} ${this.deleteSites.name}`, error);
}
}
async deleteAllUserSites(permanent: boolean = true) {
try {
const siteIds = (await this.getSites()).list.entries.map((entries) => entries.entry.id);
return await siteIds.reduce(async (previous, current) => {
await previous;
return this.deleteSite(current, permanent);
}, Promise.resolve());
} catch (error) {
this.handleError(`${this.constructor.name} ${this.deleteAllUserSites.name}`, error);
}
}
async updateSiteMember(siteId: string, userId: string, role: string) {
const siteRole = {
role: role
} as SiteMemberRoleBody;
try {
await this.apiAuth();
return await this.sitesApi.updateSiteMembership(siteId, userId, siteRole);
} catch (error) {
this.handleError(`${this.constructor.name} ${this.updateSiteMember.name}`, error);
return null;
}
}
async addSiteMember(siteId: string, userId: string, role: string) {
const memberBody = {
id: userId,
role: role
} as SiteMemberBody;
try {
await this.apiAuth();
return await this.sitesApi.createSiteMembership(siteId, memberBody);
} catch (error) {
this.handleError(`${this.constructor.name} ${this.addSiteMember.name}`, error);
return null;
}
}
async addSiteConsumer(siteId: string, userId: string): Promise<SiteMemberEntry> {
return this.addSiteMember(siteId, userId, SITE_ROLES.SITE_CONSUMER.ROLE);
}
async addSiteContributor(siteId: string, userId: string): Promise<SiteMemberEntry> {
return this.addSiteMember(siteId, userId, SITE_ROLES.SITE_CONTRIBUTOR.ROLE);
}
async addSiteCollaborator(siteId: string, userId: string): Promise<SiteMemberEntry> {
return this.addSiteMember(siteId, userId, SITE_ROLES.SITE_COLLABORATOR.ROLE);
}
async addSiteManager(siteId: string, userId: string): Promise<SiteMemberEntry> {
return this.addSiteMember(siteId, userId, SITE_ROLES.SITE_MANAGER.ROLE);
}
async deleteSiteMember(siteId: string, userId: string) {
try {
await this.apiAuth();
return await this.sitesApi.deleteSiteMembership(siteId, userId);
} catch (error) {
this.handleError(`${this.constructor.name} ${this.deleteSiteMember.name}`, error);
}
}
async requestToJoin(siteId: string): Promise<SiteMembershipRequestEntry | null> {
const body = {
id: siteId
};
try {
await this.apiAuth();
return await this.sitesApi.createSiteMembershipRequestForPerson('-me-', body);
} catch (error) {
this.handleError(`${this.constructor.name} ${this.requestToJoin.name}`, error);
return null;
}
}
async hasMembershipRequest(siteId: string) {
try {
await this.apiAuth();
const requests = (await this.sitesApi.getSiteMembershipRequests('-me-')).list.entries.map((e) => e.entry.id);
return requests.includes(siteId);
} catch (error) {
this.handleError(`${this.constructor.name} ${this.hasMembershipRequest.name}`, error);
return null;
}
}
async waitForApi(data: { expect: number }) {
try {
const sites = async () => {
const totalItems = (await this.getSites()).list.pagination.totalItems;
if (totalItems !== data.expect) {
return Promise.reject(totalItems);
} else {
return Promise.resolve(totalItems);
}
};
return await Utils.retryCall(sites);
} catch (error) {
Logger.error(`${this.constructor.name} ${this.waitForApi.name} catch: `);
Logger.error(`\tExpected: ${data.expect} items, but found ${error}`);
}
}
}