|
11 | 11 | import { getNextDigestAt } from "../../functions/src/notifications/helpers"
|
12 | 12 | import { Profile } from "../../components/db/profile/types"
|
13 | 13 | import { Script } from "./types"
|
14 |
| -import { Boolean, Record } from "runtypes" |
| 14 | +import { Boolean, Optional, Record } from "runtypes" |
15 | 15 |
|
16 | 16 | const Args = Record({
|
17 |
| - dryRun: Boolean |
| 17 | + dryRun: Optional(Boolean) |
18 | 18 | })
|
19 | 19 |
|
20 | 20 | export const script: Script = async ({ db, args }) => {
|
21 |
| - const profilesSnapshot = await db.collection("profiles").get() |
| 21 | + const { dryRun } = Args.check(args) |
| 22 | + |
| 23 | + let numProfiles = 0, |
| 24 | + numMissingFrequency = 0, |
| 25 | + numWithNextDigestAt = 0, |
| 26 | + numNeedingNextDigestAt = 0 |
22 | 27 |
|
| 28 | + // There are only ~500 profiles at the time of writing |
| 29 | + // so the bulkWriter is unnecessary |
| 30 | + const profilesSnapshot = await db.collection("profiles").get() |
23 | 31 | console.log(profilesSnapshot.docs.length, "profiles found")
|
24 | 32 |
|
25 | 33 | const updatePromises = profilesSnapshot.docs.map(async profileDoc => {
|
26 | 34 | const profile = profileDoc.data() as Profile
|
| 35 | + numProfiles++ |
27 | 36 |
|
28 | 37 | if (profile.notificationFrequency) {
|
| 38 | + const hasNextDigestAt = !!profile.nextDigestAt |
| 39 | + if (hasNextDigestAt) { |
| 40 | + numWithNextDigestAt++ |
| 41 | + } else { |
| 42 | + numNeedingNextDigestAt++ |
| 43 | + } |
| 44 | + |
29 | 45 | const nextDigestAt = getNextDigestAt(profile.notificationFrequency)
|
30 | 46 |
|
31 |
| - if (!args.dryRun) { |
| 47 | + if (!dryRun) { |
32 | 48 | await profileDoc.ref.update({ nextDigestAt })
|
33 | 49 | }
|
34 | 50 | console.log(
|
35 | 51 | `Updated nextDigestAt for ${profileDoc.id} to ${nextDigestAt?.toDate()}`
|
36 | 52 | )
|
37 | 53 | } else {
|
| 54 | + numMissingFrequency++ |
38 | 55 | console.log(
|
39 | 56 | `Profile ${profileDoc.id} does not have notificationFrequency - skipping`
|
40 | 57 | )
|
41 | 58 | }
|
42 | 59 | })
|
43 | 60 |
|
44 | 61 | await Promise.all(updatePromises)
|
| 62 | + |
| 63 | + console.log("Num profiles:", numProfiles) |
| 64 | + console.log("Num missing frequency:", numMissingFrequency) |
| 65 | + console.log("Num with nextDigestAt:", numWithNextDigestAt) |
| 66 | + console.log("Num needing nextDigestAt:", numNeedingNextDigestAt) |
45 | 67 | console.log("Backfill complete")
|
46 | 68 | }
|
0 commit comments