test commit
This commit is contained in:
@@ -44,22 +44,21 @@ export async function registerUserMessageRoutes(fastify) {
|
||||
})
|
||||
if (orders.length === 0) return { count: 0 }
|
||||
|
||||
const orderIds = orders.map((o) => o.id)
|
||||
const readStates = await prisma.userOrderMessageReadState.findMany({
|
||||
where: { userId },
|
||||
})
|
||||
const lastReadByOrder = new Map(readStates.map((r) => [r.orderId, r.lastReadAt]))
|
||||
|
||||
const adminMessages = await prisma.orderMessage.findMany({
|
||||
where: { orderId: { in: orderIds }, authorType: 'admin' },
|
||||
select: { orderId: true, createdAt: true },
|
||||
})
|
||||
|
||||
let count = 0
|
||||
for (const o of orders) {
|
||||
const lastRead = lastReadByOrder.get(o.id) ?? new Date(0)
|
||||
const n = await prisma.orderMessage.count({
|
||||
where: {
|
||||
orderId: o.id,
|
||||
authorType: 'admin',
|
||||
createdAt: { gt: lastRead },
|
||||
},
|
||||
})
|
||||
count += n
|
||||
for (const msg of adminMessages) {
|
||||
const lastRead = lastReadByOrder.get(msg.orderId) ?? new Date(0)
|
||||
if (msg.createdAt > lastRead) count++
|
||||
}
|
||||
return { count }
|
||||
})
|
||||
@@ -86,25 +85,32 @@ export async function registerUserMessageRoutes(fastify) {
|
||||
})
|
||||
const lastReadByOrder = new Map(readStates.map((r) => [r.orderId, r.lastReadAt]))
|
||||
|
||||
const orderIds = orders.map((o) => o.id)
|
||||
const unreadCounts = new Map()
|
||||
if (orderIds.length > 0) {
|
||||
const adminMessages = await prisma.orderMessage.findMany({
|
||||
where: { orderId: { in: orderIds }, authorType: 'admin' },
|
||||
select: { orderId: true, createdAt: true },
|
||||
})
|
||||
for (const msg of adminMessages) {
|
||||
const lastRead = lastReadByOrder.get(msg.orderId) ?? new Date(0)
|
||||
if (msg.createdAt > lastRead) {
|
||||
unreadCounts.set(msg.orderId, (unreadCounts.get(msg.orderId) ?? 0) + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const items = []
|
||||
for (const o of orders) {
|
||||
const lastMsg = o.messages[0]
|
||||
if (!lastMsg) continue
|
||||
const lastRead = lastReadByOrder.get(o.id) ?? new Date(0)
|
||||
const unreadCount = await prisma.orderMessage.count({
|
||||
where: {
|
||||
orderId: o.id,
|
||||
authorType: 'admin',
|
||||
createdAt: { gt: lastRead },
|
||||
},
|
||||
})
|
||||
items.push({
|
||||
orderId: o.id,
|
||||
status: o.status,
|
||||
deliveryType: o.deliveryType,
|
||||
lastMessageAt: lastMsg.createdAt,
|
||||
preview: lastMsg.text.length > 280 ? `${lastMsg.text.slice(0, 277)}…` : lastMsg.text,
|
||||
unreadCount,
|
||||
unreadCount: unreadCounts.get(o.id) ?? 0,
|
||||
})
|
||||
}
|
||||
return { items }
|
||||
|
||||
Reference in New Issue
Block a user