This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
/
_access.js
120 lines (110 loc) · 3.52 KB
/
_access.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const _ = require('lodash');
const colors = require('colors');
const utils = require('../utils');
const makeAccess = (command, recordType) => {
const recordTypePlural = `${recordType}s`;
const access = (context, email, version) => {
if (email) {
return utils
.checkCredentials()
.then(() => utils.getLinkedApp())
.then(app => {
const urlExtra =
version && !global.argOpts.remove ? `/${version}` : '';
const msgExtra = version ? ` (${version})` : '';
const url = `/apps/${app.id}/${recordTypePlural}/${email}${urlExtra}`;
if (global.argOpts.remove) {
context.line(
`Preparing to remove ${recordType} ${email} from your app "${
app.title
}".\n`
);
utils.startSpinner(`Removing ${email}`);
return utils.callAPI(url, { method: 'DELETE' });
} else {
context.line(
`Preparing to add ${recordType} ${email} to your app "${
app.title
}${msgExtra}".\n`
);
utils.startSpinner(`Adding ${email}`);
return utils.callAPI(url, { method: 'POST' });
}
})
.then(() => {
utils.endSpinner();
context.line(
`\n${_.capitalize(
recordTypePlural
)} updated! Try viewing them with \`zapier ${command}\`.`
);
});
} else {
return utils.listEndpoint(recordTypePlural).then(data => {
context.line(
`The ${recordTypePlural} on your app "${
data.app.title
}" listed below.\n`
);
const ifEmpty = colors.grey(
`${_.capitalize(
recordTypePlural
)} not found. Try adding one with \`zapier ${command} user@example.com\`.`
);
const columns = [
['Email', 'email'],
['Role', 'role'],
['Status', 'status']
];
// Invitees can get access to specific versions only
if (recordType === 'invitee') {
columns.push(['Version', 'app_version']);
// Clean up "null" in app_version
_.each(
_.get(data, 'invitees', []),
invitee => (invitee.app_version = invitee.app_version || 'All')
);
}
utils.printData(data[recordTypePlural], columns, ifEmpty);
if (data && data.invite_url) {
context.line();
context.line(
'You can invite users to this app more broadly by sharing this URL:\n\n ' +
colors.bold(data.invite_url)
);
}
if (
data &&
data.versions_invite_urls &&
Object.keys(data.versions_invite_urls).length
) {
context.line();
context.line('Or you can invite users to a specific version:\n');
_.each(data.versions_invite_urls, (invite_url, _version) => {
context.line(` ${_version}: ${colors.bold(invite_url)}`);
});
}
});
}
};
access.argsSpec = [
{
name: 'email',
help: 'which user to add/remove',
example: 'user@example.com'
}
];
access.argOptsSpec = {
remove: { flag: true, help: 'elect to remove this user' }
};
// Invitees can get access to specific versions only
if (recordType === 'invitee') {
access.argsSpec.push({
name: 'version',
help: 'only invite to a specific version',
example: '1.0.0'
});
}
return access;
};
module.exports = makeAccess;