@extrahorizon/exh-cli 1.8.2-feat-80-7fb183f → 1.8.2-feat-83-ae0f26f

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.
@@ -37,8 +37,9 @@ async function syncFunctionUser(sdk, data) {
37
37
  const roleName = `exh.tasks.${taskName}`;
38
38
  const role = await syncRoleWithPermissions(sdk, taskName, roleName, targetPermissions);
39
39
  let user = await userRepository.findUserByEmail(sdk, email);
40
+ console.group(chalk.white(`🔄 Syncing user: ${email}`));
40
41
  if (!user) {
41
- console.log(chalk.white('⚙️ Creating a user for the task'));
42
+ console.log(chalk.white('⚙️ Creating the user...'));
42
43
  const registerUserData = {
43
44
  firstName: `${taskName}`,
44
45
  lastName: 'exh.tasks',
@@ -48,11 +49,13 @@ async function syncFunctionUser(sdk, data) {
48
49
  language: 'EN',
49
50
  };
50
51
  user = await userRepository.createUser(sdk, registerUserData);
51
- console.log(chalk.green('✅ Successfully created a user for task'));
52
52
  await assignRoleToUser(sdk, user.id, role.id);
53
- return await createOAuth1Tokens(sdk, email, password);
53
+ const oAuth1Tokens = await createOAuth1Tokens(sdk, email, password);
54
+ console.groupEnd();
55
+ console.log(chalk.green('✅ Successfully synced user'));
56
+ console.log('');
57
+ return oAuth1Tokens;
54
58
  }
55
- console.log(chalk.white('⚙️ Checking for the existing users credentials'));
56
59
  const currentFunction = await functionRepository.findByName(sdk, taskName);
57
60
  const hasExistingCredentials = (currentFunction?.environmentVariables?.API_HOST?.value &&
58
61
  currentFunction?.environmentVariables?.API_OAUTH_TOKEN_SECRET?.value &&
@@ -62,11 +65,14 @@ async function syncFunctionUser(sdk, data) {
62
65
  if (!hasExistingCredentials) {
63
66
  throw new Error('❌ No credentials were found for the existing user');
64
67
  }
68
+ console.log(chalk.white('⚙️ Reusing existing user credentials...'));
65
69
  const userRole = user.roles.find(({ name }) => name === roleName);
66
70
  if (!userRole) {
67
71
  await assignRoleToUser(sdk, user.id, role.id);
68
72
  }
69
- console.log(chalk.green('✅ Using existing credentials for the user'));
73
+ console.groupEnd();
74
+ console.log(chalk.green('✅ Successfully synced user'));
75
+ console.log('');
70
76
  return {
71
77
  token: currentFunction.environmentVariables.API_OAUTH_TOKEN.value,
72
78
  tokenSecret: currentFunction.environmentVariables.API_OAUTH_TOKEN_SECRET.value,
@@ -74,46 +80,49 @@ async function syncFunctionUser(sdk, data) {
74
80
  }
75
81
  exports.syncFunctionUser = syncFunctionUser;
76
82
  async function syncRoleWithPermissions(sdk, taskName, roleName, targetPermissions) {
77
- console.log(chalk.white('⚙️ Checking if the role exists'));
83
+ console.group(chalk.white(`🔄 Syncing role: ${roleName}`));
84
+ if (targetPermissions.length === 0) {
85
+ console.log(chalk.yellow('⚠️ No permissions have been defined for the role'));
86
+ }
78
87
  let role = await userRepository.findGlobalRoleByName(sdk, roleName);
79
88
  if (!role) {
80
- console.log(chalk.white('⚙️ Role does not exist, creating a new role'));
89
+ console.log(chalk.white('⚙️ Creating the role...'));
81
90
  const roleDescription = `A role created by the CLI for the execution of the task ${taskName}`;
82
91
  role = await userRepository.createGlobalRole(sdk, roleName, roleDescription);
83
- console.log(chalk.white('⚙️ Assigning permissions to the role'));
84
- if (targetPermissions.length === 0) {
85
- console.log(chalk.yellow('⚠️ No permissions defined for the role'));
86
- return role;
92
+ if (targetPermissions.length !== 0) {
93
+ await userRepository.addPermissionsToGlobalRole(sdk, roleName, targetPermissions);
87
94
  }
88
- await userRepository.addPermissionsToGlobalRole(sdk, roleName, targetPermissions);
89
- console.log(chalk.green('✅ Successfully assigned permissions to the role'));
95
+ console.log(chalk.white(`🔐 Permissions added: [${targetPermissions.join(', ')}]`));
96
+ console.groupEnd();
97
+ console.log(chalk.green('✅ Successfully synced role'));
98
+ console.log('');
90
99
  return role;
91
100
  }
101
+ console.log(chalk.white('⚙️ Updating the role...'));
92
102
  const currentPermissions = role.permissions?.flatMap(permission => permission.name) || [];
93
103
  const permissionsToAdd = targetPermissions.filter(targetPermission => !currentPermissions.includes(targetPermission));
94
104
  const permissionsToRemove = currentPermissions.filter(currentPermission => !targetPermissions.includes(currentPermission));
95
105
  if (permissionsToAdd.length > 0) {
96
- console.log(chalk.white('⚙️ Adding missing permissions to the role'));
97
106
  await userRepository.addPermissionsToGlobalRole(sdk, roleName, permissionsToAdd);
98
- console.log(chalk.green('✅ Successfully added missing permissions to the role'));
107
+ console.log(chalk.white(`🔐 Permissions added: [${permissionsToAdd.join(',')}]`));
99
108
  }
100
109
  if (permissionsToRemove.length > 0) {
101
- console.log(chalk.white('⚙️ Removing excess permissions from the role'));
102
110
  await userRepository.removePermissionsFromGlobalRole(sdk, roleName, permissionsToRemove);
103
- console.log(chalk.green('✅ Successfully removed excess permissions from the role'));
111
+ console.log(chalk.white(`🔐 Permissions removed: [${permissionsToRemove.join(',')}]`));
104
112
  }
113
+ console.groupEnd();
114
+ console.log(chalk.green('✅ Successfully synced role'));
115
+ console.log('');
105
116
  return role;
106
117
  }
107
118
  async function assignRoleToUser(sdk, userId, roleId) {
108
- console.log(chalk.white('⚙️ Assigning the role to the user'));
119
+ console.log(chalk.white('⚙️ Assigning the role to the user...'));
109
120
  await userRepository.addGlobalRoleToUser(sdk, userId, roleId);
110
- console.log(chalk.green('✅ Successfully assigned the role to the user'));
111
121
  }
112
122
  async function createOAuth1Tokens(sdk, email, password) {
113
- console.log(chalk.white('⚙️ Creating OAuth1 tokens for the user', email));
123
+ console.log(chalk.white('⚙️ Creating credentials...'));
114
124
  const response = await authRepository.createOAuth1Tokens(sdk, email, password);
115
125
  const { token, tokenSecret } = response;
116
- console.log(chalk.green('✅ Successfully created OAuth1 tokens for the user', email));
117
126
  return { token, tokenSecret };
118
127
  }
119
128
  function validateEmail(email) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@extrahorizon/exh-cli",
3
- "version": "1.8.2-feat-80-7fb183f",
3
+ "version": "1.8.2-feat-83-ae0f26f",
4
4
  "main": "build/index.js",
5
5
  "exports": "./build/index.js",
6
6
  "license": "MIT",