|
| 1 | +import { DataSource } from 'typeorm'; |
| 2 | +import { recruiterNewCandidateNotification as worker } from '../../../src/workers/notifications/recruiterNewCandidateNotification'; |
| 3 | +import createOrGetConnection from '../../../src/db'; |
| 4 | +import { Organization, User } from '../../../src/entity'; |
| 5 | +import { OpportunityJob } from '../../../src/entity/opportunities/OpportunityJob'; |
| 6 | +import { OpportunityUser } from '../../../src/entity/opportunities/user'; |
| 7 | +import { OpportunityUserType } from '../../../src/entity/opportunities/types'; |
| 8 | +import { OpportunityType, OpportunityState } from '@dailydotdev/schema'; |
| 9 | +import { usersFixture } from '../../fixture'; |
| 10 | +import { workers } from '../../../src/workers'; |
| 11 | +import { invokeTypedNotificationWorker, saveFixtures } from '../../helpers'; |
| 12 | +import { NotificationType } from '../../../src/notifications/common'; |
| 13 | +import type { NotificationRecruiterNewCandidateContext } from '../../../src/notifications'; |
| 14 | +import { CandidateAcceptedOpportunityMessage } from '@dailydotdev/schema'; |
| 15 | + |
| 16 | +let con: DataSource; |
| 17 | + |
| 18 | +describe('recruiterNewCandidateNotification worker', () => { |
| 19 | + beforeAll(async () => { |
| 20 | + con = await createOrGetConnection(); |
| 21 | + }); |
| 22 | + |
| 23 | + beforeEach(async () => { |
| 24 | + jest.resetAllMocks(); |
| 25 | + await saveFixtures(con, User, usersFixture); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should be registered', () => { |
| 29 | + const registeredWorker = workers.find( |
| 30 | + (item) => item.subscription === worker.subscription, |
| 31 | + ); |
| 32 | + |
| 33 | + expect(registeredWorker).toBeDefined(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should send notification to all recruiters when candidate accepts', async () => { |
| 37 | + const organization = await con.getRepository(Organization).save({ |
| 38 | + id: 'org123', |
| 39 | + name: 'Test Organization', |
| 40 | + }); |
| 41 | + |
| 42 | + const opportunity = await con.getRepository(OpportunityJob).save({ |
| 43 | + id: '123e4567-e89b-12d3-a456-426614174000', |
| 44 | + type: OpportunityType.JOB, |
| 45 | + state: OpportunityState.LIVE, |
| 46 | + title: 'Senior Software Engineer', |
| 47 | + tldr: 'Great opportunity', |
| 48 | + content: {}, |
| 49 | + meta: {}, |
| 50 | + organizationId: organization.id, |
| 51 | + location: [], |
| 52 | + }); |
| 53 | + |
| 54 | + const recruiter1 = await con.getRepository(User).save({ |
| 55 | + id: 'recruiter1', |
| 56 | + name: 'John Recruiter', |
| 57 | + email: 'john@test.com', |
| 58 | + }); |
| 59 | + |
| 60 | + const recruiter2 = await con.getRepository(User).save({ |
| 61 | + id: 'recruiter2', |
| 62 | + name: 'Jane Recruiter', |
| 63 | + email: 'jane@test.com', |
| 64 | + }); |
| 65 | + |
| 66 | + await con.getRepository(OpportunityUser).save([ |
| 67 | + { |
| 68 | + opportunityId: opportunity.id, |
| 69 | + userId: recruiter1.id, |
| 70 | + type: OpportunityUserType.Recruiter, |
| 71 | + }, |
| 72 | + { |
| 73 | + opportunityId: opportunity.id, |
| 74 | + userId: recruiter2.id, |
| 75 | + type: OpportunityUserType.Recruiter, |
| 76 | + }, |
| 77 | + ]); |
| 78 | + |
| 79 | + const result = |
| 80 | + await invokeTypedNotificationWorker<'api.v1.candidate-accepted-opportunity'>( |
| 81 | + worker, |
| 82 | + new CandidateAcceptedOpportunityMessage({ |
| 83 | + userId: '1', |
| 84 | + opportunityId: '123e4567-e89b-12d3-a456-426614174000', |
| 85 | + createdAt: Math.floor(Date.now() / 1000), |
| 86 | + updatedAt: Math.floor(Date.now() / 1000), |
| 87 | + }), |
| 88 | + ); |
| 89 | + |
| 90 | + expect(result!.length).toEqual(1); |
| 91 | + expect(result![0].type).toEqual(NotificationType.RecruiterNewCandidate); |
| 92 | + |
| 93 | + const context = result![0].ctx as NotificationRecruiterNewCandidateContext; |
| 94 | + |
| 95 | + expect(context.userIds).toHaveLength(2); |
| 96 | + expect(context.userIds).toContain('recruiter1'); |
| 97 | + expect(context.userIds).toContain('recruiter2'); |
| 98 | + expect(context.opportunityId).toEqual( |
| 99 | + '123e4567-e89b-12d3-a456-426614174000', |
| 100 | + ); |
| 101 | + expect(context.candidate).toBeDefined(); |
| 102 | + expect(context.candidate.id).toEqual('1'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should return empty array when no recruiters found', async () => { |
| 106 | + const organization = await con.getRepository(Organization).save({ |
| 107 | + id: 'org456', |
| 108 | + name: 'Another Organization', |
| 109 | + }); |
| 110 | + |
| 111 | + await con.getRepository(OpportunityJob).save({ |
| 112 | + id: '123e4567-e89b-12d3-a456-426614174001', |
| 113 | + type: OpportunityType.JOB, |
| 114 | + state: OpportunityState.LIVE, |
| 115 | + title: 'Backend Developer', |
| 116 | + tldr: 'Backend role', |
| 117 | + content: {}, |
| 118 | + meta: {}, |
| 119 | + organizationId: organization.id, |
| 120 | + location: [], |
| 121 | + }); |
| 122 | + |
| 123 | + const result = |
| 124 | + await invokeTypedNotificationWorker<'api.v1.candidate-accepted-opportunity'>( |
| 125 | + worker, |
| 126 | + new CandidateAcceptedOpportunityMessage({ |
| 127 | + userId: '1', |
| 128 | + opportunityId: '123e4567-e89b-12d3-a456-426614174001', |
| 129 | + createdAt: Math.floor(Date.now() / 1000), |
| 130 | + updatedAt: Math.floor(Date.now() / 1000), |
| 131 | + }), |
| 132 | + ); |
| 133 | + |
| 134 | + expect(result).toEqual([]); |
| 135 | + }); |
| 136 | +}); |
0 commit comments