Skip to content

Commit

Permalink
fix: fixed various typings from config file and args
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Nov 12, 2020
1 parent 988cf0a commit 9e91986
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dependencies": {
"@oclif/config": "^1.17.0",
"@salesforce/command": "^3.0.4",
"@salesforce/core": "^2.14.2",
"@salesforce/core": "^2.15.2",
"tslib": "^1"
},
"devDependencies": {
Expand Down
56 changes: 43 additions & 13 deletions src/commands/force/user/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
User,
UserFields,
} from '@salesforce/core';
import { get, Dictionary } from '@salesforce/ts-types';
import { get, Dictionary, isArray } from '@salesforce/ts-types';
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';

Messages.importMessagesDirectory(__dirname);
Expand Down Expand Up @@ -54,36 +54,48 @@ export class UserCreateCommand extends SfdxCommand {
public org: Org;

private user: User;
private successes: SuccessMsg[];
private failures: FailureMsg[];
private successes: SuccessMsg[] = [];
private failures: FailureMsg[] = [];

public async run(): Promise<UserFields> {
this.logger = await Logger.child(this.constructor.name);
const defaultUserFields: DefaultUserFields = await DefaultUserFields.create({
templateUser: this.org.getUsername(),
});
const user: User = await User.create({ org: this.org });
this.user = await User.create({ org: this.org });

// merge defaults with provided values with cli > file > defaults
const fields: UserFields & Dictionary<string> = await this.aggregateFields(defaultUserFields.getFields());
// because fields is type UserFields & Dictionary<string> we can access these
const permsets = fields['permsets'];
const generatepassword: string = fields['generatepassword'];

// extract the fields and then delete, createUser doesn't expect a permsets or generatepassword
delete fields['permsets'];
delete fields['generatepassword'];
delete fields['generatePassword'];

try {
await user.createUser(fields);
await this.user.createUser(fields);
} catch (e) {
await this.catchCreateUser(e, fields);
}

// because fields is type UserFields & Dictionary<string> we can access these
const permsets: string = fields['permsets'];
const generatepassword: string = fields['generatepassword'];

// Assign permission sets to the created user
if (permsets) {
try {
await this.user.assignPermissionSets(fields.id, permsets.trim().split(','));
// permsets can be passed from cli args or file we need to create an array of permset names either way it's passed
let permsetArray: string[];
if (!isArray(permsets)) {
permsetArray = [permsets];
} else {
permsetArray = permsets as string[];
}

await this.user.assignPermissionSets(fields.id, permsetArray);
this.successes.push({
name: 'Permission Set Assignment',
value: permsets.trim(),
value: permsetArray.join(','),
});
} catch (err) {
this.failures.push({
Expand All @@ -94,7 +106,7 @@ export class UserCreateCommand extends SfdxCommand {
}

// Generate and set a password if specified
if (generatepassword === 'true') {
if (generatepassword === 'true' || generatepassword) {
try {
const password = User.generatePasswordUtf8();
await this.user.assignPassword(await AuthInfo.create({ username: fields.username }), password);
Expand Down Expand Up @@ -158,6 +170,22 @@ export class UserCreateCommand extends SfdxCommand {
});
}

// check if "profileName" was passed, this needs to become a profileId before calling User.create
if (defaultFields['profileName']) {
const name: string = defaultFields['profileName'] || 'Standard User';
this.logger.debug(`Querying org for profile name [${name}]`);
const profileQuery = `SELECT id FROM profile WHERE name='${name}'`;
const response = await this.org.getConnection().query(profileQuery);
defaultFields.profileId = get(response, 'records[0].Id') as string;
delete defaultFields['profileName'];
}

// the file schema is camelCase while the cli arg is no capitialization
if (defaultFields['generatePassword'] || defaultFields['generatepassword']) {
// standardize on 'generatepassword'
defaultFields['generatepassword'] = 'true';
}

return defaultFields;
}

Expand All @@ -170,7 +198,9 @@ export class UserCreateCommand extends SfdxCommand {
fields.username,
]);

if (this.failures) {
// we initialize to be an empty array to be able to push onto it
// so we need to check that the size is greater than 0 to know we had a failure
if (this.failures.length > 0) {
this.ux.styledHeader('Partial Success');
this.ux.log(userCreatedSuccessMsg);
this.ux.log('');
Expand Down
1 change: 0 additions & 1 deletion test/commands/user/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ describe('force:user:create', () => {
alias: 'testAlias',
email: '[email protected]',
emailEncodingKey: 'UTF-8',
generatepassword: 'false',
id: '0052D0000043PawWWR',
languageLocaleKey: 'en_US',
lastName: 'User',
Expand Down
2 changes: 1 addition & 1 deletion test/commands/user/password/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('force:user:password:generate', () => {

.stdout()
.command(['force:user:password:generate', '--json'])
.it('should throw the correct errror with warning message', (ctx) => {
.it('should throw the correct error with warning message', (ctx) => {
const result = JSON.parse(ctx.stdout);
expect(result.message).to.equal(messages.getMessage('noSelfSetError'));
expect(result.status).to.equal(1);
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@
chalk "^2.4.2"
cli-ux "^4.9.3"

"@salesforce/core@^2.14.2", "@salesforce/core@^2.6.0":
"@salesforce/core@^2.15.2", "@salesforce/core@^2.6.0":
version "2.15.2"
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-2.15.2.tgz#7e2b0ac6c1d67f850a461e007298d1381b37c07a"
integrity sha512-PCP9HdGEl4us8X66tOfwlTOGFRju8m7ezqfBlH7nRzmKw57i2l0LhXBEZ+DPwEBtAwa9wlvd9FhMmlhUYhm/EQ==
Expand Down

0 comments on commit 9e91986

Please sign in to comment.