MCPcopy Create free account
hub / github.com/chainjet/platform / extractTriggerItems

Function extractTriggerItems

apps/runner/src/utils/trigger.utils.ts:4–34  ·  view source on GitHub ↗
(
  idKey: string,
  data: Record<string, unknown>,
)

Source from the content-addressed store, hash-verified

2import _ from 'lodash'
3
4export 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
36export function extractFirstItem(
37 idKey: string,

Callers 4

createOneMethod · 0.90
updateOneMethod · 0.90

Calls 4

extractFirstItemFunction · 0.85
sortByCreationDateFunction · 0.85
getCreationDateFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected