Modifying Array inside Object Type Payload

The below payload has 4 array inside data. One of these is refresh_tokens and in it one of the properties is last_used which has a datetime in it. I want to use this datatime to know if 3 days has past from that date and then delete the segment of array with last_used beyond 3 days.

{
  "version": 1,
  "key": "auth",
  "data": {
    "users": [
      {
        "id": 456,
        "is_active": true,
        "system_generated": false
      }
    ],
    "groups": [
      {
        "id": "admin",
        "name": "deff12"
      }
    ],
    "credentials": [
      {
        "id": 45,
        "auth": null,
        "data": {
          "username": "user"
        }
      }
    ],
    "refresh_tokens": [
      {
        "user_id": 122,
        "last_used_at": null,
        "version": null
      },
      {
        "user_id": 123,
        "last_used_at": "2021-11-25T00:54:31.341554+00:00",
        "version": null
      },
      {
        "user_id": 134,
        "last_used_at": "2021-11-21T01:19:56.970797+00:00",
        "version": "2021.11.4"
      }
    ]
  }
}

For example in the above payload, the 3 segment in refresh_tokens has the last_used datetime as 2021-11-21T01:19:56.970797+00:00. More than 3 days has passed since it and so deleting it the final payload should look like below.

{
  "version": 1,
  "key": "auth",
  "data": {
    "users": [
      {
        "id": 456,
        "is_active": true,
        "system_generated": false
      }
    ],
    "groups": [
      {
        "id": "admin",
        "name": "deff12"
      }
    ],
    "credentials": [
      {
        "id": 45,
        "auth": null,
        "data": {
          "username": "user"
        }
      }
    ],
    "refresh_tokens": [
      {
        "user_id": 122,
        "last_used_at": null,
        "version": null
      },
      {
        "user_id": 123,
        "last_used_at": "2021-11-25T00:54:31.341554+00:00",
        "version": null
      }
    ]
  }
}

Use javascript's filter() to filter out based on the conditions

// @ts-nocheck
function daysSince(date) {

    let today = new Date();
    let checkDay = new Date(date);

    let difference = Math.abs(checkDay - today);
    let days = difference / (1000 * 3600 * 24)

    return days
}

// filter tokens
msg.payload.data.refresh_tokens = msg.payload.data.refresh_tokens.filter(token => {
    if (token.last_used_at && token.last_used_at !== null && (daysSince(token.last_used_at) < 3)) {
        return true
    }

})

return msg;

double check if the conditions are as you need because i didnt test it extensively

[{"id":"9021f15dce7b58b4","type":"function","z":"5847b7aa62131d37","name":"","func":"// @ts-nocheck\nfunction daysSince(date) {\n\n    let today = new Date();\n    let checkDay = new Date(date);\n\n    let difference = Math.abs(checkDay - today);\n    let days = difference / (1000 * 3600 * 24)\n\n    return days\n}\n\n// filter tokens\nmsg.payload.data.refresh_tokens = msg.payload.data.refresh_tokens.filter(token => {\n    if (token.last_used_at && token.last_used_at !== null && (daysSince(token.last_used_at) < 3)) {\n        return true\n    }\n\n})\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":1160,"wires":[["836b64b27be9a64e"]]},{"id":"401681b625dd9834","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"version\":1,\"key\":\"auth\",\"data\":{\"users\":[{\"id\":456,\"is_active\":true,\"system_generated\":false}],\"groups\":[{\"id\":\"admin\",\"name\":\"deff12\"}],\"credentials\":[{\"id\":45,\"auth\":null,\"data\":{\"username\":\"user\"}}],\"refresh_tokens\":[{\"user_id\":122,\"last_used_at\":null,\"version\":null},{\"user_id\":123,\"last_used_at\":\"2021-11-25T00:54:31.341554+00:00\",\"version\":null},{\"user_id\":134,\"last_used_at\":\"2021-11-21T01:19:56.970797+00:00\",\"version\":\"2021.11.4\"}]}}","payloadType":"json","x":260,"y":1160,"wires":[["ed259ce6ad8e5012","9021f15dce7b58b4"]]},{"id":"ed259ce6ad8e5012","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":1080,"wires":[]},{"id":"836b64b27be9a64e","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":1160,"wires":[]}]

Thanks mate

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.