private IDictionary <Guid, Guid?> ExerciseCredits(Guid creditId, IEnumerable <Guid> hierarchyPath, bool adjustAllocation, Guid exercisedById, IEnumerable <Guid> exercisedOnIds, Guid?referenceId) { var exercisedCredits = new Dictionary <Guid, ExercisedCredit>(); var exercisedOwnerIds = new List <Guid>(); var allocations = _repository.GetActiveAllocations(hierarchyPath, new[] { creditId }); var originalRemainingQuantities = allocations.SelectMany(a => a.Value).ToDictionary(a => a.Id, a => a.RemainingQuantity); foreach (var exercisedOnId in exercisedOnIds) { // Iterate through the owners looking for someone who can exercise the credit. ExercisedCredit exercisedCredit = null; foreach (var ownerId in hierarchyPath) { exercisedCredit = ExerciseCredit(allocations[ownerId], adjustAllocation, exercisedById, exercisedOnId, referenceId); if (exercisedCredit != null) { if (!exercisedOwnerIds.Contains(ownerId)) { exercisedOwnerIds.Add(ownerId); } break; } } exercisedCredits[exercisedOnId] = exercisedCredit; } // Create all exercised credits. _repository.CreateExercisedCredits(from c in exercisedCredits where c.Value != null select c.Value); // Now update all allocations where needed. var changedAllocations = from a in allocations.SelectMany(a => a.Value) where a.RemainingQuantity != originalRemainingQuantities[a.Id] select a; if (changedAllocations.Any()) { _repository.UpdateAllocations(changedAllocations); } // Fire events. if (exercisedOwnerIds.Count > 0) { var handlers = CreditExercised; if (handlers != null) { foreach (var exercisedOwnerId in exercisedOwnerIds) { handlers(this, new CreditExercisedEventArgs(creditId, exercisedOwnerId, (from a in changedAllocations where a.OwnerId == exercisedOwnerId select a).Any())); } } } return(exercisedCredits.ToDictionary(c => c.Key, c => c.Value == null ? (Guid?)null : c.Value.Id)); }
void ICreditsRepository.CreateExercisedCredit(ExercisedCredit exercisedCredit) { using (var dc = CreateContext()) { dc.CandidateAccessPurchaseEntities.InsertOnSubmit(exercisedCredit.Map()); dc.SubmitChanges(); } }
private void CreateContact(Guid employerId, Guid memberId) { var exercisedCredit = new ExercisedCredit { ExercisedById = employerId, ExercisedOnId = memberId, }; exercisedCredit.Prepare(); _creditsRepository.CreateExercisedCredit(exercisedCredit); }
public static CandidateAccessPurchaseEntity Map(this ExercisedCredit exercisedCredit) { return(new CandidateAccessPurchaseEntity { id = exercisedCredit.Id, purchaseTime = exercisedCredit.Time, searcherId = exercisedCredit.ExercisedById, candidateId = exercisedCredit.ExercisedOnId, referenceId = exercisedCredit.ReferenceId, allocationId = exercisedCredit.AllocationId, adjustedAllocation = exercisedCredit.AdjustedAllocation, }); }
private static ExercisedCredit CreateExercisedCredit(Guid allocationId, bool adjustAllocation, Guid exercisedById, Guid?exercisedOnId, Guid?referenceId) { var exercisedCredit = new ExercisedCredit { AllocationId = allocationId, AdjustedAllocation = adjustAllocation, ExercisedById = exercisedById, ExercisedOnId = exercisedOnId == Guid.Empty ? null : exercisedOnId, ReferenceId = referenceId }; exercisedCredit.Prepare(); return(exercisedCredit); }
private void ExerciseCredit(int?quantity, HierarchyPath hierarchyPath, Guid ownerId, Guid?allocationId, Guid exercisedById, Guid exercisedOnId) { // Allocate credits to owner. var credit = _creditsQuery.GetCredit <ContactCredit>(); if (allocationId != null) { // Exercise a credit using an allocation. var allocation = new Allocation { Id = allocationId.Value, OwnerId = ownerId, CreditId = credit.Id, InitialQuantity = quantity }; _allocationsCommand.CreateAllocation(allocation); // Exercise a credit. var exercisedCreditId = _exercisedCreditsCommand.ExerciseCredit(credit.Id, hierarchyPath, true, exercisedById, exercisedOnId, null); Assert.AreEqual(allocation.Id, _exercisedCreditsQuery.GetExercisedCredit(exercisedCreditId.Value).AllocationId); } else { // To test the support for old candidate access before allocations were introduced directly update the database. // A record is kept that the owner has contacted the candidate but that no allocation is used. var exercise = new ExercisedCredit { Id = Guid.NewGuid(), Time = DateTime.Now, CreditId = credit.Id, ExercisedById = exercisedById, ExercisedOnId = exercisedOnId, AllocationId = null, ReferenceId = null, }; _repository.CreateExercisedCredit(exercise); } }