dannyelo
dannyelo5mo ago

Extracted function types

Hello! I extracted a function to map a query. But I can't find the correct types. I'm looking for the type order, orderLine, ctx and q. order and orderLine are raw records in the database.
const transformOrderForClient = async (order: any, ctx: any) => {
const orderLines = await ctx.db
.query('order_lines')
.filter((q: any) => q.eq(q.field('orderId'), order._id))
.collect()

const mappedOrderLines = await Promise.all(
orderLines.map(async (orderLine: any) => {
const product = await ctx.db.get(orderLine.productId)
console.log('product', orderLine)

return {
// ...
}
}),
)

const grandTotalAmount = orderLines.reduce((acc: any, orderLine: any) => {
return acc + orderLine?.price * orderLine?.quantity
}, 0)
const grandTotalQuantity = orderLines.reduce((acc: any, orderLine: any) => {
return acc + orderLine.quantity
}, 0)

return {
// ...
}
}
const transformOrderForClient = async (order: any, ctx: any) => {
const orderLines = await ctx.db
.query('order_lines')
.filter((q: any) => q.eq(q.field('orderId'), order._id))
.collect()

const mappedOrderLines = await Promise.all(
orderLines.map(async (orderLine: any) => {
const product = await ctx.db.get(orderLine.productId)
console.log('product', orderLine)

return {
// ...
}
}),
)

const grandTotalAmount = orderLines.reduce((acc: any, orderLine: any) => {
return acc + orderLine?.price * orderLine?.quantity
}, 0)
const grandTotalQuantity = orderLines.reduce((acc: any, orderLine: any) => {
return acc + orderLine.quantity
}, 0)

return {
// ...
}
}
2 Replies
erquhart
erquhart5mo ago
You won't need to assert types, something like this should work:
import { Doc } from "../_generated/dataModel"
import { QueryCtx } from "../_generated/server"

const transformOrderForClient = async (order: Doc<'orders'>, ctx: QueryCtx) => {
const orderLines = await ctx.db
.query('order_lines')
.filter((q) => q.eq(q.field('orderId'), order._id))
.collect()

const mappedOrderLines = await Promise.all(
orderLines.map(async (orderLine) => {
const product = await ctx.db.get(orderLine.productId)
console.log('product', orderLine)

return {
// ...
}
}),
)

const grandTotalAmount = orderLines.reduce(
(acc, orderLine) => {
return acc + orderLine.price * orderLine.quantity
},
0,
)
const grandTotalQuantity = orderLines.reduce((acc, orderLine) => {
return acc + orderLine.quantity
}, 0)

return {
// ...
}
}
import { Doc } from "../_generated/dataModel"
import { QueryCtx } from "../_generated/server"

const transformOrderForClient = async (order: Doc<'orders'>, ctx: QueryCtx) => {
const orderLines = await ctx.db
.query('order_lines')
.filter((q) => q.eq(q.field('orderId'), order._id))
.collect()

const mappedOrderLines = await Promise.all(
orderLines.map(async (orderLine) => {
const product = await ctx.db.get(orderLine.productId)
console.log('product', orderLine)

return {
// ...
}
}),
)

const grandTotalAmount = orderLines.reduce(
(acc, orderLine) => {
return acc + orderLine.price * orderLine.quantity
},
0,
)
const grandTotalQuantity = orderLines.reduce((acc, orderLine) => {
return acc + orderLine.quantity
}, 0)

return {
// ...
}
}
dannyelo
dannyeloOP5mo ago
Thank you!!

Did you find this page helpful?