-
Notifications
You must be signed in to change notification settings - Fork 346
Expand file tree
/
Copy pathconvertSharingServiceData.ts
More file actions
93 lines (82 loc) · 3.74 KB
/
convertSharingServiceData.ts
File metadata and controls
93 lines (82 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {
ACCESS_COLLAB,
ACCESS_OPEN,
PERMISSION_CAN_DOWNLOAD,
PERMISSION_CAN_EDIT,
PERMISSION_CAN_PREVIEW,
} from '../../../constants';
import { convertISOStringToUTCDate } from '../../../utils/datetime';
import type { SharedLinkSettings } from '../types';
export interface ConvertSharedLinkSettingsReturnType {
password?: string | null;
permissions?: {
can_download?: boolean;
can_edit?: boolean;
can_preview: boolean;
};
unshared_at: string | null;
vanity_url: string;
}
export const convertSharedLinkPermissions = (permissionLevel: string) => {
if (!permissionLevel) {
return {};
}
const isEdit = permissionLevel === PERMISSION_CAN_EDIT;
return {
[PERMISSION_CAN_DOWNLOAD]: isEdit || permissionLevel === PERMISSION_CAN_DOWNLOAD,
[PERMISSION_CAN_EDIT]: isEdit,
[PERMISSION_CAN_PREVIEW]: permissionLevel === PERMISSION_CAN_PREVIEW,
};
};
/**
* Convert a shared link settings object from the USM into the format that the API expects.
* This function compares the provided access level to both API and internal USM access level constants, to accommodate two potential flows:
* - Changing the settings for a shared link right after the shared link has been created. The access level is saved directly from the data
* returned by the API, so it is in API format.
* - Changing the settings for a shared link in any other scenario. The access level is saved from the initial calls to the Item API and
* convertItemResponse, so it is in internal USM format.
*/
export const convertSharedLinkSettings = (
newSettings: SharedLinkSettings,
accessLevel: string,
isDownloadAvailable: boolean,
serverUrl: string,
): ConvertSharedLinkSettingsReturnType => {
const { expiration, isDownloadEnabled, isExpirationEnabled, isPasswordEnabled, password, vanityName } = newSettings;
const convertedSettings: ConvertSharedLinkSettingsReturnType = {
unshared_at:
expiration && isExpirationEnabled
? convertISOStringToUTCDate(new Date(expiration).toISOString()).toISOString()
: null,
vanity_url: serverUrl && vanityName ? `${serverUrl}${vanityName}` : '',
};
// Download permissions can only be set on "company" or "open" shared links.
if (accessLevel !== ACCESS_COLLAB) {
const permissions: ConvertSharedLinkSettingsReturnType['permissions'] = { can_preview: !isDownloadEnabled };
if (isDownloadAvailable) {
permissions.can_download = isDownloadEnabled;
}
(convertedSettings as ConvertSharedLinkSettingsReturnType).permissions = permissions;
}
/**
* This block covers the following cases:
* - Setting a new password: "isPasswordEnabled" is true, and "password" is a non-empty string.
* - Removing a password: "isPasswordEnabled" is false, and "password" is an empty string.
* The API only accepts non-empty strings and null values, so the empty string must be converted to null.
*
* Other notes:
* - Passwords can only be set on "open" shared links.
* - Attempting to set the password field on any other type of shared link will throw a 400 error.
* - When other settings are updated, and a password has already been set, the SharedLinkSettingsModal
* returns password = '' and isPasswordEnabled = true. In these cases, the password should *not*
* be converted to null, because that would remove the existing password.
*/
if (accessLevel === ACCESS_OPEN) {
if (isPasswordEnabled && !!password) {
convertedSettings.password = password;
} else if (!isPasswordEnabled) {
convertedSettings.password = null;
}
}
return convertedSettings;
};