| 17 | } |
| 18 | |
| 19 | async 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 | |
| 57 | run(); |