|
| 1 | +import { DataSource, In } from 'typeorm'; |
| 2 | +import worker from '../../src/workers/userActivatedContributionReferral'; |
| 3 | +import { ChangeObject } from '../../src/types'; |
| 4 | +import { expectSuccessfulTypedBackground, saveFixtures } from '../helpers'; |
| 5 | +import { User } from '../../src/entity'; |
| 6 | +import { PubSubSchema } from '../../src/common'; |
| 7 | +import { typedWorkers } from '../../src/workers'; |
| 8 | +import createOrGetConnection from '../../src/db'; |
| 9 | +import { remoteConfig } from '../../src/remoteConfig'; |
| 10 | +import { |
| 11 | + ContributionAction, |
| 12 | + ContributionAssistType, |
| 13 | +} from '../../src/entity/contribution/ContributionAction'; |
| 14 | +import { ContributionCause } from '../../src/entity/contribution/ContributionCause'; |
| 15 | +import { UserContributionCausePreference } from '../../src/entity/contribution/UserContributionCausePreference'; |
| 16 | +import { ContributionBlockedUser } from '../../src/entity/contribution/ContributionBlockedUser'; |
| 17 | +import { |
| 18 | + ContributionSubmission, |
| 19 | + ContributionSubmissionStatus, |
| 20 | +} from '../../src/entity/contribution/ContributionSubmission'; |
| 21 | + |
| 22 | +let con: DataSource; |
| 23 | + |
| 24 | +const referrerId = '99999999-9999-4999-8999-999999999990'; |
| 25 | +const refereeId = '99999999-9999-4999-8999-999999999991'; |
| 26 | +const causeId = '33333333-3333-4333-8333-333333333333'; |
| 27 | +const actionId = '22222222-2222-4222-8222-222222222222'; |
| 28 | + |
| 29 | +beforeAll(async () => { |
| 30 | + con = await createOrGetConnection(); |
| 31 | +}); |
| 32 | + |
| 33 | +const seedReferralAction = async ( |
| 34 | + overrides: Partial<ContributionAction> = {}, |
| 35 | +) => { |
| 36 | + await saveFixtures(con, ContributionAction, [ |
| 37 | + { |
| 38 | + id: actionId, |
| 39 | + title: 'Invite a friend to daily.dev', |
| 40 | + points: 150, |
| 41 | + evidence: {}, |
| 42 | + metadata: { assistType: ContributionAssistType.ReferralLink }, |
| 43 | + ...overrides, |
| 44 | + }, |
| 45 | + ]); |
| 46 | +}; |
| 47 | + |
| 48 | +beforeEach(async () => { |
| 49 | + await con |
| 50 | + .getRepository(ContributionSubmission) |
| 51 | + .delete({ userId: referrerId }); |
| 52 | + await con |
| 53 | + .getRepository(UserContributionCausePreference) |
| 54 | + .delete({ userId: referrerId }); |
| 55 | + await con |
| 56 | + .getRepository(ContributionBlockedUser) |
| 57 | + .delete({ userId: referrerId }); |
| 58 | + await con.getRepository(ContributionAction).delete({ id: actionId }); |
| 59 | + await con.getRepository(ContributionCause).delete({ id: causeId }); |
| 60 | + await con.getRepository(User).delete({ id: In([referrerId, refereeId]) }); |
| 61 | + |
| 62 | + remoteConfig.vars.contributionProgram = { |
| 63 | + enabled: true, |
| 64 | + allowedCountries: ['US'], |
| 65 | + currentCycleTargetPoints: 10000, |
| 66 | + }; |
| 67 | + |
| 68 | + await saveFixtures(con, User, [ |
| 69 | + { id: referrerId, reputation: 10 }, |
| 70 | + { id: refereeId, referralId: referrerId, reputation: 10 }, |
| 71 | + ]); |
| 72 | + await saveFixtures(con, ContributionCause, [{ id: causeId, title: 'OSS' }]); |
| 73 | + await saveFixtures(con, UserContributionCausePreference, [ |
| 74 | + { userId: referrerId, causeId }, |
| 75 | + ]); |
| 76 | +}); |
| 77 | + |
| 78 | +const inactiveReferee: ChangeObject<User> = { |
| 79 | + id: refereeId, |
| 80 | + referralId: referrerId, |
| 81 | + infoConfirmed: false, |
| 82 | + emailConfirmed: false, |
| 83 | +} as unknown as ChangeObject<User>; |
| 84 | + |
| 85 | +const activeReferee: ChangeObject<User> = { |
| 86 | + ...inactiveReferee, |
| 87 | + infoConfirmed: true, |
| 88 | + emailConfirmed: true, |
| 89 | +}; |
| 90 | + |
| 91 | +const runActivation = ( |
| 92 | + newProfile: ChangeObject<User> = activeReferee, |
| 93 | + user: ChangeObject<User> = inactiveReferee, |
| 94 | +) => |
| 95 | + expectSuccessfulTypedBackground(worker, { |
| 96 | + newProfile, |
| 97 | + user, |
| 98 | + } as unknown as PubSubSchema['user-updated']); |
| 99 | + |
| 100 | +const getReferrerSubmissions = () => |
| 101 | + con |
| 102 | + .getRepository(ContributionSubmission) |
| 103 | + .find({ where: { userId: referrerId } }); |
| 104 | + |
| 105 | +describe('userActivatedContributionReferral', () => { |
| 106 | + it('should be registered', () => { |
| 107 | + const registered = typedWorkers.find( |
| 108 | + (item) => item.subscription === worker.subscription, |
| 109 | + ); |
| 110 | + expect(registered).toBeDefined(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('awards the referrer when the referee activates', async () => { |
| 114 | + await seedReferralAction(); |
| 115 | + |
| 116 | + await runActivation(); |
| 117 | + |
| 118 | + const submissions = await getReferrerSubmissions(); |
| 119 | + expect(submissions).toHaveLength(1); |
| 120 | + expect(submissions[0]).toMatchObject({ |
| 121 | + actionId, |
| 122 | + status: ContributionSubmissionStatus.Approved, |
| 123 | + awardedPoints: 150, |
| 124 | + flags: { refereeId }, |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it('does not award when there is no activation transition', async () => { |
| 129 | + await seedReferralAction(); |
| 130 | + |
| 131 | + await runActivation(activeReferee, activeReferee); |
| 132 | + |
| 133 | + expect(await getReferrerSubmissions()).toHaveLength(0); |
| 134 | + }); |
| 135 | + |
| 136 | + it('does not award when the referee has no referrer', async () => { |
| 137 | + await seedReferralAction(); |
| 138 | + |
| 139 | + const noReferral = { ...activeReferee, referralId: null }; |
| 140 | + await runActivation( |
| 141 | + noReferral as unknown as ChangeObject<User>, |
| 142 | + { ...inactiveReferee, referralId: null } as unknown as ChangeObject<User>, |
| 143 | + ); |
| 144 | + |
| 145 | + expect(await getReferrerSubmissions()).toHaveLength(0); |
| 146 | + }); |
| 147 | + |
| 148 | + it('does not award when the referrer has not joined the campaign', async () => { |
| 149 | + await seedReferralAction(); |
| 150 | + await con |
| 151 | + .getRepository(UserContributionCausePreference) |
| 152 | + .delete({ userId: referrerId }); |
| 153 | + |
| 154 | + await runActivation(); |
| 155 | + |
| 156 | + expect(await getReferrerSubmissions()).toHaveLength(0); |
| 157 | + }); |
| 158 | + |
| 159 | + it('does not award when no referral action is configured', async () => { |
| 160 | + await runActivation(); |
| 161 | + |
| 162 | + expect(await getReferrerSubmissions()).toHaveLength(0); |
| 163 | + }); |
| 164 | + |
| 165 | + it('is idempotent across redelivered activation events', async () => { |
| 166 | + await seedReferralAction(); |
| 167 | + |
| 168 | + await runActivation(); |
| 169 | + await runActivation(); |
| 170 | + |
| 171 | + expect(await getReferrerSubmissions()).toHaveLength(1); |
| 172 | + }); |
| 173 | + |
| 174 | + it('respects the action cap', async () => { |
| 175 | + await seedReferralAction({ maxPerUser: 1 }); |
| 176 | + await con.getRepository(ContributionSubmission).save({ |
| 177 | + userId: referrerId, |
| 178 | + actionId, |
| 179 | + status: ContributionSubmissionStatus.Approved, |
| 180 | + awardedPoints: 150, |
| 181 | + evidence: {}, |
| 182 | + flags: { refereeId: 'some-other-referee' }, |
| 183 | + }); |
| 184 | + |
| 185 | + await runActivation(); |
| 186 | + |
| 187 | + expect(await getReferrerSubmissions()).toHaveLength(1); |
| 188 | + }); |
| 189 | +}); |
0 commit comments