(account: accounts)
| 3 | const PAGE_SIZE = 30; |
| 4 | |
| 5 | export async function buildPages(account: accounts) { |
| 6 | const channels = await prisma.channels.findMany({ |
| 7 | where: { accountId: account.id }, |
| 8 | }); |
| 9 | |
| 10 | for (const channel of channels) { |
| 11 | let page = channel.pages || 1; |
| 12 | let keepLoop = true; |
| 13 | |
| 14 | do { |
| 15 | const threads = await prisma.threads.findMany({ |
| 16 | select: { id: true, sentAt: true }, |
| 17 | where: { |
| 18 | channelId: channel.id, |
| 19 | page: null, |
| 20 | hidden: false, |
| 21 | messages: { some: {} }, |
| 22 | }, |
| 23 | orderBy: { sentAt: 'asc' }, |
| 24 | take: PAGE_SIZE, |
| 25 | }); |
| 26 | |
| 27 | if (threads.length === PAGE_SIZE) { |
| 28 | await prisma.threads.updateMany({ |
| 29 | data: { page }, |
| 30 | where: { id: { in: threads.map((t) => t.id) } }, |
| 31 | }); |
| 32 | |
| 33 | await prisma.channels.update({ |
| 34 | data: { |
| 35 | pages: page, |
| 36 | lastPageBuildAt: threads[PAGE_SIZE - 1].sentAt, |
| 37 | }, |
| 38 | where: { id: channel.id }, |
| 39 | }); |
| 40 | page++; |
| 41 | } else { |
| 42 | // latest, nothing to do |
| 43 | keepLoop = false; |
| 44 | } |
| 45 | // console.info( |
| 46 | // `${account.name || account.id} > ${channel.channelName || channel.id}`, |
| 47 | // { threads: threads.length, page, keepLoop } |
| 48 | // ); |
| 49 | } while (keepLoop); |
| 50 | } |
| 51 | return account.name || account.id; |
| 52 | } |
| 53 | |
| 54 | export async function cleanUp(accountId?: string) { |
| 55 | const accounts = await prisma.accounts.findMany({ |
no test coverage detected