Jonas Rothmann
09/28/2021, 12:24 PMRyan
09/28/2021, 12:54 PMJonas Rothmann
09/28/2021, 12:56 PMconst getParams = (id: number) => ({
where: {id},
select: {id: true, otherCustomers: { select: { id: true, otherCustomers: true } }}
})
ctx.log.debug('init customerTree')
const baseCustomer = await ctx.prisma.customer.findUnique(getParams(args.id))
if(!baseCustomer) return null
const customers: any[] = []
type TraverseCustomer = CustomerType & { customer: CustomerType | null; otherCustomers: TraverseCustomer[]; }
async function traverse(node: TraverseCustomer) {
node?.otherCustomers.forEach(customer => {
customers.push(customer)
if(customer.otherCustomers) {
const subCustomer = await ctx.prisma.customer.findUnique(getParams(customer.id))
ctx.log.debug(JSON.stringify(subCustomer, null, 2))
traverse(subCustomer)
}
})
}
traverse(baseCustomer)
Jonas Rothmann
09/28/2021, 12:56 PMRyan
09/28/2021, 1:04 PMsubCustomers
for all the customers?Jonas Rothmann
09/28/2021, 1:06 PMJonas Rothmann
09/28/2021, 1:21 PMRyan
09/28/2021, 1:22 PM