Skip to content

Commit

Permalink
Added helpers for user and translation (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
tugbobo authored Oct 24, 2022
1 parent 8f8a79d commit ad74acf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/helpers/ApiHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {MemberApiHelper} from "./MemberApiHelper";
import {MemberGroupApiHelper} from "./MemberGroupApiHelper";
import {MediaApiHelper} from "./MediaApiHelper";
import {DomainApiHelper} from "./DomainApiHelper";
import {TranslationApiHelper} from "./TranslationApiHelper";

export class ApiHelpers {
baseUrl: string = umbracoConfig.environment.baseUrl;
Expand All @@ -43,6 +44,7 @@ export class ApiHelpers {
users: UserApiHelper;
media: MediaApiHelper;
domain: DomainApiHelper;
translation: TranslationApiHelper;

constructor(page: Page) {
this.content = new ContentApiHelper(this);
Expand All @@ -65,18 +67,20 @@ export class ApiHelpers {
this.users = new UserApiHelper(this);
this.media = new MediaApiHelper(this);
this.domain = new DomainApiHelper(this);
this.translation = new TranslationApiHelper(this);
}

async getCsrfToken() {
return (await this.page.context().cookies()).filter(x => x.name === 'UMB-XSRF-TOKEN')[0].value;
}

async get(url: string) {
async get(url: string, params?: { [key: string]: string | number | boolean; }) {
const csrf = await this.getCsrfToken();
const options = {
headers: {
'X-UMB-XSRF-TOKEN': csrf
},
params: params,
ignoreHTTPSErrors: true
}
return this.page.request.get(url, options);
Expand Down
1 change: 1 addition & 0 deletions lib/helpers/ConstantHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
settings: "settings",
media: "media",
users: "users",
translation: "translation",
}
}

24 changes: 24 additions & 0 deletions lib/helpers/TranslationApiHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ApiHelpers } from "./ApiHelpers";
import { JsonHelper } from "./JsonHelper";
export class TranslationApiHelper {
api: ApiHelpers

constructor(api: ApiHelpers) {
this.api = api;
}

async ensureKeyNotExists(key: string) {
let response = await this.api.get(`${this.api.baseUrl}/umbraco/backoffice/UmbracoApi/Dictionary/GetList`)
const searchBody = await JsonHelper.getBody(response);
let keyId = null;
for (const sb of searchBody) {
if (sb.name == key) {
keyId = sb.id;
}
}

if (keyId !== null) {
await this.api.post(`${this.api.baseUrl}/umbraco/backoffice/UmbracoApi/Dictionary/DeleteById?id=${keyId}`);
}
}
}
26 changes: 25 additions & 1 deletion lib/helpers/UserApiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ export class UserApiHelper {
}
}

async ensureUserBelongsToGroup(name: string) {
let response = await this.api.get(`${this.api.baseUrl}/umbraco/backoffice/umbracoapi/authentication/GetCurrentUser`)
const searchBody = await JsonHelper.getBody(response);
let userGroup = null;
if (searchBody !== null) {
for (const ug of searchBody.userGroups) {
if (ug == name) {
userGroup = ug;
}
}

if (userGroup == null) {
let params:{ [key: string]: string | number | boolean; } = {};
params["userGroupAliases[0]"] = name;
searchBody.userGroups.forEach(function (alias, i) {
params[`userGroupAliases[${i + 1}]`] = alias;
});
params["userIds"] = searchBody.id;
await this.api.get(`${this.api.baseUrl}/umbraco/backoffice/UmbracoApi/Users/PostSetUserGroupsOnUsers`, params);
return;
}
}
}

async setCurrentLanguage(language) {
let response = await this.api.get(`${this.api.baseUrl}/umbraco/backoffice/umbracoapi/authentication/GetCurrentUser`)
const searchBody = await JsonHelper.getBody(response);
Expand All @@ -41,7 +65,7 @@ export class UserApiHelper {

async postCreateUser(user){
await this.api.post(`${umbracoConfig.environment.baseUrl}/umbraco/backoffice/umbracoapi/users/PostCreateUser`, user)
};
};



Expand Down

0 comments on commit ad74acf

Please sign in to comment.