Skip to content

Commit

Permalink
feat: add new endpoints for the custom role updates
Browse files Browse the repository at this point in the history
  • Loading branch information
finnar-bin committed Oct 29, 2024
1 parent 7fc2789 commit af8996f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
75 changes: 70 additions & 5 deletions src/FetchWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
IsiteServicesEndpoints,
IUpdateTeam,
OPTIONS,
GranularRole,
} from "types"

import FormData from "form-data"
Expand Down Expand Up @@ -127,6 +128,7 @@ export default class FetchWrapper {
// Roles
roleGET: "/roles/ROLE_ZUID",
roleDELETE: "/roles/ROLE_ZUID",
rolePUT: "/roles/ROLE_ZUID",
rolesPOST: "/roles",
roles: "/roles",
instancesRoles: "/instances/INSTANCE_ZUID/roles",
Expand All @@ -136,6 +138,7 @@ export default class FetchWrapper {
userRolesPOST: "/users/USER_ZUID/roles/ROLE_ZUID",
userRolesDELETE: "/users/USER_ZUID/roles/ROLE_ZUID",
userRolesPUT: "/users/USER_ZUID/roles/ROLE_ZUID",
bulkReassignUserRolesPUT: "/users/roles/ROLE_ZUID",
// Roles Granular
rolesGranularGET: "/roles/ROLE_ZUID/granulars/RESOURCE_ZUID",
rolesGranularDELETE: "/roles/ROLE_ZUID/granulars/RESOURCE_ZUID",
Expand Down Expand Up @@ -1142,15 +1145,34 @@ export default class FetchWrapper {
})
return await this.makeRequest(url, "DELETE", payload)
}
async createRole(name: string, entityZUID: string, systemRoleZUID: string) {
async createRole(
name: string,
entityZUID: string,
systemRoleZUID: string,
description = "",
) {
let payload = JSON.stringify({
name,
entityZUID,
systemRoleZUID,
description,
})
let url = this.accountsAPIURL + this.accountsAPIEndpoints.rolesPOST
return await this.makeRequest(url, "POST", payload)
}
async updateRole(
roleZUID: string,
data: { name: string; description: string; systemRoleZUID: string },
) {
const payload = JSON.stringify(data)
const url =
this.accountsAPIURL +
this.replaceInURL(this.accountsAPIEndpoints.rolePUT, {
ROLE_ZUID: roleZUID,
})

return await this.makeRequest(url, "PUT", payload)
}
async getRoles() {
let url = this.accountsAPIURL + this.accountsAPIEndpoints.roles
return await this.makeRequest(url)
Expand All @@ -1173,6 +1195,22 @@ export default class FetchWrapper {
})
return await this.makeRequest(url, "POST", payload)
}
async bulkReassignUsersRole({
oldRoleZUID,
newRoleZUID,
}: {
oldRoleZUID: string
newRoleZUID: string
}) {
const payload = JSON.stringify({ roleZUID: newRoleZUID })
const url =
this.accountsAPIURL +
this.replaceInURL(this.accountsAPIEndpoints.bulkReassignUserRolesPUT, {
ROLE_ZUID: oldRoleZUID,
})

return await this.makeRequest(url, "PUT", payload)
}
async deleteUserRole(userZUID: string, roleZUID: string) {
let payload = JSON.stringify({})
let url =
Expand Down Expand Up @@ -1247,8 +1285,33 @@ export default class FetchWrapper {
})
return await this.makeRequest(url, "PUT", payload)
}
async createGranularRole(roleZUID: string, resourceZUID: string, create = true) {
let payload = JSON.stringify({ resourceZUID, create })
async batchUpdateGranularRoles(roleZUID: string, granularRoles: GranularRole[]) {
let payload = JSON.stringify(granularRoles)
let url =
this.accountsAPIURL +
this.replaceInURL(this.accountsAPIEndpoints.rolesGranularPUT, {
ROLE_ZUID: roleZUID,
})

return await this.makeRequest(url, "PUT", payload)
}
async createGranularRole(
roleZUID: string,
resourceZUID: string,
create = true,
read = false,
update = false,
remove = false,
publish = false,
) {
let payload = JSON.stringify({
resourceZUID,
create,
read,
update,
delete: remove,
publish,
})
let url =
this.accountsAPIURL +
this.replaceInURL(this.accountsAPIEndpoints.rolesGranularPOST, {
Expand Down Expand Up @@ -1431,9 +1494,11 @@ export default class FetchWrapper {
}

// Search Items Function
async searchItems() {
async searchItems(params: Record<string, string> = {}) {
const searchParams = new URLSearchParams(params)?.toString()
let url = this.instancesAPIURL + this.instanceAPIEndpoints.searchItemsGET
return await this.makeRequest(url)

return await this.makeRequest(`${url}?${searchParams}`)
}

// Locales functionality
Expand Down
13 changes: 13 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export interface IaccountsAPIEndpoints {
// Roles
roleGET: string
roleDELETE: string
rolePUT: string
rolesPOST: string
roles: string
instancesRoles: string
Expand All @@ -129,6 +130,7 @@ export interface IaccountsAPIEndpoints {
userRolesPOST: string
userRolesDELETE: string
userRolesPUT: string
bulkReassignUserRolesPUT: string
// Roles Granular
rolesGranularGET: string
rolesGranularDELETE: string
Expand Down Expand Up @@ -211,3 +213,14 @@ export interface ICreateWebhook {
authorization?: string
text: string
}

export type GranularRole = {
create: boolean
delete: boolean
grant: boolean
name: string
publish: boolean
read: boolean
resourceZUID: string
update: boolean
}

0 comments on commit af8996f

Please sign in to comment.