|
| 1 | +import { |
| 2 | + ChildWorkflowCancellationType, |
| 3 | + ParentClosePolicy, |
| 4 | + continueAsNew, |
| 5 | + proxyActivities, |
| 6 | + startChild, |
| 7 | + workflowInfo, |
| 8 | +} from '@temporalio/workflow' |
| 9 | + |
| 10 | +import * as activities from '../activities' |
| 11 | +import { IScriptBatchTestArgs } from '../types' |
| 12 | +import { chunkArray } from '../utils/common' |
| 13 | + |
| 14 | +import { recalculateMemberAffiliations } from './recalculate-member-affiliations' |
| 15 | + |
| 16 | +const { |
| 17 | + findMemberWorkExperienceWithEpochDates, |
| 18 | + updateMemberWorkExperience, |
| 19 | + markMemberForAffiliationRecalc, |
| 20 | +} = proxyActivities<typeof activities>({ |
| 21 | + startToCloseTimeout: '30 minutes', |
| 22 | +}) |
| 23 | + |
| 24 | +export async function fixWorkExperienceEpochDates(args: IScriptBatchTestArgs): Promise<void> { |
| 25 | + const info = workflowInfo() |
| 26 | + const WORK_EXPERIENCES_PER_RUN = args.batchSize ?? 1000 |
| 27 | + |
| 28 | + const workExperiences = await findMemberWorkExperienceWithEpochDates(WORK_EXPERIENCES_PER_RUN) |
| 29 | + |
| 30 | + if (workExperiences?.length === 0) { |
| 31 | + console.log('No more work experiences to fix, triggering recalculation of member affiliations!') |
| 32 | + |
| 33 | + await startChild(recalculateMemberAffiliations, { |
| 34 | + workflowId: `recalculateMemberAffiliations/${info.workflowId}`, |
| 35 | + cancellationType: ChildWorkflowCancellationType.ABANDON, |
| 36 | + parentClosePolicy: ParentClosePolicy.PARENT_CLOSE_POLICY_ABANDON, |
| 37 | + retry: { |
| 38 | + backoffCoefficient: 2, |
| 39 | + initialInterval: 2 * 1000, |
| 40 | + maximumInterval: 30 * 1000, |
| 41 | + }, |
| 42 | + args: [ |
| 43 | + { |
| 44 | + batchSize: 500, |
| 45 | + }, |
| 46 | + ], |
| 47 | + }) |
| 48 | + |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + const isEpoch = (v?: string | Date | null) => { |
| 53 | + if (!v) return false |
| 54 | + const d = v instanceof Date ? v : new Date(v) |
| 55 | + return d.getTime() === 0 |
| 56 | + } |
| 57 | + |
| 58 | + for (const chunk of chunkArray(workExperiences, 10)) { |
| 59 | + await Promise.all( |
| 60 | + chunk.map((we) => { |
| 61 | + // prepare original object |
| 62 | + const original = { |
| 63 | + id: we.id, |
| 64 | + orgId: we.organizationId, |
| 65 | + jobTitle: we.title, |
| 66 | + dateStart: we.dateStart as string, |
| 67 | + dateEnd: we.dateEnd as string, |
| 68 | + source: we.source, |
| 69 | + } |
| 70 | + |
| 71 | + // prepare toUpdate object |
| 72 | + const toUpdate = { |
| 73 | + ...(isEpoch(original.dateStart) && { dateStart: null }), |
| 74 | + ...(isEpoch(original.dateEnd) && { dateEnd: null }), |
| 75 | + } |
| 76 | + |
| 77 | + if (args.testRun) { |
| 78 | + console.log(`Updating work experience for member ${we.memberId}`) |
| 79 | + console.log(`Original: ${JSON.stringify(original)}`) |
| 80 | + } |
| 81 | + |
| 82 | + return updateMemberWorkExperience(we.memberId, original, toUpdate) |
| 83 | + }), |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + // deduplicate memberIds and queue for affiliation recalculation |
| 88 | + const uniqueMemberIds = Array.from(new Set(workExperiences.map((we) => we.memberId))) |
| 89 | + |
| 90 | + await markMemberForAffiliationRecalc(uniqueMemberIds) |
| 91 | + |
| 92 | + if (args.testRun) { |
| 93 | + console.log('Test run completed - stopping after first batch!') |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + await continueAsNew<typeof fixWorkExperienceEpochDates>(args) |
| 98 | +} |
0 commit comments