UNPKG

box-ui-elements

Version:
108 lines (107 loc) 3.49 kB
import { ACCESS_COLLAB, INVITEE_ROLE_EDITOR, PERMISSION_CAN_DOWNLOAD } from '../../../constants'; import { API_TO_USM_CLASSIFICATION_COLORS_MAP } from '../constants'; import { getAllowedAccessLevels } from './getAllowedAccessLevels'; import { getAllowedPermissionLevels } from './getAllowedPermissionLevels'; export const convertItemResponse = itemApiData => { const { allowed_invitee_roles, allowed_shared_link_access_levels, allowed_shared_link_access_levels_disabled_reasons, classification, id, name, owned_by: ownedBy, permissions, shared_link, shared_link_features, type } = itemApiData; const { password: isPasswordAvailable } = shared_link_features; const { can_download: isDownloadSettingAvailable, can_invite_collaborator: canInvite, can_set_share_access: canChangeAccessLevel, can_share: canShare } = permissions; // Convert classification data for the item if available let classificationData; if (classification) { const { color, definition, name: classificationName } = classification; classificationData = { colorId: API_TO_USM_CLASSIFICATION_COLORS_MAP[color], definition, name: classificationName }; } const isEditAllowed = allowed_invitee_roles.indexOf(INVITEE_ROLE_EDITOR) !== -1; let sharedLink; if (shared_link) { const { access, effective_permission: permission, is_password_enabled: isPasswordEnabled, unshared_at: expirationTimestamp, url, vanity_name: vanityName, vanity_url: vanityUrl } = shared_link; const isDownloadAllowed = permission === PERMISSION_CAN_DOWNLOAD; const canChangeDownload = canChangeAccessLevel && isDownloadSettingAvailable && access !== ACCESS_COLLAB; // access must be "company" or "open" const canChangePassword = canChangeAccessLevel && isPasswordAvailable; const canChangeExpiration = canChangeAccessLevel && isEditAllowed; sharedLink = { access, accessLevels: getAllowedAccessLevels(allowed_shared_link_access_levels, allowed_shared_link_access_levels_disabled_reasons), expiresAt: expirationTimestamp ? new Date(expirationTimestamp).getTime() : undefined, // convert to milliseconds permission, permissionLevels: getAllowedPermissionLevels(canChangeAccessLevel, isDownloadSettingAvailable, permission), settings: { canChangeDownload, canChangeExpiration, canChangePassword, canChangeVanityName: false, // vanity URLs cannot be set via the API isDownloadAvailable: isDownloadSettingAvailable, isDownloadEnabled: isDownloadAllowed, isPasswordAvailable: isPasswordAvailable ?? false, isPasswordEnabled }, url, vanityDomain: vanityUrl, vanityName: vanityName || '' }; } const collaborationRoles = allowed_invitee_roles.map(role => ({ id: role })); return { collaborationRoles, item: { id, classification: classificationData, name, permissions: { canInviteCollaborator: !!canInvite, canSetShareAccess: canChangeAccessLevel, canShare: !!canShare }, type }, sharedLink, sharingService: { can_set_share_access: canChangeAccessLevel, can_share: canShare, ownerEmail: ownedBy.login, ownerId: ownedBy.id }, ownedBy }; }; //# sourceMappingURL=convertItemResponse.js.map