box-ui-elements
Version:
Box UI Elements
111 lines • 2.89 kB
JavaScript
import { INVITEE_ROLE_OWNER, STATUS_ACCEPTED } from '../../../constants';
import { COLLAB_USER_TYPE, COLLAB_GROUP_TYPE } from '../constants';
export const convertCollab = ({
avatarUrlMap,
collab,
currentUserId,
isCurrentUserOwner,
ownerEmailDomain
}) => {
if (!collab || collab.status !== STATUS_ACCEPTED) return null;
const {
accessible_by: {
id: collabId,
login: collabEmail,
name: collabName
},
id,
expires_at: executeAt,
role
} = collab;
const isCurrentUser = collabId === currentUserId;
const isExternal = !isCurrentUserOwner && collabEmail && ownerEmailDomain && collabEmail.split('@')[1] !== ownerEmailDomain;
const avatarUrl = avatarUrlMap ? avatarUrlMap[collabId] : undefined;
return {
avatarUrl,
email: collabEmail,
expiresAt: executeAt,
hasCustomAvatar: !!avatarUrl,
hasCustomRole: !!role,
id: id.toString(),
isCurrentUser,
isExternal,
isPending: false,
name: collabName,
role: role ? `${role[0].toUpperCase()}${role.slice(1)}` : '',
userId: collabId.toString()
};
};
export const convertCollabsResponse = (collabsApiData, currentUserId, owner, avatarUrlMap) => {
const {
entries = []
} = collabsApiData;
if (!entries.length) return [];
const {
id: ownerId,
email: ownerEmail,
name: ownerName
} = owner;
const isCurrentUserOwner = currentUserId === ownerId;
const ownerEmailDomain = ownerEmail && /@/.test(ownerEmail) ? ownerEmail.split('@')[1] : null;
const itemOwner = {
id: ownerEmail,
status: STATUS_ACCEPTED,
role: INVITEE_ROLE_OWNER,
accessible_by: {
id: ownerId,
login: ownerEmail,
name: ownerName
}
};
return [itemOwner, ...entries].flatMap(collab => {
const converted = convertCollab({
avatarUrlMap,
collab,
currentUserId,
isCurrentUserOwner,
ownerEmailDomain
});
return converted ? [converted] : [];
});
};
export const convertCollabsRequest = (collabRequest, existingCollaboratorsList) => {
const existingCollab = [];
if (existingCollaboratorsList && existingCollaboratorsList.length > 0) {
existingCollaboratorsList.forEach(collab => {
existingCollab.push(collab.userId);
});
}
const groups = [];
const users = [];
const {
role
} = collabRequest;
collabRequest.contacts.forEach(contact => {
if (existingCollab.includes(contact.id)) {
return;
}
if (contact.type === COLLAB_GROUP_TYPE) {
groups.push({
accessible_by: {
id: contact.id,
type: COLLAB_GROUP_TYPE
},
role
});
} else {
users.push({
accessible_by: {
login: contact.email,
type: COLLAB_USER_TYPE
},
role
});
}
});
return {
groups,
users
};
};
//# sourceMappingURL=convertCollaborators.js.map