Repository: Accenture/sfmc-devtools (js)

Issue:
[BUG] deploy returns all cached items instead of only deployed ones
### Is there an existing issue for this?

- [X] I have searched the existing issues

### Current Behavior

looks like the cache is merged incorrectly

### Expected Behavior

cache merging shouldnt override result object

### Steps To Reproduce

1. Go to '...'
2. Click on '....'
3. Run '...'
4. See error...


### Version

5.1.0

### Environment

- OS:
- Node:
- npm:


### Participation

- [X] I am willing to submit a pull request for this issue.

### Additional comments

_No response_

Pull request description:
#1008: ensure deploy returns deployed items, not complete cache
# PR details

## What changes did you make? (Give an overview)

- closes #1008


## Checklist

- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] test scripts updated
- n/a Wiki updated (if applicable)


Patch:
diff --git a/lib/util/cache.js b/lib/util/cache.js
index bcea3a35..b95931e3 100644
--- a/lib/util/cache.js
+++ b/lib/util/cache.js
@@ -72,10 +72,15 @@ module.exports = {
      * @returns {void}
      */
     mergeMetadata: (type, metadataMap, overrideMID) => {
-        dataStore[overrideMID || currentMID][type] = Object.assign(
-            metadataMap,
-            dataStore[currentMID][type]
-        );
+        // ensure cache exists for type
+        dataStore[currentMID][type] ||= {};
+        // if overrideMID is provided, create a copy of current MID cache
+        if (overrideMID) {
+            // ! needs to be verified if this is actually needed. When discovering an issue with this method actually overriting metadataMap, this copy-logic was present and i did not want to break things
+            dataStore[overrideMID][type] = Object.assign({}, dataStore[currentMID][type]);
+        }
+        // merge metadataMap into existing cache
+        Object.assign(dataStore[overrideMID || currentMID][type] || {}, metadataMap);
     },
     /**
      * standardized method for getting data from cache.


Test patch:
diff --git a/test/type.user.test.js b/test/type.user.test.js
index cb56fe09..810164a6 100644
--- a/test/type.user.test.js
+++ b/test/type.user.test.js
@@ -83,7 +83,7 @@ describe('type: user', () => {
         });
         it('Should create & upsert a user', async () => {
             // WHEN
-            const expectedCache = ['testNew_user', 'testExisting_user'];
+            const expectedCache = ['testExisting_user', 'testNew_user'];
             await handler.deploy('testInstance/_ParentBU_', ['user'], expectedCache);
             // THEN
             assert.equal(process.exitCode, false, 'deploy should not have thrown an error');