mstate-cli 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mstate-cli",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "main": "index.js",
5
5
  "bin": {
6
6
  "mstate": "./dist/index.js"
@@ -7,9 +7,10 @@ export const CRED_FILE_PATH = path.join(
7
7
  '.mstate_credentials',
8
8
  );
9
9
 
10
- export const MSTATE_DOMAIN: string = `https://mstate.ai/`;
10
+ // export const MSTATE_DOMAIN: string = `https://mstate.ai/`;
11
11
  // export const MSTATE_DOMAIN: string = `http://localhost:5173/`;
12
+ export const MSTATE_DOMAIN: string = `https://dev.mstate.ai/`;
12
13
 
13
- // export const MSTATE_URL: string = 'https://dev.mstate.mobioffice.io/api';
14
+ export const MSTATE_URL: string = 'https://dev.mstate.mobioffice.io/api';
14
15
  // export const MSTATE_URL = 'http://localhost:3000/api';
15
- export const MSTATE_URL = 'https://api.mstate.mobioffice.io/api';
16
+ // export const MSTATE_URL = 'https://api.mstate.mobioffice.io/api';
@@ -1,7 +1,10 @@
1
1
  export enum CmdAction {
2
2
  LOGIN = 'login',
3
3
  LINK = 'link',
4
+ UPDATE = 'update',
5
+ DEV = 'dev',
4
6
  UNLINK = 'unlink',
7
+ PERMISSION = 'permission',
5
8
  USER = 'user',
6
9
  ADD = 'add',
7
10
  CLONE = 'clone',
@@ -38,7 +38,7 @@ export function handleUpdateSecretKey(secret: string) {
38
38
  flag: 'w',
39
39
  mode: 0o600,
40
40
  });
41
- } catch (error) {}
41
+ } catch (error) { }
42
42
  }
43
43
 
44
44
  export function getSecretKey() {
@@ -14,6 +14,10 @@ enum Key {
14
14
  PERMISSIONS = 'permissions=',
15
15
  SECRET_KEY = 'secret=',
16
16
  USER_ID = 'user=',
17
+ NAME = 'name=',
18
+ EMAIL = 'email=',
19
+ PHONE = 'phone=',
20
+ COMPANY_ID = 'company=',
17
21
  }
18
22
 
19
23
  class CompanyHandler {
@@ -23,11 +27,12 @@ class CompanyHandler {
23
27
  const args = process.argv;
24
28
 
25
29
  const pString = getValueFromArgs(args, Key.PERMISSIONS).trim();
26
- const permissions = pString.split(',');
30
+ const permissions = pString.split(',').map((s) => s.trim());
27
31
  const secretKey = getSecretKey();
28
32
  const user = getValueFromArgs(args, Key.USER_ID);
33
+ const companyID = getValueFromArgs(args, Key.COMPANY_ID);
29
34
 
30
- if (!pString.length) {
35
+ if (!permissions.length && permissions[0]?.length) {
31
36
  customLog.changeAndThrow(`Parameter permissions is required`);
32
37
  return;
33
38
  }
@@ -42,6 +47,11 @@ class CompanyHandler {
42
47
  return;
43
48
  }
44
49
 
50
+ if (!companyID) {
51
+ customLog.changeAndThrow(`Parameter company is required`);
52
+ return;
53
+ }
54
+
45
55
  const url = `${MSTATE_URL}/company/permission`;
46
56
  const responseJSON = await fetch(url, {
47
57
  method: 'POST',
@@ -52,6 +62,7 @@ class CompanyHandler {
52
62
  body: JSON.stringify({
53
63
  user,
54
64
  permissions,
65
+ company: companyID,
55
66
  }),
56
67
  });
57
68
 
@@ -66,15 +77,78 @@ class CompanyHandler {
66
77
  }
67
78
  }
68
79
 
80
+ async addUser() {
81
+ const args = process.argv;
82
+
83
+ const secretKey = getSecretKey();
84
+ const name = getValueFromArgs(args, Key.NAME);
85
+ const email = getValueFromArgs(args, Key.EMAIL);
86
+ const phone = getValueFromArgs(args, Key.PHONE);
87
+ const companyID = getValueFromArgs(args, Key.COMPANY_ID);
88
+
89
+ if (!secretKey) {
90
+ customLog.changeAndThrow(`Parameter secret is required`);
91
+ return;
92
+ }
93
+
94
+ if (!name) {
95
+ customLog.changeAndThrow(`Parameter name is required`);
96
+ return;
97
+ }
98
+
99
+ if (!email) {
100
+ customLog.changeAndThrow(`Parameter email is required`);
101
+ return;
102
+ }
103
+
104
+ if (!phone) {
105
+ customLog.changeAndThrow(`Parameter phone is required`);
106
+ return;
107
+ }
108
+
109
+ if (!companyID) {
110
+ customLog.changeAndThrow(`Parameter company is required`);
111
+ return;
112
+ }
113
+
114
+ const url = `${MSTATE_URL}/um/user`;
115
+ const responseJSON = await fetch(url, {
116
+ method: 'POST',
117
+ headers: {
118
+ 'secret-key': secretKey,
119
+ 'Content-Type': 'application/json',
120
+ },
121
+ body: JSON.stringify({
122
+ companyNickName: companyID,
123
+ name,
124
+ username: name,
125
+ email,
126
+ password: '123',
127
+ phone,
128
+ }),
129
+ });
130
+
131
+ const response = await responseJSON.json();
132
+ if (response?.errors) {
133
+ customLog.changeAndThrow(
134
+ 'Invalid Parameters for Company: ',
135
+ ...response?.errors,
136
+ );
137
+ } else {
138
+ customLog.success('User Updated successfully \n', response?.data);
139
+ }
140
+ }
141
+
69
142
  async revokePermission() {
70
143
  const args = process.argv;
71
144
 
72
145
  const pString = getValueFromArgs(args, Key.PERMISSIONS);
73
- const permissions = pString.split(',');
146
+ const permissions = pString.split(',').map((s) => s.trim());
74
147
  const secretKey = getSecretKey();
75
148
  const user = getValueFromArgs(args, Key.USER_ID);
149
+ const companyID = getValueFromArgs(args, Key.COMPANY_ID);
76
150
 
77
- if (!permissions.length) {
151
+ if (!permissions.length && permissions[0]?.length) {
78
152
  customLog.changeAndThrow(`Parameter permissions is required`);
79
153
  return;
80
154
  }
@@ -89,6 +163,11 @@ class CompanyHandler {
89
163
  return;
90
164
  }
91
165
 
166
+ if (!companyID) {
167
+ customLog.changeAndThrow(`Parameter company is required`);
168
+ return;
169
+ }
170
+
92
171
  const url = `${MSTATE_URL}/company/permission`;
93
172
  const responseJSON = await fetch(url, {
94
173
  method: 'DELETE',
@@ -97,6 +176,7 @@ class CompanyHandler {
97
176
  'Content-Type': 'application/json',
98
177
  },
99
178
  body: JSON.stringify({
179
+ company: companyID,
100
180
  user,
101
181
  permissions,
102
182
  }),
@@ -126,7 +206,7 @@ class CompanyHandler {
126
206
 
127
207
  const startServer = (port: number) => {
128
208
  return app.listen(port, () => {
129
- console.log(`Server is running on port ${port}`);
209
+ console.log(`Waiting for browser response...`);
130
210
  });
131
211
  };
132
212
 
@@ -148,14 +228,14 @@ class CompanyHandler {
148
228
  const { token } = req.query;
149
229
 
150
230
  if (token) {
151
- console.log('Authentication successful! Token received.');
231
+ console.log('Authentication successful!');
152
232
 
153
233
  // Save the token locally
154
234
  handleUpdateSecretKey(token as string);
155
235
 
156
236
  res.send('<h1>Login successful! You can close this window.</h1>');
157
237
  } else {
158
- console.error('Authentication failed. No token received.');
238
+ console.error('Authentication failed.');
159
239
  res.send('<h1>Authentication failed. Please try again.</h1>');
160
240
  }
161
241
  } catch (error) {
@@ -181,8 +261,13 @@ class CompanyHandler {
181
261
  });
182
262
  }
183
263
 
264
+ handleUpdateSavedSecretKey() {
265
+ handleUpdateSecretKey(getSecretKey());
266
+ }
267
+
184
268
  logToken() {
185
269
  customLog.info('Token', getSecretKey());
270
+ customLog.info('MSTATE_URL', MSTATE_URL);
186
271
  }
187
272
  }
188
273
 
package/src/index.ts CHANGED
@@ -28,11 +28,23 @@ const [action] = argv.slice(2);
28
28
  );
29
29
  break;
30
30
 
31
+ case CmdAction.PERMISSION:
32
+ if (hasFlag(argv, '-d')) await companyHandler.revokePermission();
33
+ else await companyHandler.addToken();
34
+ break;
35
+
36
+ case CmdAction.UPDATE:
37
+ companyHandler.handleUpdateSavedSecretKey();
38
+ break;
39
+
31
40
  case CmdAction.USER:
41
+ await companyHandler.addUser();
42
+ break;
43
+
44
+ case CmdAction.DEV: // comment this case on production build
32
45
  companyHandler.logToken();
33
- // if (hasFlag(argv, '-d')) companyHandler.revokePermission();
34
- // else companyHandler.addToken();
35
46
  break;
47
+
36
48
  case CmdAction.LOGIN:
37
49
  await companyHandler.handleLogin();
38
50
  break;