MCPcopy
hub / github.com/Linen-dev/linen.dev / buildSentAtForThreads

Function buildSentAtForThreads

packages/maintenance/src/thread-sentAt.ts:19–55  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

17}
18
19async function buildSentAtForThreads() {
20 // sentAt by default will have the value 0
21 const threads = await prisma.threads.findMany({
22 where: { sentAt: { equals: 0 } },
23 include: {
24 messages: true,
25 },
26 take: 250,
27 });
28 await Promise.all(
29 threads.map(async (thread) => {
30 const message = thread?.messages
31 ?.sort((a, b) => a.sentAt.getTime() - b.sentAt.getTime())
32 ?.shift();
33 if (message) {
34 await prisma.threads.update({
35 where: { id: thread.id },
36 data: {
37 sentAt: message.sentAt.getTime(),
38 messageCount: thread?.messages?.length || 0,
39 },
40 });
41 } else {
42 // if the thread doesn't have messages, we set sentAt as 1, to be able to reprocess it later
43 await prisma.threads.update({
44 where: { id: thread.id },
45 data: {
46 sentAt: 1,
47 messageCount: 0,
48 hidden: true,
49 },
50 });
51 }
52 })
53 );
54 return threads.length;
55}
56
57run();

Callers 1

runFunction · 0.85

Calls 2

mapMethod · 0.80
updateMethod · 0.45

Tested by

no test coverage detected