|
| 1 | +import commandLineArgs from 'command-line-args' |
| 2 | + |
| 3 | +import { inferMemberOrganizationStintChanges } from '@crowd/common_services' |
| 4 | +import { |
| 5 | + createMemberOrganization, |
| 6 | + fetchEmailDomainMemberOrganizationActivityDates, |
| 7 | + fetchEmailDomainMemberOrganizationsWithoutDates, |
| 8 | + fetchMemberOrganizationsBySource, |
| 9 | + pgpQx, |
| 10 | + updateMemberOrganization, |
| 11 | +} from '@crowd/data-access-layer' |
| 12 | +import { getDbConnection } from '@crowd/data-access-layer/src/database' |
| 13 | +import { chunkArray } from '@crowd/data-access-layer/src/old/apps/merge_suggestions_worker/utils' |
| 14 | +import { getServiceLogger } from '@crowd/logging' |
| 15 | +import { getRedisClient } from '@crowd/redis' |
| 16 | +import { OrganizationSource } from '@crowd/types' |
| 17 | + |
| 18 | +import { DB_CONFIG, REDIS_CONFIG } from '@/conf' |
| 19 | + |
| 20 | +const log = getServiceLogger() |
| 21 | + |
| 22 | +const options = [ |
| 23 | + { |
| 24 | + name: 'testRun', |
| 25 | + alias: 't', |
| 26 | + type: Boolean, |
| 27 | + description: 'Run in test mode (limit to 1 batch and 10 members).', |
| 28 | + }, |
| 29 | + { |
| 30 | + name: 'afterMemberId', |
| 31 | + alias: 'a', |
| 32 | + type: String, |
| 33 | + description: 'The member ID to start processing after.', |
| 34 | + }, |
| 35 | + { |
| 36 | + name: 'batchSize', |
| 37 | + alias: 'b', |
| 38 | + type: Number, |
| 39 | + description: 'The number of members to fetch in each batch.', |
| 40 | + }, |
| 41 | + { |
| 42 | + name: 'help', |
| 43 | + alias: 'h', |
| 44 | + type: Boolean, |
| 45 | + description: 'Print this usage guide.', |
| 46 | + }, |
| 47 | +] |
| 48 | + |
| 49 | +const parameters = commandLineArgs(options) |
| 50 | + |
| 51 | +setImmediate(async () => { |
| 52 | + const testRun = parameters.testRun ?? false |
| 53 | + const BATCH_SIZE = parameters.batchSize ?? (testRun ? 10 : 500) |
| 54 | + let afterMemberId = parameters.afterMemberId ?? undefined |
| 55 | + |
| 56 | + const db = await getDbConnection({ |
| 57 | + host: DB_CONFIG.writeHost, |
| 58 | + port: DB_CONFIG.port, |
| 59 | + database: DB_CONFIG.database, |
| 60 | + user: DB_CONFIG.username, |
| 61 | + password: DB_CONFIG.password, |
| 62 | + }) |
| 63 | + |
| 64 | + const qx = pgpQx(db) |
| 65 | + const redis = await getRedisClient(REDIS_CONFIG, true) |
| 66 | + |
| 67 | + log.info({ testRun, BATCH_SIZE, afterMemberId }, 'Running script with the following parameters!') |
| 68 | + |
| 69 | + let hasMore = true |
| 70 | + |
| 71 | + while (hasMore) { |
| 72 | + const memberIds = await fetchEmailDomainMemberOrganizationsWithoutDates( |
| 73 | + qx, |
| 74 | + BATCH_SIZE, |
| 75 | + afterMemberId, |
| 76 | + ) |
| 77 | + |
| 78 | + if (memberIds.length > 0) { |
| 79 | + for (const chunk of chunkArray(memberIds, 50)) { |
| 80 | + await Promise.all( |
| 81 | + chunk.map(async (memberId) => { |
| 82 | + if (testRun) { |
| 83 | + log.info({ memberId }, 'Processing member!') |
| 84 | + } |
| 85 | + |
| 86 | + try { |
| 87 | + const [existingMemberOrganizations, activityDates] = await Promise.all([ |
| 88 | + fetchMemberOrganizationsBySource(qx, memberId, OrganizationSource.EMAIL_DOMAIN), |
| 89 | + fetchEmailDomainMemberOrganizationActivityDates(qx, memberId), |
| 90 | + ]) |
| 91 | + |
| 92 | + const changes = inferMemberOrganizationStintChanges( |
| 93 | + memberId, |
| 94 | + existingMemberOrganizations, |
| 95 | + activityDates, |
| 96 | + ) |
| 97 | + |
| 98 | + if (testRun) { |
| 99 | + log.info( |
| 100 | + { existingMemberOrganizations, activityDates, changes }, |
| 101 | + 'Previewing changes for member.', |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + if (changes.length > 0) { |
| 106 | + await qx.tx(async (tx) => { |
| 107 | + for (const change of changes) { |
| 108 | + if (change.type === 'insert') { |
| 109 | + await createMemberOrganization(tx, memberId, { |
| 110 | + organizationId: change.organizationId, |
| 111 | + dateStart: change.dateStart, |
| 112 | + dateEnd: change.dateEnd, |
| 113 | + source: OrganizationSource.EMAIL_DOMAIN, |
| 114 | + }) |
| 115 | + } else if (change.type === 'update') { |
| 116 | + await updateMemberOrganization(tx, memberId, change.id, { |
| 117 | + dateStart: change.dateStart, |
| 118 | + dateEnd: change.dateEnd, |
| 119 | + }) |
| 120 | + } |
| 121 | + |
| 122 | + if (testRun) { |
| 123 | + log.info( |
| 124 | + { memberId, orgId: change.organizationId, type: change.type }, |
| 125 | + 'Member organization updated.', |
| 126 | + ) |
| 127 | + } |
| 128 | + } |
| 129 | + }) |
| 130 | + await redis.sAdd('recalculate-member-affiliations', [memberId]) |
| 131 | + } else if (testRun) { |
| 132 | + log.info({ memberId }, 'No changes found for member!') |
| 133 | + } |
| 134 | + } catch (err) { |
| 135 | + log.error({ memberId, err }, 'Failed to process for member!') |
| 136 | + throw err |
| 137 | + } |
| 138 | + }), |
| 139 | + ) |
| 140 | + } |
| 141 | + |
| 142 | + const lastMemberId = memberIds[memberIds.length - 1] |
| 143 | + afterMemberId = lastMemberId |
| 144 | + |
| 145 | + log.info({ lastMemberId, count: memberIds.length }, 'Batch processed!') |
| 146 | + |
| 147 | + if (testRun || memberIds.length < BATCH_SIZE) { |
| 148 | + hasMore = false |
| 149 | + } |
| 150 | + } else { |
| 151 | + hasMore = false |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + process.exit(0) |
| 156 | +}) |
0 commit comments