|
241 | 241 | end |
242 | 242 | end |
243 | 243 |
|
| 244 | + describe '#create_invitation' do |
| 245 | + let(:member) { Fabricate(:member) } |
| 246 | + |
| 247 | + it 'creates a new invitation and returns it with previously_new_record? as true' do |
| 248 | + invitation = manager.send(:create_invitation, workshop, member, 'Student') |
| 249 | + |
| 250 | + expect(invitation).to be_a(WorkshopInvitation) |
| 251 | + expect(invitation).to be_persisted |
| 252 | + expect(invitation.previously_new_record?).to be true |
| 253 | + expect(invitation.workshop).to eq(workshop) |
| 254 | + expect(invitation.member).to eq(member) |
| 255 | + expect(invitation.role).to eq('Student') |
| 256 | + end |
| 257 | + |
| 258 | + it 'returns existing invitation with previously_new_record? as false on duplicate call' do |
| 259 | + # First call creates the invitation |
| 260 | + invitation1 = manager.send(:create_invitation, workshop, member, 'Student') |
| 261 | + expect(invitation1.previously_new_record?).to be true |
| 262 | + |
| 263 | + # Second call finds the existing invitation |
| 264 | + invitation2 = manager.send(:create_invitation, workshop, member, 'Student') |
| 265 | + expect(invitation2.previously_new_record?).to be false |
| 266 | + expect(invitation2.id).to eq(invitation1.id) |
| 267 | + end |
| 268 | + |
| 269 | + it 'does not create duplicate records in database' do |
| 270 | + expect do |
| 271 | + # Multiple calls should not create duplicates |
| 272 | + 3.times { manager.send(:create_invitation, workshop, member, 'Student') } |
| 273 | + end.to change(WorkshopInvitation, :count).by(1) |
| 274 | + end |
| 275 | + end |
| 276 | + |
244 | 277 | describe '#create_invitation resilience' do |
245 | 278 | let(:member) { Fabricate(:member) } |
246 | 279 |
|
247 | | - it 'returns nil when find_or_create_by raises an exception' do |
248 | | - allow(WorkshopInvitation).to receive(:find_or_create_by) |
| 280 | + it 'returns nil when find_or_initialize_by raises an exception' do |
| 281 | + allow(WorkshopInvitation).to receive(:find_or_initialize_by) |
249 | 282 | .and_raise(StandardError.new('database error')) |
250 | 283 |
|
251 | 284 | result = manager.send(:create_invitation, workshop, member, 'Student') |
|
254 | 287 | end |
255 | 288 |
|
256 | 289 | it 'logs error with member_id and workshop_id but no PII' do |
257 | | - allow(WorkshopInvitation).to receive(:find_or_create_by) |
| 290 | + allow(WorkshopInvitation).to receive(:find_or_initialize_by) |
258 | 291 | .and_raise(StandardError.new('database error')) |
259 | 292 |
|
260 | 293 | expect(Rails.logger).to receive(:error) do |message| |
|
272 | 305 | Fabricate(:students, chapter: chapter, members: students) |
273 | 306 | call_count = 0 |
274 | 307 |
|
275 | | - allow(WorkshopInvitation).to receive(:find_or_create_by) do |
| 308 | + allow(WorkshopInvitation).to receive(:find_or_initialize_by) do |
276 | 309 | call_count += 1 |
277 | 310 | if call_count == 1 |
278 | 311 | raise StandardError.new('database error') |
|
286 | 319 | end.not_to raise_error |
287 | 320 | end |
288 | 321 | end |
| 322 | + |
| 323 | + describe 'duplicate invitation prevention' do |
| 324 | + let(:initiator) { Fabricate(:member) } |
| 325 | + |
| 326 | + before do |
| 327 | + Fabricate(:students, chapter: chapter, members: students) |
| 328 | + end |
| 329 | + |
| 330 | + it 'logs skipped entries for already invited members when re-running batch' do |
| 331 | + # First invitation round |
| 332 | + manager.send_workshop_emails(workshop, 'students', initiator.id) |
| 333 | + |
| 334 | + # Get the first log |
| 335 | + first_log = InvitationLog.where(loggable: workshop, audience: 'students').order(:created_at).first |
| 336 | + expect(first_log.success_count).to eq(students.count) |
| 337 | + expect(first_log.skipped_count).to eq(0) |
| 338 | + |
| 339 | + # Second invitation round |
| 340 | + manager.send_workshop_emails(workshop, 'students', initiator.id) |
| 341 | + |
| 342 | + # Get the second log |
| 343 | + second_log = InvitationLog.where(loggable: workshop, audience: 'students').order(:created_at).last |
| 344 | + expect(second_log.success_count).to eq(0) |
| 345 | + expect(second_log.skipped_count).to eq(students.count) |
| 346 | + end |
| 347 | + |
| 348 | + it 'only sends emails to newly eligible members when batch is re-run' do |
| 349 | + # First invitation round |
| 350 | + manager.send_workshop_emails(workshop, 'students', initiator.id) |
| 351 | + ActionMailer::Base.deliveries.clear |
| 352 | + |
| 353 | + # Add a new student |
| 354 | + new_student = Fabricate(:member) |
| 355 | + Fabricate(:students, chapter: chapter, members: [new_student]) |
| 356 | + |
| 357 | + # Second invitation round - should only email the new student |
| 358 | + expect do |
| 359 | + manager.send_workshop_emails(workshop, 'students', initiator.id) |
| 360 | + end.to change { ActionMailer::Base.deliveries.count }.by(1) |
| 361 | + |
| 362 | + log = InvitationLog.where(loggable: workshop, audience: 'students').order(:created_at).last |
| 363 | + expect(log.success_count).to eq(1) |
| 364 | + expect(log.skipped_count).to eq(students.count) |
| 365 | + end |
| 366 | + end |
289 | 367 | end |
0 commit comments