( idKey: string, data: Record<string, unknown>, )
| 2 | import _ from 'lodash' |
| 3 | |
| 4 | export function extractTriggerItems( |
| 5 | idKey: string, |
| 6 | data: Record<string, unknown>, |
| 7 | ): Array<{ id: string | number; item: Record<string, unknown>; createdAt?: Date }> { |
| 8 | const keyParts = idKey.split('[].') |
| 9 | if (keyParts.length !== 2) { |
| 10 | const itemId = extractFirstItem(idKey, data) |
| 11 | return itemId ? [itemId] : [] |
| 12 | } |
| 13 | |
| 14 | const items = _.get(data, keyParts[0]) as Array<Record<string, unknown>> |
| 15 | const sortItems = sortByCreationDate(items) |
| 16 | return (Array.isArray(sortItems) ? sortItems : []) |
| 17 | .map((item) => { |
| 18 | let itemValue |
| 19 | |
| 20 | // the item is only the object containing the id |
| 21 | if (keyParts[1].includes('.')) { |
| 22 | itemValue = _.get(item, keyParts[1].split('.').slice(0, -1).join('.')) as Record<string, unknown> |
| 23 | } else { |
| 24 | itemValue = item |
| 25 | } |
| 26 | |
| 27 | return { |
| 28 | id: _.get(item, keyParts[1]) as string | number, |
| 29 | item: itemValue, |
| 30 | createdAt: getCreationDate(itemValue), |
| 31 | } |
| 32 | }) |
| 33 | .filter((item) => !!item.id && (typeof item.id === 'string' || typeof item.id === 'number')) |
| 34 | } |
| 35 | |
| 36 | export function extractFirstItem( |
| 37 | idKey: string, |
no test coverage detected