Skip to content

Commit

Permalink
chore: also apply patch for node interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanVerstraete committed Feb 28, 2023
1 parent 530f820 commit 782f6d6
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions src/mappings/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,9 @@ export async function nodeStored(
newInterface.mac = intf.mac.toString()
newInterface.ips = intf.ips.map(ip => ip.toString()).join(',')
await ctx.store.save<Interfaces>(newInterface)
newNode.interfaces.push(newInterface)
})

await Promise.all(interfacesPromisses)
await ctx.store.save<Node>(newNode)
}

export async function nodeUpdated(
Expand Down Expand Up @@ -318,31 +316,23 @@ export async function nodeUpdated(
}
}

const interfacesPromisses = nodeEvent.interfaces.map(async intf => {
let newInterface

if (savedNode.interfaces) {
// if an interface with same name exists
const found = savedNode.interfaces.findIndex(interf => interf.name === intf.name.toString())
if (found > 0) {
newInterface = savedNode.interfaces[found]
} else {
newInterface = new Interfaces()
newInterface.id = item.event.id
newInterface.node = savedNode
}
}

if (!newInterface) return
// First remove all ifs
const nodeIfs = await ctx.store.find(Interfaces, { where: { node: { nodeID: savedNode.nodeID } } })
await ctx.store.remove(nodeIfs)

// Save ones from update event
await Promise.all(nodeEvent.interfaces.map(async intf => {
const newInterface = new Interfaces()
newInterface.id = item.event.id + intf.name.toString()
newInterface.name = intf.name.toString()
newInterface.mac = intf.mac.toString()
newInterface.ips = intf.ips.map(ip => ip.toString()).join(',')

newInterface.node = savedNode
await ctx.store.save<Interfaces>(newInterface)

savedNode.interfaces.push(newInterface)
})
await Promise.all(interfacesPromisses)
}))

await ctx.store.save<Node>(savedNode)
}

Expand Down

0 comments on commit 782f6d6

Please sign in to comment.