Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/1411 delete only processes first metadata entry #1414

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions @types/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ declare class Mcdev {
* deletes metadata from MC instance by key
*
* @param {string} businessUnit references credentials from properties.json
* @param {string} type supported metadata type
* @param {string} customerKey Identifier of metadata
* @param {string | TypeKeyCombo} selectedTypes supported metadata type (single) or complex object
* @param {string[] | string} [keyArr] Identifier of metadata
* @returns {Promise.<boolean>} true if successful, false otherwise
*/
static deleteByKey(businessUnit: string, type: string, customerKey: string): Promise<boolean>;
static deleteByKey(businessUnit: string, selectedTypes: string | TypeKeyCombo, keyArr?: string[] | string): Promise<boolean>;
/**
* get name & key for provided id
*
Expand Down
2 changes: 1 addition & 1 deletion @types/lib/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,19 @@ yargs(hideBin(process.argv))
.option('metadata', {
type: 'string',
alias: 'm',
group: 'Options for retrieve:',
describe:
'type or type:key or type:i:id or type:n:name to retrieve; if not provided, all metadata will be retrieved',
group: 'Options for delete:',
describe: 'type or type:key or type:i:id or type:n:name to delete',
});
},
handler: (argv) => {
Mcdev.setOptions(argv);
const typeKeyCombo = Mcdev.metadataToTypeKey(argv.metadata, ['key', 'id'], true);
const typeKeyCombo = Mcdev.metadataToTypeKey(argv.metadata, ['key', 'id']);
if ('undefined' === typeof typeKeyCombo) {
if (argv.TYPE && argv.KEY) {
Mcdev.deleteByKey(argv.BU, argv.TYPE, argv.KEY);
Mcdev.deleteByKey(argv.BU, argv.TYPE, csvToArray(argv.KEY));
}
} else {
const type = Object.keys(typeKeyCombo)[0];
Mcdev.deleteByKey(argv.BU, type, typeKeyCombo[type][0]);
Mcdev.deleteByKey(argv.BU, typeKeyCombo, null);
}
},
})
Expand Down
73 changes: 54 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,39 +557,74 @@ class Mcdev {
* deletes metadata from MC instance by key
*
* @param {string} businessUnit references credentials from properties.json
* @param {string} type supported metadata type
* @param {string} customerKey Identifier of metadata
* @param {string | TypeKeyCombo} selectedTypes supported metadata type (single) or complex object
* @param {string[] | string} [keyArr] Identifier of metadata
* @returns {Promise.<boolean>} true if successful, false otherwise
*/
static async deleteByKey(businessUnit, type, customerKey) {
static async deleteByKey(businessUnit, selectedTypes, keyArr) {
Util.startLogger();
Util.logger.info('mcdev:: delete');
if (!Util._isValidType(type)) {
return;
/** @typedef {string[]} */
let selectedTypesArr;
/** @typedef {TypeKeyCombo} */
let selectedTypesObj;
if ('string' === typeof keyArr) {
keyArr = [keyArr];
}
if ('string' === typeof selectedTypes) {
selectedTypesArr = [selectedTypes];
} else {
selectedTypesObj = selectedTypes;
// reset keys array because it will be overriden by values from selectedTypesObj
keyArr = null;
}
// check if types are valid
for (const selectedType of selectedTypesArr || Object.keys(selectedTypesObj)) {
if (!Util._isValidType(selectedType)) {
return;
}
}
const properties = await config.getProperties();
if (!(await config.checkProperties(properties))) {
return null;
}
const buObject = await Cli.getCredentialObject(properties, businessUnit);
if (buObject !== null) {
try {
MetadataTypeInfo[type].client = auth.getSDK(buObject);
} catch (ex) {
Util.logger.error(ex.message);
if (!buObject) {
return;
}
let client;
try {
client = auth.getSDK(buObject);
} catch (ex) {
Util.logger.error(ex.message);
return;
}
let status = true;
for (const type of selectedTypesArr || Object.keys(selectedTypesObj)) {
keyArr = selectedTypesArr ? keyArr : selectedTypesObj[type];
if (!keyArr) {
Util.logger.error(`No keys set for ${type}`);
return;
}
Util.logger.info(
Util.getGrayMsg(` - Deleting ${type} ${customerKey} on BU ${businessUnit}`)
);
try {
MetadataTypeInfo[type].properties = properties;
MetadataTypeInfo[type].buObject = buObject;
return await MetadataTypeInfo[type].deleteByKey(customerKey);
} catch (ex) {
Util.logger.errorStack(ex, ` - Deleting ${type} failed`);
for (const key of keyArr) {
MetadataTypeInfo[type].client = client;

Util.logger.info(
Util.getGrayMsg(` - Deleting ${type} ${key} on BU ${businessUnit}`)
);
try {
MetadataTypeInfo[type].properties = properties;
MetadataTypeInfo[type].buObject = buObject;
const result = await MetadataTypeInfo[type].deleteByKey(key);
status &&= result;
} catch (ex) {
Util.logger.errorStack(ex, ` - Deleting ${type} failed`);
status = false;
}
}
}

return status;
}
/**
* get name & key for provided id
Expand Down
18 changes: 18 additions & 0 deletions test/general.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,24 @@ describe('GENERAL', () => {
);
});
});

describe('Delete --metadata ~~~', () => {
it('Should delete the item', async () => {
const argvMetadata = [
'asset:testExisting_asset',
'automation:testExisting_automation',
'journey:testExisting_journey_Quicksend/1',
'journey:testExisting_journey_Multistep/1',
];
const typeKeyCombo = handler.metadataToTypeKey(argvMetadata);
// WHEN
const isDeleted = await handler.deleteByKey('testInstance/testBU', typeKeyCombo);
// THEN
assert.equal(process.exitCode, 0, 'deleteByKey should not have thrown an error');
assert.equal(isDeleted, true, 'deleteByKey should have returned true');
return;
});
});
});

describe('without --metadata ================', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0175b971-71a3-4d8e-98ac-48121f3fbf4f
13 changes: 13 additions & 0 deletions test/type.journey.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,19 @@ describe('type: journey', () => {
assert.equal(isDeleted, true, 'should have deleted the item');
return;
});

it('Should delete 2 items with version', async () => {
// WHEN
const isDeleted = await handler.deleteByKey('testInstance/testBU', 'journey', [
'testExisting_journey_Quicksend/1',
'testExisting_journey_Multistep/1',
]);
// THEN
assert.equal(process.exitCode, 0, 'delete should not have thrown an error');

assert.equal(isDeleted, true, 'should have deleted the item');
return;
});
});

describe('ReplaceContentBlockByX ================', () => {
Expand Down
Loading