|
| 1 | +import { MembershipInvitation, User } from "@app/lib/models"; |
| 2 | +import { generateModelSId } from "@app/lib/utils"; |
| 3 | +import logger from "@app/logger/logger"; |
| 4 | +import { makeScript } from "@app/scripts/helpers"; |
| 5 | + |
| 6 | +const backfillUsers = async (execute: boolean) => { |
| 7 | + const users = await User.findAll({}); |
| 8 | + |
| 9 | + const chunks: User[][] = []; |
| 10 | + for (let i = 0; i < users.length; i += 16) { |
| 11 | + chunks.push(users.slice(i, i + 16)); |
| 12 | + } |
| 13 | + |
| 14 | + for (let i = 0; i < chunks.length; i++) { |
| 15 | + const chunk = chunks[i]; |
| 16 | + await Promise.all( |
| 17 | + chunk.map((u) => { |
| 18 | + return (async () => { |
| 19 | + const sId = generateModelSId(); |
| 20 | + logger.info( |
| 21 | + `Backfilling user ${u.id} with \`sId=${sId}\` [execute: ${execute}]` |
| 22 | + ); |
| 23 | + |
| 24 | + if (execute) { |
| 25 | + await u.update({ sId }); |
| 26 | + } |
| 27 | + })(); |
| 28 | + }) |
| 29 | + ); |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +const backfillInvitations = async (execute: boolean) => { |
| 34 | + const invitations = await MembershipInvitation.findAll({}); |
| 35 | + |
| 36 | + const chunks: MembershipInvitation[][] = []; |
| 37 | + for (let i = 0; i < invitations.length; i += 16) { |
| 38 | + chunks.push(invitations.slice(i, i + 16)); |
| 39 | + } |
| 40 | + |
| 41 | + for (let i = 0; i < chunks.length; i++) { |
| 42 | + const chunk = chunks[i]; |
| 43 | + await Promise.all( |
| 44 | + chunk.map((i) => { |
| 45 | + return (async () => { |
| 46 | + const sId = generateModelSId(); |
| 47 | + logger.info( |
| 48 | + `Backfilling invitation ${i.id} with \`sId=${sId}\` [execute: ${execute}]` |
| 49 | + ); |
| 50 | + |
| 51 | + if (execute) { |
| 52 | + await i.update({ sId }); |
| 53 | + } |
| 54 | + })(); |
| 55 | + }) |
| 56 | + ); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +makeScript({}, async ({ execute }) => { |
| 61 | + await backfillUsers(execute); |
| 62 | + await backfillInvitations(execute); |
| 63 | +}); |
0 commit comments